David Catuhe 8 年之前
父节点
当前提交
fd0c59ee39

文件差异内容过多而无法显示
+ 19 - 52
dist/preview release/babylon.core.js


文件差异内容过多而无法显示
+ 6885 - 6885
dist/preview release/babylon.d.ts


文件差异内容过多而无法显示
+ 23 - 68
dist/preview release/babylon.js


文件差异内容过多而无法显示
+ 22 - 15
dist/preview release/babylon.max.js


文件差异内容过多而无法显示
+ 6885 - 6885
dist/preview release/babylon.module.d.ts


文件差异内容过多而无法显示
+ 22 - 65
dist/preview release/babylon.noworker.js


+ 4 - 2
src/Lights/Shadows/babylon.shadowGenerator.ts

@@ -195,7 +195,7 @@
                     this._shadowMap2 = new RenderTargetTexture(light.name + "_shadowMap", mapSize, this._scene, false, true, textureType);
                     this._shadowMap2.wrapU = Texture.CLAMP_ADDRESSMODE;
                     this._shadowMap2.wrapV = Texture.CLAMP_ADDRESSMODE;
-                    this._shadowMap2.updateSamplingMode(Texture.TRILINEAR_SAMPLINGMODE);
+                    this._shadowMap2.updateSamplingMode(Texture.BILINEAR_SAMPLINGMODE);
 
                     this._downSamplePostprocess = new PassPostProcess("downScale", 1.0 / this.blurScale, null, Texture.BILINEAR_SAMPLINGMODE, this._scene.getEngine());
                     this._downSamplePostprocess.onApplyObservable.add(effect => {
@@ -231,6 +231,8 @@
                     mesh._bind(subMesh, this._effect, Material.TriangleFillMode);
                     var material = subMesh.getMaterial();
 
+                    this._effect.setFloat("bias", this.bias);
+
                     this._effect.setMatrix("viewProjection", this.getTransformMatrix());
                     this._effect.setVector3("lightPosition", this.getLight().position);
 
@@ -364,7 +366,7 @@
                 this._cachedDefines = join;
                 this._effect = this._scene.getEngine().createEffect("shadowMap",
                     attribs,
-                    ["world", "mBones", "viewProjection", "diffuseMatrix", "lightPosition", "depthValues"],
+                    ["world", "mBones", "viewProjection", "diffuseMatrix", "lightPosition", "depthValues", "bias"],
                     ["diffuseSampler"], join);
             }
 

+ 1 - 1
src/Materials/babylon.materialHelper.ts

@@ -235,7 +235,7 @@
                     }
                 }
                 effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
-                effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.blurScale / shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
+                effect.setFloat2("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.blurScale / shadowGenerator.getShadowMap().getSize().width);
             }
 
             return depthValuesAlreadySet;

+ 6 - 6
src/Shaders/ShadersInclude/lightFragment.fx

@@ -17,22 +17,22 @@
     #endif
 	#ifdef SHADOW{X}
 		#ifdef SHADOWESM{X}
-			shadow = computeShadowWithESM(vPositionFromLight{X}, shadowSampler{X}, shadowsInfo{X}.z, shadowsInfo{X}.x);
+			shadow = computeShadowWithESM(vPositionFromLight{X}, shadowSampler{X}, shadowsInfo{X}.x);
 		#else	
 			#ifdef SHADOWVSM{X}
-				shadow = computeShadowWithVSM(vPositionFromLight{X}, shadowSampler{X}, shadowsInfo{X}.z, shadowsInfo{X}.x);
+				shadow = computeShadowWithVSM(vPositionFromLight{X}, shadowSampler{X}, shadowsInfo{X}.x);
 			#else
 			#ifdef SHADOWPCF{X}
 				#if defined(POINTLIGHT{X})
