Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12115 was 12115, checked in by wiesep, 5 years ago

Changed folder structure, deletet some unused files and cleaned up code

File size: 2.3 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}
Note: See TracBrowser for help on using the repository browser.