struct fragment { // normalized screen position float4 pos: WPOS; float4 texCoord: TEXCOORD0; float3 view: COLOR0; }; struct pixel { float4 color: COLOR0; }; /** function for standard deferred shading */ float4 shade(fragment IN, uniform float4 color, uniform float4 position, uniform float3 normal, uniform float amb) { float4 lightDir = float4(0.8f, -1.0f, 0.7f, 0.0f); //float4 lightDir = float4(0.0f, 1.0f, 0.0f, 0.0f); float4 lightDir2 = float4(-0.5f, 0.5f, 0.4f, 0.0f); // global ambient const float4 ambient = 0.25f; // float3 L = normalize(lightPosition - position); float3 light = normalize(lightDir.xyz); float3 light2 = normalize(lightDir2.xyz); float diffuseLight = saturate(dot(normal, light)); float diffuseLight2 = saturate(dot(normal, light2)); float diffuse = diffuseLight + diffuseLight2; return (ambient + diffuse) * color * (1.0f - amb) + amb * color; } /** The mrt shader for standard rendering */ pixel main(fragment IN, uniform sampler2D colors, uniform sampler2D positions, uniform sampler2D normals ) { pixel OUT; float4 norm = tex2D(normals, IN.texCoord.xy); float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0)); float4 position = tex2D(positions, IN.texCoord.xy); // an ambient color term float amb = norm.w; // expand normal float3 normal = normalize(norm.xyz);// * 2.0f - float4(1.0f)); float4 col = shade(IN, color, position, normal, amb); //OUT.color = float4(1.0f); OUT.color = col; OUT.color.w = color.w; return OUT; } /** The mrt shader for standard rendering */ pixel main_shadow(fragment IN, uniform sampler2D colors, uniform sampler2D positions, uniform sampler2D normals, uniform sampler2D shadowMap, uniform float4x4 shadowMatrix, uniform float maxDepth, uniform float sampleWidth ) { pixel OUT; float4 norm = tex2D(normals, IN.texCoord.xy); float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0)); float4 position = tex2D(positions, IN.texCoord.xy); // an ambient color term float amb = norm.w; float3 normal = normalize(norm.xyz); float4 col = shade(IN, color, position, normal, amb); position *= maxDepth; position.w = 1.0f; float4 lightSpacePos = mul(shadowMatrix, position); float shadowDepth[9]; float w = sampleWidth; // pcf sampling using 3 x 3 tab shadowDepth[0] = tex2D(shadowMap, lightSpacePos.xy).x; shadowDepth[1] = tex2D(shadowMap, lightSpacePos.xy + float2( w, w)).x; shadowDepth[2] = tex2D(shadowMap, lightSpacePos.xy - float2( w, -w)).x; shadowDepth[3] = tex2D(shadowMap, lightSpacePos.xy - float2(-w, -w)).x; shadowDepth[4] = tex2D(shadowMap, lightSpacePos.xy - float2(-w, w)).x; shadowDepth[5] = tex2D(shadowMap, lightSpacePos.xy - float2( w, 0)).x; shadowDepth[6] = tex2D(shadowMap, lightSpacePos.xy - float2( 0, w)).x; shadowDepth[7] = tex2D(shadowMap, lightSpacePos.xy - float2(-w, 0)).x; shadowDepth[8] = tex2D(shadowMap, lightSpacePos.xy - float2( 0, -w)).x; OUT.color = col; float depth = lightSpacePos.z / lightSpacePos.w; float d = 0.0f; for (int i = 0; i < 9; ++ i) { d += step(shadowDepth[i], depth); } d /= 9.0f; /*if ((amb < 0.9) && // hack: prevent shadowing the sky (lightSpacePos.z / lightSpacePos.w > shadowDepth)) { OUT.color *= 0.1f; }*/ if (amb < 0.9) // hack: prevent shadowing the sky { const float x = 0.1f; OUT.color *= x + (1.0f - x) * (1.0f - d); } //OUT.color = lightSpacePos;//float4(lightSpacePos.z / lightSpacePos.w); //OUT.color = float4(shadowDepth); OUT.color.w = color.w; return OUT; }