source: GTP/trunk/App/Demos/Illum/Ogre/Media/materials/programs/MetalTeapot.hlsl @ 1859

Revision 1859, 7.2 KB checked in by szirmay, 18 years ago (diff)
Line 
1float3 F0;
2
3float4 readCubeMap(samplerCUBE cm, float3 coord)               
4{
5        float4 color = texCUBElod( cm, float4(coord.xy, - coord.z,0) );
6        return color;
7}
8
9float readDistanceCubeMap(samplerCUBE dcm, float3 coord)               
10{
11        float dist = texCUBElod(dcm, float4(coord.xy, - coord.z,0)).a;
12        return dist;
13}
14
15
16
17#define MAX_LIN_ITERATIONCOUNT 20
18#define MIN_LIN_ITERATIONCOUNT 6
19#define SECANT_ITERATIONCOUNT 1
20
21float Hit(float3 x, float3 R, samplerCUBE mp, out float3 newDir)
22{
23        R = normalize(R);
24       
25        float3 Ra = abs(R);
26        float3 xa = abs(x);
27       
28    float xm =  max(max(xa.x,xa.y),xa.z);
29    float Rm = max(max(Ra.x,Ra.y),Ra.z);
30   
31    float a = xm / Rm;
32   
33 
34    float dt =  length(x / xm - R / Rm) * MAX_LIN_ITERATIONCOUNT;
35    dt = max(dt, MIN_LIN_ITERATIONCOUNT);
36    dt = 1.0 / dt;
37   
38       
39        bool overshoot = false, undershoot = false;
40        float dp, dl = 0, ppp, llp;
41        float lR = readDistanceCubeMap(mp, R);
42        //float3 p = R;
43        float3 p = 0;
44               
45        //linear iteration     
46        float t;
47        float dist = 0;
48        /*
49        t = 0.9999999999999;
50        dp = a * t / (1 - t);
51        p = x + R * dp;
52        dist = readDistanceCubeMap(mp, p);
53        ppp = length(p) / dist;
54        */
55        t = dt;
56               
57        while(t <= 1.0 && !overshoot)
58        {
59                dp = a * t / (1 - t);
60                p = x + R * dp;
61                float dist = readDistanceCubeMap(mp, p);
62                if(dist > 0)
63                {
64                        ppp = length(p) / dist;
65                        if(ppp < 1)
66                        {
67                                dl = dp;
68                                llp = ppp;             
69                                undershoot = true;             
70                        }
71                        else
72                        {
73                                if (undershoot)
74                                   overshoot = true;
75                        }
76                }
77                else
78                        undershoot = false;
79       
80                t += dt;               
81        }
82       
83        //if(t >= 1.0 && undershoot && dist)
84        //      overshoot = true;       
85       
86        if(overshoot)
87        {
88                float dnew;
89                for(int i = 0; i < SECANT_ITERATIONCOUNT; i++ )
90                {
91                        dnew = dl + (dp - dl) * (1 - llp) / (ppp - llp);       
92                           
93                        p = x + R * dnew;
94                        half pppnew = length(p) / readDistanceCubeMap(mp, p);
95                        if(pppnew < 1)
96                        {
97                                llp = pppnew;
98                                dl = dnew;
99                        }
100                        else
101                        {
102                                ppp = pppnew;
103                                dp = dnew;
104                        }
105                }       
106        }
107        else
108                p = float3(0,0,0);
109       
110        newDir = p;
111        return dp;
112}
113                       
114
115float HitOld( float3 x, float3 R, samplerCUBE mp, out float3 newDir )
116{
117        //return R  + 0.00000000001 * x;
118        float rl = readDistanceCubeMap( mp, R);                         // |r|
119       
120        float ppp = length( x ) /  readDistanceCubeMap( mp, x);                 // |p|/|p’|
121        float dun = 0, pun = ppp, dov = 0, pov = 0;
122        float dl = rl * ( 1 - ppp );                                                    // eq. 2
123        float3 l = x + R * dl;                                                                  // ray equation
124
125        // iteration
126        for( int i = 0; i < SECANT_ITERATIONCOUNT; i++ )       
127        {
128                float llp = length( l ) / readDistanceCubeMap( mp, l);          // |l|/|l’|
129                if ( llp < 0.999f )                                                                     // undershooting
130                {
131                        dun = dl; pun = llp;                                                    // last undershooting
132                        dl += ( dov == 0 ) ? rl * ( 1 - llp ) :                 // eq. 2
133                                ( dl - dov ) * ( 1 - llp ) / ( llp - pov );     // eq. 3
134                } else if ( llp > 1.001f )                                                      // overshooting
135                {
136                        dov = dl; pov = llp;                                                    // last overshooting
137                        dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
138                }
139                l = x + R * dl;                                                                         // ray equation
140        }
141        newDir = l;                                                                                             // computed hit point
142       
143        return dl;
144}
145
146
147struct VertOut
148{
149        float3 wPos     : TEXCOORD1;
150        float3 cPos     : TEXCOORD3;
151        float2 texCoord : TEXCOORD0;
152        float3 mNormal  : TEXCOORD2;
153        float4 hPos : POSITION;                 
154};
155
156VertOut DefaultVS(float4 position : POSITION,                                           
157                                float2 texCoord : TEXCOORD0,
158                                float3 normal   : NORMAL,
159                                uniform float4x4 worldViewProj,
160                                uniform float4x4 world,
161                                uniform float4x4 worldview,
162                                uniform float4x4 worldI )
163{
164  VertOut OUT;
165  OUT.hPos = mul(worldViewProj, position);
166  OUT.wPos = mul(world, position).xyz; 
167  OUT.cPos = mul(worldview, position).xyz; 
168  OUT.mNormal = mul(normal, worldI); 
169  //OUT.mNormal = normal;
170  OUT.texCoord = texCoord;
171  return OUT;
172}
173
174//////////////
175//Metal
176//////////////
177float4 Metal1BouncePS(  VertOut IN,
178                uniform float3 cameraPos,
179                uniform samplerCUBE CubeMap : register(s0),
180                uniform samplerCUBE DistanceMap : register(s1),
181                uniform float3 lastCenter):COLOR0
182{
183       
184        float4 Color = float4(1,1,1,1);
185       
186        IN.mNormal = normalize(IN.mNormal);
187        float3 newTexCoord;     
188        float3 mPos = IN.wPos - lastCenter;
189        float3 V  = (IN.wPos - cameraPos);
190        float cameraDistace = length(V);
191        V = normalize(V);
192        float3 R = normalize(reflect( V, IN.mNormal));
193               
194        newTexCoord = R;       
195        HitOld(mPos, R, DistanceMap, newTexCoord);
196        Color = readCubeMap(CubeMap, newTexCoord );     
197       
198        float ctheta_in = dot(IN.mNormal,R);
199        float ctheta_out = dot(IN.mNormal,-V);
200
201        float3 F = 0;
202       
203        // F,P,G számítása
204        if ( ctheta_in > 0 && ctheta_out > 0 )
205        {
206                float3 H = normalize(R - V);    // felezõvektor
207                float cbeta  = dot(H,R);               
208                F = F0 + (1-F0)*pow(1-cbeta,5);
209        }       
210       
211        Color = Color * float4(F,1);
212       
213        return Color;   
214}
215
216
217void MetalMultipleBouncePS(  VertOut IN,
218                uniform float3 cameraPos,
219                uniform samplerCUBE CubeMap : register(s0),
220                uniform samplerCUBE DistanceMap : register(s1),
221                uniform samplerCUBE NormDistMap1 : register(s2),
222                uniform samplerCUBE NormDistMap2 : register(s3),
223                uniform float3 lastCenter,
224                uniform float SingleBounce,
225                out float4 Color :COLOR0)
226{
227        if(SingleBounce == 1)
228         Color = Metal1BouncePS(IN,cameraPos,CubeMap,DistanceMap,lastCenter);
229        else
230        {
231                Color = float4(1,1,1,1);
232               
233                IN.mNormal = normalize(IN.mNormal);
234                float3 newTexCoord;     
235                float3 mPos = IN.wPos - lastCenter;
236                float3 V  = (IN.wPos - cameraPos);
237                float cameraDistace = length(V);
238                V = normalize(V);
239                float3 R = normalize(reflect( V, IN.mNormal));
240                //float3 R = normalize(refract( V, IN.mNormal, 0.98));
241                       
242                newTexCoord = R;
243               
244                //Color = readCubeMap(NormDistMap1, mPos );
245                //return;
246               
247
248                float3 newDir1;
249                float d1 = Hit(mPos, R, NormDistMap1, newDir1);
250                float3 normal1 = readCubeMap(NormDistMap1, newDir1);
251                bool valid1 = /*dot(normal1, R) < 0 &&*/ dot(newDir1,newDir1) != 0;
252                if(valid1)
253                        newDir1 = 0;
254                float3 newDir2;
255                float d2 = Hit(mPos, R, NormDistMap2, newDir2);
256                float3 normal2 = readCubeMap(NormDistMap2, newDir2);
257                bool valid2 = /*dot(normal2, R) < 0 &&*/ dot(newDir2,newDir2) != 0;
258                if(valid2)
259                        newDir2 = 0;
260                       
261                if( !valid1 && !valid2)
262                {
263                        Hit(mPos, R, DistanceMap, newTexCoord);
264                        Color = readCubeMap(CubeMap, newTexCoord );
265                        //Color = 1;                   
266                }
267                else
268                {               
269                        float d;
270                        float3 newN;
271                       
272                        d = d2;
273                        newN = normal2;
274                       
275                        Color = float4(0,0,1,1);
276                        if( !valid2 || (valid1 && d1 < d2)  )
277                        {
278                                d = d1;
279                                newN = normal1;
280                                Color = float4(0,1,0,1);
281                        }
282                       
283                        float3 newV = R;
284                        float3 newX = mPos + R * d;
285                        float3 newR = normalize(reflect( newV, newN));
286                //      float3 newR = normalize(refract( newV, newN, 0.98));
287               
288                        Hit(newX, newR, DistanceMap, newTexCoord);     
289                        Color = readCubeMap(CubeMap, newTexCoord );
290                        //Color = float4(newR,1);
291                        //Color = float4(1,0,0,1);     
292                }
293               
294/*
295                return;
296               
297                float ctheta_in = dot(IN.mNormal,R);
298                float ctheta_out = dot(IN.mNormal,-V);
299
300                float3 F = 0;
301               
302                // F,P,G számítása
303                if ( ctheta_in > 0 && ctheta_out > 0 )
304                {
305                        float3 H = normalize(R - V);    // felezõvektor
306                        float cbeta  = dot(H,R);               
307                        F = F0 + (1-F0)*pow(1-cbeta,5);
308                }       
309               
310                Color = Color * float4(F,1);    */
311        }
312}
313
314float4 NormalDistancePS( VertOut IN):COLOR
315{
316 
317 float4 Color = float4(0, 0, 0, 0);
318 //return Color;
319 Color = float4(normalize(IN.mNormal), length(IN.cPos));
320 return Color;
321}
Note: See TracBrowser for help on using the repository browser.