source: GTP/branches/IllumWPdeliver2008dec/IlluminationWP/precompiled/app/OgreIllumModule_Resources/materials/GTPAdvancedEnvMap/diffuse/GTPDiffuse.hlsl @ 3255

Revision 3255, 14.6 KB checked in by szirmay, 15 years ago (diff)
Line 
1
2#define CUBEMAP_SIZE 128
3#define REDUCED_CUBEMAP_SIZE 4
4#define RATE 32
5
6float4 readCubeMap(samplerCUBE cm, float3 coord)               
7{
8        float4 color = texCUBElod( cm, float4(coord.xy, -coord.z, 0) );
9        color.a = 1;
10        return color;
11}
12
13float readDistanceCubeMap(samplerCUBE dcm, float3 coord)               
14{
15        float dist = texCUBElod(dcm, float4(coord.xy, - coord.z, 0)).r;
16        if(dist == 0) dist = 1000000; ///sky
17        return dist;
18}
19
20////////////////////////////
21/// Reduce cube map shader
22///////////////////////////
23struct MPos_OUT
24{
25 float4 VPos : POSITION;
26 float4 MPos : TEXCOORD0;
27};
28/*
29float4 ReduceCubeMap_PS(MPos_OUT IN,
30                                                uniform int nFace,
31                                                uniform samplerCUBE EnvironmentMapSampler : register(s0) ) : COLOR
32
33   float4 color = 0;
34   float3 dir;
35 
36   for (int i = 0; i < RATE; i++)
37     for (int j = 0; j < RATE; j++)
38    {
39                float2 pos;
40                pos.x = IN.MPos.x + (2*i + 1)/(float)CUBEMAP_SIZE;
41                pos.y = IN.MPos.y - (2*j + 1)/(float)CUBEMAP_SIZE;      // y=-u
42
43                // "scrambling"
44                if (nFace == 0) dir = float3(1, pos.y, -pos.x);
45                if (nFace == 1) dir = float3(-1, pos.y, pos.x);
46                if (nFace == 2) dir = float3(pos.x, 1, -pos.y);
47                if (nFace == 3) dir = float3(pos.x, -1, pos.y);
48                if (nFace == 4) dir = float3(pos.x, pos.y, 1);
49                if (nFace == 5) dir = float3(-pos.x, pos.y,-1);
50
51                color += texCUBE( EnvironmentMapSampler, dir);
52    }
53 
54        return color / (RATE * RATE);           
55}
56*/
57
58float4 ReduceCubeMap_PS(MPos_OUT IN,
59                                                uniform int nFace,
60                                                uniform samplerCUBE EnvironmentMapSampler : register(s0) ) : COLOR
61
62   float4 color = 0;
63   float3 dir;
64 
65   for (int i = 0; i < RATE/2; i+=1)
66     for (int j = 0; j < RATE/2; j+=1)
67    {
68                float2 pos;
69                pos.x = IN.MPos.x + (4*i + 2)/(float)CUBEMAP_SIZE;
70                pos.y = IN.MPos.y - (4*j + 2)/(float)CUBEMAP_SIZE;      // y=-u
71
72                // "scrambling"
73                if (nFace == 0) dir = float3(1, pos.y, -pos.x);
74                if (nFace == 1) dir = float3(-1, pos.y, pos.x);
75                if (nFace == 2) dir = float3(pos.x, 1, -pos.y);
76                if (nFace == 3) dir = float3(pos.x, -1, pos.y);
77                if (nFace == 4) dir = float3(pos.x, pos.y, 1);
78                if (nFace == 5) dir = float3(-pos.x, pos.y,-1);
79
80                color += texCUBE( EnvironmentMapSampler, dir);
81    }
82 
83        return color / (RATE * RATE / 4.0);             
84}
85
86////////////////
87/// Diffuse
88///////////////
89
90/// Polygon to point form factor
91
92float4 Poly2Point_Contr(float3 L, float3 L1, float3 L2, float3 L3, float3 L4, float3 pos, float3 N, samplerCUBE cubemap)
93{
94        float d;
95        //d = texCUBE(cubemap, L).a;   
96        d = readDistanceCubeMap(cubemap, L1).r;
97        L1 = d * normalize(L1);
98        d = readDistanceCubeMap(cubemap, L2).r;
99        L2 = d * normalize(L2);
100        d = readDistanceCubeMap(cubemap, L3).r;
101        L3 = d * normalize(L3);
102        d = readDistanceCubeMap(cubemap, L4).r;
103        L4 = d * normalize(L4);
104               
105       
106    float3 r1 = normalize(L1 - pos);   
107    float3 r2 = normalize(L2 - pos);
108    float3 r3 = normalize(L3 - pos);
109    float3 r4 = normalize(L4 - pos);
110  /*           
111        float tri1 = acos(dot(r1, r2)) * dot(cross(r1, r2), N);
112        float tri2 = acos(dot(r2, r3)) * dot(cross(r2, r3), N);
113        float tri3 = acos(dot(r3, r4)) * dot(cross(r3, r4), N);
114        float tri4 = acos(dot(r4, r1)) * dot(cross(r4, r1), N);
115  */
116  float3 crossP = cross(r1, r2);
117  float r = length(crossP);
118  float dd = dot(r1,r2);
119  float tri1 = acos(dd) * dot(crossP/r, N);
120 
121  crossP = cross(r2, r3);
122  r = length(crossP);
123  dd = dot(r1,r2);
124  float tri2 = acos(dd) * dot(crossP/r, N);
125 
126  crossP = cross(r3, r4);
127  r = length(crossP);
128  dd = dot(r1,r2);
129  float tri3 = acos(dd) * dot(crossP/r, N);
130 
131  crossP = cross(r4, r1);
132  r = length(crossP);
133  dd = dot(r1,r2);
134  float tri4= acos(dd) * dot(crossP/r, N);
135 
136 
137        return max(tri1 + tri2 + tri3 + tri4, 0);       
138        //return tri1 + tri2 + tri3 + tri4;     
139}
140
141struct Shaded_OUT
142{
143 float4 vPos : POSITION;
144 float4 wNormal : TEXCOORD0;
145 float4 wPos    : TEXCOORD1;
146};
147
148float4 Diffuse_Poly2Point_PS( Shaded_OUT IN,
149                                                        uniform float3 lastCenter,
150                                                        uniform samplerCUBE SmallEnvMapSampler : register(s0),
151                                                        uniform samplerCUBE DistanceEnvMapSampler : register(s1)
152            ) : COLOR0
153{
154    float M = REDUCED_CUBEMAP_SIZE;
155       
156        float3 N = IN.wNormal.xyz;
157    N = normalize( N );
158    float3 pos = IN.wPos.xyz - lastCenter;
159   
160    float4 I = 0;                                                                       
161        float3 L1, L2, L3, L4, L;                                               
162        float4 Le;                                                                             
163        float width = 1.0 / M;                                                 
164        float width2 = width * 2;
165        float d;
166       
167        for (float x = 1; x < M; x++)                   
168         for (float y = 1; y < M; y++)                                                                         
169         {                                                                                                                             
170                float2 p, tpos;
171            tpos.x = x * width;
172            tpos.y = y * width;
173           
174            p = tpos.xy;   
175            p = 2.0 * p - 1.0; //-1..1
176                           
177                L1 = float3(p.x - width, p.y - width, 1);       
178                L2 = float3(p.x + width, p.y - width, 1);       
179                L3 = float3(p.x + width, p.y + width, 1);       
180                L4 = float3(p.x - width, p.y + width, 1);
181                L = float3(p.x, p.y, 1);
182                Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
183               
184                I += 0.5 * Le * Poly2Point_Contr( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);                   
185                       
186        }                                                                                                                                                       
187       
188        for (float x = 1; x < M; x++)                   
189         for (float y = 1; y < M; y++)                                                                                 
190         {                                                                                                                             
191                float2 p, tpos;
192            tpos.x = x * width; // 0..1
193            tpos.y = y * width; // 0..1
194           
195            p = tpos.xy;   
196            p = 2.0 * p - 1.0; //-1..1
197                           
198                L4 = float3(p.x - width, p.y - width, -1);     
199                L3 = float3(p.x + width, p.y - width, -1);     
200                L2 = float3(p.x + width, p.y + width, -1);     
201                L1 = float3(p.x - width, p.y + width, -1);
202                L = float3(p.x, p.y, -1);
203                Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
204               
205                I += 0.5 * Le * Poly2Point_Contr( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);                   
206         }     
207         
208        for (float x = 1; x < M; x++)                   
209         for (float y = 1; y < M; y++)                                                                                 
210         {                                                                                                                             
211                float2 p, tpos;
212            tpos.x = x * width; // 0..1
213            tpos.y = y * width; // 0..1
214           
215            p = tpos.xy;   
216            p = 2.0 * p - 1.0; //-1..1
217                           
218                L4 = float3(p.x - width, 1, p.y - width);
219                L3 = float3(p.x + width, 1, p.y - width);       
220                L2 = float3(p.x + width, 1, p.y + width);       
221                L1 = float3(p.x - width, 1, p.y + width);                       
222                L = float3(p.x, 1, p.y);
223                Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
224               
225                I += 0.5 * Le * Poly2Point_Contr( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);                   
226         }             
227         
228        for (float x = 1; x < M; x++)                   
229         for (float y = 1; y < M; y++)                                                                                 
230         {                                                                                                                             
231                float2 p, tpos;
232            tpos.x = x * width; // 0..1
233            tpos.y = y * width; // 0..1
234           
235            p = tpos.xy;   
236            p = 2.0 * p - 1.0; //-1..1
237                           
238                L1 = float3(p.x - width, -1, p.y - width);
239                L2 = float3(p.x + width, -1, p.y - width);     
240                L3 = float3(p.x + width, -1, p.y + width);     
241                L4 = float3(p.x - width, -1, p.y + width);                     
242                L = float3(p.x, -1, p.y);
243                Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
244               
245                I += 0.5 * Le * Poly2Point_Contr( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);                   
246         }
247         
248         for (float x = 1; x < M; x++)                 
249         for (float y = 1; y < M; y++)                                                                         
250                {                                                                                                                               
251                float2 p, tpos;
252            tpos.x = x * width; // 0..1
253            tpos.y = y * width; // 0..1
254           
255            p = tpos.xy;   
256            p = 2.0 * p - 1.0; //-1..1
257                           
258                L1 = float3(1, p.x - width, p.y - width);
259                L2 = float3(1, p.x + width, p.y - width);       
260                L3 = float3(1, p.x + width, p.y + width);       
261                L4 = float3(1, p.x - width, p.y + width);       
262                L = float3(1, p.x, p.y);
263                Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
264               
265                I += 0.5 * Le * Poly2Point_Contr( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);                   
266        }
267         
268        for (float x = 1; x < M; x++)                   
269         for (float y = 1; y < M; y++)                                                                                 
270         {                                                                                                                             
271                float2 p, tpos;
272            tpos.x = x * width; // 0..1
273            tpos.y = y * width; // 0..1
274           
275            p = tpos.xy;   
276            p = 2.0 * p - 1.0; //-1..1
277                           
278                L4 = float3(-1, p.x - width, p.y - width);
279                L3 = float3(-1, p.x + width, p.y - width);     
280                L2 = float3(-1, p.x + width, p.y + width);     
281                L1 = float3(-1, p.x - width, p.y + width);     
282                L = float3(-1, p.x, p.y);
283                Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
284               
285                I += 0.5 * Le * Poly2Point_Contr( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);                           
286         }
287         float kd = 0.32;                                                                                                                                               
288        return kd * I;
289}
290
291/// Point to point form factor
292
293float4 Point2Point_Contr(float3 N, float3 Nl, float3 pos, float3 L, samplerCUBE cubemap, samplerCUBE distmap)
294{
295        Nl = normalize(Nl);
296        L = normalize(L);
297        float4 Le = readCubeMap(cubemap, L);
298        float d = readDistanceCubeMap(distmap, L);
299        float3 Lpos = L * d;
300        float3 Ldir = Lpos - pos;
301        float dist = 4 * dot(Ldir, Ldir);
302        Ldir = normalize(Ldir);
303               
304        return Le * (max(dot(N, Ldir),0) * dot(Nl, -1 * Ldir)) / dist; 
305}
306
307float4 Diffuse_Point2Point_PS( Shaded_OUT IN,
308                                                        uniform float3 lastCenter,
309                                                        uniform samplerCUBE SmallEnvMapSampler : register(s0),
310                                                        uniform samplerCUBE DistanceEnvMapSampler : register(s1)) : COLOR                       
311{               
312        float M = REDUCED_CUBEMAP_SIZE;
313       
314    float3 N = IN.wNormal.xyz;
315    N = normalize( N );
316    float3 pos = IN.wPos.xyz - lastCenter;                                                                                                                                             
317                                                                                                       
318    float4 I = 0;                                                                       
319        float3 L;                                               
320        float4 Le;                                                                             
321        float width = 1.0 / M;
322        float d;       
323       
324        float kd = 1.0;                                                                                                                                         
325               
326        for (float x = 0.5; x < M; x++)                 
327         for (float y = 0.5; y < M; y++)                                                                                       
328         {                                                                                                                             
329                float2 p, tpos;
330            tpos.x = x * width;
331            tpos.y = y * width;
332           
333            p = tpos.xy;   
334            p = 2.0 * p - 1.0; //-1..1
335                           
336                I += Point2Point_Contr(N, float3(0,0,-1), pos, float3(p.x, p.y, 1), SmallEnvMapSampler, DistanceEnvMapSampler);
337                I += Point2Point_Contr(N, float3(0,0,1), pos, float3(-p.x, p.y, -1), SmallEnvMapSampler, DistanceEnvMapSampler);
338                I += Point2Point_Contr(N, float3(-1,0,0), pos, float3(1, p.y, -p.x), SmallEnvMapSampler, DistanceEnvMapSampler);
339                I += Point2Point_Contr(N, float3(1,0,0), pos, float3(-1, p.y, p.x), SmallEnvMapSampler, DistanceEnvMapSampler);
340                I += Point2Point_Contr(N, float3(0,-1,0), pos, float3(p.x, 1, -p.y), SmallEnvMapSampler, DistanceEnvMapSampler);
341                I += Point2Point_Contr(N, float3(0,1,0), pos, float3(p.x, -1, p.y), SmallEnvMapSampler, DistanceEnvMapSampler);
342        }
343                                                                                                                                               
344        return kd * I;                                                                                                                 
345}
346
347/// Disc to point form factor
348
349float4 Disc2Point_Contr(float3 L, float3 pos, float3 N, float3 V, samplerCUBE SmallEnvMapSampler, samplerCUBE DistanceEnvMapSampler)    // Phong-Blinn
350// L: a hossza lényeges (az egységkocka faláig ér)
351{
352        float mindist = 1.0;
353   
354        float kd = 0.3; // 0.3
355        float ks = 0;   // 0.5
356        float shininess = 10;
357       
358        float l = length(L);
359        L = normalize(L);
360
361        //dw
362        float dw = 4 / (REDUCED_CUBEMAP_SIZE*REDUCED_CUBEMAP_SIZE*l*l*l + 4/3.1416f);
363        //Lin
364        float4 Lin = readCubeMap(SmallEnvMapSampler, L);
365        //r
366        float doy = readDistanceCubeMap(DistanceEnvMapSampler, L);
367        float dxy = length(L * doy - pos);
368       
369        dxy = max(mindist, dxy);
370               
371
372        //dws
373        float dws = (doy*doy * dw) / (dxy*dxy*(1 - dw/3.1416f) + doy*doy*dw/3.1416f);   // localization:
374        //float dws = dw;
375       
376        //L = L * doy - pos;    // L: x->y, az objektumtól induljon, ne a középpontból
377        L = normalize(L);
378        float3 H = normalize(L + V);    // felezõvektor
379
380        float a = kd * max(dot(N,L),0) +
381                          ks * pow(max(dot(N,H),0), shininess); // diffuse + specular
382
383        // 1.: eddigi
384        //return Lin * a * dws;
385       
386        float ctheta_in = dot(N,L);
387        float ctheta_out = dot(N,V);   
388       
389        return Lin * a * dws;           
390}
391
392float4 Diffuse_Disc2Point_PS(Shaded_OUT IN,
393                                                        uniform float3 lastCenter,
394                                                        uniform float3 cameraPos,
395                                                        uniform samplerCUBE SmallEnvMapSampler : register(s0),
396                                                        uniform samplerCUBE DistanceEnvMapSampler : register(s1)
397                   ) : COLOR0
398{
399        float M = REDUCED_CUBEMAP_SIZE;
400       
401    float3 N = IN.wNormal.xyz;
402    N = normalize( N );
403    float3 V = normalize(IN.wPos.xyz - cameraPos);
404    float3 pos = IN.wPos.xyz - lastCenter;                                                                                                                                             
405                                                                                                       
406    float4 I = 0;                                                                       
407        float3 L;                                               
408        float4 Le;                                                                             
409        float width = 1.0 / M;
410        float d;       
411                       
412        for (float x = 0.5; x < M; x++)                 
413         for (float y = 0.5; y < M; y++)                                                                                       
414         {                                                                                                                             
415                float2 p, tpos;
416            tpos.x = x * width;
417            tpos.y = y * width;
418           
419            p = tpos.xy;   
420            p = 2.0 * p - 1.0; //-1..1
421           
422            float3 L;
423           
424                L = float3(p.x, p.y, 1);
425                I += Disc2Point_Contr( L, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
426               
427                L = float3(p.x, p.y, -1);
428                I += Disc2Point_Contr( L, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
429               
430                L = float3(p.x, 1, p.y);
431                I += Disc2Point_Contr( L, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
432               
433                L = float3(p.x, -1, p.y);
434                I += Disc2Point_Contr( L, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
435               
436                L = float3(1, p.x, p.y);
437                I += Disc2Point_Contr( L, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
438               
439                L = float3(-1, p.x, p.y);
440                I += Disc2Point_Contr( L, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
441        }
442        float kd = 1.0;
443        return kd * I;
444}
445
446float4 Glossy_Disc2Point_PS(Shaded_OUT IN,
447                                                        uniform float3 lastCenter,
448                                                        uniform float3 cameraPos,
449                                                        uniform samplerCUBE SmallEnvMapSampler : register(s0),
450                                                        uniform samplerCUBE DistanceEnvMapSampler : register(s1)
451                   ) : COLOR0
452{
453        float M = REDUCED_CUBEMAP_SIZE;
454       
455    float3 N = IN.wNormal.xyz;
456    N = normalize( N );
457    float3 V = normalize(IN.wPos.xyz - cameraPos);
458    float3 pos = IN.wPos.xyz - lastCenter;                                                                                                                                             
459    float3 R = reflect( V, N );                                                                 
460
461    float rr = max( max(abs(R.x), abs(R.y)), abs(R.z) );        // select the largest component
462    R /= rr;    // scale the largest component to value +/-1
463
464    float3 offset1 = float3(1,0,0);
465    float3 offset2 = float3(0,1,0);
466    if (abs(R.x) > abs(R.y) && abs(R.x) > abs(R.z))
467                offset1 = float3(0,0,1);       
468    if (abs(R.y) > abs(R.x) && abs(R.y) > abs(R.z))
469                offset2 = float3(0,0,1);
470
471
472    float4 I = 0;                                                                       
473    float3 L;                                           
474    float width = 2.0 / M;
475           
476        L = R;
477        I += Disc2Point_Contr( L * 0.75, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
478       
479        L = R + offset1 * width;
480        I += Disc2Point_Contr( L * 0.75, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
481       
482        L = R - offset1 * width;
483        I += Disc2Point_Contr( L * 0.75, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
484               
485        L = R + offset2 * width;
486        I += Disc2Point_Contr( L * 0.75, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
487               
488        L = R - offset2 * width;
489        I += Disc2Point_Contr( L * 0.75, pos, N, V, SmallEnvMapSampler, DistanceEnvMapSampler);
490               
491       
492   float kd = 1.0;
493//return readCubeMap(SmallEnvMapSampler, pos) + lastCenter.x*0.0000000001;
494   return kd * I * 2 * M;
495}
Note: See TracBrowser for help on using the repository browser.