dynamic shadow bias

pull/2/head
cosmonaut 2020-09-16 16:58:21 -07:00
parent 345ffaa247
commit 7062992d9b
3 changed files with 19 additions and 25 deletions

Binary file not shown.

View File

@ -73,9 +73,10 @@ float GeometrySmith(float3 N, float3 V, float3 L, float roughness)
return ggx1 * ggx2;
}
float ComputeShadow(float4 positionLightSpace)
float ComputeShadow(float4 positionLightSpace, float3 N, float L)
{
float bias = 0.001; //0.005;
float bias = 0.005 * tan(acos(dot(N, L)));
bias = clamp(bias, 0, 0.01);
// maps to [-1, 1]
float3 projectionCoords = positionLightSpace.xyz / positionLightSpace.w;
@ -99,7 +100,7 @@ float ComputeShadow(float4 positionLightSpace)
}
float3 ComputeLight(
float3 lightDir,
float3 L,
float3 radiance,
float3 F0,
float3 V,
@ -109,7 +110,6 @@ float3 ComputeLight(
float roughness,
float shadow
) {
float3 L = normalize(lightDir);
float3 H = normalize(V + L);
float NDF = DistributionGGX(N, H, roughness);
@ -136,9 +136,6 @@ float4 ComputeColor(
float metallic,
float roughness
) {
float4 positionLightSpace = mul(float4(worldPosition, 1.0), LightSpaceMatrix);
float shadow = ComputeShadow(positionLightSpace);
float3 V = normalize(EyePosition - worldPosition);
float3 N = normalize(worldNormal);
@ -151,18 +148,22 @@ float4 ComputeColor(
for (int i = 0; i < MAX_POINT_LIGHTS; i++)
{
float3 lightDir = PointLightPositions[i] - worldPosition;
float3 L = normalize(lightDir);
float distance = length(lightDir);
float attenuation = 1.0 / (distance * distance);
float3 radiance = PointLightColors[i] * attenuation;
Lo += ComputeLight(lightDir, radiance, F0, V, N, albedo, metallic, roughness, 1.0);
Lo += ComputeLight(L, radiance, F0, V, N, albedo, metallic, roughness, 1.0);
}
// directional light
float3 lightDir = DirectionalLightDirection;
float3 L = normalize(DirectionalLightDirection);
float3 radiance = DirectionalLightColor;
Lo += ComputeLight(lightDir, radiance, F0, V, N, albedo, metallic, roughness, (1.0 - shadow));
float4 positionLightSpace = mul(float4(worldPosition, 1.0), LightSpaceMatrix);
float shadow = ComputeShadow(positionLightSpace, N, L);
Lo += ComputeLight(L, radiance, F0, V, N, albedo, metallic, roughness, (1.0 - shadow));
float3 ambient = float3(0.03, 0.03, 0.03) * albedo; // * AO;
float3 color = ambient + Lo;

View File

@ -171,7 +171,6 @@ namespace Kav
SpriteBatch.End();
}
// for shadow mapping
public void ShadowMapRender(Camera camera, IEnumerable<(Model, Matrix)> modelTransforms, DirectionalLight directionalLight)
{
GraphicsDevice.SetRenderTarget(ShadowRenderTarget);
@ -199,24 +198,18 @@ namespace Kav
}
BoundingBox lightBox = BoundingBox.CreateFromPoints(frustumCorners);
Vector3 boxSize = lightBox.Max - lightBox.Min;
Vector3 halfBoxSize = boxSize * 0.5f;
Vector3 lightPosition = frustumCenter + directionalLight.Direction * -lightBox.Min.Z;
//lightPosition.Z = lightBox.Min.Z;
//lightPosition = Vector3.Transform(lightPosition, Matrix.Invert(lightRotation));
SimpleDepthEffect.View = Matrix.CreateLookAt(lightPosition, frustumCenter, camera.View.Right);
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.Projection = directionalLight.Projection;
SimpleDepthEffect.Projection = Matrix.CreateOrthographicOffCenter(
lightBox.Min.X,
lightBox.Max.X,
lightBox.Min.Y,
lightBox.Max.Y,
0,
lightBox.Max.Z - lightBox.Min.Z
);
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)
{