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

Revision 3255, 10.0 KB checked in by szirmay, 15 years ago (diff)
Line 
1//metal:
2float3 n, k;    // R 700 nm, G 550 nm, B 435 nm
3float3 F0;
4/*
5       
6        // copper       
7                n = float3(0.21f, 0.96f, 1.17f);       
8                k = float3(4.16f, 2.57f, 2.32f); }
9        // gold
10                n = float3(0.16f, 0.35f, 1.6f);         
11                k = float3(3.98f, 2.71f, 1.92f); }
12        // silver
13                n = float3(0.142f, 0.124f, 0.158f);     
14                k = float3(4.52f,  3.33f, 2.32f); }
15        // alu
16                n = float3(1.83f, 0.96f, 0.577f);
17                k = float3(8.31f,  6.69f, 5.288f); }
18
19*/     
20
21// material constants
22const float3 ambientMaterial= float3(0.3, 0.3, 0.3);
23const float3 diffuseMaterial = float3(1.0, 1.0, 1.0);   
24const float3 specularMaterial = float3(1.0, 1.0, 1.0);
25
26//------------------------------------------------------------------------------------
27void DISCARD_BY_TEX(float2 Tex)
28{
29        if (Tex.x > 1 || Tex.x < 0 || Tex.y > 1 || Tex.y < 0)
30                discard;
31}
32
33//------------------------------------------
34// Attenuation with the square of the distance
35//------------------------------------------
36float SQR(float x) { return x*x; }
37float Attenuation(float3 Light)
38{
39        return 1.0 / dot(Light, Light);
40}
41
42//-----------------------------------------------------------------------------
43// Illumination function used in shading: all input vectors are normalized
44//-----------------------------------------------------------------------------
45/*
46float4 Illumination(float3 Light, float3 Normal, float3 View, float2 TexCoord, float Attenuation)
47{
48        // Blinn lighting
49        float3 Half = normalize(Light + View);
50        float Diffuse = dot(Normal, Light);
51        float Specular = dot(Normal, Half);
52        float4 Lighting = lit(Diffuse, Specular, 16);
53        float4 Color= tex2D(ColorMapSampler, TexCoord);
54
55        return float4(
56                Lighting.x * ambientMaterial * Color +
57                (Lighting.y * diffuseMaterial * Color +
58                Lighting.z * specularMaterial) * Attenuation, 1);
59}*/
60
61//-----------------------------------------------------------------------------
62// Computes transformation matrix from modeling to tangent space
63//-----------------------------------------------------------------------------
64float3x3 TransfModelToTangent( in  float3 Tangent, in float3 Binormal, in float3 Normal ) {
65        float T2 = dot(Tangent, Tangent);
66        float B2 = dot(Binormal, Binormal);
67        float N2 = dot(Normal, Normal);
68        float BT = dot(Binormal, Tangent);
69        float det = B2 * T2 - BT * BT;
70
71        return float3x3( (B2 * Tangent - BT * Binormal)/det,
72                         (T2 * Binormal - BT * Tangent)/det,
73                         Normal/N2 );
74/*                                                     
75        // simplified solution
76        return float3x3(Tangent/T2, Binormal/B2, Normal/N2);
77*/
78}
79
80float4 readCubeMap(samplerCUBE cm, float3 coord)               
81{
82        float4 color = texCUBE( cm, float3(coord.xy, - coord.z) );
83        return color;
84}
85
86float readDistanceCubeMap(samplerCUBE dcm, float3 coord)               
87{
88        float dist = texCUBE(dcm, float3(coord.xy, - coord.z)).r;
89        if(dist == 0) dist = 10000; ///sky
90        return dist;
91}
92
93// This function is called several times.
94float3 Hit( float3 x, float3 R, samplerCUBE mp )
95{
96        float rl = readDistanceCubeMap( mp, R);                         // |r|
97       
98        float ppp = length( x ) /  readDistanceCubeMap( mp, x);                 // |p|/|p’|
99        float dun = 0, pun = ppp, dov = 0, pov = 0;
100        float dl = rl * ( 1 - ppp );                                                    // eq. 2
101        float3 l = x + R * dl;                                                                  // ray equation
102
103        // iteration
104        for( int i = 0; i < 6; i++ )    // 2 !!!!!!!!!!!!!!!!!!!!!!!
105        {
106                float llp = length( l ) / readDistanceCubeMap( mp, l);          // |l|/|l’|
107                if ( llp < 0.999f )                                                                     // undershooting
108                {
109                        dun = dl; pun = llp;                                                    // last undershooting
110                        dl += ( dov == 0 ) ? rl * ( 1 - llp ) :                 // eq. 2
111                                ( dl - dov ) * ( 1 - llp ) / ( llp - pov );     // eq. 3
112                } else if ( llp > 1.001f )                                                      // overshooting
113                {
114                        dov = dl; pov = llp;                                                    // last overshooting
115                        dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
116                }
117                l = x + R * dl;                                                                         // ray equation
118        }
119        return l;                                                                                               // computed hit point
120}
121
122float4 metal_reflectivity(float3 L, float3 N, float3 V)
123{
124       
125       
126        float ctheta_in = dot(N,L);
127        float ctheta_out = dot(N,V);
128
129        float4 intens = 0;
130       
131        // F,P,G számítása
132        if ( ctheta_in > 0 && ctheta_out > 0 )
133        {
134                float3 H = normalize(L + V);    // felezõvektor
135                float calpha = dot(N,H);
136                float cbeta  = dot(H,L);
137               
138                float3 F = ( (n-1)*(n-1) + pow(1-cbeta,5) * 4*n + k*k) / ( (n+1)*(n+1) + k*k );
139
140                float m = 0.8;
141                float m2 = m*m;
142                float PI = 3.14159f;
143
144                float calpha2 = calpha*calpha;
145                float salpha2 = 1-calpha2;
146               
147                float P = exp( -( salpha2 / calpha2 / m2 )) / (m2 * PI * calpha * calpha2);
148               
149                float f1 = 2*calpha*ctheta_out/cbeta;
150                float f2 = 2*calpha*ctheta_in /cbeta;
151                float G = min(1, min( f1, f2 )) / 4 / ctheta_in / ctheta_out;
152                //float GG = 1 / (4*cbeta*cbeta);
153
154                //F*P*G*ctheta_in               
155                intens += float4(F, 1) * P * G * ctheta_in;
156        }
157       
158        return 3*intens;
159}
160
161float3 expand(float3 v)
162{
163        return (v - 0.5) * 2;
164}
165
166float2 PARALLAX_MAPPING(sampler2D heightMap, float2 TexCoord, float3 View)
167{
168        float HEIGHT_SCALE = 0.06;
169        float HEIGHT_BIAS = -0.12;
170    float h = tex2D(heightMap, TexCoord).r * HEIGHT_SCALE + HEIGHT_BIAS;
171    return TexCoord + h * View.xy / View.z;
172}
173
174struct VS_INPUT {
175    float4 Position  : POSITION;     // point in modeling space
176    float2 TexCoord      : TEXCOORD0;    // texture coordinates
177    float3 Tangent   : TEXCOORD1;        // model space tangent vector
178    float3 Normal    : NORMAL;           // model space triangle normal vector
179};
180
181struct VS_OUTPUT {
182    float4 hPosition : POSITION;     // point in normalized device space before homogeneous division
183    float2 TexCoord  : TEXCOORD0;    // texture coordinates
184    float3 Normal    : TEXCOORD1;        // model space triangle normal vector
185    float3 wPosition : TEXCOORD2;        // model space tangent vector
186        float3 Tangent   : TEXCOORD3;    // model space tangent vector
187    float3 Binormal  : TEXCOORD4;        // model space binormal vector
188};
189
190VS_OUTPUT LocalizedBumpVS(VS_INPUT IN,
191                uniform float4x4 worldViewProj,
192                uniform float4x4 worldViewIT,
193                uniform float4x4 world)
194{
195  VS_OUTPUT OUT;
196  OUT.hPosition = mul(worldViewProj, IN.Position);
197  OUT.wPosition = mul(world, IN.Position).xyz; 
198  //OUT.Normal = mul(worldViewIT, IN.Normal);
199  OUT.Normal = IN.Normal;
200  OUT.TexCoord = IN.TexCoord;
201  OUT.Tangent = IN.Tangent; 
202  OUT.Binormal = cross(IN.Tangent, IN.Normal);
203 
204  return OUT;
205}
206
207//////////////
208//Localized reflection with bump
209//////////////
210/*
211VS_OUTPUT BaseVS(VS_INPUT IN)
212{
213        VS_OUTPUT OUT;
214
215               
216                OUT.Tangent = IN.Tangent;
217                OUT.Binormal = IN.Binormal;
218                OUT.Normal = IN.Normal;
219               
220        // vertex position before homogenious division
221        OUT.hPosition = mul(IN.Position, WorldViewProj);
222                // tex coordinates passed to pixel shader
223        OUT.TexCoord = IN.TexCoord0;
224
225                return OUT;
226}
227float4 BumpPS(VS_OUTPUT IN) : COLOR
228{
229 
230    // get model space normal vector
231        float3x3 ModelToTangent = TransfModelToTangent(IN.Tangent, IN.Binormal, IN.Normal );
232    // get model space normal vector
233    float3 tNormal = tex2D(BumpMapSampler, IN.TexCoord).rgb;
234        // Normal vector should be transformed with the inverse transpose of TangentToModel
235        // which is the transpose of ModelToTangent
236    float3 mNormal = normalize( mul( tNormal, ModelToTangent ) );     
237   
238}
239*/
240
241void LocalizedBumpPS(  VS_OUTPUT IN,
242                uniform float3 cameraPos,
243                uniform samplerCUBE CubeMap : register(s0),
244                uniform samplerCUBE DistanceMap : register(s1),
245                uniform sampler2D NormalMap : register(s2),
246                uniform sampler2D DisplacementMap : register(s3),
247                uniform float3 lastCenter,
248                uniform float3 lightPosition,           
249                out float4 Color :COLOR0)
250{
251        // get model space normal vector
252        float3x3 ModelToTangent = TransfModelToTangent(IN.Tangent, IN.Binormal, IN.Normal );
253    // get model space normal vector
254       
255        //parallax
256        /*
257        float3 tView = mul(ModelToTangent, IN.wPosition );
258    float2 ParallaxTex = PARALLAX_MAPPING(DisplacementMap, IN.TexCoord, tView);
259    float3 tNormal = tex2D(NormalMap, ParallaxTex).rgb;
260    */
261       
262    float3 tNormal = tex2D(NormalMap, IN.TexCoord).rgb;         
263    float3 mNormal = normalize( mul( tNormal, ModelToTangent ) );
264       
265   //Color = float4(tex2D(DisplacementMap, IN.TexCoord).rgb, 1 + lastCenter.x * 0.000000001);
266        Color = float4(1,1,1,1);
267       
268        float3 RR, TT; 
269        float3 mPos = IN.wPosition - lastCenter;
270        float3 V = normalize(IN.wPosition - cameraPos);
271        float3 R = (reflect( V, mNormal));
272       
273        float3 T = refract(V, mNormal, 0.9);
274               
275        RR = R; TT = T;
276        RR = Hit(mPos, R, DistanceMap);
277        TT = Hit(mPos, T, DistanceMap);
278       
279        float4 reflectcolor = readCubeMap(CubeMap, RR );               
280        float4 refractcolor = readCubeMap(CubeMap, TT );               
281       
282        float cos_theta = -dot(V, mNormal);
283        float sFresnel = 0.4;
284        float F = (sFresnel + pow(1-cos_theta, 5.0f) * (1-sFresnel));
285   
286    float3 L = normalize(lightPosition - IN.wPosition);
287        float3 H = normalize(L+V);
288        float4 lighting = lit(dot(mNormal, L),dot(mNormal, H), 30);
289        Color = (F * reflectcolor + (1-F) * refractcolor)/* + lighting.z *1000*/;       
290}
291
292
293//////////////
294//Metal
295//////////////
296void LocalizedMetalPS(  float2 texCoord : TEXCOORD0,
297                float3 wPos     : TEXCOORD1,   
298                float3 mNormal  : TEXCOORD2,
299                uniform float3 cameraPos,
300                uniform samplerCUBE CubeMap : register(s0),
301                uniform samplerCUBE DistanceMap : register(s1),
302                uniform float3 lastCenter,
303                uniform float3 lightPosition,                           
304                out float4 Color :COLOR0)
305{
306       
307        Color = float4(1,1,1,1);
308       
309        mNormal = normalize(mNormal);
310        float3 newTexCoord;     
311        float3 mPos = wPos - lastCenter;
312        float3 V = normalize(wPos - cameraPos);
313        float3 R = normalize(reflect( V, mNormal));
314               
315        newTexCoord = R;       
316       
317        newTexCoord = Hit(mPos, R, DistanceMap);
318       
319        Color = readCubeMap(CubeMap, newTexCoord );     
320       
321        float ctheta_in = dot(mNormal,R);
322        float ctheta_out = dot(mNormal,-V);
323
324        float3 F = 0;
325       
326        // F,P,G számítása
327        if ( ctheta_in > 0 && ctheta_out > 0 )
328        {
329                float3 H = normalize(R - V);    // felezõvektor
330                float cbeta  = dot(H,R);               
331                //F = ( (n-1)*(n-1) + pow(1-cbeta,5) * 4*n + k*k) / ( (n+1)*(n+1) + k*k );
332                //float3 F0 = ((n-1)*(n-1) + k*k) / ( (n+1)*(n+1) + k*k );
333                //float3 F1 = float3(1.0f,1.0f,1.0f) - F0;
334                F = F0 + (1-F0)*pow(1-cbeta,5);
335        }
336       
337        //Color *= metal_reflectivity(R, mNormal, -V);
338       
339        Color = Color * float4(F,1);   
340}
Note: See TracBrowser for help on using the repository browser.