瀏覽代碼

Second Pass

sebastien 7 年之前
父節點
當前提交
0c74dde1d8

+ 4 - 3
src/Engine/babylon.engine.ts

@@ -3732,7 +3732,7 @@
             internalTexture.samplingMode = bilinearFiltering ? Texture.BILINEAR_SAMPLINGMODE : Texture.NEAREST_SAMPLINGMODE;
             internalTexture.type = Engine.TEXTURETYPE_UNSIGNED_INT;
             internalTexture._comparisonFunction = comparisonFunction;
-
+            
             var gl = this._gl;
             var target = internalTexture.isCube ? gl.TEXTURE_CUBE_MAP : gl.TEXTURE_2D;
             var samplingParameters = getSamplingParameters(internalTexture.samplingMode, false, gl);
@@ -3760,7 +3760,8 @@
          */
         public createDepthStencilTexture(size: number | { width: number, height: number }, options: DepthTextureCreationOptions) : InternalTexture {
             if (options.isCube) {
-                return this._createDepthStencilCubeTexture(size.width || size, options);
+                let width = (<{ width: number, height: number }>size).width || <number>size;
+                return this._createDepthStencilCubeTexture(width, options);
             }
             else {
                 return this._createDepthStencilTexture(size, options);
@@ -5309,7 +5310,7 @@
 
             let internalTexture: InternalTexture;
             if (depthStencilTexture) {
-                internalTexture = (<RenderTargetTexture>texture).depthStencilTexture;
+                internalTexture = (<RenderTargetTexture>texture).depthStencilTexture!;
             }
             else if (texture.isReady()) {
                 internalTexture = <InternalTexture>texture.getInternalTexture();

+ 1 - 1
src/Lights/Shadows/babylon.shadowGenerator.ts

@@ -625,7 +625,7 @@
             this._shadowMap.onBeforeRenderObservable.add((faceIndex: number) => {
                 this._currentFaceIndex = faceIndex;
                 if (this._useDepthStencilTexture) {
-                    //engine.setColorWrite(false);
+                    engine.setColorWrite(false);
                 }
             });
 

+ 2 - 5
src/Materials/Textures/babylon.internalTexture.ts

@@ -322,13 +322,10 @@ module BABYLON {
                         bilinearFiltering: this.samplingMode !== Texture.BILINEAR_SAMPLINGMODE,
                         comparisonFunction: this._comparisonFunction,
                         generateStencil: this._generateStencilBuffer,
+                        isCube: this.isCube
                     };
 
-                    if (this.isCube) {
-                        proxy = this._engine.createDepthStencilTexture({ width: this.width, height: this.height }, depthTextureOptions);
-                    } else {
-                        proxy = this._engine.createDepthStencilCubeTexture(this.width, depthTextureOptions);
-                    }
+                    proxy = this._engine.createDepthStencilTexture({ width: this.width, height: this.height }, depthTextureOptions);
                     proxy._swapAndDie(this);
 
                     this.isReady = true;

+ 30 - 8
src/Shaders/ShadersInclude/shadowsFragmentFunctions.fx

@@ -7,6 +7,25 @@
 		}
 	#endif
 
+#ifdef USEDEPTHSTENCILTEXTURE
+	float computeShadowFromDepthTextureCube(vec3 lightPosition, samplerCubeShadow shadowSampler, float darkness, vec2 depthValues)
+	{
+		vec4 directionToLight = vec4(vPositionW - lightPosition, 1.0);
+		directionToLight.w = length(directionToLight.xyz);
+		directionToLight.w = (directionToLight.w + depthValues.x) / (depthValues.y);
+		directionToLight.w = clamp(directionToLight.w, 0., 1.0);
+
+		directionToLight.xyz = normalize(directionToLight.xyz);
+		directionToLight.y = -directionToLight.y;
+		
+		float shadow = textureCube(shadowSampler, directionToLight);
+		if (shadow < 1.0)
+		{
+			return shadow * (1. - darkness) + darkness;
+		}
+		return 1.0;
+	}
+#else
 	float computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, vec2 depthValues)
 	{
 		vec3 directionToLight = vPositionW - lightPosition;
@@ -29,6 +48,7 @@
 		}
 		return 1.0;
 	}
+#endif
 
 	float computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, float mapSize, float darkness, vec2 depthValues)
 	{
@@ -110,7 +130,8 @@
 	float computeShadowFromDepthTexture(vec4 vPositionFromLight, float depthMetric, sampler2DShadow shadowSampler, float darkness, float frustumEdgeFalloff)
 	{
 		vec3 clipSpace = vPositionFromLight.xyz / vPositionFromLight.w;
-		vec3 uvDepth = vec3(0.5 * clipSpace.xy + vec2(0.5), 0.9);
+		//vec3 uvDepth = vec3(0.5 * clipSpace.xyz + vec3(0.5));
+		vec3 uvDepth = vec3(0.5 * clipSpace.xy + vec2(0.5), depthMetric);
 
 		if (uvDepth.x < 0. || uvDepth.x > 1.0 || uvDepth.y < 0. || uvDepth.y > 1.0)
 		{
@@ -118,14 +139,14 @@
 		}
 
 		float shadow = texture2D(shadowSampler, uvDepth);
-		// if (shadow < 1.0)
-		// {
-		// 	return computeFallOff((1. - shadow) * darkness, clipSpace.xy, frustumEdgeFalloff);
-		// }
-		return shadow;
+		if (shadow < 1.0)
+		{
+			shadow = shadow * (1. - darkness) + darkness;
+			return computeFallOff(shadow, clipSpace.xy, frustumEdgeFalloff);
+		}
+		return 1.0;
 	}
-#endif
-
+#else
 	float computeShadow(vec4 vPositionFromLight, float depthMetric, sampler2D shadowSampler, float darkness, float frustumEdgeFalloff)
 	{
 		vec3 clipSpace = vPositionFromLight.xyz / vPositionFromLight.w;
@@ -150,6 +171,7 @@
 		}
 		return 1.;
 	}
+#endif
 
 	float computeShadowWithPCF(vec4 vPositionFromLight, float depthMetric, sampler2D shadowSampler, float mapSize, float darkness, float frustumEdgeFalloff)
 	{

+ 1 - 4
src/Shaders/shadowMap.vertex.fx

@@ -30,13 +30,10 @@ void main(void)
 
 vec4 worldPos = finalWorld * vec4(position, 1.0);
 gl_Position = viewProjection * worldPos;
-
+gl_Position.z += biasAndScale.x * depthValues.y;
 
 vDepthMetric = ((gl_Position.z + depthValues.x) / (depthValues.y)) + biasAndScale.x;
 
- gl_Position.z = vDepthMetric;
-// gl_Position.w = 1.0;
-
 #ifdef ALPHATEST
 	#ifdef UV1
 		vUV = vec2(diffuseMatrix * vec4(uv, 1.0, 0.0));