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 normals,
|
---|
64 | uniform float3 lightDir
|
---|
65 | )
|
---|
66 | {
|
---|
67 | pixel OUT;
|
---|
68 |
|
---|
69 | float4 norm = tex2D(normals, IN.texCoord);
|
---|
70 | float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
71 |
|
---|
72 |
|
---|
73 | // an ambient color term
|
---|
74 | float amb = color.w;
|
---|
75 | float3 normal = normalize(norm.xyz);
|
---|
76 | float4 col = shade(IN, color, normal, amb, lightDir);
|
---|
77 |
|
---|
78 | OUT.color = col;
|
---|
79 |
|
---|
80 | // store scaled view vector from now on so wie don't have to normalize for e.g., ssao
|
---|
81 | float3 viewDir = IN.view;
|
---|
82 | const float lenView = length(viewDir);
|
---|
83 |
|
---|
84 | OUT.color.w = color.w / lenView;
|
---|
85 |
|
---|
86 | return OUT;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | float CalcShadowTerm(fragment IN,
|
---|
91 | uniform sampler2D shadowMap,
|
---|
92 | uniform float scale,
|
---|
93 | uniform float2 lightSpacePos,
|
---|
94 | uniform float depth,
|
---|
95 | uniform float2 samples[NUM_PCF_TABS],
|
---|
96 | uniform float weights[NUM_PCF_TABS],
|
---|
97 | uniform sampler2D noiseTexture
|
---|
98 | )
|
---|
99 | {
|
---|
100 | //float shadowDepth = tex2D(shadowMap, lightSpacePos).x;
|
---|
101 | //return step(depth, shadowDepth);
|
---|
102 |
|
---|
103 | float total_d = .0f;
|
---|
104 | float total_w = .0f;
|
---|
105 |
|
---|
106 | for (int i = 0; i < NUM_PCF_TABS; ++ i)
|
---|
107 | {
|
---|
108 | const float2 offset = samples[i];
|
---|
109 | const float w = weights[i];
|
---|
110 |
|
---|
111 | #if 1
|
---|
112 | ////////////////////
|
---|
113 | //-- add random noise: reflect around random normal vector (warning: slow!)
|
---|
114 |
|
---|
115 | float2 mynoise = tex2D(noiseTexture, IN.texCoord).xy;
|
---|
116 | const float2 offsetTransformed = myreflect(offset, mynoise);
|
---|
117 | #else
|
---|
118 | const float2 offsetTransformed = offset;
|
---|
119 | #endif
|
---|
120 | // weight with projected coordinate to reach similar kernel size for near and far
|
---|
121 | float2 texcoord = lightSpacePos + offsetTransformed * scale;
|
---|
122 |
|
---|
123 | float shadowDepth = tex2D(shadowMap, texcoord).x;
|
---|
124 |
|
---|
125 | total_d += w * step(depth, shadowDepth);
|
---|
126 | total_w += w;
|
---|
127 | }
|
---|
128 |
|
---|
129 | total_d /= (float)total_w;
|
---|
130 |
|
---|
131 | return total_d;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
|
---|
136 | {
|
---|
137 | float3 x1 = lerp(bl, tl, w.y);
|
---|
138 | float3 x2 = lerp(br, tr, w.y);
|
---|
139 | float3 v = lerp(x1, x2, w.x);
|
---|
140 |
|
---|
141 | return v;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | pixel main_shadow(fragment IN,
|
---|
146 | uniform sampler2D colors,
|
---|
147 | uniform sampler2D positions,
|
---|
148 | uniform sampler2D normals,
|
---|
149 | uniform sampler2D shadowMap,
|
---|
150 | uniform float4x4 shadowMatrix,
|
---|
151 | uniform float sampleWidth,
|
---|
152 | uniform sampler2D noise,
|
---|
153 | uniform float2 samples[NUM_PCF_TABS],
|
---|
154 | uniform float weights[NUM_PCF_TABS],
|
---|
155 | uniform float3 lightDir,
|
---|
156 | uniform float3 eyePos,
|
---|
157 | uniform float3 bl,
|
---|
158 | uniform float3 br,
|
---|
159 | uniform float3 tl,
|
---|
160 | uniform float3 tr
|
---|
161 | )
|
---|
162 | {
|
---|
163 | pixel OUT;
|
---|
164 |
|
---|
165 | float4 norm = tex2D(normals, IN.texCoord.xy);
|
---|
166 | const float3 normal = normalize(norm.xyz);
|
---|
167 |
|
---|
168 | float4 color = tex2Dlod(colors, float4(IN.texCoord, 0, 0));
|
---|
169 |
|
---|
170 | /// reconstruct position from the eye space depth
|
---|
171 | float3 viewDir = IN.view;
|
---|
172 | const float lenView = length(viewDir);
|
---|
173 | viewDir /= lenView;
|
---|
174 |
|
---|
175 | const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w;
|
---|
176 |
|
---|
177 | const float4 worldPos = float4(eyePos - viewDir * eyeDepth, 1);
|
---|
178 |
|
---|
179 | // diffuse intensity
|
---|
180 | const float angle = saturate(dot(normal, lightDir));
|
---|
181 | const float4 lightDiffuse = glstate.light[0].diffuse;
|
---|
182 |
|
---|
183 | float4 diffuse = lightDiffuse * angle;
|
---|
184 |
|
---|
185 | // hack: prevent shadowing the sky
|
---|
186 | const bool useShading = (color.w < 1e19f);
|
---|
187 |
|
---|
188 | // calc diffuse illumination + shadow term
|
---|
189 | if (useShading &&
|
---|
190 | (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
|
---|
191 | )
|
---|
192 | {
|
---|
193 | float4 lightSpacePos = mul(shadowMatrix, worldPos);
|
---|
194 | lightSpacePos /= lightSpacePos.w;
|
---|
195 |
|
---|
196 | float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, weights, noise);
|
---|
197 | //float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noise);
|
---|
198 |
|
---|
199 | diffuse *= shadowTerm;
|
---|
200 | }
|
---|
201 |
|
---|
202 | // light ambient term
|
---|
203 | const float4 ambient = glstate.light[0].ambient;
|
---|
204 | // compute shading
|
---|
205 | OUT.color = useShading ? (ambient + diffuse) * color : color;
|
---|
206 |
|
---|
207 | // store scaled view vector from now on so wie don't have to normalize for e.g., ssao
|
---|
208 | OUT.color.w = color.w / lenView;
|
---|
209 |
|
---|
210 | return OUT;
|
---|
211 | }
|
---|
212 |
|
---|