浏览代码

Materials Library, added specular color support to water material

luaacro 9 年之前
父节点
当前提交
0e120f7051

文件差异内容过多而无法显示
+ 2 - 2
materialsLibrary/dist/babylon.waterMaterial.js


+ 56 - 53
materialsLibrary/materials/water/water.fragment.fx

@@ -334,7 +334,7 @@ struct lightingInfo
 #endif
 #endif
 };
 };
 
 
-lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, float range, float glossiness) {
+lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, float range, float glossiness, vec3 bumpColor) {
 	lightingInfo result;
 	lightingInfo result;
 
 
 	vec3 lightVectorW;
 	vec3 lightVectorW;
@@ -357,16 +357,21 @@ lightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData,
 
 
 	// Specular
 	// Specular
 #ifdef SPECULARTERM
 #ifdef SPECULARTERM
-	vec3 angleW = normalize(viewDirectionW + lightVectorW);
-	float specComp = max(0., dot(vNormal, angleW));
-	specComp = pow(specComp, max(1., glossiness));
-	result.specular = specComp * specularColor * attenuation;
+	vec3 angleW = normalize(vEyePosition - lightVectorW);
+	vec2 perturbation = bumpHeight * (bumpColor.rg - 0.5);
+	
+	vec3 upVector = vec3(0.0, 1.0, 0.0);
+	float fresnelTerm = max(dot(angleW, upVector), 0.0);
+	
+	vec3 halfvec = normalize(angleW + lightVectorW + vec3(perturbation.x * fresnelTerm, perturbation.y * fresnelTerm, 0.0) * max(1., glossiness));
+	float temp = pow(dot(halfvec, vNormal), max(1., glossiness));
+	result.specular = specularColor * temp * attenuation;
 #endif
 #endif
 
 
 	return result;
 	return result;
 }
 }
 
 
-lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 specularColor, vec3 diffuseColor, float range, float glossiness) {
+lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 specularColor, vec3 diffuseColor, float range, float glossiness, vec3 bumpColor) {
 	lightingInfo result;
 	lightingInfo result;
 
 
 	vec3 direction = lightData.xyz - vPositionW;
 	vec3 direction = lightData.xyz - vPositionW;
@@ -387,11 +392,16 @@ lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightDa
 		result.diffuse = ndl * spotAtten * diffuseColor * attenuation;
 		result.diffuse = ndl * spotAtten * diffuseColor * attenuation;
 
 
 		// Specular
 		// Specular
-#ifdef SPECULARTERM
-		vec3 angleW = normalize(viewDirectionW - lightDirection.xyz);
-		float specComp = max(0., dot(vNormal, angleW));
-		specComp = pow(specComp, max(1., glossiness));
-		result.specular = specComp * specularColor * spotAtten * attenuation;
+#ifdef SPECULARTERM		
+		vec3 angleW = normalize(vEyePosition - lightVectorW);
+		vec2 perturbation = bumpHeight * (bumpColor.rg - 0.5);
+		
+		vec3 upVector = vec3(0.0, 1.0, 0.0);
+		float fresnelTerm = max(dot(angleW, upVector), 0.0);
+		
+		vec3 halfvec = normalize(angleW + lightVectorW + vec3(perturbation.x * fresnelTerm, perturbation.y * fresnelTerm, 0.0) * max(1., glossiness));
+		float temp = pow(dot(halfvec, vNormal), max(1., glossiness));
+		result.specular = specularColor * temp * spotAtten * attenuation;
 #endif
 #endif
 		return result;
 		return result;
 	}
 	}
@@ -404,7 +414,7 @@ lightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightDa
 	return result;
 	return result;
 }
 }
 
 
-lightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor, float glossiness) {
+lightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor, float glossiness, vec3 bumpColor) {
 	lightingInfo result;
 	lightingInfo result;
 
 
 	// Diffuse
 	// Diffuse
@@ -413,10 +423,16 @@ lightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4
 	
 	
 	// Specular
 	// Specular
 #ifdef SPECULARTERM
 #ifdef SPECULARTERM
-	vec3 angleW = normalize(viewDirectionW + lightData.xyz);
-	float specComp = max(0., dot(vNormal, angleW));
-	specComp = pow(specComp, max(1., glossiness));
-	result.specular = specComp * specularColor;
+	vec3 lightVectorW = normalize(vec3(0.1, 0.6, 0.5));
+	vec3 angleW = normalize(viewDirectionW - lightVectorW);
+	vec2 perturbation = bumpHeight * (bumpColor.rg - 0.5);
+	
+	vec3 upVector = vec3(0.0, 1.0, 0.0);
+	float fresnelTerm = max(dot(angleW, upVector), 0.0);
+	
+	vec3 halfvec = normalize(angleW + lightVectorW + vec3(perturbation.x * fresnelTerm, perturbation.y * fresnelTerm, 0.0) * max(1., glossiness));
+	float temp = pow(dot(halfvec, vNormal), max(1., glossiness));
+	result.specular = specularColor * temp;
 #endif
 #endif
 
 
 	return result;
 	return result;
@@ -455,15 +471,26 @@ void main(void) {
 #endif
 #endif
 
 
 	baseColor.rgb *= vNormalInfos.y;
 	baseColor.rgb *= vNormalInfos.y;
+#else
+	vec3 bumpColor = vec3(1.0);
 #endif
 #endif
 
 
 #ifdef VERTEXCOLOR
 #ifdef VERTEXCOLOR
 	baseColor.rgb *= vColor.rgb;
 	baseColor.rgb *= vColor.rgb;
 #endif
 #endif
 
 
+	// Bump
+#ifdef NORMAL
+	vec3 normalW = normalize(vNormalW);
+	vec2 perturbation = bumpHeight * (baseColor.rg - 0.5);
+#else
+	vec3 normalW = vec3(1.0, 1.0, 1.0);
+	vec2 perturbation = bumpHeight * (vec2(1.0, 1.0) - 0.5);
+#endif
+
 #ifdef REFLECTION
 #ifdef REFLECTION
 	// Water
 	// Water
-	vec2 perturbation = bumpHeight * (baseColor.rg - 0.5);
+	vec3 eyeVector = normalize(vEyePosition - vPosition);
 	
 	
 	vec2 projectedRefractionTexCoords = clamp(vRefractionMapTexCoord.xy / vRefractionMapTexCoord.z + perturbation, 0.0, 1.0);
 	vec2 projectedRefractionTexCoords = clamp(vRefractionMapTexCoord.xy / vRefractionMapTexCoord.z + perturbation, 0.0, 1.0);
 	vec4 refractiveColor = texture2D(refractionSampler, projectedRefractionTexCoords);
 	vec4 refractiveColor = texture2D(refractionSampler, projectedRefractionTexCoords);
@@ -471,7 +498,6 @@ void main(void) {
 	vec2 projectedReflectionTexCoords = clamp(vReflectionMapTexCoord.xy / vReflectionMapTexCoord.z + perturbation, 0.0, 1.0);
 	vec2 projectedReflectionTexCoords = clamp(vReflectionMapTexCoord.xy / vReflectionMapTexCoord.z + perturbation, 0.0, 1.0);
 	vec4 reflectiveColor = texture2D(reflectionSampler, projectedReflectionTexCoords);
 	vec4 reflectiveColor = texture2D(reflectionSampler, projectedReflectionTexCoords);
 	
 	
-	vec3 eyeVector = normalize(vEyePosition - vPosition);
 	vec3 upVector = vec3(0.0, 1.0, 0.0);
 	vec3 upVector = vec3(0.0, 1.0, 0.0);
 	
 	
 	float fresnelTerm = max(dot(eyeVector, upVector), 0.0);
 	float fresnelTerm = max(dot(eyeVector, upVector), 0.0);
@@ -481,13 +507,6 @@ void main(void) {
 	baseColor = colorBlendFactor * waterColor + (1.0 - colorBlendFactor) * combinedColor;
 	baseColor = colorBlendFactor * waterColor + (1.0 - colorBlendFactor) * combinedColor;
 #endif
 #endif
 
 
-	// Bump
-#ifdef NORMAL
-	vec3 normalW = normalize(vNormalW);
-#else
-	vec3 normalW = vec3(1.0, 1.0, 1.0);
-#endif
-
 	// Lighting
 	// Lighting
 	vec3 diffuseBase = vec3(0., 0., 0.);
 	vec3 diffuseBase = vec3(0., 0., 0.);
 #ifdef SPECULARTERM
 #ifdef SPECULARTERM
@@ -500,13 +519,13 @@ void main(void) {
 	vec3 vLightSpecular0 = vec3(0.0);
 	vec3 vLightSpecular0 = vec3(0.0);
 #endif
 #endif
 #ifdef SPOTLIGHT0
 #ifdef SPOTLIGHT0
-	lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, glossiness);
+	lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, glossiness, bumpColor);
 #endif
 #endif
 #ifdef HEMILIGHT0
 #ifdef HEMILIGHT0
-	lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0, glossiness);
+	lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0, glossiness, bumpColor);
 #endif
 #endif
 #if defined(POINTLIGHT0) || defined(DIRLIGHT0)
 #if defined(POINTLIGHT0) || defined(DIRLIGHT0)
-	lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, glossiness);
+	lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, glossiness, bumpColor);
 #endif
 #endif
 #ifdef SHADOW0
 #ifdef SHADOW0
 #ifdef SHADOWVSM0
 #ifdef SHADOWVSM0
