1 | // Soft shadow with shadow accumulation
|
---|
2 |
|
---|
3 |
|
---|
4 | // SHADOWMAP generation
|
---|
5 |
|
---|
6 | //-----------------------------------------------------------------------------
|
---|
7 | // Vertex Shader: RenderShadowMap_07_VS
|
---|
8 | // Desc: Process vertex for the shadow map
|
---|
9 | //-----------------------------------------------------------------------------
|
---|
10 | void RenderShadowMap_07_VS(
|
---|
11 | float4 Position : POSITION,
|
---|
12 | out float4 ldPosition : POSITION,
|
---|
13 | out float zcam : TEXCOORD0 )
|
---|
14 | {
|
---|
15 | ldPosition = mul( Position, WorldLightProj ); // Compute the projected coordinates
|
---|
16 | zcam = ldPosition.w; // Store light's camera z
|
---|
17 | }
|
---|
18 |
|
---|
19 | //-----------------------------------------------------------------------------
|
---|
20 | // Pixel Shader: RenderShadowMap_07_PS
|
---|
21 | // Desc: Process pixel for the shadow map
|
---|
22 | //-----------------------------------------------------------------------------
|
---|
23 | float4 RenderShadowMap_07_PS( float zcam : TEXCOORD0 ):COLOR
|
---|
24 | {
|
---|
25 | return float4(zcam,0,0,0);
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | // SCENE RENDERING
|
---|
30 |
|
---|
31 | //-----------------------------------------------------------------------------
|
---|
32 | // Vertex Shader: RenderSceneWithTechnique_07_VS
|
---|
33 | // Desc: Process vertex for scene
|
---|
34 | //-----------------------------------------------------------------------------
|
---|
35 | VS_OUTPUT RenderSceneWithTechnique_07_VS( VS_INPUT IN )
|
---|
36 | {
|
---|
37 | VS_OUTPUT OUT = (VS_OUTPUT)0;
|
---|
38 | OUT.Color = IN.Color;
|
---|
39 | OUT.Tex = IN.Tex;
|
---|
40 |
|
---|
41 | // transform model-space vertex position to light's camera space:
|
---|
42 | OUT.ldPosition = mul(IN.Position, WorldLightProj);
|
---|
43 | OUT.lcPosition = mul(IN.Position, WorldLight);
|
---|
44 |
|
---|
45 | // transform model-space vertex position to normalized screen space:
|
---|
46 | OUT.hPosition = mul(IN.Position, WorldViewProj);
|
---|
47 | return OUT;
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | //-----------------------------------------------------------------------------
|
---|
52 | // Pixel Shader: RenderSceneWithTechnique_07_PS
|
---|
53 | // Desc: shadow accumulation
|
---|
54 | //-----------------------------------------------------------------------------
|
---|
55 | float4 RenderSceneWithTechnique_07_PS( VS_OUTPUT IN) : COLOR
|
---|
56 | {
|
---|
57 | half R = max( g_fLightSize, 0.1h ) / 3.0;
|
---|
58 | g_fBiasSlope = max( g_fBiasSlope, 0.1h );
|
---|
59 | half m = 3.0h * R;
|
---|
60 | half mapsize = 2.0h * m * tan( g_fFov / 2.0h ); // size of half shadow map in world space
|
---|
61 | half Deltal = mapsize / SHADOWMAP_SIZE; // size of a lexel edge in world space
|
---|
62 | half step = 1.0h;
|
---|
63 |
|
---|
64 | half3 r = IN.lcPosition; // shaded point in light's camera space
|
---|
65 | half RR = R * (r.z-m)/r.z; // directional set in which the light is seen from the shaded point
|
---|
66 | //half RR = R; // directional set in which the light is seen from the shaded point
|
---|
67 | half RR2 = RR * RR;
|
---|
68 | half2 q = r.xy / r.z * m; // projection of r onto the shadow plane
|
---|
69 | half2 quv = q / mapsize;
|
---|
70 | quv = half2( ( 0.5 + quv.x ), ( 0.5 - quv.y ) ); // r in texture coordinates
|
---|
71 | half shadow = 0;
|
---|
72 | for ( int i = 0; i < g_iKernelSize; i++ )
|
---|
73 | {
|
---|
74 | for ( int j = 0; j < g_iKernelSize; j++ )
|
---|
75 | {
|
---|
76 | half2 uvoffset = half2( i - g_iKernelSize / 2.0f, j - g_iKernelSize / 2.0f ) / g_iKernelSize * step;
|
---|
77 | half2 l = q + half2( uvoffset.x, -uvoffset.y ) * mapsize;
|
---|
78 | half cz = tex2D( g_ShadowMapColorSampler, quv + uvoffset ).x;
|
---|
79 | half size = (r.z - m)/(r.z - cz) * cz / m;
|
---|
80 |
|
---|
81 | half2 lc = (m - cz)/(r.z - cz) * r.xy + size * l.xy;
|
---|
82 | if( cz < r.z - g_fShadowBias * 10.0f && dot(lc - q, lc - q) < RR2 + g_fBiasSlope )
|
---|
83 | //if( cz < r.z - g_fShadowBias * 10.0f && dot(lc - q, lc - q) < RR2 ) // nem jo, pottyosodik, kicsi lightSize-nal
|
---|
84 | {
|
---|
85 | shadow += size * size;
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 | // shadow /= RR2 * PI / Deltal / Deltal;
|
---|
90 | //shadow *= Deltal * Deltal * step * step / RR2;
|
---|
91 | shadow /= g_iKernelSize * g_iKernelSize;
|
---|
92 | return tex2D( g_samScene, IN.Tex ) * ( 1 - shadow * g_fIntensity * g_fIntensity ) * g_vMaterial;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | // TECHNIQUE
|
---|
97 |
|
---|
98 | //-----------------------------------------------------------------------------
|
---|
99 | // Techniques: RenderShadowMap
|
---|
100 | // Desc: Render the shadow map
|
---|
101 | //-----------------------------------------------------------------------------
|
---|
102 |
|
---|
103 | technique RenderShadowMap_7
|
---|
104 | {
|
---|
105 | pass p0
|
---|
106 | {
|
---|
107 | VertexShader = compile vs_2_0 RenderShadowMap_07_VS();
|
---|
108 | PixelShader = compile ps_2_0 RenderShadowMap_07_PS();
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | technique RenderSceneWithTechnique_7
|
---|
114 | {
|
---|
115 | pass p0
|
---|
116 | {
|
---|
117 | VertexShader = compile vs_2_0 RenderSceneWithTechnique_07_VS();
|
---|
118 | PixelShader = compile ps_3_0 RenderSceneWithTechnique_07_PS();
|
---|
119 | }
|
---|
120 | } |
---|