-					shadow = computeShadowWithPCFCube(vLightData{X}.xyz, shadowSampler{X}, shadowsInfo{X}.y, shadowsInfo{X}.z, shadowsInfo{X}.x);
+					shadow = computeShadowWithPCFCube(vLightData{X}.xyz, shadowSampler{X}, shadowsInfo{X}.y, shadowsInfo{X}.x);
 				#else
-					shadow = computeShadowWithPCF(vPositionFromLight{X}, shadowSampler{X}, shadowsInfo{X}.y, shadowsInfo{X}.z, shadowsInfo{X}.x);
+					shadow = computeShadowWithPCF(vPositionFromLight{X}, shadowSampler{X}, shadowsInfo{X}.y, shadowsInfo{X}.x);
 				#endif
 			#else
 				#if defined(POINTLIGHT{X})
-					shadow = computeShadowCube(vLightData{X}.xyz, shadowSampler{X}, shadowsInfo{X}.x, shadowsInfo{X}.z);
+					shadow = computeShadowCube(vLightData{X}.xyz, shadowSampler{X}, shadowsInfo{X}.x);
 				#else
-					shadow = computeShadow(vPositionFromLight{X}, shadowSampler{X}, shadowsInfo{X}.x, shadowsInfo{X}.z);
+					shadow = computeShadow(vPositionFromLight{X}, shadowSampler{X}, shadowsInfo{X}.x);
 				#endif
 			#endif
 		#endif

+ 1 - 1
src/Shaders/ShadersInclude/lightFragmentDeclaration.fx

@@ -11,7 +11,7 @@
 		#else
 			uniform samplerCube shadowSampler{X};
 		#endif
-		uniform vec3 shadowsInfo{X};
+		uniform vec2 shadowsInfo{X};
 	#endif
 	#ifdef SPOTLIGHT{X}
 		uniform vec4 vLightDirection{X};

+ 36 - 41
src/Shaders/ShadersInclude/shadowsFragmentFunctions.fx

@@ -9,7 +9,7 @@
 
 	uniform vec2 depthValues;
 
-	float computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, float bias)
+	float computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness)
 	{
 		vec3 directionToLight = vPositionW - lightPosition;
 		float depth = length(directionToLight);
@@ -20,9 +20,9 @@
 		directionToLight.y = -directionToLight.y;
 		
 		#ifndef SHADOWFULLFLOAT
-			float shadow = unpack(textureCube(shadowSampler, directionToLight)) + bias;
+			float shadow = unpack(textureCube(shadowSampler, directionToLight));
 		#else
-			float shadow = textureCube(shadowSampler, directionToLight).x + bias;
+			float shadow = textureCube(shadowSampler, directionToLight).x;
 		#endif
 
 		if (depth > shadow)
@@ -32,7 +32,7 @@
 		return 1.0;
 	}
 
-	float computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, float mapSize, float bias, float darkness)
+	float computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, float mapSize, float darkness)
 	{
 		vec3 directionToLight = vPositionW - lightPosition;
 		float depth = length(directionToLight);
@@ -52,24 +52,23 @@
 		poissonDisk[3] = vec3(1.0, -1.0, 1.0);
 
 		// Poisson Sampling
-		float biasedDepth = depth - bias;
 
 		#ifndef SHADOWFULLFLOAT
-			if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[0] * mapSize)) < biasedDepth) visibility -= 0.25;
-			if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[1] * mapSize)) < biasedDepth) visibility -= 0.25;
-			if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[2] * mapSize)) < biasedDepth) visibility -= 0.25;
-			if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[3] * mapSize)) < biasedDepth) visibility -= 0.25;
+			if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[0] * mapSize)) < depth) visibility -= 0.25;
+			if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[1] * mapSize)) < depth) visibility -= 0.25;
+			if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[2] * mapSize)) < depth) visibility -= 0.25;
+			if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[3] * mapSize)) < depth) visibility -= 0.25;
 		#else
