Water.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "// Thanks for the contribution Jonas\n\
  3. // http://29a.ch/2012/7/19/webgl-terrain-rendering-water-fog\n\
  4. \n\
  5. uniform sampler2D specularMap;\n\
  6. uniform sampler2D normalMap;\n\
  7. uniform vec4 baseWaterColor;\n\
  8. uniform vec4 blendColor;\n\
  9. uniform float frequency;\n\
  10. uniform float animationSpeed;\n\
  11. uniform float amplitude;\n\
  12. uniform float specularIntensity;\n\
  13. uniform float fadeFactor;\n\
  14. \n\
  15. czm_material czm_getMaterial(czm_materialInput materialInput)\n\
  16. {\n\
  17. czm_material material = czm_getDefaultMaterial(materialInput);\n\
  18. \n\
  19. float time = czm_frameNumber * animationSpeed;\n\
  20. \n\
  21. // fade is a function of the distance from the fragment and the frequency of the waves\n\
  22. float fade = max(1.0, (length(materialInput.positionToEyeEC) / 10000000000.0) * frequency * fadeFactor);\n\
  23. \n\
  24. float specularMapValue = texture2D(specularMap, materialInput.st).r;\n\
  25. \n\
  26. // note: not using directional motion at this time, just set the angle to 0.0;\n\
  27. vec4 noise = czm_getWaterNoise(normalMap, materialInput.st * frequency, time, 0.0);\n\
  28. vec3 normalTangentSpace = noise.xyz * vec3(1.0, 1.0, (1.0 / amplitude));\n\
  29. \n\
  30. // fade out the normal perturbation as we move further from the water surface\n\
  31. normalTangentSpace.xy /= fade;\n\
  32. \n\
  33. // attempt to fade out the normal perturbation as we approach non water areas (low specular map value)\n\
  34. normalTangentSpace = mix(vec3(0.0, 0.0, 50.0), normalTangentSpace, specularMapValue);\n\
  35. \n\
  36. normalTangentSpace = normalize(normalTangentSpace);\n\
  37. \n\
  38. // get ratios for alignment of the new normal vector with a vector perpendicular to the tangent plane\n\
  39. float tsPerturbationRatio = clamp(dot(normalTangentSpace, vec3(0.0, 0.0, 1.0)), 0.0, 1.0);\n\
  40. \n\
  41. // fade out water effect as specular map value decreases\n\
  42. material.alpha = mix(blendColor.a, baseWaterColor.a, specularMapValue) * specularMapValue;\n\
  43. \n\
  44. // base color is a blend of the water and non-water color based on the value from the specular map\n\
  45. // may need a uniform blend factor to better control this\n\
  46. material.diffuse = mix(blendColor.rgb, baseWaterColor.rgb, specularMapValue);\n\
  47. \n\
  48. // diffuse highlights are based on how perturbed the normal is\n\
  49. material.diffuse += (0.1 * tsPerturbationRatio);\n\
  50. \n\
  51. material.diffuse = material.diffuse;\n\
  52. \n\
  53. material.normal = normalize(materialInput.tangentToEyeMatrix * normalTangentSpace);\n\
  54. \n\
  55. material.specular = specularIntensity;\n\
  56. material.shininess = 10.0;\n\
  57. \n\
  58. return material;\n\
  59. }\n\
  60. ";