1 | #include "../shaderenv.h"
|
---|
2 |
|
---|
3 |
|
---|
4 | struct fragment
|
---|
5 | {
|
---|
6 | // normalized screen position
|
---|
7 | float4 pos: WPOS;
|
---|
8 | float2 texCoord: TEXCOORD0;
|
---|
9 | float3 view: TEXCOORD1;
|
---|
10 | };
|
---|
11 |
|
---|
12 |
|
---|
13 | struct pixel
|
---|
14 | {
|
---|
15 | float4 color: COLOR0;
|
---|
16 | };
|
---|
17 |
|
---|
18 |
|
---|
19 | float2 myreflect(float2 pt, float2 n)
|
---|
20 | {
|
---|
21 | // distance to plane
|
---|
22 | float d = dot(n, pt);
|
---|
23 | // reflect around plane
|
---|
24 | float2 rpt = pt - d * 2.0f * n;
|
---|
25 |
|
---|
26 | return rpt;
|
---|
27 | }
|
---|
28 |
|
---|
29 |
|
---|
30 | /** function for standard deferred shading
|
---|
31 | */
|
---|
32 | float4 shade(fragment IN,
|
---|
33 | uniform float4 color,
|
---|
34 | uniform float3 normal,
|
---|
35 | uniform float emmisive,
|
---|
36 | float3 lightDir)
|
---|
37 | {
|
---|
38 | // diffuse intensity
|
---|
39 | const float angle = saturate(dot(normal, lightDir));
|
---|
40 |
|
---|
41 | float4 lightDiffuse = glstate.light[0].diffuse;
|
---|
42 | float4 diffuse = angle * lightDiffuse;
|
---|
43 |
|
---|
44 | // global ambient
|
---|
45 | const float4 ambient = glstate.light[0].ambient;
|
---|
46 |
|
---|
47 | float4 outColor;
|
---|
48 |
|
---|
49 | // hack: prevent shading the sky
|
---|
50 | if (color.w > 1e19f) outColor = color;
|
---|
51 | //if (emmisive > 1.5f) outColor = color;
|
---|
52 | else outColor = (ambient + diffuse) * color;
|
---|
53 |
|
---|
54 | return outColor;
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 |
|
---|
59 | /** The mrt shader for standard rendering
|
---|
60 | */
|
---|
61 | pixel main(fragment IN,
|
---|
62 | uniform sampler2D colors,
|
---|
63 | uniform sampler2D positions,
|
---|
64 | uniform sampler2D normals,
|
---|
65 | uniform float3 lightDir
|
---|
66 | //, uniform sampler2D oldColors
|
---|
67 | )
|
---|
68 | {
|
---|
69 | pixel OUT;
|
---|
70 |
|
---|
71 | float4 norm = tex2D(normals, IN.texCoord);
|
---|
72 | float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
73 |
|
---|
74 |
|
---|
75 | // an ambient color term
|
---|
76 | float amb = color.w;
|
---|
77 | float3 normal = normalize(norm.xyz);
|
---|
78 | float4 col = shade(IN, color, normal, amb, lightDir);
|
---|
79 |
|
---|
80 | OUT.color = col;
|
---|
81 | OUT.color.w = color.w;
|
---|
82 |
|
---|
83 | return OUT;
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | float CalcShadowTerm(fragment IN,
|
---|
88 | uniform sampler2D shadowMap,
|
---|
89 | uniform float w,
|
---|
90 | uniform float2 lightSpacePos,
|
---|
91 | uniform float depth,
|
---|
92 | uniform float2 samples[NUM_PCF_TABS],
|
---|
93 | uniform sampler2D noiseTexture
|
---|
94 | )
|
---|
95 | {
|
---|
96 | //float shadowDepth = tex2D(shadowMap, lightSpacePos).x;
|
---|
97 | //return step(depth, shadowDepth);
|
---|
98 |
|
---|
99 | float total_d = 0.0;
|
---|
100 |
|
---|
101 | for (int i = 0; i < NUM_PCF_TABS; ++ i)
|
---|
102 | {
|
---|
103 | const float2 offset = samples[i];
|
---|
104 |
|
---|
105 | #if 1
|
---|
106 | ////////////////////
|
---|
107 | //-- add random noise: reflect around random normal vector (warning: slow!)
|
---|
108 |
|
---|
109 | float2 mynoise = tex2D(noiseTexture, IN.texCoord).xy;
|
---|
110 | const float2 offsetTransformed = myreflect(offset, mynoise);
|
---|
111 | #else
|
---|
112 | const float2 offsetTransformed = offset;
|
---|
113 | #endif
|
---|
114 | // weight with projected coordinate to reach similar kernel size for near and far
|
---|
115 | float2 texcoord = lightSpacePos + offsetTransformed * w;
|
---|
116 |
|
---|
117 | float shadowDepth = tex2D(shadowMap, texcoord).x;
|
---|
118 |
|
---|
119 | total_d += step(depth, shadowDepth);
|
---|
120 | }
|
---|
121 |
|
---|
122 | total_d /= (float)NUM_PCF_TABS;
|
---|
123 |
|
---|
124 | return total_d;
|
---|
125 | }
|
---|
126 |
|
---|
127 | inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
|
---|
128 | {
|
---|
129 | float3 x1 = lerp(bl, tl, w.y);
|
---|
130 | float3 x2 = lerp(br, tr, w.y);
|
---|
131 | float3 v = lerp(x1, x2, w.x);
|
---|
132 |
|
---|
133 | return v;
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | pixel main_shadow(fragment IN,
|
---|
138 | uniform sampler2D colors,
|
---|
139 | uniform sampler2D positions,
|
---|
140 | uniform sampler2D normals,
|
---|
141 | uniform sampler2D shadowMap,
|
---|
142 | uniform float4x4 shadowMatrix,
|
---|
143 | uniform float maxDepth,
|
---|
144 | uniform float sampleWidth,
|
---|
145 | uniform sampler2D noiseTexture,
|
---|
146 | uniform float2 samples[NUM_PCF_TABS],
|
---|
147 | uniform float3 lightDir,
|
---|
148 | uniform float3 eyePos,
|
---|
149 | uniform float3 bl,
|
---|
150 | uniform float3 br,
|
---|
151 | uniform float3 tl,
|
---|
152 | uniform float3 tr
|
---|
153 | )
|
---|
154 | {
|
---|
155 | pixel OUT;
|
---|
156 |
|
---|
157 | float4 norm = tex2D(normals, IN.texCoord.xy);
|
---|
158 | const float3 normal = normalize(norm.xyz);
|
---|
159 |
|
---|
160 | float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
161 |
|
---|
162 | /// reconstruct position from the eye space depth
|
---|
163 | float3 viewDir = IN.view;
|
---|
164 | const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w;
|
---|
165 |
|
---|
166 | float4 position;
|
---|
167 | position.xyz = eyePos - viewDir * eyeDepth;
|
---|
168 |
|
---|
169 |
|
---|
170 | // diffuse intensity
|
---|
171 | const float angle = saturate(dot(normal, lightDir));
|
---|
172 | //float4 lightDiffuse = float4(1.0f, 1.0f, 0.9f, 1.0f);
|
---|
173 | const float4 lightDiffuse = glstate.light[0].diffuse;
|
---|
174 | //float4(1.0f, 1.0f, 0.9f, 1.0f);
|
---|
175 |
|
---|
176 | float4 diffuse = lightDiffuse * angle;
|
---|
177 |
|
---|
178 | // hack: prevent shadowing the sky
|
---|
179 | const bool useShading = (color.w < 1e19f);
|
---|
180 |
|
---|
181 | // calc diffuse illumination + shadow term
|
---|
182 | if (useShading &&
|
---|
183 | (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
|
---|
184 | )
|
---|
185 | {
|
---|
186 | position *= maxDepth;
|
---|
187 | position.w = 1.0f;
|
---|
188 |
|
---|
189 | float4 lightSpacePos = mul(shadowMatrix, position);
|
---|
190 | lightSpacePos /= lightSpacePos.w;
|
---|
191 |
|
---|
192 | float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noiseTexture);
|
---|
193 |
|
---|
194 | diffuse *= shadowTerm;
|
---|
195 | }
|
---|
196 |
|
---|
197 | // light ambient term
|
---|
198 | const float4 ambient = glstate.light[0].ambient;
|
---|
199 | // compute shading
|
---|
200 | OUT.color = useShading ? (ambient + diffuse) * color : color;
|
---|
201 |
|
---|
202 | OUT.color.w = color.w;
|
---|
203 |
|
---|
204 | return OUT;
|
---|
205 | }
|
---|
206 |
|
---|