Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/branches/Shader_HS18/programs/Example/Cg/Example_Fresnel.cg @ 12083

Last change on this file since 12083 was 12083, checked in by wiesep, 6 years ago

Reorganised shader programs

File size: 3.7 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                float4 pos                      : POSITION,
40    float3 noiseCoord                   : TEXCOORD0,
41                float4 projectionCoord          : TEXCOORD1,
42                float3 eyeDir                           : TEXCOORD2,
43                float3 normal                           : TEXCOORD3,
44               
45                out float4 col          : COLOR,
46               
47                uniform float4 tintColour,
48                uniform float noiseScale,
49                uniform float fresnelBias,
50                uniform float fresnelScale,
51                uniform float fresnelPower,
52                uniform sampler2D noiseMap : register(s0),
53                uniform sampler2D reflectMap : register(s1),
54                uniform sampler2D refractMap : register(s2)
55                )
56{
57        // Do the tex projection manually so we can distort _after_
58        float2 final = projectionCoord.xy / projectionCoord.w;
59
60        // Noise
61        float3 noiseNormal = (tex2D(noiseMap, (noiseCoord.xy / 5)).rgb - 0.5).rbg * noiseScale;
62        final += noiseNormal.xz;
63
64        // Fresnel
65        //normal = normalize(normal + noiseNormal.xz);
66        float fresnel = fresnelBias + fresnelScale * pow(1 + dot(eyeDir, normal), fresnelPower);
67
68        // Reflection / refraction
69        float4 reflectionColour = tex2D(reflectMap, final);
70        float4 refractionColour = tex2D(refractMap, final) + tintColour;
71
72        // Final colour
73        col = lerp(refractionColour, reflectionColour, fresnel);
74
75
76}
77
78
79// Old version to match ATI PS 1.3 implementation
80void main_vp_old(
81                float4 pos                      : POSITION,
82                float4 normal           : NORMAL,
83                float2 tex                      : TEXCOORD0,
84               
85                out float4 oPos         : POSITION,
86                out float fresnel   : COLOR,
87                out float3 noiseCoord : TEXCOORD0,
88                out float4 projectionCoord : TEXCOORD1,
89
90                uniform float4x4 worldViewProjMatrix,
91                uniform float3 eyePosition, // object space
92                uniform float fresnelBias,
93                uniform float fresnelScale,
94                uniform float fresnelPower,
95                uniform float timeVal,
96                uniform float scale,  // the amount to scale the noise texture by
97                uniform float scroll, // the amount by which to scroll the noise
98                uniform float noise  // the noise perturb as a factor of the  time
99                )
100{
101        oPos = mul(worldViewProjMatrix, pos);
102        // Projective texture coordinates, adjust for mapping
103        float4x4 scalemat = float4x4(0.5,   0,   0, 0.5,
104                                       0,-0.5,   0, 0.5,
105                                                                   0,   0, 0.5, 0.5,
106                                                                   0,   0,   0,   1);
107        projectionCoord = mul(scalemat, oPos);
108        // Noise map coords
109        noiseCoord.xy = (tex + (timeVal * scroll)) * scale;
110        noiseCoord.z = noise * timeVal;
111
112        // calc fresnel factor (reflection coefficient)
113        float3 eyeDir = normalize(pos.xyz - eyePosition);
114        fresnel = fresnelBias + fresnelScale * pow(1 + dot(eyeDir, normal), fresnelPower);
115       
116}
Note: See TracBrowser for help on using the repository browser.