[3824] | 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 vec4 waterColor; |
---|
| 13 | |
---|
| 14 | void main() |
---|
| 15 | { |
---|
| 16 | const float kShine = 128.0; |
---|
| 17 | const float kDistortion = 0.015; |
---|
| 18 | const float kRefraction = 0.009; |
---|
| 19 | |
---|
| 20 | vec4 distOffset = texture2D(dudvMap, normCoords.xy) * kDistortion; |
---|
| 21 | vec4 dudvColor = texture2D(dudvMap, vec2(refrCoords + distOffset)); |
---|
| 22 | dudvColor = normalize(dudvColor * 2.0 - 1.0) * kRefraction; |
---|
| 23 | |
---|
| 24 | vec4 normalVector = texture2D(normalMap, vec2(refrCoords + distOffset)); |
---|
| 25 | normalVector = normalVector * 2.0 - 1.0; |
---|
| 26 | normalVector.a = 0.0; |
---|
| 27 | |
---|
| 28 | vec4 lightReflection = normalize( reflect(-lightTangetSpace, normalVector) ); |
---|
| 29 | |
---|
| 30 | vec4 invertedFresnel = vec4( dot(normalVector, lightReflection ) ); |
---|
| 31 | vec4 fresnelTerm = 1.0 - invertedFresnel; |
---|
| 32 | |
---|
| 33 | vec4 projCoord = viewCoords / viewCoords.q; |
---|
| 34 | projCoord = (projCoord + 1.0) * 0.5; |
---|
| 35 | projCoord += dudvColor; |
---|
| 36 | projCoord = clamp(projCoord, 0.001, 0.999); |
---|
| 37 | |
---|
| 38 | vec4 reflectionColor = texture2D(reflection, projCoord.xy); |
---|
| 39 | vec4 refractionColor = texture2D(refraction, projCoord.xy); |
---|
| 40 | vec4 depthValue = texture2D(depthMap, projCoord.xy); |
---|
| 41 | |
---|
| 42 | vec4 invDepth = 1.0 - depthValue; |
---|
| 43 | refractionColor *= invertedFresnel * invDepth; |
---|
| 44 | refractionColor += waterColor * depthValue * invertedFresnel; |
---|
| 45 | |
---|
| 46 | reflectionColor *= fresnelTerm; |
---|
| 47 | |
---|
| 48 | vec4 localView = normalize(viewTangetSpace); |
---|
| 49 | float intensity = max(0.0, dot(lightReflection, localView) ); |
---|
| 50 | vec4 specular = vec4(pow(intensity, kShine)); |
---|
| 51 | |
---|
| 52 | gl_FragColor = refractionColor + reflectionColor + specular; |
---|
| 53 | } |
---|