@@ -531,26 +550,22 @@ void main(void) {
 #endif
 #endif
 	diffuseBase += info.diffuse * shadow;
 	diffuseBase += info.diffuse * shadow;
 #ifdef SPECULARTERM
 #ifdef SPECULARTERM
-#ifdef BUMP
-	specularBase += (info.specular / bumpHeight * (bumpColor.rgb + 0.0)) * shadow;
-#else
 	specularBase += info.specular * shadow;
 	specularBase += info.specular * shadow;
 #endif
 #endif
 #endif
 #endif
-#endif
 
 
 #ifdef LIGHT1
 #ifdef LIGHT1
 #ifndef SPECULARTERM
 #ifndef SPECULARTERM
 	vec3 vLightSpecular1 = vec3(0.0);
 	vec3 vLightSpecular1 = vec3(0.0);
 #endif
 #endif
 #ifdef SPOTLIGHT1
 #ifdef SPOTLIGHT1
-	info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, glossiness;
+	info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, glossiness, bumpColor);
 #endif
 #endif
 #ifdef HEMILIGHT1
 #ifdef HEMILIGHT1
-	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1.a, glossiness);
+	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1.a, glossiness, bumpColor);
 #endif
 #endif
 #if defined(POINTLIGHT1) || defined(DIRLIGHT1)
 #if defined(POINTLIGHT1) || defined(DIRLIGHT1)
-	info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, glossiness);
+	info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, glossiness, bumpColor);
 #endif
 #endif
 #ifdef SHADOW1
 #ifdef SHADOW1
 #ifdef SHADOWVSM1
 #ifdef SHADOWVSM1
@@ -575,26 +590,22 @@ void main(void) {
 #endif
 #endif
 	diffuseBase += info.diffuse * shadow;
 	diffuseBase += info.diffuse * shadow;
 #ifdef SPECULARTERM
 #ifdef SPECULARTERM
-#ifdef BUMP
-	specularBase += (info.specular / bumpHeight * (bumpColor.rgb + 0.0)) * shadow;
-#else
 	specularBase += info.specular * shadow;
 	specularBase += info.specular * shadow;
 #endif
 #endif
 #endif
 #endif
-#endif
 
 
 #ifdef LIGHT2
 #ifdef LIGHT2
 #ifndef SPECULARTERM
 #ifndef SPECULARTERM
 	vec3 vLightSpecular2 = vec3(0.0);
 	vec3 vLightSpecular2 = vec3(0.0);
 #endif
 #endif
 #ifdef SPOTLIGHT2
 #ifdef SPOTLIGHT2
-	info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, glossiness);
+	info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, glossiness, bumpColor);
 #endif
 #endif
 #ifdef HEMILIGHT2
 #ifdef HEMILIGHT2
