1 | #include "../shaderenv.h"
|
---|
2 |
|
---|
3 |
|
---|
4 | struct fragment
|
---|
5 | {
|
---|
6 | // normalized screen position
|
---|
7 | float4 pos: WPOS;
|
---|
8 | float4 texCoord: TEXCOORD0;
|
---|
9 | float3 view: COLOR0;
|
---|
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 float4 position,
|
---|
35 | uniform float3 normal,
|
---|
36 | uniform float emmisive,
|
---|
37 | float3 lightDir)
|
---|
38 | {
|
---|
39 | // diffuse intensity
|
---|
40 | const float angle = saturate(dot(normal, lightDir));
|
---|
41 |
|
---|
42 | float4 lightDiffuse = glstate.light[0].diffuse;
|
---|
43 | float4 diffuse = angle * lightDiffuse;
|
---|
44 |
|
---|
45 | // global ambient
|
---|
46 | const float4 ambient = glstate.light[0].ambient;
|
---|
47 |
|
---|
48 | float4 outColor;
|
---|
49 |
|
---|
50 | if (position.w > 1e19f) outColor = color;
|
---|
51 | //if (emmisive > 1.5f) outColor = color;
|
---|
52 | else outColor = (ambient + diffuse) * color;
|
---|
53 |
|
---|
54 | return outColor;
|
---|
55 | //return saturate((((ambient + diffuse)) * (1.0f - emmisive) + emmisive) * color);
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 |
|
---|
60 | /** The mrt shader for standard rendering
|
---|
61 | */
|
---|
62 | pixel main(fragment IN,
|
---|
63 | uniform sampler2D colors,
|
---|
64 | uniform sampler2D positions,
|
---|
65 | uniform sampler2D normals,
|
---|
66 | uniform float3 lightDir,
|
---|
67 | uniform sampler2D oldColors
|
---|
68 | )
|
---|
69 | {
|
---|
70 | pixel OUT;
|
---|
71 |
|
---|
72 | float4 norm = tex2D(normals, IN.texCoord.xy);
|
---|
73 | float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
|
---|
74 | float4 position = tex2D(positions, IN.texCoord.xy);
|
---|
75 |
|
---|
76 | // an ambient color term
|
---|
77 | float amb = color.w;
|
---|
78 |
|
---|
79 | float3 normal = normalize(norm.xyz);
|
---|
80 |
|
---|
81 | float4 col = shade(IN, color, position, normal, amb, lightDir);
|
---|
82 |
|
---|
83 | OUT.color = col;
|
---|
84 |
|
---|
85 | OUT.color.xyz = color.w;
|
---|
86 |
|
---|
87 | #if 1
|
---|
88 |
|
---|
89 | OUT.color.w = color.w;
|
---|
90 |
|
---|
91 | #else
|
---|
92 |
|
---|
93 | ////////////
|
---|
94 | //-- write out logaritmic luminance for tone mapping
|
---|
95 |
|
---|
96 | float4 oldColor = tex2Dlod(colors, float4(IN.texCoord.xy, 0, MAX_LOD_LEVEL));
|
---|
97 |
|
---|
98 | const float3 w = float3(0.299f, 0.587f, 0.114f);
|
---|
99 |
|
---|
100 | float lum = dot(OUT.color.rgb, w);
|
---|
101 | float logLum = log(1e-5f + lum);
|
---|
102 |
|
---|
103 | float logLumOffset = MINLOGLUM * INV_LOGLUM_RANGE;
|
---|
104 | float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
|
---|
105 |
|
---|
106 | if (oldColor.w > 0)
|
---|
107 | OUT.color.w = lerp(oldColor.w, logLumScaled, 0.1f);
|
---|
108 | else
|
---|
109 | OUT.color.w = logLumScaled;
|
---|
110 |
|
---|
111 | #endif
|
---|
112 |
|
---|
113 | return OUT;
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | float CalcShadowTerm(fragment IN,
|
---|
118 | uniform sampler2D shadowMap,
|
---|
119 | uniform float w,
|
---|
120 | uniform float2 lightSpacePos,
|
---|
121 | uniform float depth,
|
---|
122 | uniform float2 samples[NUM_PCF_TABS],
|
---|
123 | uniform sampler2D noiseTexture
|
---|
124 | )
|
---|
125 | {
|
---|
126 | //float shadowDepth = tex2D(shadowMap, lightSpacePos).x;
|
---|
127 | //return step(depth, shadowDepth);
|
---|
128 |
|
---|
129 | float total_d = 0.0;
|
---|
130 |
|
---|
131 | for (int i = 0; i < NUM_PCF_TABS; ++ i)
|
---|
132 | {
|
---|
133 | const float2 offset = samples[i];
|
---|
134 |
|
---|
135 | #if 1
|
---|
136 | ////////////////////
|
---|
137 | //-- add random noise: reflect around random normal vector (warning: slow!)
|
---|
138 |
|
---|
139 | float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy).xy;
|
---|
140 | const float2 offsetTransformed = myreflect(offset, mynoise);
|
---|
141 | #else
|
---|
142 | const float2 offsetTransformed = offset;
|
---|
143 | #endif
|
---|
144 | // weight with projected coordinate to reach similar kernel size for near and far
|
---|
145 | float2 texcoord = lightSpacePos + offsetTransformed * w;
|
---|
146 |
|
---|
147 | float shadowDepth = tex2D(shadowMap, texcoord).x;
|
---|
148 |
|
---|
149 | total_d += step(depth, shadowDepth);
|
---|
150 | }
|
---|
151 |
|
---|
152 | total_d /= (float)NUM_PCF_TABS;
|
---|
153 |
|
---|
154 | return total_d;
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | pixel main_shadow(fragment IN,
|
---|
159 | uniform sampler2D colors,
|
---|
160 | uniform sampler2D positions,
|
---|
161 | uniform sampler2D normals,
|
---|
162 | uniform sampler2D shadowMap,
|
---|
163 | uniform float4x4 shadowMatrix,
|
---|
164 | uniform float maxDepth,
|
---|
165 | uniform float sampleWidth,
|
---|
166 | uniform sampler2D noiseTexture,
|
---|
167 | uniform float2 samples[NUM_PCF_TABS],
|
---|
168 | uniform float3 lightDir,
|
---|
169 | uniform sampler2D oldColors
|
---|
170 | )
|
---|
171 | {
|
---|
172 | pixel OUT;
|
---|
173 |
|
---|
174 | float4 norm = tex2D(normals, IN.texCoord.xy);
|
---|
175 | float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
|
---|
176 | float4 position = tex2D(positions, IN.texCoord.xy);
|
---|
177 |
|
---|
178 | const float3 normal = normalize(norm.xyz);
|
---|
179 |
|
---|
180 | // hack: an emmisive color term
|
---|
181 | float emmisive = color.w;
|
---|
182 |
|
---|
183 | // diffuse intensity
|
---|
184 | const float angle = saturate(dot(normal, lightDir));
|
---|
185 | //float4 lightDiffuse = float4(1.0f, 1.0f, 0.9f, 1.0f);
|
---|
186 | const float4 lightDiffuse = glstate.light[0].diffuse;
|
---|
187 | //float4(1.0f, 1.0f, 0.9f, 1.0f);
|
---|
188 |
|
---|
189 | float4 diffuse = lightDiffuse * angle;
|
---|
190 |
|
---|
191 | // calc diffuse illumination + shadow term
|
---|
192 | //if ((emmisive < 1.5f) // hack: prevent shadowing the sky
|
---|
193 | if (
|
---|
194 | (position.w > 1e19f)
|
---|
195 | && (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
|
---|
196 | )
|
---|
197 | {
|
---|
198 | position *= maxDepth;
|
---|
199 | position.w = 1.0f;
|
---|
200 |
|
---|
201 | float4 lightSpacePos = mul(shadowMatrix, position);
|
---|
202 | lightSpacePos /= lightSpacePos.w;
|
---|
203 |
|
---|
204 | float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noiseTexture);
|
---|
205 |
|
---|
206 | diffuse *= shadowTerm;
|
---|
207 | }
|
---|
208 |
|
---|
209 | // light ambient term
|
---|
210 | const float4 ambient = glstate.light[0].ambient;
|
---|
211 |
|
---|
212 | // base lighting
|
---|
213 | //OUT.color = (emmisive > 1.5f) ? color: (ambient + diffuse) * color;
|
---|
214 | OUT.color = (position.w > 1e19f) ? color: (ambient + diffuse) * color;
|
---|
215 |
|
---|
216 |
|
---|
217 | ////////////
|
---|
218 | //-- write out logaritmic luminance for tone mapping
|
---|
219 |
|
---|
220 | float4 oldColor = tex2Dlod(colors, float4(IN.texCoord.xy, 0, MAX_LOD_LEVEL));
|
---|
221 |
|
---|
222 | const float3 w = float3(0.299f, 0.587f, 0.114f);
|
---|
223 |
|
---|
224 | float lum = dot(OUT.color.rgb, w);
|
---|
225 | float logLum = log(1e-5f + lum);
|
---|
226 |
|
---|
227 | float logLumOffset = MINLOGLUM * INV_LOGLUM_RANGE;
|
---|
228 | float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
|
---|
229 |
|
---|
230 | if (oldColor.w > 0)
|
---|
231 | OUT.color.w = lerp(oldColor.w, logLumScaled, 0.1f);
|
---|
232 | else
|
---|
233 | OUT.color.w = logLumScaled;
|
---|
234 |
|
---|
235 | return OUT;
|
---|
236 | }
|
---|
237 |
|
---|