struct LightVPos_OUT { float4 VPos : POSITION; float4 LightVPos : TEXCOORD0; }; struct LightCPos_OUT { float4 VPos : POSITION; float4 LightVPos : TEXCOORD0; float4 LightCPos : TEXCOORD1; }; #define DEPTH_BIAS 0.001 #define DEPTH_BIAS_VSM 0.001 #define DEPTH_EPSILON 0.05 #define DIST_BIAS 0.005 #define DIST_BIAS_VSM 0.0005 #define DIST_EPSILON 0.001 //#define shadowColor float4(0.95,0.95,0.95,1) #define shadowColor float4(0.2,0.2,0.2,1) float4 shadowMapDepth(LightVPos_OUT IN, uniform sampler2D shadowMap) : COLOR { float4 light = shadowColor; // if(IN.LightVPos.z > 0.0) { float4 pos = (IN.LightVPos / IN.LightVPos.w); pos.xy = (pos.xy + 1.0) / 2.0; pos.y = 1.0 - pos.y; float storedDepth = abs(tex2D(shadowMap, pos.xy).r); //light = (pos.z - storedDepth.r)*100.0; light = max(storedDepth + DEPTH_BIAS > pos.z, shadowColor); } return light; } float4 shadowMapDepth_Variance(LightVPos_OUT IN, uniform sampler2D shadowMap) : COLOR { float4 light = float4(1,1,1,1); float4 pos = (IN.LightVPos / IN.LightVPos.w); if( pos.z > 0.0) { float depth = pos.z; pos.xy = (pos.xy + 1.0) / 2.0; pos.y = 1.0 - pos.y; float4 storedDepth = tex2D(shadowMap, pos.xy); depth -= DEPTH_BIAS_VSM; float lit_factor = light * (depth <= storedDepth.r); float M1 = storedDepth.r; float M2 = storedDepth.g; float v2 = min(max(M2 - M1 * M1, 0.0) + DEPTH_EPSILON, 1.0); float m_d = M1 - depth; 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 shadowColor + (1 - shadowColor) * light; } float4 shadowMapDist(LightCPos_OUT IN, uniform float lightFarPlane, uniform sampler2D shadowMap) : COLOR { float4 light = 0;//shadowColor; if( IN.LightVPos.z > 0.0) { float4 pos = (IN.LightVPos / IN.LightVPos.w); float d = length(pos.xy); //light = saturate((1.0 - d)/0.05); if(d <= 1.0) { float dist = length(IN.LightCPos.xyz) / lightFarPlane; pos.xy = (pos.xy + 1.0) / 2.0; pos.y = 1.0 - pos.y; float storedDist = tex2D(shadowMap, pos.xy).r; light = dist < storedDist + DIST_BIAS; //if(dist < storedDist + DIST_BIAS) // light = 1; } } light = max(light, shadowColor); return light; } float4 shadowMapDist_Variance(LightCPos_OUT IN, uniform float lightFarPlane, uniform sampler2D shadowMap) : COLOR { float4 light = float4(1,1,1,1); if( IN.LightVPos.z > 0.0) { float4 pos = (IN.LightVPos / IN.LightVPos.w); float d = length(pos.xy); if(d <= 1.0) { float dist = length(IN.LightCPos.xyz) / lightFarPlane; pos.xy = (pos.xy + 1.0) / 2.0; pos.y = 1.0 - pos.y; float4 storedDist = tex2D(shadowMap, pos.xy); dist -= DIST_BIAS_VSM; float lit_factor = light * (dist <= storedDist.r); float M1 = storedDist.r; float M2 = storedDist.g; float v2 = min(max(M2 - M1 * M1, 0.0) + DIST_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); } } return shadowColor + (1 - shadowColor) * light; } float4 shadowMapDist_POINT_Variance(LightCPos_OUT IN, uniform float lightFarPlane, uniform samplerCUBE shadowMap) : COLOR { float4 light = float4(1,1,1,1); float dist = length(IN.LightCPos.xyz) / lightFarPlane; float4 storedDist = texCUBE(shadowMap, float3(IN.LightCPos.xy, -IN.LightCPos.z)); dist -= DIST_BIAS_VSM; float lit_factor = light * (dist <= storedDist.r); float M1 = storedDist.r; float M2 = storedDist.g; float v2 = min(max(M2 - M1 * M1, 0.0) + DIST_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); return (shadowColor + (1 - shadowColor) * light); }