Basic shadow mapping shader?

Started by
14 comments, last by antmck2013 11 years ago

...aaandddd... here it is! A semi-working effect-file shadow mapping implementation!

[attachment=14822:shadowmap!.jpg]

As you can see it's not perfect yet (for some reason, the pyramid doesn't have any diffuse lighting even though it's facing the light) but wow - I'm so pleased! I am really looking foward to seeing how this performs in my main game project at the moment!

EDIT: I figured out the diffuse problem - I was passing the wrong light location into the shader.

Advertisement

I have had a lot of trouble getting shadow mapping working with terrain. I've used code from tutes too. In fact pretty much the same code as listed further up this page. I'm using SharpDX which is Directx10/11 based. Either the entire terrain is in shadow or it's lit. Looks really odd when the sun moves through a complete cycle. Still don't have it working. People tell me to put other objects in the scene and turn off shadows on the terrain, but I only have terrain. At this point I just want the terrain to be correctly shadowed. I'd like to do real-time editing and watch the shadows change.

What are you using for your depth comparison bias?

Actually I've tried several different values - done look right. Is there a way calculate the ideal value?

For the offset I just started with a small value and increased it until the nasty effects stopped. For long-range shading on terrain (not counting self shadowing) I can get away with a zero offset. As for the other problems, I guess people can't do anything unless they see some code - I suppose your shader would be a good place to start! I probably can't help much myself because I've only just got it working, but maybe I can spot a mistake that I also made and tell you about it!

Here's the complete code for shader.

At the moment I've calculated the normals for each vertex but not using them yet.

Also, no real lighting just adding shadows, so I'm darkening pixels where a shadow is.

As I said before I've tried several bias values. The shadows just don't look right.

This is self shadows on terrain only - at the moment.

float4x4 WorldMatrix;
float4x4 ViewMatrix;
float4x4 ProjectionMatrix;

float4x4 LightViewMatrix;
float4x4 LightProjectionMatrix;

float maxHeight; //maximum height of the terrain

Texture2D heightMap;
Texture2D shadowMap;

Texture2D sandTexture;
Texture2D grassTexture;
Texture2D snowTexture;
Texture2D rockTexture;

SamplerState pointSampler;
SamplerState linearSampler;
SamplerState shadowMapSampler;

struct VS_INPUT
{
float4 Position : SV_POSITION;
float2 UV : TEXCOORD0;
};

struct VS_OUTPUT
{
float4 Position : SV_POSITION;
float4 Normal : NORMAL0;
float2 UV : TEXCOORD0;
float4 TextureWeights : TEXCOORD1; // Weights used for multitexturing
float Depth : TEXCOORD2; // Used for texture LOD
float4 LightPosition : TEXCOORD3; // Position in light space

};

struct SHADOW_VS_INPUT
{
float4 Position : SV_POSITION;
float2 UV : TEXCOORD0;
};

struct SHADOW_VS_OUTPUT
{
float4 Position : SV_POSITION;
float4 Depth : TEXTURE0;
};

//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
VS_OUTPUT VS(VS_INPUT input)
{
float height = heightMap.SampleLevel(pointSampler, input.UV, 0).r;
input.Position.y = height * maxHeight;

float4x4 worldViewProj = mul(mul(WorldMatrix, ViewMatrix), ProjectionMatrix);
float4x4 lightWorldViewProj = mul(mul(WorldMatrix, LightViewMatrix), LightProjectionMatrix);


// Height values of adjacent vertices
float y = input.Position.y;
float N = heightMap.SampleLevel(pointSampler, input.UV, 0, float2(0,-1)).r * maxHeight;
float S = heightMap.SampleLevel(pointSampler, input.UV, 0, float2(0,1)).r * maxHeight;
float W = heightMap.SampleLevel(pointSampler, input.UV, 0, float2(-1,0)).r * maxHeight;
float E = heightMap.SampleLevel(pointSampler, input.UV, 0, float2(1,0)).r * maxHeight;
float NE = heightMap.SampleLevel(pointSampler, input.UV, 0, float2(1,-1)).r * maxHeight;
float SW = heightMap.SampleLevel(pointSampler, input.UV, 0, float2(-1,1)).r * maxHeight;

// Vectors joins adjacent vertices
float3 vectorN = float3(0, N - y, 1);
float3 vectorS = float3(0, S - y, -1);
float3 vectorE = float3(1, E - y, 0);
float3 vectorW = float3(-1, W - y, 0);
float3 vectorNE = float3(1, NE - y, 1);
float3 vectorSW = float3(-1, SW - y, -1);

// Average normal for current vertex
float3 normal = normalize(cross(vectorN, vectorNE) + cross(vectorNE, vectorE) + cross(vectorE, vectorS)
+ cross(vectorS, vectorSW) + cross(vectorSW, vectorW) + cross(vectorW, vectorN));

VS_OUTPUT output = (VS_OUTPUT)0;
output.Position = mul(input.Position, worldViewProj);
output.Normal = mul(float4(normal, 1), WorldMatrix);

float4 texWeights = 0;

texWeights.x = saturate(1.0f - height * 3.0f);
texWeights.y = saturate(1.0f - abs(height - 0.4f) * 3.0f);
texWeights.z = saturate(1.0f - abs(height - 0.9f) * 3.0f);
texWeights.w = 1;

float totalWeight = texWeights.x + texWeights.y + texWeights.z;
texWeights.xyz /= totalWeight;

texWeights.w = 1;

if (normal.y > 0.81f)
{
texWeights.w = 0;
}
else if (normal.y > 0.75)
{
texWeights.w = normal.y;
}

output.TextureWeights = texWeights;
output.UV = input.UV;

output.Depth = output.Position.z/output.Position.w;

output.LightPosition = mul(input.Position, lightWorldViewProj);
//output.LightPosition /= output.LightPosition.w;

return output;
}

