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

Revision 2514, 3.8 KB checked in by szirmay, 17 years ago (diff)
Line 
1#define EFFECT
2#define LOCALIZATION
3
4float4 readCubeMap(samplerCUBE cm, float3 coord)               
5{
6        float4 color = texCUBE( cm, float3(coord.xy, - coord.z) );
7        return color;
8}
9
10float readDistanceCubeMap(samplerCUBE dcm, float3 coord)               
11{
12        float dist = texCUBE(dcm, float3(coord.xy, - coord.z)).r;
13        if(dist == 0) dist = 40; ///sky
14        return dist;
15}
16
17// This function is called several times.
18float3 Hit( float3 x, float3 R, samplerCUBE mp )
19{
20        float rl = readDistanceCubeMap( mp, R);                         // |r|
21       
22        float ppp = length( x ) /  readDistanceCubeMap( mp, x);                 // |p|/|p’|
23        float dun = 0, pun = ppp, dov = 0, pov = 0;
24        float dl = rl * ( 1 - ppp );                                                    // eq. 2
25        float3 l = x + R * dl;                                                                  // ray equation
26
27        // iteration
28        for( int i = 0; i < 2; i++ )    // 2 !!!!!!!!!!!!!!!!!!!!!!!
29        {
30                float llp = length( l ) / readDistanceCubeMap( mp, l);          // |l|/|l’|
31                if ( llp < 0.999f )                                                                     // undershooting
32                {
33                        dun = dl; pun = llp;                                                    // last undershooting
34                        dl += ( dov == 0 ) ? rl * ( 1 - llp ) :                 // eq. 2
35                                ( dl - dov ) * ( 1 - llp ) / ( llp - pov );     // eq. 3
36                } else if ( llp > 1.001f )                                                      // overshooting
37                {
38                        dov = dl; pov = llp;                                                    // last overshooting
39                        dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
40                }
41                l = x + R * dl;                                                                         // ray equation
42        }
43        return l;                                                                                               // computed hit point
44}
45
46void BottleVS(float4 position : POSITION,
47                out float3 wPos : TEXCOORD1,                           
48                float2 texCoord : TEXCOORD0,
49                out float2 otexCoord : TEXCOORD0,
50                float3 normal   : NORMAL,
51                out float3 mNormal  : TEXCOORD2,
52                out float4 hPos : POSITION,             
53                uniform float4x4 worldViewProj,
54                uniform float4x4 world,
55                uniform float4x4 worldI)
56{
57 
58  hPos = mul(worldViewProj, position);
59  wPos = mul(world, position).xyz; 
60  mNormal = mul(normal, worldI);
61 // mNormal = normal;
62  otexCoord = texCoord;
63}
64
65//////////////
66//Localized reflection
67//////////////
68void BottlePS(  float2 texCoord : TEXCOORD0,
69                float3 wPos     : TEXCOORD1,   
70                float3 mNormal  : TEXCOORD2,
71                uniform float3 cameraPos,
72                uniform samplerCUBE CubeMap : register(s0),
73                uniform samplerCUBE DistanceMap : register(s1),
74                uniform float3 lastCenter,
75                uniform float3 lightPosition,
76                uniform float3 color,           
77                out float4 Color :COLOR0)
78{
79       
80        Color = float4(1,1,1,1);
81       
82        mNormal = normalize(mNormal);
83        float3 RR, TT; 
84        float3 mPos = wPos - lastCenter;
85        float3 V = normalize(wPos - cameraPos);
86
87#ifdef EFFECT
88                float3 R = normalize(reflect( V, mNormal));
89       
90        float3 T =  normalize(refract(V, mNormal, 0.9));
91               
92        RR = R; TT = T;
93       
94        #ifdef LOCALIZATION
95        RR = Hit(mPos, R, DistanceMap);
96        TT = Hit(mPos, T, DistanceMap);
97        #endif
98       
99        float4 reflectcolor = readCubeMap(CubeMap, RR );               
100        float4 refractcolor = readCubeMap(CubeMap, TT );               
101       
102        float cos_theta = -dot(V, mNormal);
103        float sFresnel = 0.2;
104        float F = (sFresnel + pow(1-cos_theta, 5.0f) * (1-sFresnel));
105
106        Color = (F * reflectcolor + (1-F) * refractcolor * float4(color,1)) ;
107#else
108        Color = float4(color, 1);
109#endif
110        half3 light = lightPosition;
111        V = -V;
112        half3 L = normalize(light);
113        half3 H = normalize(L+V);
114        half4 lighting = lit(dot(mNormal, L),dot(mNormal, H), 100);
115       
116        Color = (0.7 + lighting.y) * Color + lighting.z;
117}
118
119
120float4 BottlePhotonMapPS(  float2 texCoord : TEXCOORD0,
121                float3 wPos     : TEXCOORD1,   
122                float3 mNormal  : TEXCOORD2,
123                uniform float3 cameraPos,
124                uniform samplerCUBE DistanceMap : register(s0),
125                uniform float3 lastCenter):COLOR0
126{
127       
128        float4 Color = float4(1,1,1,1);
129       
130        mNormal = normalize(mNormal);
131        float3 newTexCoord;     
132        float3 mPos = wPos - lastCenter;
133        float3 V = normalize(wPos - cameraPos);
134        float3 R = normalize(refract( V, mNormal, 0.65 ));
135               
136        newTexCoord = R;       
137       
138        newTexCoord = Hit(mPos, R, DistanceMap);
139               
140        Color = float4(normalize(newTexCoord),1);
141       
142        //if(dot(V,mNormal)>0)
143        //{
144        //      Color = float4(1,0,0,0);               
145        //}             
146        return Color;
147}
Note: See TracBrowser for help on using the repository browser.