1 | // Vertex program for fresnel reflections / refractions |
---|
2 | void main_vp( |
---|
3 | float4 pos : POSITION, |
---|
4 | float4 normal : NORMAL, |
---|
5 | float2 tex : TEXCOORD0, |
---|
6 | |
---|
7 | out float4 oPos : POSITION, |
---|
8 | out float fresnel : COLOR, |
---|
9 | out float3 noiseCoord : TEXCOORD0, |
---|
10 | out float4 projectionCoord : TEXCOORD1, |
---|
11 | |
---|
12 | uniform float4x4 worldViewProjMatrix, |
---|
13 | uniform float3 eyePosition, // object space |
---|
14 | uniform float fresnelBias, |
---|
15 | uniform float fresnelScale, |
---|
16 | uniform float fresnelPower, |
---|
17 | uniform float timeVal, |
---|
18 | uniform float scale, // the amount to scale the noise texture by |
---|
19 | uniform float scroll, // the amount by which to scroll the noise |
---|
20 | uniform float noise // the noise perturb as a factor of the time |
---|
21 | ) |
---|
22 | { |
---|
23 | oPos = mul(worldViewProjMatrix, pos); |
---|
24 | // Projective texture coordinates, adjust for mapping |
---|
25 | float4x4 scalemat = float4x4(0.5, 0, 0, 0.5, |
---|
26 | 0,-0.5, 0, 0.5, |
---|
27 | 0, 0, 0.5, 0.5, |
---|
28 | 0, 0, 0, 1); |
---|
29 | projectionCoord = mul(scalemat, oPos); |
---|
30 | // Noise map coords |
---|
31 | noiseCoord.xy = (tex + (timeVal * scroll)) * scale; |
---|
32 | noiseCoord.z = noise * timeVal; |
---|
33 | |
---|
34 | // calc fresnel factor (reflection coefficient) |
---|
35 | float3 eyeDir = normalize(pos.xyz - eyePosition); |
---|
36 | fresnel = fresnelBias + fresnelScale * pow(1 + dot(eyeDir, normal), fresnelPower); |
---|
37 | |
---|
38 | } |
---|
39 | |
---|
40 | // Fragment program for distorting a texture using a 3D noise texture |
---|
41 | void main_fp( |
---|
42 | float fresnel : COLOR, |
---|
43 | float3 noiseCoord : TEXCOORD0, |
---|
44 | float4 projectionCoord : TEXCOORD1, |
---|
45 | |
---|
46 | out float4 col : COLOR, |
---|
47 | |
---|
48 | uniform float distortionRange, |
---|
49 | uniform float4 tintColour, |
---|
50 | uniform sampler3D noiseMap, |
---|
51 | uniform sampler2D reflectMap, |
---|
52 | uniform sampler2D refractMap |
---|
53 | ) |
---|
54 | { |
---|
55 | // Randomly chosen offset for y noise sample |
---|
56 | float3 yoffset = float3(0.31, 0.58, 0.23); |
---|
57 | float2 distort; |
---|
58 | // Sample the noise texture at 2 places |
---|
59 | distort.x = tex3D(noiseMap, noiseCoord).x; |
---|
60 | distort.y = tex3D(noiseMap, noiseCoord + yoffset).x; |
---|
61 | // Scale the distortion from [0,1] to [-range,range] |
---|
62 | distort = (distort * 2 - 1) * distortionRange; |
---|
63 | |
---|
64 | // Do the tex projection manually so we can distort _after_ |
---|
65 | float2 final = projectionCoord.xy / projectionCoord.w; |
---|
66 | final += distort; |
---|
67 | |
---|
68 | float4 reflectionColour = tex2D(reflectMap, final); |
---|
69 | float4 refractionColour = tex2D(refractMap, final) + tintColour; |
---|
70 | col = lerp(refractionColour, reflectionColour, fresnel); |
---|
71 | |
---|
72 | |
---|
73 | } |
---|