CloudNoiseFS.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "uniform vec3 u_noiseTextureDimensions;\n\
  3. uniform float u_noiseDetail;\n\
  4. uniform vec3 u_noiseOffset;\n\
  5. varying vec2 v_position;\n\
  6. \n\
  7. float textureSliceWidth = u_noiseTextureDimensions.x;\n\
  8. float inverseNoiseTextureRows = u_noiseTextureDimensions.z;\n\
  9. \n\
  10. float wrap(float value, float rangeLength) {\n\
  11. if(value < 0.0) {\n\
  12. float absValue = abs(value);\n\
  13. float modValue = mod(absValue, rangeLength);\n\
  14. return mod(rangeLength - modValue, rangeLength);\n\
  15. }\n\
  16. return mod(value, rangeLength);\n\
  17. }\n\
  18. \n\
  19. vec3 wrapVec(vec3 value, float rangeLength) {\n\
  20. return vec3(wrap(value.x, rangeLength),\n\
  21. wrap(value.y, rangeLength),\n\
  22. wrap(value.z, rangeLength));\n\
  23. }\n\
  24. \n\
  25. vec3 random3(vec3 p) {\n\
  26. float dot1 = dot(p, vec3(127.1, 311.7, 932.8));\n\
  27. float dot2 = dot(p, vec3(269.5, 183.3, 421.4));\n\
  28. return fract(vec3(sin(dot1 - dot2), cos(dot1 * dot2), dot1 * dot2));\n\
  29. }\n\
  30. \n\
  31. // Frequency corresponds to cell size.\n\
  32. // The higher the frequency, the smaller the cell size.\n\
  33. vec3 getWorleyCellPoint(vec3 centerCell, vec3 offset, float freq) {\n\
  34. vec3 cell = centerCell + offset;\n\
  35. cell = wrapVec(cell, textureSliceWidth / u_noiseDetail);\n\
  36. cell += floor(u_noiseOffset / u_noiseDetail);\n\
  37. vec3 p = offset + random3(cell);\n\
  38. return p;\n\
  39. }\n\
  40. \n\
  41. float worleyNoise(vec3 p, float freq) {\n\
  42. vec3 centerCell = floor(p * freq);\n\
  43. vec3 pointInCell = fract(p * freq);\n\
  44. float shortestDistance = 1000.0;\n\
  45. \n\
  46. for(float z = -1.0; z <= 1.0; z++) {\n\
  47. for(float y = -1.0; y <= 1.0; y++) {\n\
  48. for(float x = -1.0; x <= 1.0; x++) {\n\
  49. vec3 offset = vec3(x, y, z);\n\
  50. vec3 point = getWorleyCellPoint(centerCell, offset, freq);\n\
  51. \n\
  52. float distance = length(pointInCell - point);\n\
  53. if(distance < shortestDistance) {\n\
  54. shortestDistance = distance;\n\
  55. }\n\
  56. }\n\
  57. }\n\
  58. }\n\
  59. \n\
  60. return shortestDistance;\n\
  61. }\n\
  62. \n\
  63. const float MAX_FBM_ITERATIONS = 10.0;\n\
  64. \n\
  65. float worleyFBMNoise(vec3 p, float octaves, float scale) {\n\
  66. float noise = 0.0;\n\
  67. float freq = 1.0;\n\
  68. float persistence = 0.625;\n\
  69. for(float i = 0.0; i < MAX_FBM_ITERATIONS; i++) {\n\
  70. if(i >= octaves) {\n\
  71. break;\n\
  72. }\n\
  73. \n\
  74. noise += worleyNoise(p * scale, freq * scale) * persistence;\n\
  75. persistence *= 0.5;\n\
  76. freq *= 2.0;\n\
  77. }\n\
  78. return noise;\n\
  79. }\n\
  80. \n\
  81. void main() {\n\
  82. float x = mod(v_position.x, textureSliceWidth);\n\
  83. float y = mod(v_position.y, textureSliceWidth);\n\
  84. float sliceRow = floor(v_position.y / textureSliceWidth);\n\
  85. float z = floor(v_position.x / textureSliceWidth) + sliceRow * inverseNoiseTextureRows * textureSliceWidth;\n\
  86. \n\
  87. vec3 position = vec3(x, y, z);\n\
  88. position /= u_noiseDetail;\n\
  89. float worley0 = clamp(worleyFBMNoise(position, 3.0, 1.0), 0.0, 1.0);\n\
  90. float worley1 = clamp(worleyFBMNoise(position, 3.0, 2.0), 0.0, 1.0);\n\
  91. float worley2 = clamp(worleyFBMNoise(position, 3.0, 3.0), 0.0, 1.0);\n\
  92. gl_FragColor = vec4(worley0, worley1, worley2, 1.0);\n\
  93. }\n\
  94. ";