Jump to content

  • Log In with Google      Sign In   
  • Create Account


antmck2013

Member Since 08 Apr 2013
Offline Last Active Apr 26 2013 11:26 PM

Posts I've Made

In Topic: Basic shadow mapping shader?

19 April 2013 - 08:28 AM

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()));
    }
}


In Topic: Basic shadow mapping shader?

19 April 2013 - 02:04 AM

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


In Topic: Basic shadow mapping shader?

18 April 2013 - 09:47 PM

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.


In Topic: Need some help with shadow mapping

12 April 2013 - 12:52 AM

I only have terrain.  I want to get the self shadowing working.  I used code from tutes, so I assumed it would work.

I've looked at tutes on http://www.riemers.net and http://takinginitiative.net/2011/05/15/directx10-tutorial-10-shadow-mapping/


In Topic: Need some help with shadow mapping

11 April 2013 - 11:55 PM

I've set the size of shadow map to equal the window client size so 800x600., but the window size can be changed - that doesn't cause any problems.

I am still having trouble with shadows though.  At lease the shadow map is actually adding shadows now, but shadows still aren't right.

Maybe something to do with converting the current vertex/pixel position into light space and then into texture space??

 

I have in the vertex shader:

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

output.LightPosition /= output.LightPosition.w;

 

And in the pixel shader:

 

 

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;

 

float realDistance = input.LightPosition.z;

 

if ((realDistance - 1.0f / 100.0f) <= shadowMapDepth)

{

    colour *= 0.25;

}

 

return colour;

 

I 'm using a left handed co-ord system.

I added a screenshot.  The sun is 25deg above the east (x axis) and looking at the origin.

The camera is looking west or in the -x direction.  There should not be that much shadow.

The greyscale image in the corner is the heightmap.


PARTNERS