source: GTP/branches/IllumWPdeliver2008dec/IlluminationWP/precompiled/app/OgreIllumModule_Resources/materials/GTPEnvMap/GTPEnvMap.hlsl @ 3255

Revision 3255, 4.9 KB checked in by szirmay, 15 years ago (diff)
Line 
1float4 readCubeMap(samplerCUBE cm, float3 coord)               
2{
3        float4 color = texCUBE( cm, float3(coord.xy, - coord.z) );
4        color.a = 1;
5        return color;
6}
7
8float readDistanceCubeMap(samplerCUBE dcm, float3 coord)               
9{
10        float dist = texCUBE(dcm, float3(coord.xy, - coord.z)).a;
11        if(dist == 0) dist = 1000; ///sky
12        return dist;
13}
14
15#define SECANT_ITERATIONCOUNT 20
16
17float3 Hit( float3 x, float3 R, samplerCUBE mp )
18{
19        float rl = readDistanceCubeMap( mp, R);                         // |r|
20       
21        float ppp = length( x ) /  readDistanceCubeMap( mp, x);                 // |p|/|p’|
22        float dun = 0, pun = ppp, dov = 0, pov = 0;
23        float dl = rl * ( 1 - ppp );                                                    // eq. 2
24        float3 l = x + R * dl;                                                                  // ray equation
25
26        // iteration
27        for( int i = 0; i < SECANT_ITERATIONCOUNT ; i++ )
28        {
29                float llp = length( l ) / readDistanceCubeMap( mp, l);          // |l|/|l’|
30                if ( llp < 0.999f )                                                                     // undershooting
31                {
32                        dun = dl; pun = llp;                                                    // last undershooting
33                        dl += ( dov == 0 ) ? rl * ( 1 - llp ) :                 // eq. 2
34                                ( dl - dov ) * ( 1 - llp ) / ( llp - pov );     // eq. 3
35                } else if ( llp > 1.001f )                                                      // overshooting
36                {
37                        dov = dl; pov = llp;                                                    // last overshooting
38                        dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
39                }
40                l = x + R * dl;                                                                         // ray equation
41        }
42        return l;                                                                                               // computed hit point
43}
44
45
46struct Shaded_OUT
47{
48 float4 vPos : POSITION;
49 float4 wNormal : TEXCOORD0;
50 float4 wPos    : TEXCOORD1;
51};
52
53
54float4 EnvMap_Default_PS(Shaded_OUT IN,
55                                                uniform samplerCUBE CubeMap : register(s0),
56                                                uniform float3 cameraPos) : COLOR0
57{
58        float3 N = normalize(IN.wNormal.xyz);
59        float3 V = normalize(IN.wPos.xyz - cameraPos);
60        float3 R = reflect( V, N);     
61       
62        return readCubeMap(CubeMap, R );       
63}
64
65float4 EnvMap_Refract_Default_PS
66                                        (Shaded_OUT IN,
67                                        uniform samplerCUBE CubeMap : register(s0),
68                                        uniform float3 cameraPos,
69                                        uniform float sRefraction,
70                                        uniform float transparency ) : COLOR0
71{
72        float3 N = normalize(IN.wNormal.xyz);
73        float3 V = normalize(IN.wPos.xyz - cameraPos);
74        float3 R = reflect( V, N);     
75        float3 R2 = refract( V, N, sRefraction);       
76       
77        return 0.5;//(1.0 - transparency) * readCubeMap(CubeMap, R ) + transparency * readCubeMap(CubeMap, R2);
78}
79
80float4  EnvMap_Localized_Reflection_PS( Shaded_OUT IN,
81                                                                                uniform samplerCUBE CubeMap : register(s0),
82                                                                                uniform samplerCUBE DistanceMap : register(s1),
83                                                                                uniform float3 cameraPos,
84                                                                                uniform float3 lastCenter,             
85                                                                                uniform float sFresnel,
86                                                                                uniform float sRefraction ) :COLOR0
87{
88        float3 N = normalize(IN.wNormal.xyz);
89        float3 RR;     
90        float3 V = normalize(IN.wPos.xyz - cameraPos);
91        float3 cubePos = IN.wPos.xyz - lastCenter;
92        float3 R = reflect( V, N);     
93               
94        RR = R;
95        RR = Hit(cubePos, R, DistanceMap);
96       
97        return readCubeMap(CubeMap, RR );       
98}
99
100float4  EnvMap_Localized_Refraction_PS( Shaded_OUT IN,
101                                                                                uniform samplerCUBE CubeMap : register(s0),
102                                                                                uniform samplerCUBE DistanceMap : register(s1),
103                                                                                uniform float3 cameraPos,
104                                                                                uniform float3 lastCenter,             
105                                                                                uniform float4 sFresnel,
106                                                                                uniform float sRefraction ) :COLOR0
107{
108        float4 Color = 0;
109        float3 N = normalize(IN.wNormal.xyz);
110        float3 RR, TT; 
111        float3 V = normalize(IN.wPos.xyz - cameraPos);
112        float3 cubePos = IN.wPos.xyz - lastCenter;
113        float3 R = reflect( V, N);     
114        float3 T = refract(V, N, sRefraction);
115
116               
117        RR = R; TT = T;
118        RR = Hit(cubePos, R, DistanceMap);
119        float4 reflectcolor = readCubeMap(CubeMap, RR );               
120       
121        if(dot(T,T)!=0)
122        {
123                TT = Hit(cubePos, T, DistanceMap);
124                float4 refractcolor = readCubeMap(CubeMap, TT );               
125       
126                float cos_theta = -dot(V, N);
127                float F = (sFresnel + pow(1 - cos_theta, 5.0f) * (1 - sFresnel));
128                F = saturate(F);
129                Color =  (F * reflectcolor + (1 - F) * refractcolor);
130        }
131        else
132        {
133                Color = reflectcolor;
134        }
135  return Color;
136}
137
138
139float4 EnvMap_LocalizedMetal_PS(  Shaded_OUT IN,
140                                                                        uniform samplerCUBE CubeMap : register(s0),
141                                                                        uniform samplerCUBE DistanceMap : register(s1),
142                                                                        uniform float3 cameraPos,
143                                                                        uniform float3 lastCenter,
144                                                                        uniform float3 F0 ):COLOR0
145{       
146        float4 Color;
147        float3 N = normalize(IN.wNormal.xyz);
148        float3 RR;     
149        float3 V = normalize(IN.wPos.xyz - cameraPos);
150        float3 cubePos = IN.wPos.xyz - lastCenter;
151        float3 R = reflect( V, N);
152               
153        RR = R;
154        RR = Hit(cubePos, R, DistanceMap);
155        Color = readCubeMap(CubeMap, RR);
156        //Color = readDistanceCubeMap(DistanceMap, cubePos)/300.0;
157        //return Color;
158       
159        float ctheta_in = dot(N, R);
160        float ctheta_out = dot(N, -V);
161
162        float3 F = 0;
163       
164        // compute F,P,G
165        //if ( ctheta_in > 0 && ctheta_out > 0 )
166        {
167                float3 H = normalize(R - V);    // felezõvektor
168                float cbeta  = dot(H,R);               
169                //F = ( (n-1)*(n-1) + pow(1-cbeta,5) * 4*n + k*k) / ( (n+1)*(n+1) + k*k );
170                //float3 F0 = ((n-1)*(n-1) + k*k) / ( (n+1)*(n+1) + k*k );
171                //float3 F1 = float3(1.0f,1.0f,1.0f) - F0;
172                F = F0 + (1 - F0) * pow(1 - cbeta, 5);
173        }
174               
175        return Color * float4(F,1);     
176}
Note: See TracBrowser for help on using the repository browser.