Parcourir la source

Merge remote-tracking branch 'refs/remotes/origin/master' into BabylonJS/master

László Matuska il y a 9 ans
Parent
commit
e932348998

+ 30 - 4
materialsLibrary/materials/water/babylon.waterMaterial.ts

@@ -18,6 +18,8 @@ module BABYLON {
         public BonesPerMesh = 0;
         public BonesPerMesh = 0;
         public INSTANCES = false;
         public INSTANCES = false;
         public SPECULARTERM = false;
         public SPECULARTERM = false;
+        public LOGARITHMICDEPTH = false;
+
 
 
         constructor() {
         constructor() {
             super();
             super();
@@ -106,8 +108,10 @@ module BABYLON {
 
 
         private _defines = new WaterMaterialDefines();
         private _defines = new WaterMaterialDefines();
         private _cachedDefines = new WaterMaterialDefines();
         private _cachedDefines = new WaterMaterialDefines();
-		
-		/**
+
+        private _useLogarithmicDepth: boolean;
+
+        /**
 		* Constructor
 		* Constructor
 		*/
 		*/
 		constructor(name: string, scene: Scene, public renderTargetSize: Vector2 = new Vector2(512, 512)) {
 		constructor(name: string, scene: Scene, public renderTargetSize: Vector2 = new Vector2(512, 512)) {
@@ -116,7 +120,16 @@ module BABYLON {
 			// Create render targets
 			// Create render targets
 			this._createRenderTargets(scene, renderTargetSize);
 			this._createRenderTargets(scene, renderTargetSize);
         }
         }
-		
+
+        @serialize()
+        public get useLogarithmicDepth(): boolean {
+            return this._useLogarithmicDepth;
+        }
+
+        public set useLogarithmicDepth(value: boolean) {
+            this._useLogarithmicDepth = value && this.getScene().getEngine().getCaps().fragmentDepthSupported;
+        }
+
         // Get / Set
         // Get / Set
         public get refractionTexture(): RenderTargetTexture {
         public get refractionTexture(): RenderTargetTexture {
             return this._refractionRTT;
             return this._refractionRTT;
@@ -228,6 +241,10 @@ module BABYLON {
                 this._defines.POINTSIZE = true;
                 this._defines.POINTSIZE = true;
             }
             }
 
 
+            if (this.useLogarithmicDepth) {
+                this._defines.LOGARITHMICDEPTH = true;
+            }
+
             // Fog
             // Fog
             if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
             if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
                 this._defines.FOG = true;
                 this._defines.FOG = true;
@@ -284,6 +301,10 @@ module BABYLON {
                     fallbacks.addFallback(1, "FOG");
                     fallbacks.addFallback(1, "FOG");
                 }
                 }
 
 
+                if (this._defines.LOGARITHMICDEPTH) {
+                    fallbacks.addFallback(0, "LOGARITHMICDEPTH");
+                }
+
                 MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks, this.maxSimultaneousLights);
                 MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks, this.maxSimultaneousLights);
              
              
                 if (this._defines.NUM_BONE_INFLUENCERS > 0) {
                 if (this._defines.NUM_BONE_INFLUENCERS > 0) {
@@ -320,6 +341,8 @@ module BABYLON {
                     "vNormalInfos", 
                     "vNormalInfos", 
                     "mBones",
                     "mBones",
                     "vClipPlane", "normalMatrix",
                     "vClipPlane", "normalMatrix",
+                    "logarithmicDepthConstant",
+
                     // Water
                     // Water
                     "worldReflectionViewProjection", "windDirection", "waveLength", "time", "windForce",
                     "worldReflectionViewProjection", "windDirection", "waveLength", "time", "windForce",
                     "cameraPosition", "bumpHeight", "waveHeight", "waterColor", "colorBlendFactor", "waveSpeed"
                     "cameraPosition", "bumpHeight", "waveHeight", "waterColor", "colorBlendFactor", "waveSpeed"
@@ -403,7 +426,10 @@ module BABYLON {
 
 
             // Fog
             // Fog
             MaterialHelper.BindFogParameters(scene, mesh, this._effect);
             MaterialHelper.BindFogParameters(scene, mesh, this._effect);
-            
+
+            // Log. depth
+            MaterialHelper.BindLogDepth(this._defines, this._effect, scene);
+
             // Water
             // Water
             if (StandardMaterial.ReflectionTextureEnabled) {
             if (StandardMaterial.ReflectionTextureEnabled) {
                 this._effect.setTexture("refractionSampler", this._refractionRTT);
                 this._effect.setTexture("refractionSampler", this._refractionRTT);

+ 21 - 8
materialsLibrary/materials/water/water.fragment.fx

@@ -1,3 +1,7 @@
+#ifdef LOGARITHMICDEPTH
+#extension GL_EXT_frag_depth : enable
+#endif
+
 precision highp float;
 precision highp float;
 
 
 // Constants
 // Constants
@@ -51,6 +55,7 @@ varying vec3 vReflectionMapTexCoord;
 varying vec3 vPosition;
 varying vec3 vPosition;
 
 
 #include<clipPlaneFragmentDeclaration>
 #include<clipPlaneFragmentDeclaration>
+#include<logDepthDeclaration>
 
 
 // Fog
 // Fog
 #include<fogFragmentDeclaration>
 #include<fogFragmentDeclaration>
@@ -69,7 +74,8 @@ void main(void) {
 	float alpha = vDiffuseColor.a;
 	float alpha = vDiffuseColor.a;
 
 
 #ifdef BUMP
 #ifdef BUMP
-	baseColor = texture2D(normalSampler, vNormalUV);
+    //smaller bumps superimposed (better moving waves, no "conveyor belt" look):
+	baseColor = (texture2D(normalSampler, vNormalUV) + texture2D(normalSampler,vec2(-vNormalUV.y*0.33,vNormalUV.x*0.33)))/2.0;
 	vec3 bumpColor = baseColor.rgb;
 	vec3 bumpColor = baseColor.rgb;
 
 
 #ifdef ALPHATEST
 #ifdef ALPHATEST
@@ -88,8 +94,9 @@ void main(void) {
 
 
 	// Bump
 	// Bump
 #ifdef NORMAL
 #ifdef NORMAL
-	vec3 normalW = normalize(vNormalW);
+    //reflection angle is also perturbed
 	vec2 perturbation = bumpHeight * (baseColor.rg - 0.5);
 	vec2 perturbation = bumpHeight * (baseColor.rg - 0.5);
+	vec3 normalW = normalize(vNormalW + vec3(perturbation.x*3.0,perturbation.y*3.0,0.0));
 #else
 #else
 	vec3 normalW = vec3(1.0, 1.0, 1.0);
 	vec3 normalW = vec3(1.0, 1.0, 1.0);
 	vec2 perturbation = bumpHeight * (vec2(1.0, 1.0) - 0.5);
 	vec2 perturbation = bumpHeight * (vec2(1.0, 1.0) - 0.5);
@@ -101,17 +108,21 @@ void main(void) {
 	
 	
 	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);
-	
+
+    //refraction (sea bed) combined with the water color: TODO: fog-like shallow underwater
+    refractiveColor = colorBlendFactor*waterColor + (1.0-colorBlendFactor)*refractiveColor;
+
 	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 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);
+
+	//physically correct water reflection by look angle
+	float fresnelTerm = min(0.95, pow(max(dot(eyeVector, upVector), 0.0),1.5));
 	
 	
 	vec4 combinedColor = refractiveColor * fresnelTerm + reflectiveColor * (1.0 - fresnelTerm);
 	vec4 combinedColor = refractiveColor * fresnelTerm + reflectiveColor * (1.0 - fresnelTerm);
 	
 	
-	baseColor = colorBlendFactor * waterColor + (1.0 - colorBlendFactor) * combinedColor;
+	baseColor = combinedColor;
 #endif
 #endif
 
 
 	// Lighting
 	// Lighting
@@ -134,16 +145,18 @@ void main(void) {
 #endif
 #endif
 
 
 #ifdef SPECULARTERM
 #ifdef SPECULARTERM
-	vec3 finalSpecular = specularBase * specularColor;
+    //specular glare: more concentrated (no flat surface, specular is only for hard lights)
+	vec3 finalSpecular = specularBase * 2.0 * specularColor * specularColor;
 #else
 #else
 	vec3 finalSpecular = vec3(0.0);
 	vec3 finalSpecular = vec3(0.0);
 #endif
 #endif
 
 
-	vec3 finalDiffuse = clamp(diffuseBase * diffuseColor, 0.0, 1.0) * baseColor.rgb;
+	vec3 finalDiffuse = clamp(baseColor.rgb, 0.0, 1.0);
 
 
 	// Composition
 	// Composition
 	vec4 color = vec4(finalDiffuse + finalSpecular, alpha);
 	vec4 color = vec4(finalDiffuse + finalSpecular, alpha);
 
 
+#include<logDepthFragment>
 #include<fogFragment>
 #include<fogFragment>
 	
 	
 	gl_FragColor = color;
 	gl_FragColor = color;

+ 7 - 0
materialsLibrary/materials/water/water.vertex.fx

@@ -48,6 +48,8 @@ varying vec4 vColor;
 #include<fogVertexDeclaration>
 #include<fogVertexDeclaration>
 #include<shadowsVertexDeclaration>[0..maxSimultaneousLights]
 #include<shadowsVertexDeclaration>[0..maxSimultaneousLights]
 
 
+#include<logDepthDeclaration>
+
 // Water uniforms
 // Water uniforms
 uniform mat4 worldReflectionViewProjection;
 uniform mat4 worldReflectionViewProjection;
 uniform vec2 windDirection;
 uniform vec2 windDirection;
@@ -62,6 +64,8 @@ varying vec3 vPosition;
 varying vec3 vRefractionMapTexCoord;
 varying vec3 vRefractionMapTexCoord;
 varying vec3 vReflectionMapTexCoord;
 varying vec3 vReflectionMapTexCoord;
 
 
+
+
 void main(void) {
 void main(void) {
 
 
     #include<instancesVertex>
     #include<instancesVertex>
@@ -134,4 +138,7 @@ void main(void) {
 	vReflectionMapTexCoord.y = 0.5 * (worldPos.w + worldPos.y);
 	vReflectionMapTexCoord.y = 0.5 * (worldPos.w + worldPos.y);
 	vReflectionMapTexCoord.z = worldPos.w;
 	vReflectionMapTexCoord.z = worldPos.w;
 #endif
 #endif
+
+#include<logDepthVertex>
+
 }
 }

+ 6 - 0
src/Lights/babylon.light.ts

@@ -42,6 +42,12 @@
         @serialize()
         @serialize()
         public excludeWithLayerMask = 0;
         public excludeWithLayerMask = 0;
 
 
+        @serialize()
+        public lightmapExcluded = false;
+
+        @serialize()
+        public lightmapNoSpecular = false;
+
         // PBR Properties.
         // PBR Properties.
         @serialize()
         @serialize()
         public radius = 0.00001;
         public radius = 0.00001;

+ 24 - 0
src/Materials/babylon.materialHelper.ts

@@ -5,6 +5,7 @@
             var needNormals = false;
             var needNormals = false;
             var needRebuild = false;
             var needRebuild = false;
             var needShadows = false;
             var needShadows = false;
+            var lightmapExcluded = false;
 
 
             for (var index = 0; index < scene.lights.length; index++) {
             for (var index = 0; index < scene.lights.length; index++) {
                 var light = scene.lights[index];
                 var light = scene.lights[index];
@@ -103,6 +104,22 @@
                     }
                     }
                 }
                 }
 
 
+                //lightmapExcluded
+                if (defines["LIGHTMAPEXCLUDED" + lightIndex] === undefined) {
+                    needRebuild = true;
+                }
+                defines["LIGHTMAPEXCLUDED" + lightIndex] = light.lightmapExcluded;
+                if (light.lightmapExcluded) {
+                    lightmapExcluded = true;
+                }
+
+                //lightmapNoSpecular
+                if (defines["LIGHTMAPNOSPECULAR" + lightIndex] === undefined) {
+                    needRebuild = true;
+                }
+                defines["LIGHTMAPNOSPECULAR" + lightIndex] = light.lightmapNoSpecular;
+
+
                 lightIndex++;
                 lightIndex++;
                 if (lightIndex === maxSimultaneousLights)
                 if (lightIndex === maxSimultaneousLights)
                     break;
                     break;
@@ -117,6 +134,13 @@
                 defines["SHADOWFULLFLOAT"] = true;
                 defines["SHADOWFULLFLOAT"] = true;
             }
             }
 
 
+            if (defines["LIGHTMAPEXCLUDED"] === undefined) {
+                needRebuild = true;
+            }
+            if (lightmapExcluded) {
+                defines["LIGHTMAPEXCLUDED"] = true;
+            }
+
             if (needRebuild) {
             if (needRebuild) {
                 defines.rebuild();
                 defines.rebuild();
             }
             }

+ 28 - 15
src/Shaders/ShadersInclude/lightFragment.fx

@@ -1,16 +1,20 @@
 #ifdef LIGHT{X}
 #ifdef LIGHT{X}
-	#ifndef SPECULARTERM
-		vec3 vLightSpecular{X} = vec3(0.);
-	#endif
-	#ifdef SPOTLIGHT{X}
-		info = computeSpotLighting(viewDirectionW, normalW, vLightData{X}, vLightDirection{X}, vLightDiffuse{X}.rgb, vLightSpecular{X}, vLightDiffuse{X}.a, glossiness);
-	#endif
-	#ifdef HEMILIGHT{X}
-		info = computeHemisphericLighting(viewDirectionW, normalW, vLightData{X}, vLightDiffuse{X}.rgb, vLightSpecular{X}, vLightGround{X}, glossiness);
-	#endif
-	#if defined(POINTLIGHT{X}) || defined(DIRLIGHT{X})
-		info = computeLighting(viewDirectionW, normalW, vLightData{X}, vLightDiffuse{X}.rgb, vLightSpecular{X}, vLightDiffuse{X}.a, glossiness);
-	#endif
+    #if defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED{X}) && defined(LIGHTMAPNOSPECULAR{X})
+        //No light calculation
+    #else
+        #ifndef SPECULARTERM
+            vec3 vLightSpecular{X} = vec3(0.);
+        #endif
+        #ifdef SPOTLIGHT{X}
+            info = computeSpotLighting(viewDirectionW, normalW, vLightData{X}, vLightDirection{X}, vLightDiffuse{X}.rgb, vLightSpecular{X}, vLightDiffuse{X}.a, glossiness);
+        #endif
+        #ifdef HEMILIGHT{X}
+            info = computeHemisphericLighting(viewDirectionW, normalW, vLightData{X}, vLightDiffuse{X}.rgb, vLightSpecular{X}, vLightGround{X}, glossiness);
+        #endif
+        #if defined(POINTLIGHT{X}) || defined(DIRLIGHT{X})
+            info = computeLighting(viewDirectionW, normalW, vLightData{X}, vLightDiffuse{X}.rgb, vLightSpecular{X}, vLightDiffuse{X}.a, glossiness);
+        #endif
+    #endif
 	#ifdef SHADOW{X}
 	#ifdef SHADOW{X}
 		#ifdef SHADOWVSM{X}
 		#ifdef SHADOWVSM{X}
 			shadow = computeShadowWithVSM(vPositionFromLight{X}, shadowSampler{X}, shadowsInfo{X}.z, shadowsInfo{X}.x);
 			shadow = computeShadowWithVSM(vPositionFromLight{X}, shadowSampler{X}, shadowsInfo{X}.z, shadowsInfo{X}.x);
@@ -32,8 +36,17 @@
 	#else
 	#else
 		shadow = 1.;
 		shadow = 1.;
 	#endif
 	#endif
-		diffuseBase += info.diffuse * shadow;
-	#ifdef SPECULARTERM
-		specularBase += info.specular * shadow;
+    #if defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED{X})
+	    diffuseBase += lightmapColor * shadow;
+	    #ifdef SPECULARTERM
+            #ifndef LIGHTMAPNOSPECULAR{X}
+                specularBase += info.specular * shadow * lightmapColor;
+            #endif
+        #endif
+    #else
+	    diffuseBase += info.diffuse * shadow;
+	    #ifdef SPECULARTERM
+		    specularBase += info.specular * shadow;
+	    #endif
 	#endif
 	#endif
 #endif
 #endif

+ 12 - 7
src/Shaders/default.fragment.fx

@@ -232,6 +232,10 @@ void main(void) {
 #endif
 #endif
 	float shadow = 1.;
 	float shadow = 1.;
 
 
+#ifdef LIGHTMAP
+	vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV).rgb * vLightmapInfos.y;
+#endif
+
 #include<lightFragment>[0..maxSimultaneousLights]
 #include<lightFragment>[0..maxSimultaneousLights]
 
 
 	// Refraction
 	// Refraction
@@ -387,14 +391,15 @@ void main(void) {
 	vec4 color = vec4(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor + refractionColor, alpha);
 	vec4 color = vec4(finalDiffuse * baseAmbientColor + finalSpecular + reflectionColor + refractionColor, alpha);
 #endif
 #endif
 
 
+//Old lightmap calculation method
 #ifdef LIGHTMAP
 #ifdef LIGHTMAP
-	vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV).rgb * vLightmapInfos.y;
-
-#ifdef USELIGHTMAPASSHADOWMAP
-	color.rgb *= lightmapColor;
-#else
-	color.rgb += lightmapColor;
-#endif
+    #ifndef LIGHTMAPEXCLUDED
+        #ifdef USELIGHTMAPASSHADOWMAP
+            color.rgb *= lightmapColor;
+        #else
+            color.rgb += lightmapColor;
+        #endif
+    #endif
 #endif
 #endif
 
 
 #include<logDepthFragment>
 #include<logDepthFragment>