source: GTP/branches/IllumWPdeliver2008dec/IlluminationWP/precompiled/app/OgreIllumModule_Resources/materials/GTPAdvancedEnvMap/multibounce/multipleRefraction2L/GTPMultipleRefraction2L.hlsl @ 3255

Revision 3255, 6.2 KB checked in by szirmay, 15 years ago (diff)
Line 
1float4 readCubeMap(samplerCUBE cm, float3 coord)               
2{
3        float4 color = texCUBElod( cm, float4(coord.xy, - coord.z,0) );
4        return color;
5}
6
7float readDistanceCubeMap(samplerCUBE dcm, float3 coord)               
8{
9        float dist = texCUBElod(dcm, float4(coord.xy, - coord.z,0)).a;
10        return dist;
11}
12
13#define MAX_LIN_ITERATIONCOUNT 2  //80
14#define MIN_LIN_ITERATIONCOUNT 1  //60
15#define SECANT_ITERATIONCOUNT 1
16#define MAX_RAY_DEPTH 2
17
18void linearSearch(  float3 x, float3 R, float3 N, samplerCUBE mp,
19                    out float3 p,
20                    out float dl,
21                    out float dp,
22                    out float llp,
23                    out float ppp)
24{
25 float3 Ra = abs(R), xa = abs(x);
26 float xm =  max(max(xa.x,xa.y),xa.z);
27 float Rm = max(max(Ra.x,Ra.y),Ra.z);
28 float a = xm / Rm;
29 
30 int shootL = 0, shootP = 0;         
31 bool found = false;
32 
33 float dt =  length(x / xm - R / Rm) * MAX_LIN_ITERATIONCOUNT;
34 dt = max(dt, MIN_LIN_ITERATIONCOUNT);
35 dt = 1.0 / dt;
36   
37 float t = 0.01;//dt;
38 float pa;
39 
40 //Linear iteration
41 while(t <= 1.0 && !found)
42 {
43   dp = a * t / (1 - t);
44   p = x + R * dp;
45   pa = readDistanceCubeMap(mp, p);
46     
47   if(pa > 0)
48   {
49    ppp = length(p) / pa;
50    if(ppp < 1)
51      shootP = -1;
52    else
53      shootP = 1;     
54    if(shootL * shootP == -1)
55      found = true;
56    else
57    {
58      shootL = shootP;
59      dl = dp;
60      llp = ppp;
61    }
62   }
63   else
64     shootL = 0;
65     
66   t += dt; 
67 }
68
69 if(!found)
70  p = float3(0,0,0);
71}
72
73
74void secantSearch(float3 x, float3 R, samplerCUBE mp,
75                       float dl,
76                       float dp,
77                       float llp,
78                       float ppp,
79                       out float3 p)
80{
81  for(int i= 0; i < SECANT_ITERATIONCOUNT; i++)
82  {
83   float dnew;
84   dnew = dl + (dp - dl) * (1 - llp) / (ppp - llp);
85   p = x + R * dnew;
86   half pppnew = length(p) / readDistanceCubeMap(mp, p);
87   if(pppnew < 1)
88   {
89    llp = pppnew;
90    dl = dnew;
91   }
92   else
93   {
94    ppp = pppnew;
95    dp = dnew;
96   }
97  }
98}
99/*
100float3 Hit(float3 x, float3 R, float3 N,
101           samplerCUBE mp1,
102           samplerCUBE mp3Color,
103           samplerCUBE mp3Dist,
104           out float4 Il, out float3 Nl)
105{
106 float dl1 = 0, dp1, llp1, ppp1;
107 float3 p1;
108 linearSearch(x, R, N, mp1, p1, dl1, dp1, llp1, ppp1);
109 
110 bool valid1 = dot(p1,p1) != 0;
111               
112 float dl, dp, llp, ppp;
113 float3 p;
114 
115 if(!valid1)
116 {
117    linearSearch(x, R, N, mp3Dist, p, dl, dp, llp, ppp);
118    Il.a = 1;
119    secantSearch(x, R, mp3Dist, dl, dp, llp, ppp, p);
120    Il.rgb =  Nl.rgb = readCubeMap(mp3Color, p).rgb; 
121 }
122 else
123 {
124    secantSearch(x, R, mp1, dl1, dp1, llp1, ppp1, p1);
125    Il.rgb =  Nl.rgb = readCubeMap(mp1, p1).rgb;
126    p = p1;   
127    Il.a = 0;   
128 }
129 
130 return p;
131}*/
132
133float3 Hit( float3 x, float3 R, samplerCUBE mp )
134{
135        float rl = readDistanceCubeMap( mp, R);                         // |r|
136       
137        float ppp = length( x ) /  readDistanceCubeMap( mp, x);                 // |p|/|p’|
138        float dun = 0, pun = ppp, dov = 0, pov = 0;
139        float dl = rl * ( 1 - ppp );                                                    // eq. 2
140        float3 l = x + R * dl;                                                                  // ray equation
141
142        // iteration
143        for( int i = 0; i < SECANT_ITERATIONCOUNT ; i++ )
144        {
145                float llp = length( l ) / readDistanceCubeMap( mp, l);          // |l|/|l’|
146                if ( llp < 0.999f )                                                                     // undershooting
147                {
148                        dun = dl; pun = llp;                                                    // last undershooting
149                        dl += ( dov == 0 ) ? rl * ( 1 - llp ) :                 // eq. 2
150                                ( dl - dov ) * ( 1 - llp ) / ( llp - pov );     // eq. 3
151                } else if ( llp > 1.001f )                                                      // overshooting
152                {
153                        dov = dl; pov = llp;                                                    // last overshooting
154                        dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
155                }
156                l = x + R * dl;                                                                         // ray equation
157        }
158        return l;                                                                                               // computed hit point
159}
160
161
162struct Shaded_OUT
163{
164 float4 vPos : POSITION;
165 float4 wNormal : TEXCOORD0;
166 float4 wPos    : TEXCOORD1;
167};
168
169
170float4 MultipleRefractionPS(Shaded_OUT IN,
171                                                        uniform samplerCUBE CubeMap : register(s0),
172                                                        uniform samplerCUBE DistanceMap : register(s1),
173                                                        uniform samplerCUBE NormDistMap1 : register(s2),
174                                                        uniform float3 cameraPos,
175                                                        uniform float3 lastCenter,
176                                                        uniform float sFresnel,
177                                                        uniform float refIndex ) : COLOR0
178{
179         float4 I = float4(0,0,0,0);
180                       
181         float3 N = normalize(IN.wNormal.xyz);
182         float3 x = IN.wPos.xyz - lastCenter;
183         float3 V  = (IN.wPos.xyz - cameraPos);
184          V = normalize(V);
185         
186         float F = sFresnel + pow(1 - dot(N, -V), 5) * (1 - sFresnel);   
187       
188         float3 l;       
189         float ri = refIndex;
190         float3 R;
191         
192         if(dot(V,N) > 0)
193         {
194                ri = 1.0 / refIndex;
195                N = -N;     
196         }
197         float4 Irefr = 0;
198         R = refract( V, N, ri);
199         if(dot(R,R) != 0)
200         {
201           R = refract( V, N, ri);
202           l = Hit(x, R, DistanceMap);
203           x = l;         
204           N = readCubeMap(NormDistMap1, l).rgb;
205           V = R;
206           
207           if(dot(V,N) > 0)
208           {
209                ri = 1.0 / refIndex;
210                N = -N;     
211           }
212           R = refract( V, N, ri);         
213           if(dot(R,R) != 0)
214           {
215                 l = Hit(x, R, DistanceMap);
216                 Irefr =  readCubeMap(CubeMap, l);
217           }       
218         }
219         
220         N = normalize(IN.wNormal.xyz);
221         V  = normalize(IN.wPos.xyz - cameraPos);
222         x = IN.wPos.xyz - lastCenter;
223         R = reflect( V, N);
224         l = Hit(x, R, DistanceMap);
225         float4 Irefl = readCubeMap(CubeMap, l);
226         
227         I = F * Irefl + (1 - F) * Irefr;   
228       
229        return I;
230}
231
232float4 MultipleRefractionPhotonMap_PS(Shaded_OUT IN,
233                                                                        uniform samplerCUBE DistanceMap : register(s0),
234                                                                        uniform samplerCUBE NormDistMap1 : register(s1),
235                                                                        uniform float3 cameraPos,
236                                                                        uniform float3 lastCenter,
237                                                                        uniform float refIndex) : COLOR0
238{
239        float4 I = float4(0,0,0,0);
240                 
241        float3 N = normalize(IN.wNormal.xyz);
242        float3 x = IN.wPos.xyz - lastCenter;
243        float3 V  = (IN.wPos.xyz - cameraPos);
244        V = normalize(V);
245       
246        float3 l = 0;   
247        float ri = refIndex;
248        float3 R;
249         
250         if(dot(V,N) > 0)
251         {
252                ri = 1.0 / refIndex;
253                N = -N;     
254         }
255         float4 Irefr = 0;
256         R = refract( V, N, ri);
257         if(dot(R,R) != 0)
258         {
259           R = refract( V, N, ri);
260           l = Hit(x, R, DistanceMap);
261           x = l;         
262           N = readCubeMap(NormDistMap1, l).rgb;
263           V = R;
264           
265           if(dot(V,N) > 0)
266           {
267                ri = 1.0 / refIndex;
268                N = -N;     
269           }
270           R = refract( V, N, ri);         
271           if(dot(R,R) != 0)
272           {
273                 l = Hit(x, R, DistanceMap);             
274           }       
275         }
276                 
277        return float4(l,1);
278}
Note: See TracBrowser for help on using the repository browser.