source: GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/GameTools_Diffuse.hlsl @ 2054

Revision 2054, 15.4 KB checked in by szirmay, 17 years ago (diff)
Line 
1float REDUCED_CUBEMAP_SIZE;
2int CUBEMAP_SIZE = 128;
3int RATE = 32;
4
5float4 readCubeMap(samplerCUBE cm, float3 coord)               
6{
7        float4 color = texCUBElod( cm, float4(coord.xy, -coord.z, 1) );
8        color.a = 1;
9        return color;
10}
11
12float readDistanceCubeMap(samplerCUBE dcm, float3 coord)               
13{
14        float dist = texCUBElod(dcm, float4(coord.xy, - coord.z, 1)).r;
15        if(dist == 0) dist = 1000000; ///sky
16        return dist;
17}
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   RATE = 32;
34   CUBEMAP_SIZE = 128;
35   float4 color = 0;
36   float3 dir;
37 
38   for (int i = 0; i < RATE; i++)
39     for (int j = 0; j < RATE; j++)
40    {
41                float2 pos;
42                pos.x = IN.mPos.x + (2*i + 1)/(float)CUBEMAP_SIZE;
43                pos.y = IN.mPos.y - (2*j + 1)/(float)CUBEMAP_SIZE;      // y=-u
44
45                // "scrambling"
46                if (nFace == 0) dir = float3(1, pos.y, -pos.x);
47                if (nFace == 1) dir = float3(-1, pos.y, pos.x);
48                if (nFace == 2) dir = float3(pos.x, 1, -pos.y);
49                if (nFace == 3) dir = float3(pos.x, -1, pos.y);
50                if (nFace == 4) dir = float3(pos.x, pos.y, 1);
51                if (nFace == 5) dir = float3(-pos.x, pos.y,-1);
52
53                color += texCUBE( EnvironmentMapSampler, dir);
54    }
55 
56        return color / (RATE*RATE);             
57}
58
59
60/////////////////////
61///// Diffuse
62///////////////////
63
64float4 GetContibution(float3 L, float3 L1, float3 L2, float3 L3, float3 L4, float3 pos, float3 N, samplerCUBE cubemap)
65{
66        float d;
67        //d = texCUBE(cubemap, L).a;   
68        d = readDistanceCubeMap(cubemap, L1).r;
69        L1 = d * normalize(L1);
70        d = readDistanceCubeMap(cubemap, L2).r;
71        L2 = d * normalize(L2);
72        d = readDistanceCubeMap(cubemap, L3).r;
73        L3 = d * normalize(L3);
74        d = readDistanceCubeMap(cubemap, L4).r;
75        L4 = d * normalize(L4);
76               
77       
78    float3 r1 = normalize(L1 - pos);   
79    float3 r2 = normalize(L2 - pos);
80    float3 r3 = normalize(L3 - pos);
81    float3 r4 = normalize(L4 - pos);
82  /*           
83        float tri1 = acos(dot(r1, r2)) * dot(cross(r1, r2), N);
84        float tri2 = acos(dot(r2, r3)) * dot(cross(r2, r3), N);
85        float tri3 = acos(dot(r3, r4)) * dot(cross(r3, r4), N);
86        float tri4 = acos(dot(r4, r1)) * dot(cross(r4, r1), N);
87  */
88  float3 crossP = cross(r1, r2);
89  float r = length(crossP);
90  float dd = dot(r1,r2);
91  float tri1 = acos(dd) * dot(crossP/r, N);
92 
93  crossP = cross(r2, r3);
94  r = length(crossP);
95  dd = dot(r1,r2);
96  float tri2 = acos(dd) * dot(crossP/r, N);
97 
98  crossP = cross(r3, r4);
99  r = length(crossP);
100  dd = dot(r1,r2);
101  float tri3 = acos(dd) * dot(crossP/r, N);
102 
103  crossP = cross(r4, r1);
104  r = length(crossP);
105  dd = dot(r1,r2);
106  float tri4= acos(dd) * dot(crossP/r, N);
107 
108 
109        return max(tri1 + tri2 + tri3 + tri4, 0);       
110        //return tri1 + tri2 + tri3 + tri4;     
111}
112
113
114struct vertOUT
115{
116        float4 hPos :POSITION;
117        float3 wPos     :TEXCOORD1;
118        float2 texCoord :TEXCOORD0;
119        float3 mNormal :TEXCOORD2;
120};
121
122vertOUT DiffuseVS(float4 position : POSITION,
123                float3 normal   : NORMAL,
124                half3 tangent   : TEXCOORD1,
125                float2 texCoord : TEXCOORD0,                   
126                uniform float4x4 worldViewProj,
127                uniform float4x4 worldI,
128                uniform float4x4 world)
129{
130  vertOUT OUT;
131  OUT.hPos = mul(worldViewProj, position);
132  OUT.wPos = mul(world, position).xyz; 
133  OUT.mNormal = mul(normal, worldI);
134  //OUT.mNormal = normal;
135  OUT.texCoord = texCoord;
136  return OUT;
137}
138
139
140float4 DiffusePS( vertOUT IN,
141           uniform float3 cameraPos,
142           uniform float3 lastCenter,   //LI//
143           uniform samplerCUBE SmallEnvMapSampler : register(s0),
144           uniform samplerCUBE DistanceEnvMapSampler : register(s1)
145            ) : COLOR0
146{
147    float M = 4;
148       
149        float3 N = IN.mNormal;
150    N = normalize( N );
151    float3 pos = IN.wPos - lastCenter;
152   
153    //return  float4(N,1)+ lastCenter.x*0.000001;
154   // return float4(N,1)+ lastCenter.x*0.000001;
155  //  return  readCubeMap(SmallEnvMapSampler, pos) + lastCenter.x*0.000001;
156   
157    float4 I = 0;                                                                       
158        float3 L1, L2, L3, L4, L;                                               
159        float4 Le;                                                                             
160        float width = 1.0 / M;                                                 
161        float width2 = width * 2;
162        float d;
163       
164        for (float x = 1; x < M; x++)                   
165         for (float y = 1; y < M; y++)                                                                         
166         {                                                                                                                             
167                float2 p, tpos;
168            tpos.x = x * width;
169            tpos.y = y * width;
170           
171            p = tpos.xy;   
172            p = 2.0 * p - 1.0; //-1..1
173                           
174                L1 = float3(p.x - width, p.y - width, 1);       
175                L2 = float3(p.x + width, p.y - width, 1);       
176                L3 = float3(p.x + width, p.y + width, 1);       
177                L4 = float3(p.x - width, p.y + width, 1);
178                L = float3(p.x, p.y, 1);
179                Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
180               
181                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);                     
182                       
183        }                                                                                                                                                       
184        /*
185        for (float x = 1; x < M; x++)                   
186         for (float y = 1; y < M; y++)                                                                                 
187         {                                                                                                                             
188                float2 p, tpos;
189            tpos.x = x * width; // 0..1
190            tpos.y = y * width; // 0..1
191           
192            p = tpos.xy;   
193            p = 2.0 * p - 1.0; //-1..1
194                           
195                L4 = float3(p.x - width, p.y - width, -1);     
196                L3 = float3(p.x + width, p.y - width, -1);     
197                L2 = float3(p.x + width, p.y + width, -1);     
198                L1 = float3(p.x - width, p.y + width, -1);
199                L = float3(p.x, p.y, -1);
200                Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
201               
202                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);                     
203         }     
204         
205        for (float x = 1; x < M; x++)                   
206         for (float y = 1; y < M; y++)                                                                                 
207         {                                                                                                                             
208                float2 p, tpos;
209            tpos.x = x * width; // 0..1
210            tpos.y = y * width; // 0..1
211           
212            p = tpos.xy;   
213            p = 2.0 * p - 1.0; //-1..1
214                           
215                L4 = float3(p.x - width, 1, p.y - width);
216                L3 = float3(p.x + width, 1, p.y - width);       
217                L2 = float3(p.x + width, 1, p.y + width);       
218                L1 = float3(p.x - width, 1, p.y + width);                       
219                L = float3(p.x, 1, p.y);
220                Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
221               
222                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);                     
223         }             
224         
225        for (float x = 1; x < M; x++)                   
226         for (float y = 1; y < M; y++)                                                                                 
227         {                                                                                                                             
228                float2 p, tpos;
229            tpos.x = x * width; // 0..1
230            tpos.y = y * width; // 0..1
231           
232            p = tpos.xy;   
233            p = 2.0 * p - 1.0; //-1..1
234                           
235                L1 = float3(p.x - width, -1, p.y - width);
236                L2 = float3(p.x + width, -1, p.y - width);     
237                L3 = float3(p.x + width, -1, p.y + width);     
238                L4 = float3(p.x - width, -1, p.y + width);                     
239                L = float3(p.x, -1, p.y);
240                Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
241               
242                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);                     
243         }
244         
245         for (float x = 1; x < M; x++)                 
246         for (float y = 1; y < M; y++)                                                                         
247                {                                                                                                                               
248                float2 p, tpos;
249            tpos.x = x * width; // 0..1
250            tpos.y = y * width; // 0..1
251           
252            p = tpos.xy;   
253            p = 2.0 * p - 1.0; //-1..1
254                           
255                L1 = float3(1, p.x - width, p.y - width);
256                L2 = float3(1, p.x + width, p.y - width);       
257                L3 = float3(1, p.x + width, p.y + width);       
258                L4 = float3(1, p.x - width, p.y + width);       
259                L = float3(1, p.x, p.y);
260                Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
261               
262                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);                     
263        }
264         
265        for (float x = 1; x < M; x++)                   
266         for (float y = 1; y < M; y++)                                                                                 
267         {                                                                                                                             
268                float2 p, tpos;
269            tpos.x = x * width; // 0..1
270            tpos.y = y * width; // 0..1
271           
272            p = tpos.xy;   
273            p = 2.0 * p - 1.0; //-1..1
274                           
275                L4 = float3(-1, p.x - width, p.y - width);
276                L3 = float3(-1, p.x + width, p.y - width);     
277                L2 = float3(-1, p.x + width, p.y + width);     
278                L1 = float3(-1, p.x - width, p.y + width);     
279                L = float3(-1, p.x, p.y);
280                Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
281               
282                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);                             
283         }      */
284         float kd = 0.5;                                                                                                                                               
285        return kd * I;
286}
287
288struct vertOUTBump
289{
290        float4 hPos :POSITION;
291        float3 wPos     :TEXCOORD1;
292        float2 texCoord :TEXCOORD0;
293        float3 mNormal  :TEXCOORD2;
294        float3 tangent  :TEXCOORD3;
295        float3 binormal :TEXCOORD4;
296};
297
298vertOUTBump DiffuseBumpVS(float4 position : POSITION,
299                float3 normal   : NORMAL,
300                half3 tangent   : TEXCOORD1,
301                float2 texCoord : TEXCOORD0,                   
302                uniform float4x4 worldViewProj,
303                uniform float4x4 world)
304{
305  vertOUTBump OUT;
306  OUT.hPos = mul(worldViewProj, position);
307  OUT.wPos = mul(world, position).xyz; 
308  OUT.mNormal = normal;
309  OUT.tangent  = tangent;
310  OUT.texCoord = texCoord;
311  OUT.binormal = cross(tangent, normal);
312  return OUT;
313}
314
315float4 DiffuseBumpPS( vertOUTBump IN,
316           uniform float3 cameraPos,
317           uniform float3 lastCenter,   //LI//
318           uniform samplerCUBE SmallEnvMapSampler : register(s0),
319           uniform samplerCUBE DistanceEnvMapSampler : register(s1),
320           uniform sampler2D NormalMap : register(s2),
321           uniform float4x4 worldI
322            ) : COLOR0
323{
324    float M = 4;
325       
326    float3 N = IN.mNormal;
327    N = normalize( N );
328    float3x3 TangentToModel = float3x3(IN.tangent, IN.binormal, N);
329   
330    half3 tNormal = tex2D(NormalMap, IN.texCoord).rgb; 
331   
332    N = mul(tNormal, TangentToModel );   
333     
334    N = normalize( N );
335    N = mul(N, worldI);
336
337    float3 pos = IN.wPos - lastCenter;
338   
339    //return float4(N,1)+ lastCenter.x*0.000001;
340  //  return  readCubeMap(SmallEnvMapSampler, pos) + lastCenter.x*0.000001;
341   
342    float4 I = 0;                                                                       
343        float3 L1, L2, L3, L4, L;                                               
344        float4 Le;                                                                             
345        float width = 1.0 / M;                                                 
346        float width2 = width * 2;
347        float d;
348        /*
349        for (float x = 0.5; x < M; x++)                 
350         for (float y = 0.5; y < M; y++)                                                                                       
351         {                                                                                                                             
352                float2 p, tpos;
353            tpos.x = x * width;
354            tpos.y = y * width;
355           
356            p = tpos.xy;   
357            p = 2.0 * p - 1.0; //-1..1
358                           
359                L1 = float3(p.x - width, p.y - width, 1);       
360                L2 = float3(p.x + width, p.y - width, 1);       
361                L3 = float3(p.x + width, p.y + width, 1);       
362                L4 = float3(p.x - width, p.y + width, 1);
363                L = float3(p.x, p.y, 1);
364                Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
365               
366                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);                     
367                       
368        }                                                                                                                                                       
369       
370        for (float x = 0.5; x < M; x++)                 
371         for (float y = 0.5; y < M; y++)                                                                                       
372         {                                                                                                                             
373                float2 p, tpos;
374            tpos.x = x * width; // 0..1
375            tpos.y = y * width; // 0..1
376           
377            p = tpos.xy;   
378            p = 2.0 * p - 1.0; //-1..1
379                           
380                L4 = float3(p.x - width, p.y - width, -1);     
381                L3 = float3(p.x + width, p.y - width, -1);     
382                L2 = float3(p.x + width, p.y + width, -1);     
383                L1 = float3(p.x - width, p.y + width, -1);
384                L = float3(p.x, p.y, -1);
385                Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
386               
387                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);                     
388         }     
389         
390        for (float x = 0.5; x < M; x++)                 
391         for (float y = 0.5; y < M; y++)                                                                                       
392         {                                                                                                                             
393                float2 p, tpos;
394            tpos.x = x * width; // 0..1
395            tpos.y = y * width; // 0..1
396           
397            p = tpos.xy;   
398            p = 2.0 * p - 1.0; //-1..1
399                           
400                L4 = float3(p.x - width, 1, p.y - width);
401                L3 = float3(p.x + width, 1, p.y - width);       
402                L2 = float3(p.x + width, 1, p.y + width);       
403                L1 = float3(p.x - width, 1, p.y + width);                       
404                L = float3(p.x, 1, p.y);
405                Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
406               
407                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);                     
408         }             
409         
410        for (float x = 0.5; x < M; x++)                 
411         for (float y = 0.5; y < M; y++)                                                                                       
412         {                                                                                                                             
413                float2 p, tpos;
414            tpos.x = x * width; // 0..1
415            tpos.y = y * width; // 0..1
416           
417            p = tpos.xy;   
418            p = 2.0 * p - 1.0; //-1..1
419                           
420                L1 = float3(p.x - width, -1, p.y - width);
421                L2 = float3(p.x + width, -1, p.y - width);     
422                L3 = float3(p.x + width, -1, p.y + width);     
423                L4 = float3(p.x - width, -1, p.y + width);                     
424                L = float3(p.x, -1, p.y);
425                Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
426               
427                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);                     
428         }
429         
430         for (float x = 0.5; x < M; x++)                       
431                for (float y = 0.5; y < M; y++)                                                                                 
432                {                                                                                                                               
433                float2 p, tpos;
434            tpos.x = x * width; // 0..1
435            tpos.y = y * width; // 0..1
436           
437            p = tpos.xy;   
438            p = 2.0 * p - 1.0; //-1..1
439                           
440                L1 = float3(1, p.x - width, p.y - width);
441                L2 = float3(1, p.x + width, p.y - width);       
442                L3 = float3(1, p.x + width, p.y + width);       
443                L4 = float3(1, p.x - width, p.y + width);       
444                L = float3(1, p.x, p.y);
445                Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
446               
447                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);                     
448        }
449         
450        for (float x = 0.5; x < M; x++)                 
451         for (float y = 0.5; y < M; y++)                                                                                       
452         {                                                                                                                             
453                float2 p, tpos;
454            tpos.x = x * width; // 0..1
455            tpos.y = y * width; // 0..1
456           
457            p = tpos.xy;   
458            p = 2.0 * p - 1.0; //-1..1
459                           
460                L4 = float3(-1, p.x - width, p.y - width);
461                L3 = float3(-1, p.x + width, p.y - width);     
462                L2 = float3(-1, p.x + width, p.y + width);     
463                L1 = float3(-1, p.x - width, p.y + width);     
464                L = float3(-1, p.x, p.y);
465                Le = float4(readCubeMap(SmallEnvMapSampler, L).rgb, 1);
466               
467                I += 0.5 * Le * GetContibution( L, L1, L2, L3, L4, pos, N, DistanceEnvMapSampler);                             
468         }      */
469         float kd = 0.3;                                                                                                                                               
470        return kd * I;
471}
472
473float4 P2PContr(float3 N, float3 Nl, float3 pos, float3 L, samplerCUBE cubemap, samplerCUBE distmap)
474{
475        Nl = normalize(Nl);
476        L = normalize(L);
477        float4 Le = readCubeMap(cubemap, L);
478        float d = readDistanceCubeMap(distmap, L);
479        float3 Lpos = L * d;
480        float3 Ldir = Lpos - pos;
481        float dist = 4 * dot(Ldir, Ldir);
482        Ldir = normalize(Ldir);
483               
484        return Le * (max(dot(N, Ldir),0) * dot(Nl, -1 * Ldir)) / dist; 
485}
486
487float4 EnvMapDiffuseP2PPS( vertOUT IN,
488                                                        uniform float3 lastCenter,      //LI//
489                                                        uniform samplerCUBE SmallEnvMapSampler : register(s0),
490                                                        uniform samplerCUBE DistanceEnvMapSampler : register(s1)) : COLOR                       
491{               
492        float M = 4.0; 
493       
494    float3 N = IN.mNormal;
495    N = normalize( N );
496    float3 pos = IN.wPos - lastCenter;                                                                                                                                         
497                                                                                                       
498    float4 I = 0;                                                                       
499        float3 L;                                               
500        float4 Le;                                                                             
501        float width = 1.0 / M;
502        float d;       
503       
504        float kd = 0.3;                                                                                                                                         
505       
506        //return kd * readCubeMap(SmallEnvMapSampler, N) + lastCenter.x * 0.00000001;
507       
508        for (float x = 0.5; x < M; x++)                 
509         for (float y = 0.5; y < M; y++)                                                                                       
510         {                                                                                                                             
511                float2 p, tpos;
512            tpos.x = x * width;
513            tpos.y = y * width;
514           
515            p = tpos.xy;   
516            p = 2.0 * p - 1.0; //-1..1
517                           
518                I += P2PContr(N, float3(0,0,-1), pos, float3(p.x, p.y, 1), SmallEnvMapSampler, DistanceEnvMapSampler);
519                I += P2PContr(N, float3(0,0,1), pos, float3(-p.x, p.y, -1), SmallEnvMapSampler, DistanceEnvMapSampler);
520                I += P2PContr(N, float3(-1,0,0), pos, float3(1, p.y, -p.x), SmallEnvMapSampler, DistanceEnvMapSampler);
521                I += P2PContr(N, float3(1,0,0), pos, float3(-1, p.y, p.x), SmallEnvMapSampler, DistanceEnvMapSampler);
522                I += P2PContr(N, float3(0,-1,0), pos, float3(p.x, 1, -p.y), SmallEnvMapSampler, DistanceEnvMapSampler);
523                I += P2PContr(N, float3(0,1,0), pos, float3(p.x, -1, p.y), SmallEnvMapSampler, DistanceEnvMapSampler);
524        }
525                                                                                                                                               
526        return kd * I;                                                                                                                 
527}
Note: See TracBrowser for help on using the repository browser.