1 | varying vec4 refrCoords; |
---|
2 | varying vec4 normCoords; |
---|
3 | varying vec4 viewCoords; |
---|
4 | varying vec4 viewTangetSpace; |
---|
5 | varying vec4 lightTangetSpace; |
---|
6 | |
---|
7 | uniform sampler2D reflection; |
---|
8 | uniform sampler2D refraction; |
---|
9 | uniform sampler2D normalMap; |
---|
10 | uniform sampler2D dudvMap; |
---|
11 | uniform sampler2D depthMap; |
---|
12 | uniform float kShine; |
---|
13 | uniform float shineStrength; |
---|
14 | uniform float reflStrength; |
---|
15 | uniform float kRefraction; |
---|
16 | uniform vec4 waterColor; |
---|
17 | |
---|
18 | void main() |
---|
19 | { |
---|
20 | const float kDistortion = 0.015; |
---|
21 | |
---|
22 | vec4 distOffset = texture2D(dudvMap, normCoords.xy) * kDistortion; |
---|
23 | vec4 dudvColor = texture2D(dudvMap, vec2(refrCoords + distOffset)); |
---|
24 | dudvColor = normalize(dudvColor * 2.0 - 1.0) * kRefraction; |
---|
25 | |
---|
26 | vec4 normalVector = texture2D(normalMap, vec2(refrCoords + distOffset)); |
---|
27 | normalVector = normalVector * 2.0 - 1.0; |
---|
28 | normalVector.a = 0.0; |
---|
29 | |
---|
30 | vec4 lightReflection = normalize( reflect(-lightTangetSpace, normalVector) ); |
---|
31 | |
---|
32 | vec4 invertedFresnel = vec4( dot(normalVector, lightReflection ) ); |
---|
33 | vec4 fresnelTerm = 1.0 - invertedFresnel; |
---|
34 | |
---|
35 | vec4 projCoord = viewCoords / viewCoords.q; |
---|
36 | projCoord = (projCoord + 1.0) * 0.5; |
---|
37 | projCoord += dudvColor; |
---|
38 | projCoord = clamp(projCoord, 0.001, 0.999); |
---|
39 | |
---|
40 | vec4 reflectionColor = texture2D(reflection, projCoord.xy); |
---|
41 | vec4 refractionColor = texture2D(refraction, projCoord.xy); |
---|
42 | vec4 depthValue = texture2D(depthMap, projCoord.xy); |
---|
43 | |
---|
44 | vec4 invDepth = 1.0 - depthValue; |
---|
45 | refractionColor *= invertedFresnel * invDepth; |
---|
46 | refractionColor += waterColor * depthValue * invertedFresnel; |
---|
47 | |
---|
48 | reflectionColor *= fresnelTerm; |
---|
49 | |
---|
50 | vec4 localView = normalize(viewTangetSpace); |
---|
51 | float intensity = max(0.0, dot(lightReflection, localView) ); |
---|
52 | vec4 specular = vec4(pow(intensity, kShine)); |
---|
53 | |
---|
54 | gl_FragColor = refractionColor + reflStrength * reflectionColor + shineStrength * specular; |
---|
55 | } |
---|