//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PS(VS_OUTPUT input) : SV_TARGET
{
float blendDistance = 0.99f;
float blendWidth = 0.005f;
float blendFactor = clamp((input.Depth - blendDistance) / blendWidth, 0, 1);

float4 weights = input.TextureWeights;

float scale = 4;
float4 rock = rockTexture.Sample(linearSampler, input.UV * scale);
float4 sand = sandTexture.Sample(linearSampler, input.UV * scale);
float4 grass = grassTexture.Sample(linearSampler, input.UV * scale);
float4 snow = snowTexture.Sample(linearSampler, input.UV * scale);
float4 farColour = rock * weights.w + (1 - weights.w) * (sand * weights.x + grass * weights.y + snow * weights.z);

scale = 16;
rock = rockTexture.Sample(linearSampler, input.UV * scale);
sand = sandTexture.Sample(linearSampler, input.UV * scale);
grass = grassTexture.Sample(linearSampler, input.UV * scale);
snow = snowTexture.Sample(linearSampler, input.UV * scale);
float4 nearColour = rock * weights.w + (1 - weights.w) * (sand * weights.x + grass * weights.y + snow * weights.z);

float4 colour = lerp(nearColour, farColour, blendFactor);

//--------------------------------------------------------------------------------------
// Shadows
//--------------------------------------------------------------------------------------

input.LightPosition.xyz /= input.LightPosition.w;

float2 projectedTexCoords;
projectedTexCoords[0] = input.LightPosition.x / 2.0f + 0.5f;
projectedTexCoords[1] = input.LightPosition.y / -2.0f + 0.5f;

float shadowMapDepth = shadowMap.Sample(shadowMapSampler, projectedTexCoords).r;

if (shadowMapDepth < input.LightPosition.z - 0.0007f)
{
colour *= 0.25;
}

return colour;
}

//--------------------------------------------------------------------------------------
// Shadow Vertex Shader
//--------------------------------------------------------------------------------------
SHADOW_VS_OUTPUT ShadowMapVS(SHADOW_VS_INPUT input)
{
input.Position.w = 1.0f;

// Generate terrain height from heightmap
float height = heightMap.SampleLevel(pointSampler, input.UV, 0).r;
input.Position.y = height * maxHeight;

//float4x4 lightWorldViewProj = mul(mul(WorldMatrix, LightViewMatrix), LightProjectionMatrix);

SHADOW_VS_OUTPUT output = (SHADOW_VS_OUTPUT)0;
//output.Position = mul(input.Position, lightWorldViewProj);

output.Position = mul(input.Position, WorldMatrix);
output.Position = mul(output.Position, LightViewMatrix);
output.Position = mul(output.Position, LightProjectionMatrix);

output.Depth = output.Position;

return output;
}

//--------------------------------------------------------------------------------------
// Shadow Pixel Shader
//--------------------------------------------------------------------------------------
float4 ShadowMapPS(SHADOW_VS_OUTPUT input) : SV_TARGET
{
float zValue = input.Depth.z / input.Depth.w;

return float4(zValue, zValue, zValue, 1.0f);
}

//--------------------------------------------------------------------------------------
// Techniques
//--------------------------------------------------------------------------------------
technique ShadowMap
{
pass Pass0
{
SetVertexShader(CompileShader(vs_4_0, ShadowMapVS()));
SetGeometryShader(NULL);
SetPixelShader(CompileShader(ps_4_0, ShadowMapPS()));
}
}

technique Terrain
{
pass Pass0
{
SetVertexShader(CompileShader(vs_4_0, VS()));
SetGeometryShader(NULL);
SetPixelShader(CompileShader(ps_4_0, PS()));
}
}

This topic is closed to new replies.

Advertisement