Water.js 2.6 KB

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