source: GTP/branches/IllumWPdeliver2008dec/IlluminationWP/demos/OgreGames/SpaceStation/Media/GTPEnvMap/GTPEnvMap.hlsl @ 3255

Revision 3255, 4.4 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_Localized_Reflection_PS( Shaded_OUT IN,
66                                                                                uniform samplerCUBE CubeMap : register(s0),
67                                                                                uniform samplerCUBE DistanceMap : register(s1),
68                                                                                uniform float3 cameraPos,
69                                                                                uniform float3 lastCenter,             
70                                                                                uniform float sFresnel,
71                                                                                uniform float sRefraction ) :COLOR0
72{
73        float3 N = normalize(IN.wNormal.xyz);
74        float3 RR;     
75        float3 V = normalize(IN.wPos.xyz - cameraPos);
76        float3 cubePos = IN.wPos.xyz - lastCenter;
77        float3 R = reflect( V, N);     
78               
79        RR = R;
80        RR = Hit(cubePos, R, DistanceMap);
81       
82        return readCubeMap(CubeMap, RR );       
83}
84
85float4  EnvMap_Localized_Refraction_PS( Shaded_OUT IN,
86                                                                                uniform samplerCUBE CubeMap : register(s0),
87                                                                                uniform samplerCUBE DistanceMap : register(s1),
88                                                                                uniform float3 cameraPos,
89                                                                                uniform float3 lastCenter,             
90                                                                                uniform float sFresnel,
91                                                                                uniform float sRefraction ) :COLOR0
92{
93        float4 Color = 0;
94        float3 N = normalize(IN.wNormal.xyz);
95        float3 RR, TT; 
96        float3 V = normalize(IN.wPos.xyz - cameraPos);
97        float3 cubePos = IN.wPos.xyz - lastCenter;
98        float3 R = reflect( V, N);     
99        float3 T = refract(V, N, sRefraction);
100
101               
102        RR = R; TT = T;
103        RR = Hit(cubePos, R, DistanceMap);
104        float4 reflectcolor = readCubeMap(CubeMap, RR );               
105       
106        if(dot(T,T)!=0)
107        {
108                TT = Hit(cubePos, T, DistanceMap);
109                float4 refractcolor = readCubeMap(CubeMap, TT );               
110       
111                float cos_theta = -dot(V, N);
112                float F = (sFresnel + pow(1 - cos_theta, 5.0f) * (1 - sFresnel));
113                F = saturate(F);
114                Color =  (F * reflectcolor + (1 - F) * refractcolor);
115        }
116        else
117        {
118                Color = reflectcolor;
119        }
120  return Color;
121}
122
123
124float4 EnvMap_LocalizedMetal_PS(  Shaded_OUT IN,
125                                                                        uniform samplerCUBE CubeMap : register(s0),
126                                                                        uniform samplerCUBE DistanceMap : register(s1),
127                                                                        uniform float3 cameraPos,
128                                                                        uniform float3 lastCenter,
129                                                                        uniform float3 F0 ):COLOR0
130{       
131        float4 Color;
132        float3 N = normalize(IN.wNormal.xyz);
133        float3 RR;     
134        float3 V = normalize(IN.wPos.xyz - cameraPos);
135        float3 cubePos = IN.wPos.xyz - lastCenter;
136        float3 R = reflect( V, N);
137               
138        RR = R;
139        RR = Hit(cubePos, R, DistanceMap);
140        Color = readCubeMap(CubeMap, RR);
141        //Color = readDistanceCubeMap(DistanceMap, cubePos)/300.0;
142        //return Color;
143       
144        float ctheta_in = dot(N, R);
145        float ctheta_out = dot(N, -V);
146
147        float3 F = 0;
148       
149        // compute F,P,G
150        //if ( ctheta_in > 0 && ctheta_out > 0 )
151        {
152                float3 H = normalize(R - V);    // felezõvektor
153                float cbeta  = dot(H,R);               
154                //F = ( (n-1)*(n-1) + pow(1-cbeta,5) * 4*n + k*k) / ( (n+1)*(n+1) + k*k );
155                //float3 F0 = ((n-1)*(n-1) + k*k) / ( (n+1)*(n+1) + k*k );
156                //float3 F1 = float3(1.0f,1.0f,1.0f) - F0;
157                F = F0 + (1 - F0) * pow(1 - cbeta, 5);
158        }
159               
160        return Color * float4(F,1);     
161}
Note: See TracBrowser for help on using the repository browser.