1 | struct fragment
|
---|
2 | {
|
---|
3 | // normalized screen position
|
---|
4 | float4 pos: WPOS;
|
---|
5 | float4 texCoord: TEXCOORD0;
|
---|
6 | float3 view: COLOR0;
|
---|
7 | };
|
---|
8 |
|
---|
9 |
|
---|
10 | struct pixel
|
---|
11 | {
|
---|
12 | float4 color: COLOR0;
|
---|
13 | };
|
---|
14 |
|
---|
15 |
|
---|
16 |
|
---|
17 | /** function for standard deferred shading
|
---|
18 | */
|
---|
19 | float4 shade(fragment IN,
|
---|
20 | uniform float4 color,
|
---|
21 | uniform float4 position,
|
---|
22 | uniform float3 normal,
|
---|
23 | uniform float amb)
|
---|
24 | {
|
---|
25 | float4 lightDir = float4(0.8f, -1.0f, 0.7f, 0.0f);
|
---|
26 | //float4 lightDir = float4(0.0f, 1.0f, 0.0f, 0.0f);
|
---|
27 | float4 lightDir2 = float4(-0.5f, 0.5f, 0.4f, 0.0f);
|
---|
28 |
|
---|
29 | // global ambient
|
---|
30 | const float4 ambient = 0.25f;
|
---|
31 |
|
---|
32 | // float3 L = normalize(lightPosition - position);
|
---|
33 | float3 light = normalize(lightDir.xyz);
|
---|
34 | float3 light2 = normalize(lightDir2.xyz);
|
---|
35 |
|
---|
36 | float diffuseLight = saturate(dot(normal, light));
|
---|
37 | float diffuseLight2 = saturate(dot(normal, light2));
|
---|
38 |
|
---|
39 | float diffuse = diffuseLight + diffuseLight2;
|
---|
40 |
|
---|
41 | return (ambient + diffuse) * color * (1.0f - amb) + amb * color;
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 |
|
---|
46 | /** The mrt shader for standard rendering
|
---|
47 | */
|
---|
48 | pixel main(fragment IN,
|
---|
49 | uniform sampler2D colors,
|
---|
50 | uniform sampler2D positions,
|
---|
51 | uniform sampler2D normals
|
---|
52 | )
|
---|
53 | {
|
---|
54 | pixel OUT;
|
---|
55 |
|
---|
56 | float4 norm = tex2D(normals, IN.texCoord.xy);
|
---|
57 | float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
|
---|
58 | float4 position = tex2D(positions, IN.texCoord.xy);
|
---|
59 |
|
---|
60 | // an ambient color term
|
---|
61 | float amb = norm.w;
|
---|
62 |
|
---|
63 | // expand normal
|
---|
64 | float3 normal = normalize(norm.xyz);// * 2.0f - float4(1.0f));
|
---|
65 |
|
---|
66 | float4 col = shade(IN, color, position, normal, amb);
|
---|
67 |
|
---|
68 | //OUT.color = float4(1.0f);
|
---|
69 | OUT.color = col;
|
---|
70 | OUT.color.w = color.w;
|
---|
71 |
|
---|
72 | return OUT;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /** The mrt shader for standard rendering
|
---|
76 | */
|
---|
77 | pixel main_shadow(fragment IN,
|
---|
78 | uniform sampler2D colors,
|
---|
79 | uniform sampler2D positions,
|
---|
80 | uniform sampler2D normals,
|
---|
81 | uniform sampler2D shadowMap,
|
---|
82 | uniform float4x4 shadowMatrix,
|
---|
83 | uniform float maxDepth,
|
---|
84 | uniform float sampleWidth
|
---|
85 | )
|
---|
86 | {
|
---|
87 | pixel OUT;
|
---|
88 |
|
---|
89 | float4 norm = tex2D(normals, IN.texCoord.xy);
|
---|
90 | float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
|
---|
91 | float4 position = tex2D(positions, IN.texCoord.xy);
|
---|
92 |
|
---|
93 |
|
---|
94 | // an ambient color term
|
---|
95 | float amb = norm.w;
|
---|
96 |
|
---|
97 | float3 normal = normalize(norm.xyz);
|
---|
98 | float4 col = shade(IN, color, position, normal, amb);
|
---|
99 |
|
---|
100 | position *= maxDepth;
|
---|
101 | position.w = 1.0f;
|
---|
102 |
|
---|
103 | float4 lightSpacePos = mul(shadowMatrix, position);
|
---|
104 |
|
---|
105 | float shadowDepth[9];
|
---|
106 |
|
---|
107 | float w = sampleWidth;
|
---|
108 |
|
---|
109 | // pcf sampling using 3 x 3 tab
|
---|
110 |
|
---|
111 | shadowDepth[0] = tex2D(shadowMap, lightSpacePos.xy).x;
|
---|
112 |
|
---|
113 | shadowDepth[1] = tex2D(shadowMap, lightSpacePos.xy + float2( w, w)).x;
|
---|
114 | shadowDepth[2] = tex2D(shadowMap, lightSpacePos.xy - float2( w, -w)).x;
|
---|
115 | shadowDepth[3] = tex2D(shadowMap, lightSpacePos.xy - float2(-w, -w)).x;
|
---|
116 | shadowDepth[4] = tex2D(shadowMap, lightSpacePos.xy - float2(-w, w)).x;
|
---|
117 |
|
---|
118 | shadowDepth[5] = tex2D(shadowMap, lightSpacePos.xy - float2( w, 0)).x;
|
---|
119 | shadowDepth[6] = tex2D(shadowMap, lightSpacePos.xy - float2( 0, w)).x;
|
---|
120 | shadowDepth[7] = tex2D(shadowMap, lightSpacePos.xy - float2(-w, 0)).x;
|
---|
121 | shadowDepth[8] = tex2D(shadowMap, lightSpacePos.xy - float2( 0, -w)).x;
|
---|
122 |
|
---|
123 | OUT.color = col;
|
---|
124 |
|
---|
125 | float depth = lightSpacePos.z / lightSpacePos.w;
|
---|
126 |
|
---|
127 | float d = 0.0f;
|
---|
128 |
|
---|
129 | for (int i = 0; i < 9; ++ i)
|
---|
130 | {
|
---|
131 | d += step(shadowDepth[i], depth);
|
---|
132 | }
|
---|
133 |
|
---|
134 | d /= 9.0f;
|
---|
135 |
|
---|
136 | /*if ((amb < 0.9) && // hack: prevent shadowing the sky
|
---|
137 | (lightSpacePos.z / lightSpacePos.w > shadowDepth))
|
---|
138 | {
|
---|
139 | OUT.color *= 0.1f;
|
---|
140 | }*/
|
---|
141 |
|
---|
142 | if (amb < 0.9) // hack: prevent shadowing the sky
|
---|
143 | {
|
---|
144 | const float x = 0.1f;
|
---|
145 | OUT.color *= x + (1.0f - x) * (1.0f - d);
|
---|
146 | }
|
---|
147 |
|
---|
148 | //OUT.color = lightSpacePos;//float4(lightSpacePos.z / lightSpacePos.w);
|
---|
149 | //OUT.color = float4(shadowDepth);
|
---|
150 | OUT.color.w = color.w;
|
---|
151 |
|
---|
152 | return OUT;
|
---|
153 | }
|
---|