fix fragment depth being translated to clip space
							parent
							
								
									3540e098d5
								
							
						
					
					
						commit
						345ffaa247
					
				
										
											Binary file not shown.
										
									
								
							|  | @ -75,21 +75,27 @@ float GeometrySmith(float3 N, float3 V, float3 L, float roughness) | ||||||
| 
 | 
 | ||||||
| float ComputeShadow(float4 positionLightSpace) | float ComputeShadow(float4 positionLightSpace) | ||||||
| { | { | ||||||
|     float bias = 0.01; |     float bias = 0.001; //0.005;  | ||||||
| 
 | 
 | ||||||
|     // maps to [-1, 1] |     // maps to [-1, 1] | ||||||
|     float3 projectionCoords = positionLightSpace.xyz / positionLightSpace.w; |     float3 projectionCoords = positionLightSpace.xyz / positionLightSpace.w; | ||||||
| 
 | 
 | ||||||
|     // maps to [0, 1] |     // maps to [0, 1] | ||||||
|     projectionCoords = (projectionCoords * 0.5) + 0.5; |     projectionCoords.x = (projectionCoords.x * 0.5) + 0.5; | ||||||
|  |     projectionCoords.y = (projectionCoords.y * 0.5) + 0.5; | ||||||
|     projectionCoords.y *= -1; |     projectionCoords.y *= -1; | ||||||
| 
 | 
 | ||||||
|     float closestDepth = SAMPLE_TEXTURE(shadowMap, projectionCoords.xy).r; |     float closestDepth = SAMPLE_TEXTURE(shadowMap, projectionCoords.xy).r; | ||||||
|     float currentDepth = projectionCoords.z; |     float currentDepth = projectionCoords.z; | ||||||
| 
 | 
 | ||||||
|     float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0; |     if (currentDepth - bias > closestDepth) | ||||||
| 
 |     { | ||||||
|     return shadow; |         return 1.0; | ||||||
|  |     } | ||||||
|  |     else | ||||||
|  |     { | ||||||
|  |         return 0.0; | ||||||
|  |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| float3 ComputeLight( | float3 ComputeLight( | ||||||
|  |  | ||||||
|  | @ -52,8 +52,8 @@ namespace Kav | ||||||
|         { |         { | ||||||
|             if ((dirtyFlags & EffectDirtyFlags.WorldViewProj) != 0) |             if ((dirtyFlags & EffectDirtyFlags.WorldViewProj) != 0) | ||||||
|             { |             { | ||||||
|                 Matrix.Multiply(ref model, ref view, out Matrix modelView); |                 Matrix.Multiply(ref view, ref projection, out Matrix viewProjection); | ||||||
|                 Matrix.Multiply(ref modelView, ref projection, out Matrix worldViewProj); |                 Matrix.Multiply(ref model, ref viewProjection, out Matrix worldViewProj); | ||||||
| 
 | 
 | ||||||
|                 modelViewProjectionParam.SetValue(worldViewProj); |                 modelViewProjectionParam.SetValue(worldViewProj); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										35
									
								
								Renderer.cs
								
								
								
								
							
							
						
						
									
										35
									
								
								Renderer.cs
								
								
								
								
							|  | @ -181,32 +181,43 @@ namespace Kav | ||||||
|              |              | ||||||
|             var right = Vector3.Cross(Vector3.Up, directionalLight.Direction); |             var right = Vector3.Cross(Vector3.Up, directionalLight.Direction); | ||||||
|             var up = Vector3.Cross(directionalLight.Direction, right); |             var up = Vector3.Cross(directionalLight.Direction, right); | ||||||
| 
 |  | ||||||
|             var lightRotation = Matrix.CreateLookAt(Vector3.Zero, -directionalLight.Direction, up); |  | ||||||
| 
 |  | ||||||
|             var cameraBoundingFrustum = new BoundingFrustum(camera.View * camera.Projection); |             var cameraBoundingFrustum = new BoundingFrustum(camera.View * camera.Projection); | ||||||
| 
 |  | ||||||
|             Vector3[] frustumCorners = cameraBoundingFrustum.GetCorners(); |             Vector3[] frustumCorners = cameraBoundingFrustum.GetCorners(); | ||||||
|  | 
 | ||||||
|  |             Vector3 frustumCenter = Vector3.Zero; | ||||||
|             for (var i = 0; i < frustumCorners.Length; i++) |             for (var i = 0; i < frustumCorners.Length; i++) | ||||||
|             { |             { | ||||||
|                 frustumCorners[i] = Vector3.Transform(frustumCorners[i], lightRotation); |                 frustumCenter += frustumCorners[i]; | ||||||
|  |             } | ||||||
|  |             frustumCenter /= 8f; | ||||||
|  | 
 | ||||||
|  |             var lightView = Matrix.CreateLookAt(frustumCenter, frustumCenter - directionalLight.Direction, camera.View.Right); | ||||||
|  | 
 | ||||||
|  |             for (var i = 0; i < frustumCorners.Length; i++) | ||||||
|  |             { | ||||||
|  |                 frustumCorners[i] = Vector3.Transform(frustumCorners[i], lightView); | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             BoundingBox lightBox = BoundingBox.CreateFromPoints(frustumCorners); |             BoundingBox lightBox = BoundingBox.CreateFromPoints(frustumCorners); | ||||||
|             Vector3 boxSize = lightBox.Max - lightBox.Min; |             Vector3 boxSize = lightBox.Max - lightBox.Min; | ||||||
|             Vector3 halfBoxSize = boxSize * 0.5f; |             Vector3 halfBoxSize = boxSize * 0.5f; | ||||||
| 
 | 
 | ||||||
|             Vector3 lightPosition = lightBox.Min + halfBoxSize; |             Vector3 lightPosition = frustumCenter + directionalLight.Direction * -lightBox.Min.Z; | ||||||
|             lightPosition.Z = lightBox.Min.Z; |             //lightPosition.Z = lightBox.Min.Z; | ||||||
|             lightPosition = Vector3.Transform(lightPosition, Matrix.Invert(lightRotation)); |             //lightPosition = Vector3.Transform(lightPosition, Matrix.Invert(lightRotation)); | ||||||
|              |              | ||||||
|             //SimpleDepthEffect.View = Matrix.CreateLookAt(lightPosition, lightPosition - directionalLight.Direction, up); |             SimpleDepthEffect.View = Matrix.CreateLookAt(lightPosition, frustumCenter, camera.View.Right); | ||||||
|             //SimpleDepthEffect.Projection = Matrix.CreateOrthographic(boxSize.X, boxSize.Y, -boxSize.Z, boxSize.Z); |             SimpleDepthEffect.Projection = Matrix.CreateOrthographicOffCenter(lightBox.Min.X, lightBox.Max.X, lightBox.Min.Y, lightBox.Max.Y, 0, lightBox.Max.Z - lightBox.Min.Z); | ||||||
| 
 | 
 | ||||||
|             SimpleDepthEffect.View = directionalLight.View; |             //SimpleDepthEffect.View = directionalLight.View; | ||||||
|             SimpleDepthEffect.Projection = directionalLight.Projection; |             //SimpleDepthEffect.Projection = directionalLight.Projection; | ||||||
|             DeferredPBREffect.LightSpaceMatrix = SimpleDepthEffect.View * SimpleDepthEffect.Projection; |             DeferredPBREffect.LightSpaceMatrix = SimpleDepthEffect.View * SimpleDepthEffect.Projection; | ||||||
| 
 | 
 | ||||||
|  |             var globalShadowView = Matrix.CreateLookAt(frustumCenter + directionalLight.Direction * -0.5f, frustumCenter, camera.View.Right); | ||||||
|  |             var globalShadowProjection = Matrix.CreateOrthographic(1f, 1f, 0f, 1f); | ||||||
|  |             //var texScaleBias = Matrix.CreateScale(0.5f, -0.5f, 1f) * Matrix.CreateTranslation(0.5f, 0.5f, 0f); | ||||||
|  |             //DeferredPBREffect.LightSpaceMatrix = globalShadowView * globalShadowProjection; // * texScaleBias; | ||||||
|  |              | ||||||
|             foreach (var (model, transform) in modelTransforms) |             foreach (var (model, transform) in modelTransforms) | ||||||
|             { |             { | ||||||
|                 foreach (var modelMesh in model.Meshes) |                 foreach (var modelMesh in model.Meshes) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue