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

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