Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/Samples/Media/materials/programs/Example_Fresnel.cg @ 3

Last change on this file since 3 was 3, checked in by anonymous, 17 years ago

=update

File size: 3.5 KB
Line 
1// Vertex program for fresnel reflections / refractions
2void main_vp(
3                float4 pos                      : POSITION,
4                float4 normal           : NORMAL,
5                float2 tex                      : TEXCOORD0,
6               
7                out float4 oPos         : POSITION,
8                out float3 noiseCoord : TEXCOORD0,
9                out float4 projectionCoord : TEXCOORD1,
10                out float3 oEyeDir : TEXCOORD2,
11                out float3 oNormal : TEXCOORD3,
12
13                uniform float4x4 worldViewProjMatrix,
14                uniform float3 eyePosition, // object space
15                uniform float timeVal,
16                uniform float scale,  // the amount to scale the noise texture by
17                uniform float scroll, // the amount by which to scroll the noise
18                uniform float noise  // the noise perturb as a factor of the  time
19                )
20{
21        oPos = mul(worldViewProjMatrix, pos);
22        // Projective texture coordinates, adjust for mapping
23        float4x4 scalemat = float4x4(0.5,   0,   0, 0.5,
24                                       0,-0.5,   0, 0.5,
25                                                                   0,   0, 0.5, 0.5,
26                                                                   0,   0,   0,   1);
27        projectionCoord = mul(scalemat, oPos);
28        // Noise map coords
29        noiseCoord.xy = (tex + (timeVal * scroll)) * scale;
30        noiseCoord.z = noise * timeVal;
31
32        oEyeDir = normalize(pos.xyz - eyePosition);
33        oNormal = normal.rgb;
34       
35}
36
37// Fragment program for distorting a texture using a 3D noise texture
38void main_fp(
39                float3 noiseCoord                       : TEXCOORD0,
40                float4 projectionCoord          : TEXCOORD1,
41                float3 eyeDir                           : TEXCOORD2,
42                float3 normal                           : TEXCOORD3,
43               
44                out float4 col          : COLOR,
45               
46                uniform float4 tintColour,
47                uniform float noiseScale,
48                uniform float fresnelBias,
49                uniform float fresnelScale,
50                uniform float fresnelPower,
51                uniform sampler2D noiseMap : register(s0),
52                uniform sampler2D reflectMap : register(s1),
53                uniform sampler2D refractMap : register(s2)
54                )
55{
56        // Do the tex projection manually so we can distort _after_
57        float2 final = projectionCoord.xy / projectionCoord.w;
58
59        // Noise
60        float3 noiseNormal = (tex2D(noiseMap, (noiseCoord.xy / 5)).rgb - 0.5).rbg * noiseScale;
61        final += noiseNormal.xz;
62
63        // Fresnel
64        //normal = normalize(normal + noiseNormal.xz);
65        float fresnel = fresnelBias + fresnelScale * pow(1 + dot(eyeDir, normal), fresnelPower);
66
67        // Reflection / refraction
68        float4 reflectionColour = tex2D(reflectMap, final);
69        float4 refractionColour = tex2D(refractMap, final) + tintColour;
70
71        // Final colour
72        col = lerp(refractionColour, reflectionColour, fresnel);
73
74
75}
76
77
78// Old version to match ATI PS 1.3 implementation
79void main_vp_old(
80                float4 pos                      : POSITION,
81                float4 normal           : NORMAL,
82                float2 tex                      : TEXCOORD0,
83               
84                out float4 oPos         : POSITION,
85                out float fresnel   : COLOR,
86                out float3 noiseCoord : TEXCOORD0,
87                out float4 projectionCoord : TEXCOORD1,
88
89                uniform float4x4 worldViewProjMatrix,
90                uniform float3 eyePosition, // object space
91                uniform float fresnelBias,
92                uniform float fresnelScale,
93                uniform float fresnelPower,
94                uniform float timeVal,
95                uniform float scale,  // the amount to scale the noise texture by
96                uniform float scroll, // the amount by which to scroll the noise
97                uniform float noise  // the noise perturb as a factor of the  time
98                )
99{
100        oPos = mul(worldViewProjMatrix, pos);
101        // Projective texture coordinates, adjust for mapping
102        float4x4 scalemat = float4x4(0.5,   0,   0, 0.5,
103                                       0,-0.5,   0, 0.5,
104                                                                   0,   0, 0.5, 0.5,
105                                                                   0,   0,   0,   1);
106        projectionCoord = mul(scalemat, oPos);
107        // Noise map coords
108        noiseCoord.xy = (tex + (timeVal * scroll)) * scale;
109        noiseCoord.z = noise * timeVal;
110
111        // calc fresnel factor (reflection coefficient)
112        float3 eyeDir = normalize(pos.xyz - eyePosition);
113        fresnel = fresnelBias + fresnelScale * pow(1 + dot(eyeDir, normal), fresnelPower);
114       
115}
Note: See TracBrowser for help on using the repository browser.