-	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2, glossiness);
+	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2, glossiness, bumpColor);
 #endif
 #endif
 #if defined(POINTLIGHT2) || defined(DIRLIGHT2)
 #if defined(POINTLIGHT2) || defined(DIRLIGHT2)
-	info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, glossiness);
+	info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, glossiness, bumpColor);
 #endif
 #endif
 #ifdef SHADOW2
 #ifdef SHADOW2
 #ifdef SHADOWVSM2
 #ifdef SHADOWVSM2
@@ -619,26 +630,22 @@ void main(void) {
 #endif
 #endif
 	diffuseBase += info.diffuse * shadow;
 	diffuseBase += info.diffuse * shadow;
 #ifdef SPECULARTERM
 #ifdef SPECULARTERM
-#ifdef BUMP
-	specularBase += (info.specular / bumpHeight * (bumpColor.rgb + 0.0)) * shadow;
-#else
 	specularBase += info.specular * shadow;
 	specularBase += info.specular * shadow;
 #endif
 #endif
 #endif
 #endif
-#endif
 
 
 #ifdef LIGHT3
 #ifdef LIGHT3
 #ifndef SPECULARTERM
 #ifndef SPECULARTERM
 	vec3 vLightSpecular3 = vec3(0.0);
 	vec3 vLightSpecular3 = vec3(0.0);
 #endif
 #endif
 #ifdef SPOTLIGHT3
 #ifdef SPOTLIGHT3
-	info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, glossiness);
+	info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, glossiness, bumpColor);
 #endif
 #endif
 #ifdef HEMILIGHT3
 #ifdef HEMILIGHT3
-	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3, glossiness);
+	info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3, glossiness, bumpColor);
 #endif
 #endif
 #if defined(POINTLIGHT3) || defined(DIRLIGHT3)
 #if defined(POINTLIGHT3) || defined(DIRLIGHT3)
-	info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, glossiness);
+	info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, glossiness, bumpColor);
 #endif
 #endif
 #ifdef SHADOW3
 #ifdef SHADOW3
 #ifdef SHADOWVSM3
 #ifdef SHADOWVSM3
@@ -663,13 +670,9 @@ void main(void) {
 #endif
 #endif
 	diffuseBase += info.diffuse * shadow;
 	diffuseBase += info.diffuse * shadow;
 #ifdef SPECULARTERM
 #ifdef SPECULARTERM
-#ifdef BUMP
-	specularBase += (info.specular / bumpHeight * (bumpColor.rgb + 0.0)) * shadow;
-#else
 	specularBase += info.specular * shadow;
 	specularBase += info.specular * shadow;
 #endif
 #endif
 #endif
 #endif
-#endif
 
 
 #ifdef VERTEXALPHA
 #ifdef VERTEXALPHA
 	alpha *= vColor.a;
 	alpha *= vColor.a;

+ 2 - 2
materialsLibrary/materials/water/water.vertex.fx

@@ -185,8 +185,8 @@ void main(void) {
 #endif
 #endif
 
 
 	vec3 p = position;
 	vec3 p = position;
-	float newY = (sin(((p.x / 0.05) + time * windForce) * windDirection.x) * waveHeight * 5.0) * waveSpeed
-			   + (cos(((p.z / 0.05) + time * windForce) * windDirection.y) * waveHeight * 5.0) * waveSpeed;
+	float newY = (sin(((p.x / 0.05) + time * waveSpeed * windForce) * windDirection.x) * waveHeight * 5.0)
+			   + (cos(((p.z / 0.05) + time * waveSpeed * windForce) * windDirection.y) * waveHeight * 5.0);
 	p.y += abs(newY);
 	p.y += abs(newY);
 	
 	
 	gl_Position = viewProjection * finalWorld * vec4(p, 1.0);
 	gl_Position = viewProjection * finalWorld * vec4(p, 1.0);