1 | float REDUCED_CUBEMAP_SIZE;
|
---|
2 |
|
---|
3 | float4 readCubeMap(samplerCUBE cm, float3 coord)
|
---|
4 | {
|
---|
5 | float4 color = texCUBE( cm, float3(coord.xy, -coord.z) );
|
---|
6 | color.a = 1;
|
---|
7 | return color;
|
---|
8 | }
|
---|
9 |
|
---|
10 | float readDistanceCubeMap(samplerCUBE dcm, float3 coord)
|
---|
11 | {
|
---|
12 | float dist = texCUBE(dcm, float3(coord.xy, - coord.z)).r;
|
---|
13 | if(dist == 0) dist = 1000000; ///sky
|
---|
14 | return dist;
|
---|
15 | }
|
---|
16 |
|
---|
17 | // This function is called several times.
|
---|
18 | float3 Hit( float3 x, float3 R, samplerCUBE mp )
|
---|
19 | {
|
---|
20 | float rl = readDistanceCubeMap( mp, R); // |r|
|
---|
21 |
|
---|
22 | float ppp = length( x ) / readDistanceCubeMap( mp, x); // |p|/|p|
|
---|
23 | float dun = 0, pun = ppp, dov = 0, pov = 0;
|
---|
24 | float dl = rl * ( 1 - ppp ); // eq. 2
|
---|
25 | float3 l = x + R * dl; // ray equation
|
---|
26 |
|
---|
27 | // iteration
|
---|
28 | for( int i = 0; i < 2; i++ ) // 2 !!!!!!!!!!!!!!!!!!!!!!!
|
---|
29 | {
|
---|
30 | float llp = length( l ) / readDistanceCubeMap( mp, l); // |l|/|l|
|
---|
31 | if ( llp < 0.999f ) // undershooting
|
---|
32 | {
|
---|
33 | dun = dl; pun = llp; // last undershooting
|
---|
34 | dl += ( dov == 0 ) ? rl * ( 1 - llp ) : // eq. 2
|
---|
35 | ( dl - dov ) * ( 1 - llp ) / ( llp - pov ); // eq. 3
|
---|
36 | } else if ( llp > 1.001f ) // overshooting
|
---|
37 | {
|
---|
38 | dov = dl; pov = llp; // last overshooting
|
---|
39 | dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
|
---|
40 | }
|
---|
41 | l = x + R * dl; // ray equation
|
---|
42 | }
|
---|
43 | return l; // computed hit point
|
---|
44 | }
|
---|
45 |
|
---|
46 | /////////////////////
|
---|
47 | ///// Diffuse
|
---|
48 | ///////////////////
|
---|
49 |
|
---|
50 | float4 GetContibution(float3 L, float3 L1, float3 L2, float3 L3, float3 L4, float3 pos, float3 N, samplerCUBE cubemap)
|
---|
51 | {
|
---|
52 | float d;
|
---|
53 | //d = texCUBE(cubemap, L).a;
|
---|
54 | d = readDistanceCubeMap(cubemap, L1).r;
|
---|
55 | L1 = d * normalize(L1);
|
---|
56 | d = readDistanceCubeMap(cubemap, L2).r;
|
---|
57 | L2 = d * normalize(L2);
|
---|
58 | d = readDistanceCubeMap(cubemap, L3).r;
|
---|
59 | L3 = d * normalize(L3);
|
---|
60 | d = readDistanceCubeMap(cubemap, L4).r;
|
---|
61 | L4 = d * normalize(L4);
|
---|
62 |
|
---|
63 |
|
---|
64 | float3 r1 = normalize(L1 - pos);
|
---|
65 | float3 r2 = normalize(L2 - pos);
|
---|
66 | float3 r3 = normalize(L3 - pos);
|
---|
67 | float3 r4 = normalize(L4 - pos);
|
---|
68 | /*
|
---|
69 | float tri1 = acos(dot(r1, r2)) * dot(cross(r1, r2), N);
|
---|
70 | float tri2 = acos(dot(r2, r3)) * dot(cross(r2, r3), N);
|
---|
71 | float tri3 = acos(dot(r3, r4)) * dot(cross(r3, r4), N);
|
---|
72 | float tri4 = acos(dot(r4, r1)) * dot(cross(r4, r1), N);
|
---|
73 | */
|
---|
74 | float3 crossP = cross(r1, r2);
|
---|
75 | float r = length(crossP);
|
---|
76 | float dd = dot(r1,r2);
|
---|
77 | float tri1 = acos(dd) * dot(crossP/r, N);
|
---|
78 |
|
---|
79 | crossP = cross(r2, r3);
|
---|
80 | r = length(crossP);
|
---|
81 | dd = dot(r1,r2);
|
---|
82 | float tri2 = acos(dd) * dot(crossP/r, N);
|
---|
83 |
|
---|
84 | crossP = cross(r3, r4);
|
---|
85 | r = length(crossP);
|
---|
86 | dd = dot(r1,r2);
|
---|
87 | float tri3 = acos(dd) * dot(crossP/r, N);
|
---|
88 |
|
---|
89 | crossP = cross(r4, r1);
|
---|
90 | r = length(crossP);
|
---|
91 | dd = dot(r1,r2);
|
---|
92 | float tri4= acos(dd) * dot(crossP/r, N);
|
---|
93 |
|
---|
94 |
|
---|
95 | return max(tri1 + tri2 + tri3 + tri4, 0);
|
---|
96 | //return tri1 + tri2 + tri3 + tri4;
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | struct vertOUT
|
---|
101 | {
|
---|
102 | float4 hPos :POSITION;
|
---|
103 | float3 wPos :TEXCOORD1;
|
---|
104 | float2 texCoord :TEXCOORD0;
|
---|
105 | float3 mNormal :TEXCOORD2;
|
---|
106 | };
|
---|
107 |
|
---|
108 | vertOUT DiffuseVS(float4 position : POSITION,
|
---|
109 | float3 normal : NORMAL,
|
---|
110 | half3 tangent : TEXCOORD1,
|
---|
111 | float2 texCoord : TEXCOORD0,
|
---|
112 | uniform float4x4 worldViewProj,
|
---|
113 | uniform float4x4 world)
|
---|
114 | {
|
---|
115 | vertOUT OUT;
|
---|
116 | OUT.hPos = mul(worldViewProj, position);
|
---|
117 | OUT.wPos = mul(world, position).xyz;
|
---|
118 | OUT.mNormal = mul(world, normal);
|
---|
119 | //OUT.mNormal = normal;
|
---|
120 | OUT.texCoord = texCoord;
|
---|
121 | return OUT;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | float4 DiffusePS( vertOUT IN,
|
---|
126 | uniform float3 cameraPos,
|
---|
127 | uniform float3 lastCenter, //LI//
|
---|
128 | uniform samplerCUBE SmallEnvMapSampler : register(s0),
|
---|
129 | uniform samplerCUBE DistanceEnvMapSampler : register(s1)
|
---|
130 | ) : COLOR0
|
---|
131 | {
|
---|
132 | float M = 4;
|
---|
133 |
|
---|
134 | float3 N = IN.mNormal;
|
---|
135 | N = normalize( N );
|
---|
136 | float3 pos = IN.wPos - lastCenter;
|
---|
137 |
|
---|
138 | // return float4(N,1)+ lastCenter.x*0.000001;
|
---|
139 | // return readCubeMap(SmallEnvMapSampler, pos) + lastCenter.x*0.000001;
|
---|
140 |
|
---|
141 | float4 I = 0;
|
---|
142 | float3 L1, L2, L3, L4, L;
|
---|
143 | float4 Le;
|
---|
144 | float width = 1.0 / M;
|
---|
145 | float width2 = width * 2;
|
---|
146 | float d;
|
---|
147 |
|
---|
148 | for (float x = 0.5; x < M; x++)
|
---|
149 | for (float y = 0.5; y < M; y++)
|
---|
150 | {
|
---|
151 | float2 p, tpos;
|
---|
152 | tpos.x = x * width;
|
---|
153 | tpos.y = y * width;
|
---|
154 |
|
---|
155 | p = tpos.xy;
|
---|
156 | p = 2.0 * p - 1.0; //-1..1
|
---|
157 |
|
---|
158 | L1 = float3(p.x - width, p.y - width, 1);
|
---|
159 | L2 = float3(p.x + width, p.y - width, 1);
|
---|
160 | L3 = float3(p.x + width, p.y + width, 1);
|
---|
161 | L4 = float3(p.x - width, p.y + width, 1);
|
---|
162 | L = float3(p.x, p.y, 1);
|
---|
163 | Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
|
---|
164 |
|
---|
165 | I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);
|
---|
166 |
|
---|
167 | }
|
---|
168 |
|
---|
169 | for (float x = 0.5; x < M; x++)
|
---|
170 | for (float y = 0.5; y < M; y++)
|
---|
171 | {
|
---|
172 | float2 p, tpos;
|
---|
173 | tpos.x = x * width; // 0..1
|
---|
174 | tpos.y = y * width; // 0..1
|
---|
175 |
|
---|
176 | p = tpos.xy;
|
---|
177 | p = 2.0 * p - 1.0; //-1..1
|
---|
178 |
|
---|
179 | L4 = float3(p.x - width, p.y - width, -1);
|
---|
180 | L3 = float3(p.x + width, p.y - width, -1);
|
---|
181 | L2 = float3(p.x + width, p.y + width, -1);
|
---|
182 | L1 = float3(p.x - width, p.y + width, -1);
|
---|
183 | L = float3(p.x, p.y, -1);
|
---|
184 | Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
|
---|
185 |
|
---|
186 | I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);
|
---|
187 | }
|
---|
188 |
|
---|
189 | for (float x = 0.5; x < M; x++)
|
---|
190 | for (float y = 0.5; y < M; y++)
|
---|
191 | {
|
---|
192 | float2 p, tpos;
|
---|
193 | tpos.x = x * width; // 0..1
|
---|
194 | tpos.y = y * width; // 0..1
|
---|
195 |
|
---|
196 | p = tpos.xy;
|
---|
197 | p = 2.0 * p - 1.0; //-1..1
|
---|
198 |
|
---|
199 | L4 = float3(p.x - width, 1, p.y - width);
|
---|
200 | L3 = float3(p.x + width, 1, p.y - width);
|
---|
201 | L2 = float3(p.x + width, 1, p.y + width);
|
---|
202 | L1 = float3(p.x - width, 1, p.y + width);
|
---|
203 | L = float3(p.x, 1, p.y);
|
---|
204 | Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
|
---|
205 |
|
---|
206 | I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);
|
---|
207 | }
|
---|
208 |
|
---|
209 | for (float x = 0.5; x < M; x++)
|
---|
210 | for (float y = 0.5; y < M; y++)
|
---|
211 | {
|
---|
212 | float2 p, tpos;
|
---|
213 | tpos.x = x * width; // 0..1
|
---|
214 | tpos.y = y * width; // 0..1
|
---|
215 |
|
---|
216 | p = tpos.xy;
|
---|
217 | p = 2.0 * p - 1.0; //-1..1
|
---|
218 |
|
---|
219 | L1 = float3(p.x - width, -1, p.y - width);
|
---|
220 | L2 = float3(p.x + width, -1, p.y - width);
|
---|
221 | L3 = float3(p.x + width, -1, p.y + width);
|
---|
222 | L4 = float3(p.x - width, -1, p.y + width);
|
---|
223 | L = float3(p.x, -1, p.y);
|
---|
224 | Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
|
---|
225 |
|
---|
226 | I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);
|
---|
227 | }
|
---|
228 |
|
---|
229 | for (float x = 0.5; x < M; x++)
|
---|
230 | for (float y = 0.5; y < M; y++)
|
---|
231 | {
|
---|
232 | float2 p, tpos;
|
---|
233 | tpos.x = x * width; // 0..1
|
---|
234 | tpos.y = y * width; // 0..1
|
---|
235 |
|
---|
236 | p = tpos.xy;
|
---|
237 | p = 2.0 * p - 1.0; //-1..1
|
---|
238 |
|
---|
239 | L1 = float3(1, p.x - width, p.y - width);
|
---|
240 | L2 = float3(1, p.x + width, p.y - width);
|
---|
241 | L3 = float3(1, p.x + width, p.y + width);
|
---|
242 | L4 = float3(1, p.x - width, p.y + width);
|
---|
243 | L = float3(1, p.x, p.y);
|
---|
244 | Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
|
---|
245 |
|
---|
246 | I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);
|
---|
247 | }
|
---|
248 |
|
---|
249 | for (float x = 0.5; x < M; x++)
|
---|
250 | for (float y = 0.5; y < M; y++)
|
---|
251 | {
|
---|
252 | float2 p, tpos;
|
---|
253 | tpos.x = x * width; // 0..1
|
---|
254 | tpos.y = y * width; // 0..1
|
---|
255 |
|
---|
256 | p = tpos.xy;
|
---|
257 | p = 2.0 * p - 1.0; //-1..1
|
---|
258 |
|
---|
259 | L4 = float3(-1, p.x - width, p.y - width);
|
---|
260 | L3 = float3(-1, p.x + width, p.y - width);
|
---|
261 | L2 = float3(-1, p.x + width, p.y + width);
|
---|
262 | L1 = float3(-1, p.x - width, p.y + width);
|
---|
263 | L = float3(-1, p.x, p.y);
|
---|
264 | Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
|
---|
265 |
|
---|
266 | I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);
|
---|
267 | }
|
---|
268 | float kd = 0.3;
|
---|
269 | return kd * I;
|
---|
270 | }
|
---|
271 |
|
---|
272 | float4 P2PContr(float3 N, float3 Nl, float3 pos, float3 L, samplerCUBE cubemap, samplerCUBE distmap)
|
---|
273 | {
|
---|
274 | Nl = normalize(Nl);
|
---|
275 | L = normalize(L);
|
---|
276 | float4 Le = readCubeMap(cubemap, L);
|
---|
277 | float d = readDistanceCubeMap(distmap, L);
|
---|
278 | float3 Lpos = L * d;
|
---|
279 | float3 Ldir = Lpos - pos;
|
---|
280 | float dist = dot(Ldir, Ldir);
|
---|
281 | Ldir = normalize(Ldir);
|
---|
282 | dist /= 10000.0;
|
---|
283 | return Le * max(dot(N, Ldir),0) * dot(Nl, -1 * Ldir) / dist;
|
---|
284 | }
|
---|
285 |
|
---|
286 | float4 EnvMapDiffuseP2PPS( vertOUT IN,
|
---|
287 | uniform float3 lastCenter, //LI//
|
---|
288 | uniform samplerCUBE SmallEnvMapSampler : register(s0),
|
---|
289 | uniform samplerCUBE DistanceEnvMapSampler : register(s1)) : COLOR
|
---|
290 | {
|
---|
291 | float M = 4.0;
|
---|
292 |
|
---|
293 | //float3 N = mul( world_IT, float4(IN.mNormal,1)).xyz;
|
---|
294 | float3 N = IN.mNormal;
|
---|
295 | N = normalize( N );
|
---|
296 | float3 pos = IN.wPos - lastCenter;
|
---|
297 |
|
---|
298 | float4 I = 0;
|
---|
299 | float3 L;
|
---|
300 | float4 Le;
|
---|
301 | float width = 1.0 / M;
|
---|
302 | float d;
|
---|
303 |
|
---|
304 | //float vdir = length(pos);
|
---|
305 | //return (readDistanceCubeMap(DistanceEnvMapSampler, pos) - vdir);
|
---|
306 | //return readCubeMap(SmallEnvMapSampler, pos);
|
---|
307 | //return readDistanceCubeMap(DistanceEnvMapSampler, pos);
|
---|
308 |
|
---|
309 | for (float x = 0.5; x < M; x++)
|
---|
310 | for (float y = 0.5; y < M; y++)
|
---|
311 | {
|
---|
312 | float2 p, tpos;
|
---|
313 | tpos.x = x * width;
|
---|
314 | tpos.y = y * width;
|
---|
315 |
|
---|
316 | p = tpos.xy;
|
---|
317 | p = 2.0 * p - 1.0; //-1..1
|
---|
318 |
|
---|
319 | I += P2PContr(N, float3(0,0,-1), pos, float3(p.x, p.y, 1), SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
320 | I += P2PContr(N, float3(0,0,1), pos, float3(-p.x, p.y, -1), SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
321 | I += P2PContr(N, float3(-1,0,0), pos, float3(1, p.y, -p.x), SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
322 | I += P2PContr(N, float3(1,0,0), pos, float3(-1, p.y, p.x), SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
323 | I += P2PContr(N, float3(0,-1,0), pos, float3(p.x, 1, -p.y), SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
324 | I += P2PContr(N, float3(0,1,0), pos, float3(p.x, -1, p.y), SmallEnvMapSampler, DistanceEnvMapSampler);
|
---|
325 | }
|
---|
326 |
|
---|
327 | float kd = 0.8;
|
---|
328 | return kd * I;
|
---|
329 | } |
---|