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