///////depth map struct VS_OUT { float4 hPosition : POSITION; float4 Position : TEXCOORD0; }; VS_OUT DepthVS(float4 position : POSITION, uniform float4x4 worldViewProj) { VS_OUT OUT; OUT.hPosition = mul(worldViewProj, position); OUT.Position = mul(worldViewProj, position); return OUT; } //for directional light float4 DepthPS(VS_OUT IN ):COLOR { //return 1; float4 pos = (IN.Position / IN.Position.w); //pos = (pos +1.0 ) / 2.0; return float4(pos.z, pos.z * pos.z, 1, 1); } VS_OUT DepthDistVS(float4 position : POSITION, uniform float4x4 worldViewProj, uniform float4x4 worldView) { VS_OUT OUT; OUT.hPosition = mul(worldViewProj, position); OUT.Position = mul(worldView, position); return OUT; } float4 DepthDistPS(VS_OUT IN, uniform float farPlane ):COLOR { float dist = length(IN.Position.xyz) / farPlane; return float4(dist, dist * dist, 1, 1); //return farPlane; } ///////////////Shadow struct VS_OUT_SHADOW { float4 hPosition : POSITION; float4 Position : TEXCOORD0; float4 lPosition : TEXCOORD1; float4 lVPosition : TEXCOORD2; }; VS_OUT_SHADOW depthShadowVS(float4 position : POSITION, uniform float4x4 worldViewProj, uniform float4x4 world, uniform float4x4 lightViewProj) { VS_OUT_SHADOW OUT; OUT.hPosition = mul(worldViewProj, position); float4 wPos = mul(world, position); OUT.lPosition = mul(lightViewProj, wPos); OUT.lVPosition = 0; OUT.Position = position; return OUT; } float4 depthShadowPS(VS_OUT_SHADOW IN, uniform float4x4 lightViewProj, uniform sampler2D depthShadowMap : register(s0) ):COLOR { float bias = 0.001; float4 light = float4(1,1,1,1); float4 shadow = float4(0.25,0.25,0.25,1); //float4 shadow = float4(0,0,0,1); if(IN.lPosition.z > 0.0) { float4 pos = (IN.lPosition / IN.lPosition.w); if(length(pos.xy)>1) light = shadow; else { pos.xy = (pos.xy + 1.0) / 2.0; pos.y = 1.0 - pos.y; float4 storedDepth = tex2D(depthShadowMap, pos.xy); float M1 = storedDepth.r; float M2 = storedDepth.g; float v2 = M2 - M1 * M1; float pmax = v2 / (v2 + pow(M1 - pos.z, 2)); light = saturate(shadow + (1 - shadow) * pmax); } } else light = shadow; return light; } VS_OUT_SHADOW distShadowVS(float4 position : POSITION, uniform float4x4 worldViewProj, uniform float4x4 world, uniform float4x4 lightView, uniform float4x4 lightViewProj) { VS_OUT_SHADOW OUT; OUT.hPosition = mul(worldViewProj, position); float4 wPos = mul(world, position); OUT.lPosition = mul(lightViewProj, wPos); OUT.lVPosition = mul(lightView, wPos); OUT.Position = position; return OUT; } float4 distShadowPS(VS_OUT_SHADOW IN, uniform float4x4 lightViewProj, uniform float lightFarPlane, uniform sampler2D depthShadowMap : register(s0) ):COLOR { float bias = 0.001; float epsilon = 0.001; float4 light = float4(1,1,1,1); float4 shadow = float4(0.85,0.85,0.85,1); if(IN.lPosition.z > 0.0) { float4 pos = (IN.lPosition / IN.lPosition.w); float d = length(pos.xy); light = saturate((1.0 - d)/0.05); if(d <= 1.0) { float dist = length(IN.lVPosition.xyz) / lightFarPlane; pos.xy = (pos.xy + 1.0) / 2.0; pos.y = 1.0 - pos.y; float4 storedDist = tex2D(depthShadowMap, pos.xy); dist -= bias; float lit_factor = light * (dist <= storedDist.r); float M1 = storedDist.r; float M2 = storedDist.g; float v2 = min(max(M2 - M1 * M1, 0.0) + epsilon, 1.0); float m_d = M1 - dist; float pmax = v2 / (v2 + m_d * m_d); // Adjust the light color based on the shadow attenuation light = max(lit_factor, pmax); } } else light = 0; return shadow + (1 - shadow) * light; } /* float4 depthShadowPS(VS_OUT_SHADOW IN, uniform float4x4 lightViewProj, uniform sampler2D depthShadowMap : register(s0) ):COLOR { float bias = 0.0001; float4 light = float4(1,1,1,1); float4 shadow = float4(0.95,0.95,0.95,1); //float4 shadow = float4(0,0,0,1); if(IN.lPosition.z > 0.0) { float4 pos = (IN.lPosition / IN.lPosition.w); if(length(pos.xy)>1) light = shadow; else { pos.xy = (pos.xy + 1.0) / 2.0; pos.y = 1.0 - pos.y; float4 storedDepth = tex2D(depthShadowMap, pos.xy); if(storedDepth.r + bias < pos.z) { light = shadow; } } } else light = shadow; return light; } */