-			if (textureCube(shadowSampler, directionToLight + poissonDisk[0] * mapSize).x < biasedDepth) visibility -= 0.25;
-			if (textureCube(shadowSampler, directionToLight + poissonDisk[1] * mapSize).x < biasedDepth) visibility -= 0.25;
-			if (textureCube(shadowSampler, directionToLight + poissonDisk[2] * mapSize).x < biasedDepth) visibility -= 0.25;
-			if (textureCube(shadowSampler, directionToLight + poissonDisk[3] * mapSize).x < biasedDepth) visibility -= 0.25;
+			if (textureCube(shadowSampler, directionToLight + poissonDisk[0] * mapSize).x < depth) visibility -= 0.25;
+			if (textureCube(shadowSampler, directionToLight + poissonDisk[1] * mapSize).x < depth) visibility -= 0.25;
+			if (textureCube(shadowSampler, directionToLight + poissonDisk[2] * mapSize).x < depth) visibility -= 0.25;
+			if (textureCube(shadowSampler, directionToLight + poissonDisk[3] * mapSize).x < depth) visibility -= 0.25;
 		#endif
 
 		return  min(1.0, visibility + darkness);
 	}
 
-	float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)
+	float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness)
 	{
 		vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
 		depth = 0.5 * depth + vec3(0.5);
@@ -81,9 +80,9 @@
 		}
 
 		#ifndef SHADOWFULLFLOAT
-			float shadow = unpack(texture2D(shadowSampler, uv)) + bias;
+			float shadow = unpack(texture2D(shadowSampler, uv));
 		#else
-			float shadow = texture2D(shadowSampler, uv).x + bias;
+			float shadow = texture2D(shadowSampler, uv).x;
 		#endif
 
 		if (depth.z > shadow)
@@ -93,7 +92,7 @@
 		return 1.;
 	}
 
-	float computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, float mapSize, float bias, float darkness)
+	float computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, float mapSize, float darkness)
 	{
 		vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
 		depth = 0.5 * depth + vec3(0.5);
@@ -113,18 +112,17 @@
 		poissonDisk[3] = vec2(0.34495938, 0.29387760);
 
 		// Poisson Sampling
-		float biasedDepth = depth.z - bias;
 
 		#ifndef SHADOWFULLFLOAT
-			if (unpack(texture2D(shadowSampler, uv + poissonDisk[0] * mapSize)) < biasedDepth) visibility -= 0.25;
-			if (unpack(texture2D(shadowSampler, uv + poissonDisk[1] * mapSize)) < biasedDepth) visibility -= 0.25;
-			if (unpack(texture2D(shadowSampler, uv + poissonDisk[2] * mapSize)) < biasedDepth) visibility -= 0.25;
-			if (unpack(texture2D(shadowSampler, uv + poissonDisk[3] * mapSize)) < biasedDepth) visibility -= 0.25;
+			if (unpack(texture2D(shadowSampler, uv + poissonDisk[0] * mapSize)) < depth.z) visibility -= 0.25;
+			if (unpack(texture2D(shadowSampler, uv + poissonDisk[1] * mapSize)) < depth.z) visibility -= 0.25;
+			if (unpack(texture2D(shadowSampler, uv + poissonDisk[2] * mapSize)) < depth.z) visibility -= 0.25;
+			if (unpack(texture2D(shadowSampler, uv + poissonDisk[3] * mapSize)) < depth.z) visibility -= 0.25;
 		#else
-			if (texture2D(shadowSampler, uv + poissonDisk[0] * mapSize).x < biasedDepth) visibility -= 0.25;
-			if (texture2D(shadowSampler, uv + poissonDisk[1] * mapSize).x < biasedDepth) visibility -= 0.25;
-			if (texture2D(shadowSampler, uv + poissonDisk[2] * mapSize).x < biasedDepth) visibility -= 0.25;
-			if (texture2D(shadowSampler, uv + poissonDisk[3] * mapSize).x < biasedDepth) visibility -= 0.25;
+			if (texture2D(shadowSampler, uv + poissonDisk[0] * mapSize).x < depth.z) visibility -= 0.25;
+			if (texture2D(shadowSampler, uv + poissonDisk[1] * mapSize).x < depth.z) visibility -= 0.25;
+			if (texture2D(shadowSampler, uv + poissonDisk[2] * mapSize).x < depth.z) visibility -= 0.25;
+			if (texture2D(shadowSampler, uv + poissonDisk[3] * mapSize).x < depth.z) visibility -= 0.25;
 		#endif
 
 		return  min(1.0, visibility + darkness);
@@ -142,9 +140,9 @@
 		return clamp((v - low) / (high - low), 0.0, 1.0);
 	}
 
