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

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