From 44dcef714f7addbaf04b0b370fbabe2f53eb7d72 Mon Sep 17 00:00:00 2001 From: Kevin Ring Date: Tue, 12 Feb 2013 10:08:26 -0500 Subject: [PATCH] Eliminate duplication of water noise GLSL function. --- Source/Shaders/BuiltinFunctions.glsl | 38 +++++++++++++++++++++++++++ Source/Shaders/CentralBodyFS.glsl | 39 +--------------------------- Source/Shaders/Materials/Water.glsl | 37 +------------------------- 3 files changed, 40 insertions(+), 74 deletions(-) diff --git a/Source/Shaders/BuiltinFunctions.glsl b/Source/Shaders/BuiltinFunctions.glsl index db9e7428d91..cc805cc32fa 100644 --- a/Source/Shaders/BuiltinFunctions.glsl +++ b/Source/Shaders/BuiltinFunctions.glsl @@ -934,3 +934,41 @@ vec3 czm_translateRelativeToEye(vec3 high, vec3 low) return highDifference + lowDifference; } + +/** + * @private + */ +vec4 czm_getWaterNoise(sampler2D normalMap, vec2 uv, float time, float angleInRadians) +{ + float cosAngle = cos(angleInRadians); + float sinAngle = sin(angleInRadians); + + // time dependent sampling directions + vec2 s0 = vec2(1.0/17.0, 0.0); + vec2 s1 = vec2(-1.0/29.0, 0.0); + vec2 s2 = vec2(1.0/101.0, 1.0/59.0); + vec2 s3 = vec2(-1.0/109.0, -1.0/57.0); + + // rotate sampling direction by specified angle + s0 = vec2((cosAngle * s0.x) - (sinAngle * s0.y), (sinAngle * s0.x) + (cosAngle * s0.y)); + s1 = vec2((cosAngle * s1.x) - (sinAngle * s1.y), (sinAngle * s1.x) + (cosAngle * s1.y)); + s2 = vec2((cosAngle * s2.x) - (sinAngle * s2.y), (sinAngle * s2.x) + (cosAngle * s2.y)); + s3 = vec2((cosAngle * s3.x) - (sinAngle * s3.y), (sinAngle * s3.x) + (cosAngle * s3.y)); + + vec2 uv0 = (uv/103.0) + (time * s0); + vec2 uv1 = uv/107.0 + (time * s1) + vec2(0.23); + vec2 uv2 = uv/vec2(897.0, 983.0) + (time * s2) + vec2(0.51); + vec2 uv3 = uv/vec2(991.0, 877.0) + (time * s3) + vec2(0.71); + + uv0 = fract(uv0); + uv1 = fract(uv1); + uv2 = fract(uv2); + uv3 = fract(uv3); + vec4 noise = (texture2D(normalMap, uv0)) + + (texture2D(normalMap, uv1)) + + (texture2D(normalMap, uv2)) + + (texture2D(normalMap, uv3)); + + // average and scale to between -1 and 1 + return ((noise / 4.0) - 0.5) * 2.0; +} diff --git a/Source/Shaders/CentralBodyFS.glsl b/Source/Shaders/CentralBodyFS.glsl index 060681f692c..09436256cf3 100644 --- a/Source/Shaders/CentralBodyFS.glsl +++ b/Source/Shaders/CentralBodyFS.glsl @@ -141,43 +141,6 @@ void main() gl_FragColor = color; } -#ifdef SHOW_OCEAN_WAVES -vec4 getNoise(vec2 uv, float time) -{ - float cosAngle = 1.0; // cos(angleInRadians); - float sinAngle = 0.0; // sin(angleInRadians); - - // time dependent sampling directions - vec2 s0 = vec2(1.0/17.0, 0.0); - vec2 s1 = vec2(-1.0/29.0, 0.0); - vec2 s2 = vec2(1.0/101.0, 1.0/59.0); - vec2 s3 = vec2(-1.0/109.0, -1.0/57.0); - - // rotate sampling direction by specified angle - s0 = vec2((cosAngle * s0.x) - (sinAngle * s0.y), (sinAngle * s0.x) + (cosAngle * s0.y)); - s1 = vec2((cosAngle * s1.x) - (sinAngle * s1.y), (sinAngle * s1.x) + (cosAngle * s1.y)); - s2 = vec2((cosAngle * s2.x) - (sinAngle * s2.y), (sinAngle * s2.x) + (cosAngle * s2.y)); - s3 = vec2((cosAngle * s3.x) - (sinAngle * s3.y), (sinAngle * s3.x) + (cosAngle * s3.y)); - - vec2 uv0 = (uv/103.0) + (time * s0); - vec2 uv1 = uv/107.0 + (time * s1) + vec2(0.23); - vec2 uv2 = uv/vec2(897.0, 983.0) + (time * s2) + vec2(0.51); - vec2 uv3 = uv/vec2(991.0, 877.0) + (time * s3) + vec2(0.71); - - uv0 = fract(uv0); - uv1 = fract(uv1); - uv2 = fract(uv2); - uv3 = fract(uv3); - vec4 noise = (texture2D(u_oceanNormalMap, uv0)) + - (texture2D(u_oceanNormalMap, uv1)) + - (texture2D(u_oceanNormalMap, uv2)) + - (texture2D(u_oceanNormalMap, uv3)); - - // average and scale to between -1 and 1 - return ((noise / 4.0) - 0.5) * 2.0; -} -#endif // #ifdef SHOW_OCEAN_WAVES - #ifdef SHOW_REFLECTIVE_OCEAN float waveFade(float edge0, float edge1, float x) @@ -208,7 +171,7 @@ vec4 computeWaterColor(vec3 positionEyeCoordinates, vec2 textureCoordinates, mat float waveIntensity = waveFade(70000.0, 1000000.0, positionToEyeECLength); #ifdef SHOW_OCEAN_WAVES - vec4 noise = getNoise(textureCoordinates * oceanFrequency, time); + vec4 noise = czm_getWaterNoise(u_oceanNormalMap, textureCoordinates * oceanFrequency, time, 0.0); vec3 normalTangentSpace = noise.xyz * vec3(1.0, 1.0, (1.0 / oceanAmplitude)); // fade out the normal perturbation as we move farther from the water surface diff --git a/Source/Shaders/Materials/Water.glsl b/Source/Shaders/Materials/Water.glsl index 0142e23054c..8abc2e87388 100644 --- a/Source/Shaders/Materials/Water.glsl +++ b/Source/Shaders/Materials/Water.glsl @@ -11,41 +11,6 @@ uniform float amplitude; uniform float specularIntensity; uniform float fadeFactor; -vec4 getNoise(vec2 uv, float time, float angleInRadians) { - - float cosAngle = cos(angleInRadians); - float sinAngle = sin(angleInRadians); - - // time dependent sampling directions - vec2 s0 = vec2(1.0/17.0, 0.0); - vec2 s1 = vec2(-1.0/29.0, 0.0); - vec2 s2 = vec2(1.0/101.0, 1.0/59.0); - vec2 s3 = vec2(-1.0/109.0, -1.0/57.0); - - // rotate sampling direction by specified angle - s0 = vec2((cosAngle * s0.x) - (sinAngle * s0.y), (sinAngle * s0.x) + (cosAngle * s0.y)); - s1 = vec2((cosAngle * s1.x) - (sinAngle * s1.y), (sinAngle * s1.x) + (cosAngle * s1.y)); - s2 = vec2((cosAngle * s2.x) - (sinAngle * s2.y), (sinAngle * s2.x) + (cosAngle * s2.y)); - s3 = vec2((cosAngle * s3.x) - (sinAngle * s3.y), (sinAngle * s3.x) + (cosAngle * s3.y)); - - vec2 uv0 = (uv/103.0) + (time * s0); - vec2 uv1 = uv/107.0 + (time * s1) + vec2(0.23); - vec2 uv2 = uv/vec2(897.0, 983.0) + (time * s2) + vec2(0.51); - vec2 uv3 = uv/vec2(991.0, 877.0) + (time * s3) + vec2(0.71); - - uv0 = fract(uv0); - uv1 = fract(uv1); - uv2 = fract(uv2); - uv3 = fract(uv3); - vec4 noise = (texture2D(normalMap, uv0)) + - (texture2D(normalMap, uv1)) + - (texture2D(normalMap, uv2)) + - (texture2D(normalMap, uv3)); - - // average and scale to between -1 and 1 - return ((noise / 4.0) - 0.5) * 2.0; -} - czm_material czm_getMaterial(czm_materialInput materialInput) { czm_material material = czm_getDefaultMaterial(materialInput); @@ -58,7 +23,7 @@ czm_material czm_getMaterial(czm_materialInput materialInput) float specularMapValue = texture2D(specularMap, materialInput.st).r; // note: not using directional motion at this time, just set the angle to 0.0; - vec4 noise = getNoise(materialInput.st * frequency, time, 0.0); + vec4 noise = czm_getWaterNoise(normalMap, materialInput.st * frequency, time, 0.0); vec3 normalTangentSpace = noise.xyz * vec3(1.0, 1.0, (1.0 / amplitude)); // fade out the normal perturbation as we move further from the water surface