-	float ChebychevInequality(vec2 moments, float compare, float bias)
+	float ChebychevInequality(vec2 moments, float compare)
 	{
-		float p = smoothstep(compare - bias, compare, moments.x);
+		float p = smoothstep(compare, compare, moments.x);
 		float variance = max(moments.y - moments.x * moments.x, 0.02);
 		float d = compare - moments.x;
 		float p_max = linstep(0.2, 1.0, variance / (variance + d * d));
@@ -152,7 +150,7 @@
 		return clamp(max(p, p_max), 0.0, 1.0);
 	}
 
-	float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, float bias, float darkness)
+	float computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness)
 	{
 		vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;
 		depth = 0.5 * depth + vec3(0.5);
@@ -171,15 +169,15 @@
 			vec2 moments = texel.xy;
 		#endif
 
-		return min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);
+		return min(1.0, 1.0 - ChebychevInequality(moments, depth.z) + darkness);
 	}
 
-	float computeShadowWithESM(vec4 vPositionFromLight, sampler2D shadowSampler, float bias, float darkness)
+	float computeShadowWithESM(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness)
 	{
 		vec3 clipSpace = vPositionFromLight.xyz / vPositionFromLight.w;
 		vec3 depth = 0.5 * clipSpace + vec3(0.5);
 		vec2 uv = depth.xy;
-		float shadowPixelDepth = depth.z;
+		float shadowPixelDepth = clipSpace.z;
 
 		if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)
 		{
@@ -193,16 +191,13 @@
 		#endif
 
 		const float shadowStrength = 50.0;
-	//	const float exponentLimit = 87.0; //this prevents float overflow in the exp, should be ~< ln(MAX_FLOAT)
-	//	float delta = shadowStrength * (shadowMapSample - shadowPixelDepth); // note: farclip - nearclip?
-	//	float esm = exp(clamp(delta, -exponentLimit, 0.0));
-		float esm = exp(shadowStrength * shadowPixelDepth) * shadowMapSample;
-		return esm;
+		float esm = clamp(exp(shadowStrength * shadowPixelDepth) * shadowMapSample + darkness, 0., 1.);
+
 		// Apply fade out at frustum edge
-		// const float fadeDistance = 0.07;
-		// vec2 cs2 = clipSpace.xy * clipSpace.xy; //squarish falloff
-		// float mask = smoothstep(1.0, 1.0 - fadeDistance, dot(cs2, cs2));
+		const float fadeDistance = 0.07;
+		vec2 cs2 = clipSpace.xy * clipSpace.xy; //squarish falloff
+		float mask = smoothstep(1.0, 1.0 - fadeDistance, dot(cs2, cs2));
 
-		//return mix(1.0, esm, mask) + darkness;
+		return mix(1.0, esm, mask);
 	}
 #endif

+ 4 - 0
src/Shaders/shadowMap.fragment.fx

@@ -32,6 +32,8 @@ uniform vec3 lightPosition;
 uniform vec2 depthValues;
 #endif
 
+uniform float bias;
+
 void main(void)
 {
 #ifdef ALPHATEST
@@ -50,6 +52,8 @@ void main(void)
 	depth = depth * 0.5 + 0.5;	
 #endif
 
+	depth += bias;
+
 #ifdef ESM
 	const float shadowStrength = 50.0;
 	depth = exp(-shadowStrength * depth);