antmck2013

Members
  • Content count

    9
  • Joined

  • Last visited

Community Reputation

113 Neutral

About antmck2013

  • Rank
    Newbie
  1. Basic shadow mapping shader?

    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()));     } }
  2. Basic shadow mapping shader?

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

    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.
  4. Need some help with shadow mapping

    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/
  5. Need some help with shadow mapping

    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.
  6. Need some help with shadow mapping

    As I'm using SharpDX I posted their forums and found out that I need to set the render target before binding the shadow map. Now it renders shadows - there not quite right yet, but at least I have something to work with.
  7. Need some help with shadow mapping

    Right, so no-one has an idea what's going on then?  Guess I'm never going to finish this - damn.
  8. I'm having a similar problem.  I have a height map that the vertex shader generates y values from, and I have 4 textures to render the terrain with in the pixel shader. I have a another vertex shader to generate a shadow map.  All of that works fine.  But adding shadows to the terrain (after texturing) doesn't work.  The line where the shadow map is sampled always returns zero.  All the other texture sampling works, just not the with the shadow map.  I have saved the shadow map texture and it looks fine.  The texture co-ords look OK too.  I don't understand what is going on.  All the other texture sampling is fine - why not this one???
  9. I'm new to shadow mapping.  I've followed some tutes on the net, but can't get this working. I've created a terrain using a heightmap and textured it comparing slope and height.  All that's working. Then I tried getting shadows to work.  I've created a shadow map by using the orthographic projection (for the sun) - projection matrix and view matrix from the light.  I've translated the current vertex/pixel to light-space.  I then used a texture sampler to lookup depth value from the shadow map texture.  At the moment I'm just testing if a pixel is in shadow or not.  If it's in shadow I add the shadow by darkening the pixel.  I've checked the tutes and my code "seems" OK, but no shadows show up.  I've attached the shadow map the was generated and some HLSL code.   Vertex Shader: ... output.LightPosition = mul(input.Position, lightWorldViewProj); output.LightPosition /= output.LightPosition.w;   return output; }   Pixel Shader: ...   float2projectedTexCoords;     projectedTexCoords[0] = input.LightPosition.x / 2.0f + 0.5f;     projectedTexCoords[1] = input.LightPosition.y / -2.0f + 0.5f;     float depthStoredInShadowMap = shadowMap.Sample(shadowMapSampler, projectedTexCoords).r;   float realDistance = input.LightPosition.z;   if ((realDistance - 1.0f / 100.0f) <= depthStoredInShadowMap) { colour.xyz *= 0.25f; }   return colour; }     edit:  It seems that the line float depthStoredInShadowMap = shadowMap.Sample(shadowMapSampler, projectedTexCoords).r; is always returning zero?? What is happening??