source: GTP/branches/IllumWPdeliver2008dec/IlluminationWP/demos/OgreGames/CarGame/Media/materials/programs/CarReflections.hlsl @ 3255

Revision 3255, 4.7 KB checked in by szirmay, 15 years ago (diff)
Line 
1float3 F0;
2
3float4 readCubeMap(samplerCUBE cm, float3 coord)               
4{
5        float4 color = texCUBE( cm, float3(coord.xy, - coord.z) );
6        return color;
7}
8
9float readDistanceCubeMap(samplerCUBE dcm, float3 coord)               
10{
11        float dist = texCUBE(dcm, float3(coord.xy, - coord.z)).r;
12        if(dist == 0) dist = 1000000; ///sky
13        return dist;
14}
15
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 < 2; i++ )    // 2 !!!!!!!!!!!!!!!!!!!!!!!
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
45void LocalizedVS(float4 position : POSITION,
46                out float3 wPos : TEXCOORD1,                           
47                float2 texCoord : TEXCOORD0,
48                out float2 otexCoord : TEXCOORD0,
49                float3 normal   : NORMAL,
50                out float3 mNormal  : TEXCOORD2,
51                out float4 hPos : POSITION,             
52                uniform float4x4 worldViewProj,
53                uniform float4x4 world,
54                uniform float4x4 worldIT)
55{
56 
57  hPos = mul(worldViewProj, position);
58  wPos = mul(world, position).xyz; 
59  mNormal = mul(normal, worldIT);
60  //mNormal *=-1;
61 //mNormal = normal; 
62  otexCoord = texCoord;
63}
64
65
66void LocalizedMetalPS(  float2 texCoord : TEXCOORD0,
67                float3 wPos     : TEXCOORD1,   
68                float3 mNormal  : TEXCOORD2,
69                uniform float3 cameraPos,
70                uniform samplerCUBE CubeMap : register(s0),
71                uniform samplerCUBE DistanceMap : register(s1),
72                uniform float3 lastCenter,
73                uniform float3 lightPosition,
74                uniform float reflectivity,
75                uniform float3 phongColor,                             
76                out float4 Color :COLOR0)
77{       
78        Color = float4(1,1,1,1);
79
80        //metallic reflection   
81        mNormal = normalize(mNormal);
82       
83        float3 newTexCoord;     
84        float3 mPos = wPos - lastCenter;
85        float3 V = normalize(wPos - cameraPos);
86        float3 R = normalize(reflect( V, mNormal));
87               
88        newTexCoord = R;       
89       
90        newTexCoord = Hit(mPos, R, DistanceMap);
91        Color = readCubeMap(CubeMap, newTexCoord );     
92       
93        //Color =  0.000000001 *Color +  float4(mNormal, 1);
94       
95        float ctheta_in = dot(mNormal,R);
96        float ctheta_out = dot(mNormal,-V);
97
98        float3 F = 0;
99       
100        if ( ctheta_in > 0 && ctheta_out > 0 )
101        {
102                float3 H1 = normalize(R - V);   // felezõvektor
103                float cbeta  = dot(H1,R);               
104                F = F0 + (1-F0)*pow(1-cbeta,5);
105        }               
106       
107        Color = Color * float4(F,1);
108       
109       
110//phong
111       
112        half3 light = lightPosition; //(lightPosition -wPos) ;// / 200.0;
113        V = -V;
114        half3 L = normalize(light);
115        half3 H = normalize(L+V);
116        half4 lighting = lit(dot(mNormal, L),dot(mNormal, H), 200);
117       
118//addition
119        Color = Color * reflectivity + (1 - reflectivity) * (
120                                                                                (lighting.x * float4(phongColor * 0.2,1) +
121                                                                                 lighting.y * float4(phongColor,1) +
122                                                                                 lighting.z * float4(4,4,4,4) )) ;
123}
124
125void MirrorPS(  float2 texCoord : TEXCOORD0,
126                float3 wPos     : TEXCOORD1,   
127                float3 mNormal  : TEXCOORD2,
128                uniform float3 cameraPos,
129                uniform samplerCUBE CubeMap : register(s0),
130                uniform samplerCUBE DistanceMap : register(s1),
131                uniform float3 lastCenter,
132                out float4 Color :COLOR0)
133{
134       
135        Color = float4(1,1,1,1);
136
137        //metallic reflection   
138        mNormal = normalize(mNormal);
139       
140        float3 newTexCoord;     
141        float3 mPos = wPos - lastCenter;
142        float3 V = normalize(wPos - cameraPos);
143        float3 R = normalize(reflect( V, mNormal));
144               
145        newTexCoord = R;       
146       
147        newTexCoord = Hit(mPos, R, DistanceMap);
148        Color = readCubeMap(CubeMap, newTexCoord );             
149       
150}
151
152void LampGlassPS(  float2 texCoord : TEXCOORD0,
153                float3 wPos     : TEXCOORD1,   
154                float3 mNormal  : TEXCOORD2,
155                uniform float3 cameraPos,
156                uniform samplerCUBE CubeMap : register(s0),
157                uniform samplerCUBE DistanceMap : register(s1),
158                uniform float3 lastCenter,
159                uniform float4 glasColor,
160                out float4 Color :COLOR0)
161{
162       
163        Color = float4(1,1,1,1);
164
165        //metallic reflection   
166        mNormal = normalize(mNormal);
167       
168        float3 newTexCoord;     
169        float3 mPos = wPos - lastCenter;
170        float3 V = normalize(wPos - cameraPos);
171        float3 R = normalize(reflect( V, mNormal));
172               
173        newTexCoord = R;       
174       
175        newTexCoord = Hit(mPos, R, DistanceMap);
176        Color = readCubeMap(CubeMap, newTexCoord );
177        Color.a = 1;
178        Color *= glasColor;             
179       
180}
Note: See TracBrowser for help on using the repository browser.