Kav/Effects/HLSL/Shadow.fxh

123 lines
3.3 KiB
HLSL

static float2 poissonDisk[16] =
{
float2( -0.94201624, -0.39906216 ),
float2( 0.94558609, -0.76890725 ),
float2( -0.094184101, -0.92938870 ),
float2( 0.34495938, 0.29387760 ),
float2( -0.91588581, 0.45771432 ),
float2( -0.81544232, -0.87912464 ),
float2( -0.38277543, 0.27676845 ),
float2( 0.97484398, 0.75648379 ),
float2( 0.44323325, -0.97511554 ),
float2( 0.53742981, -0.47373420 ),
float2( -0.26496911, -0.41893023 ),
float2( 0.79197514, 0.19090188 ),
float2( -0.24188840, 0.99706507 ),
float2( -0.81409955, 0.91437590 ),
float2( 0.19984126, 0.78641367 ),
float2( 0.14383161, -0.14100790 )
};
// TODO: this should probably sample a noise texture instead
// Returns a random number based on a vec3 and an int.
float random(float3 seed, int i){
float4 seed4 = float4(seed, i);
float dot_product = dot(seed4, float4(12.9898,78.233,45.164,94.673));
return frac(sin(dot_product) * 43758.5453);
}
float PoissonCoord(sampler shadowMap, float3 worldPosition, float2 texCoord, float fragmentDepth, float bias)
{
float visibility = 1.0;
for (int i = 0; i < 16; i++)
{
int index = int(16.0 * random(floor(worldPosition * 1000.0), i)) % 16;
if (tex2D(shadowMap, texCoord + poissonDisk[index] / 1024.0).r < fragmentDepth - bias)
{
visibility -= 0.05;
}
}
return visibility;
}
float PoissonShadow(
float3 positionWorldSpace,
float3 N,
float3 L,
float4x4 lightSpaceMatrix,
sampler shadowMap,
int shadowMapSize
) {
float bias = 0.005 * tan(acos(dot(N, L)));
bias = clamp(bias, 0, 0.01);
float4 positionLightSpace = mul(float4(positionWorldSpace, 1.0), lightSpaceMatrix);
// maps to [-1, 1]
float3 projectionCoords = positionLightSpace.xyz / positionLightSpace.w;
// maps to [0, 1]
// NOTE: In XNA, clip space z is [0, 1] range
projectionCoords.x = (projectionCoords.x * 0.5) + 0.5;
projectionCoords.y = (projectionCoords.y * 0.5) + 0.5;
projectionCoords.y *= -1;
if (projectionCoords.z > 1.0)
{
return 1.0;
}
float inc = 1.0 / shadowMapSize;
// Poisson soft shadows
float visibility = 0.0;
visibility = PoissonCoord(
shadowMap,
positionWorldSpace,
projectionCoords.xy,
projectionCoords.z,
bias
);
return visibility;
}
float HardShadow(
float3 positionWorldSpace,
float3 N,
float3 L,
float4x4 lightSpaceMatrix,
sampler shadowMap,
int shadowMapSize
) {
// float bias = 0.005 * tan(acos(dot(N, L)));
// bias = clamp(bias, 0, 0.01);
float bias = max(0.05 * (1.0 - dot(N, L)), 0.005);
float4 positionLightSpace = mul(float4(positionWorldSpace, 1.0), lightSpaceMatrix);
// maps to [-1, 1]
float3 projectionCoords = positionLightSpace.xyz / positionLightSpace.w;
// maps to [0, 1]
// NOTE: In XNA, clip space z is [0, 1] range
projectionCoords.x = (projectionCoords.x * 0.5) + 0.5;
projectionCoords.y = (projectionCoords.y * 0.5) + 0.5;
projectionCoords.y *= -1;
if (projectionCoords.z > 1.0)
{
return 1.0;
}
float closestDepth = tex2D(shadowMap, projectionCoords.xy);
float currentDepth = projectionCoords.z;
return (currentDepth - bias > closestDepth ? 0.25 : 1.0);
}