source: GTP/trunk/App/Games/CarDriving_BME/Media/materials/programs/CarReflections.hlsl @ 2514

Revision 2514, 4.8 KB checked in by szirmay, 17 years ago (diff)
Line 
1#define EFFECTS
2#define LOCALIZATION
3
4float3 F0;
5
6float4 readCubeMap(samplerCUBE cm, float3 coord)               
7{
8        float4 color = texCUBE( cm, float3(coord.xy, - coord.z) );
9        return color;
10}
11
12float readDistanceCubeMap(samplerCUBE dcm, float3 coord)               
13{
14        float dist = texCUBE(dcm, float3(coord.xy, - coord.z)).r;
15        if(dist == 0) dist = 1000000; ///sky
16        return dist;
17}
18
19
20float3 Hit( float3 x, float3 R, samplerCUBE mp )
21{
22        float rl = readDistanceCubeMap( mp, R);                         // |r|
23       
24        float ppp = length( x ) /  readDistanceCubeMap( mp, x);                 // |p|/|p’|
25        float dun = 0, pun = ppp, dov = 0, pov = 0;
26        float dl = rl * ( 1 - ppp );                                                    // eq. 2
27        float3 l = x + R * dl;                                                                  // ray equation
28
29        // iteration
30        for( int i = 0; i < 2; i++ )    // 2 !!!!!!!!!!!!!!!!!!!!!!!
31        {
32                float llp = length( l ) / readDistanceCubeMap( mp, l);          // |l|/|l’|
33                if ( llp < 0.999f )                                                                     // undershooting
34                {
35                        dun = dl; pun = llp;                                                    // last undershooting
36                        dl += ( dov == 0 ) ? rl * ( 1 - llp ) :                 // eq. 2
37                                ( dl - dov ) * ( 1 - llp ) / ( llp - pov );     // eq. 3
38                } else if ( llp > 1.001f )                                                      // overshooting
39                {
40                        dov = dl; pov = llp;                                                    // last overshooting
41                        dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
42                }
43                l = x + R * dl;                                                                         // ray equation
44        }
45        return l;                                                                                               // computed hit point
46}
47
48void LocalizedVS(float4 position : POSITION,
49                out float3 wPos : TEXCOORD1,                           
50                float2 texCoord : TEXCOORD0,
51                out float2 otexCoord : TEXCOORD0,
52                float3 normal   : NORMAL,
53                out float3 mNormal  : TEXCOORD2,
54                out float4 hPos : POSITION,             
55                uniform float4x4 worldViewProj,
56                uniform float4x4 world,
57                uniform float4x4 worldI)
58{
59 
60  hPos = mul(worldViewProj, position);
61  wPos = mul(world, position).xyz; 
62  mNormal = mul(normal, worldI);
63  //mNormal *=-1;
64 //mNormal = normal; 
65  otexCoord = texCoord;
66}
67
68
69void LocalizedMetalPS(  float2 texCoord : TEXCOORD0,
70                float3 wPos     : TEXCOORD1,   
71                float3 mNormal  : TEXCOORD2,
72                uniform float3 cameraPos,
73                uniform samplerCUBE CubeMap : register(s0),
74                uniform samplerCUBE DistanceMap : register(s1),
75                uniform float3 lastCenter,
76                uniform float3 lightPosition,
77                uniform float reflectivity,
78                uniform float3 phongColor,                             
79                out float4 Color :COLOR0)
80{       
81        Color = float4(1,1,1,1);
82
83        //metallic reflection   
84        mNormal = normalize(mNormal);
85       
86        float3 newTexCoord;     
87        float3 mPos = wPos - lastCenter;
88        float3 V = normalize(wPos - cameraPos);
89
90#ifdef EFFECTS
91        float3 R = normalize(reflect( V, mNormal));
92               
93        newTexCoord = R;       
94#ifdef LOCALIZATION     
95        newTexCoord = Hit(mPos, R, DistanceMap);
96#endif
97        Color = readCubeMap(CubeMap, newTexCoord );     
98       
99        //Color =  0.000000001 *Color +  float4(mNormal, 1);
100       
101        float ctheta_in = dot(mNormal,R);
102        float ctheta_out = dot(mNormal,-V);
103
104        float3 F = 0;
105       
106        if ( ctheta_in > 0 && ctheta_out > 0 )
107        {
108                float3 H1 = normalize(R - V);   // felezõvektor
109                float cbeta  = dot(H1,R);               
110                F = F0 + (1-F0)*pow(1-cbeta,5);
111        }               
112       
113        Color = Color * float4(F,1);
114       
115#endif 
116//phong
117       
118        half3 light = lightPosition; //(lightPosition -wPos) ;// / 200.0;
119        V = -V;
120        half3 L = normalize(light);
121        half3 H = normalize(L+V);
122        half4 lighting = lit(dot(mNormal, L),dot(mNormal, H), 200);
123       
124//addition
125        Color = Color * reflectivity + (1 - reflectivity) * (
126                                                                                (lighting.x * float4(phongColor * 0.2,1) +
127                                                                                 lighting.y * float4(phongColor,1) +
128                                                                                 lighting.z * float4(4,4,4,4) )) ;
129}
130
131void MirrorPS(  float2 texCoord : TEXCOORD0,
132                float3 wPos     : TEXCOORD1,   
133                float3 mNormal  : TEXCOORD2,
134                uniform float3 cameraPos,
135                uniform samplerCUBE CubeMap : register(s0),
136                uniform samplerCUBE DistanceMap : register(s1),
137                uniform float3 lastCenter,
138                out float4 Color :COLOR0)
139{
140       
141        Color = float4(1,1,1,1);
142
143#ifdef EFFECTS
144        //metallic reflection   
145        mNormal = normalize(mNormal);
146       
147        float3 newTexCoord;     
148        float3 mPos = wPos - lastCenter;
149        float3 V = normalize(wPos - cameraPos);
150        float3 R = normalize(reflect( V, mNormal));
151               
152        newTexCoord = R;       
153#ifdef LOCALIZATION     
154        newTexCoord = Hit(mPos, R, DistanceMap);
155#endif
156        Color = readCubeMap(CubeMap, newTexCoord );             
157#endif
158       
159}
160
161void LampGlassPS(  float2 texCoord : TEXCOORD0,
162                float3 wPos     : TEXCOORD1,   
163                float3 mNormal  : TEXCOORD2,
164                uniform float3 cameraPos,
165                uniform samplerCUBE CubeMap : register(s0),
166                uniform samplerCUBE DistanceMap : register(s1),
167                uniform float3 lastCenter,
168                uniform float4 glasColor,
169                out float4 Color :COLOR0)
170{
171       
172        Color = float4(1,1,1,1);
173
174#ifdef EFFECTS
175        //metallic reflection   
176        mNormal = normalize(mNormal);
177       
178        float3 newTexCoord;     
179        float3 mPos = wPos - lastCenter;
180        float3 V = normalize(wPos - cameraPos);
181        float3 R = normalize(reflect( V, mNormal));
182               
183        newTexCoord = R;       
184#ifdef LOCALIZATION
185        newTexCoord = Hit(mPos, R, DistanceMap);
186#endif
187        Color = readCubeMap(CubeMap, newTexCoord );
188        Color.a = 1;
189#endif
190        Color *= glasColor;             
191       
192}
Note: See TracBrowser for help on using the repository browser.