source: GTP/branches/IllumWPdeliver2008dec/IlluminationWP/demos/Standalone/MultipleReflections [DirectX]/Media/Shaders/basicShaders.fx @ 3255

Revision 3255, 5.5 KB checked in by szirmay, 15 years ago (diff)
Line 
1float4x4 World;                                 ///< World matrix for the current object
2float4x4 WorldIT;                               ///< World matrix IT (inverse transposed) to transform surface normals of the current object
3float4x4 WorldView;
4float4x4 WorldViewIT;
5float4x4 WorldViewProj;
6float3 referencePos;
7float3 eyePos;
8float3 F0; // Freshnel factor
9float N0; // Refraction coefficient
10
11texture colorMap;
12sampler colorSampler = sampler_state
13{
14    MinFilter = LINEAR;
15    MagFilter = LINEAR;
16    MipFilter = POINT;   
17    Texture   = <colorMap>;
18    AddressU  = WRAP;
19    AddressV  = WRAP;
20};
21
22texture envCube;
23sampler envCubeSampler = sampler_state
24{
25    MinFilter = LINEAR;
26    MagFilter = LINEAR;
27    MipFilter = POINT;   
28    Texture   = <envCube>;
29    AddressU  = WRAP;
30    AddressV  = WRAP;
31};
32
33//-------------------------------------------------------------------------
34// Technique: Simple textured object
35//-------------------------------------------------------------------------
36        struct Textured_VS_OUT
37        {
38                float4 hPosition :POSITION0;
39                float2 texCoord :TEXCOORD0;
40        };
41
42        Textured_VS_OUT texturedVS(float4 position : POSITION0,
43                                                        float2 texCoord : TEXCOORD0)
44        {
45                Textured_VS_OUT OUT;
46                OUT.hPosition = mul(position, WorldViewProj);
47                OUT.texCoord = texCoord;
48                return OUT;
49        }
50
51        float4 texturedPS(Textured_VS_OUT IN) : COLOR0
52        {
53                return tex2D(colorSampler, IN.texCoord);
54        }
55
56        technique Textured                                                                             
57        {                                                                                                                               
58                        pass p0                                                                                                         
59                        {                                                                                                                       
60                                VertexShader = compile vs_2_0 texturedVS();             
61                                PixelShader  = compile ps_2_0 texturedPS();     
62                        }                                                                                                                       
63        }
64
65//-------------------------------------------------------------------------
66// Technique: Classical environment mapping
67//-------------------------------------------------------------------------
68        struct EnvMapped_VS_OUT
69        {
70                float4 hPosition : POSITION0;
71                float2 texCoord  : TEXCOORD0;
72                float3 wNormal   : TEXCOORD1; //world normal
73                float3 cmPos     : TEXCOORD2; //cubemap position
74                float3 V                 : TEXCOORD3; //view direction in world space
75        };
76
77        EnvMapped_VS_OUT EnvMappedVS(float4 position : POSITION0,
78                                                        float2 texCoord : TEXCOORD0,
79                                                        float3 normal : NORMAL)
80        {
81                EnvMapped_VS_OUT OUT;
82                OUT.hPosition = mul(position, WorldViewProj);
83                OUT.wNormal = mul(normal, WorldIT);
84                OUT.cmPos = mul(position, World).xyz;
85                OUT.V = OUT.cmPos - eyePos;
86                OUT.cmPos -= referencePos;
87                OUT.texCoord = texCoord;
88                return OUT;
89        }
90       
91        float4 EnvMappedPS(EnvMapped_VS_OUT IN) : COLOR0
92        {
93                float3 N = normalize(IN.wNormal);
94                float3 V = normalize(IN.V);
95                float3 R;
96                float3 I = 1;
97               
98                float3 F = F0 + pow(1-dot(N, -V), 5) * (1 - F0);
99                if (N0 <= 0) // reflective material
100                {
101                R = reflect(V, N);
102                I *= F; // Fresnel reflection
103        }
104        else //refractive material
105        {
106                R = refract(V, N, N0);
107                if (dot(R, R) == 0)     // no refraction direction exits
108                        R = reflect(V, N); // total reflection                         
109                        else
110                        I *= (1 - F);      // Fresnel refraction
111        }       
112                return texCUBE(envCubeSampler, R) * float4(I,1);
113        }
114
115        technique EnvMapped                                                                             
116        {                                                                                                                               
117                        pass p0                                                                                                         
118                        {                                                                                                                       
119                                VertexShader = compile vs_2_0 EnvMappedVS();           
120                                PixelShader  = compile ps_2_0 EnvMappedPS();   
121                        }                                                                                                                       
122        }
123       
124//-------------------------------------------------------------------------
125// Technique: write color and distance from camera
126//-------------------------------------------------------------------------
127   
128    struct ColorDist_VS_OUT
129        {
130                float4 hPosition : POSITION0;
131                float2 texCoord  : TEXCOORD0;
132                float3 cPos      : TEXCOORD1; //view space position             
133        };
134       
135        ColorDist_VS_OUT ColorDistVS(float4 position : POSITION0,
136                                                        float2 texCoord : TEXCOORD0)
137        {
138                ColorDist_VS_OUT OUT;
139                OUT.hPosition = mul(position, WorldViewProj);
140                OUT.cPos = mul(position, WorldView).xyz;
141                OUT.texCoord = texCoord;
142                return OUT;
143        }
144       
145        float4 ColorDistPS(ColorDist_VS_OUT IN) : COLOR0
146        {
147                float4 color = tex2D(colorSampler, IN.texCoord);
148                color.a = length(IN.cPos);
149                return color;           
150        }
151
152        technique ColorDistance                                                                         
153        {                                                                                                                               
154                        pass p0                                                                                                         
155                        {                                                                                                                       
156                                VertexShader = compile vs_2_0 ColorDistVS();           
157                                PixelShader  = compile ps_2_0 ColorDistPS();   
158                        }                                                                                                                       
159        }
160
161//-------------------------------------------------------------------------
162// Technique: write normal and distance from camera
163//-------------------------------------------------------------------------
164   
165    struct NormalDist_VS_OUT
166        {
167                float4 hPosition : POSITION0;
168                float2 texCoord  : TEXCOORD0;
169                float3 wNormal   : TEXCOORD1; //world normal position           
170                float3 cPos          : TEXCOORD2; //view space position
171        };
172       
173        NormalDist_VS_OUT NormalDistVS(float4 position : POSITION0,
174                                                        float2 texCoord : TEXCOORD0,
175                                                        float3 normal : NORMAL)
176        {
177                NormalDist_VS_OUT OUT;
178                OUT.hPosition = mul(position, WorldViewProj);
179                OUT.wNormal = mul(normal, WorldIT).xyz;
180                OUT.cPos = mul(position, WorldView).xyz;
181                OUT.texCoord = texCoord;
182                return OUT;
183        }
184       
185        float4 NormalDistPS(NormalDist_VS_OUT IN) : COLOR0
186        {
187                float4 color;
188                color.rgb = normalize(IN.wNormal);
189                color.a = length(IN.cPos);
190                return color;           
191        }
192
193        technique NormalDistance                                                                               
194        {                                                                                                                               
195                        pass p0                                                                                                         
196                        {                                                                                                                       
197                                VertexShader = compile vs_2_0 NormalDistVS();           
198                                PixelShader  = compile ps_2_0 NormalDistPS();   
199                        }                                                                                                                       
200        }
Note: See TracBrowser for help on using the repository browser.