opensimplex2.odin 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. OpenSimplex2 noise implementation.
  3. Ported from https://github.com/KdotJPG/OpenSimplex2.
  4. Copyright 2022 Yuki2 (https://github.com/NoahR02)
  5. */
  6. package math_noise
  7. /*
  8. Input coordinate vectors
  9. */
  10. Vec2 :: [2]f64
  11. Vec3 :: [3]f64
  12. Vec4 :: [4]f64
  13. /*
  14. Noise Evaluators
  15. */
  16. /*
  17. 2D Simplex noise, standard lattice orientation.
  18. */
  19. noise_2d :: proc(seed: i64, coord: Vec2) -> (value: f32) {
  20. // Get points for A2* lattice
  21. skew := SKEW_2D * (coord.x + coord.y)
  22. skewed := coord + skew
  23. return _internal_noise_2d_unskewed_base(seed, skewed)
  24. }
  25. /*
  26. 2D Simplex noise, with Y pointing down the main diagonal.
  27. Might be better for a 2D sandbox style game, where Y is vertical.
  28. Probably slightly less optimal for heightmaps or continent maps,
  29. unless your map is centered around an equator. It's a subtle
  30. difference, but the option is here to make it an easy choice.
  31. */
  32. noise_2d_improve_x :: proc(seed: i64, coord: Vec2) -> (value: f32) {
  33. // Skew transform and rotation baked into one.
  34. xx := coord.x * ROOT_2_OVER_2
  35. yy := coord.y * (ROOT_2_OVER_2 * (1 + 2 * SKEW_2D))
  36. return _internal_noise_2d_unskewed_base(seed, Vec2{yy + xx, yy - xx})
  37. }
  38. /*
  39. 3D OpenSimplex2 noise, with better visual isotropy in (X, Y).
  40. Recommended for 3D terrain and time-varied animations.
  41. The Z coordinate should always be the "different" coordinate in whatever your use case is.
  42. If Y is vertical in world coordinates, call `noise_3d_improve_xz(x, z, Y)` or use `noise_3d_xz_before_y`.
  43. If Z is vertical in world coordinates, call `noise_3d_improve_xz(x, y, Z)`.
  44. For a time varied animation, call `noise_3d_improve_xz(x, y, T)`.
  45. */
  46. noise_3d_improve_xy :: proc(seed: i64, coord: Vec3) -> (value: f32) {
  47. /*
  48. Re-orient the cubic lattices without skewing, so Z points up the main lattice diagonal,
  49. and the planes formed by XY are moved far out of alignment with the cube faces.
  50. Orthonormal rotation. Not a skew transform.
  51. */
  52. xy := coord.x + coord.y
  53. s2 := xy * ROTATE_3D_ORTHOGONALIZER
  54. zz := coord.z * ROOT_3_OVER_3
  55. r := Vec3{coord.x + s2 + zz, coord.y + s2 + zz, xy * -ROOT_3_OVER_3 + zz}
  56. // Evaluate both lattices to form a BCC lattice.
  57. return _internal_noise_3d_unrotated_base(seed, r)
  58. }
  59. /*
  60. 3D OpenSimplex2 noise, with better visual isotropy in (X, Z).
  61. Recommended for 3D terrain and time-varied animations.
  62. The Y coordinate should always be the "different" coordinate in whatever your use case is.
  63. If Y is vertical in world coordinates, call `noise_3d_improve_xz(x, Y, z)`.
  64. If Z is vertical in world coordinates, call `noise_3d_improve_xz(x, Z, y)` or use `noise_3d_improve_xy`.
  65. For a time varied animation, call `noise_3d_improve_xz(x, T, y)` or use `noise_3d_improve_xy`.
  66. */
  67. noise_3d_improve_xz :: proc(seed: i64, coord: Vec3) -> (value: f32) {
  68. /*
  69. Re-orient the cubic lattices without skewing, so Y points up the main lattice diagonal,
  70. and the planes formed by XZ are moved far out of alignment with the cube faces.
  71. Orthonormal rotation. Not a skew transform.
  72. */
  73. xz := coord.x + coord.z
  74. s2 := xz * ROTATE_3D_ORTHOGONALIZER
  75. yy := coord.y * ROOT_3_OVER_3
  76. r := Vec3{coord.x + s2 + yy, xz * -ROOT_3_OVER_3 + yy, coord.z + s2 + yy}
  77. // Evaluate both lattices to form a BCC lattice.
  78. return _internal_noise_3d_unrotated_base(seed, r)
  79. }
  80. /*
  81. 3D OpenSimplex2 noise, fallback rotation option
  82. Use `noise_3d_improve_xy` or `noise_3d_improve_xz` instead, wherever appropriate.
  83. They have less diagonal bias. This function's best use is as a fallback.
  84. */
  85. noise_3d_fallback :: proc(seed: i64, coord: Vec3) -> (value: f32) {
  86. /*
  87. Re-orient the cubic lattices via rotation, to produce a familiar look.
  88. Orthonormal rotation. Not a skew transform.
  89. */
  90. bias := FALLBACK_ROTATE_3D * (coord.x + coord.y + coord.z)
  91. biased := bias - coord
  92. // Evaluate both lattices to form a BCC lattice.
  93. return _internal_noise_3d_unrotated_base(seed, biased)
  94. }
  95. /*
  96. 4D OpenSimplex2 noise, with XYZ oriented like `noise_3d_improve_xy`
  97. and W for an extra degree of freedom. W repeats eventually.
  98. Recommended for time-varied animations which texture a 3D object (W=time)
  99. in a space where Z is vertical.
  100. */
  101. noise_4d_improve_xyz_improve_xy :: proc(seed: i64, coord: Vec4) -> (value: f32) {
  102. xy := coord.x + coord.y
  103. s2 := xy * -0.21132486540518699998
  104. zz := coord.z * 0.28867513459481294226
  105. ww := coord.w * 0.2236067977499788
  106. xr, yr : f64 = coord.x + (zz + ww + s2), coord.y + (zz + ww + s2)
  107. zr : f64 = xy * -0.57735026918962599998 + (zz + ww)
  108. wr : f64 = coord.z * -0.866025403784439 + ww
  109. return _internal_noise_4d_unskewed_base(seed, Vec4{xr, yr, zr, wr})
  110. }
  111. /*
  112. 4D OpenSimplex2 noise, with XYZ oriented like `noise_3d_improve_xz`
  113. and W for an extra degree of freedom. W repeats eventually.
  114. Recommended for time-varied animations which texture a 3D object (W=time)
  115. in a space where Y is vertical.
  116. */
  117. noise_4d_improve_xyz_improve_xz :: proc(seed: i64, coord: Vec4) -> (value: f32) {
  118. xz := coord.x + coord.z
  119. s2 := xz * -0.21132486540518699998
  120. yy := coord.y * 0.28867513459481294226
  121. ww := coord.w * 0.2236067977499788
  122. xr, zr : f64 = coord.x + (yy + ww + s2), coord.z + (yy + ww + s2)
  123. yr := xz * -0.57735026918962599998 + (yy + ww)
  124. wr := coord.y * -0.866025403784439 + ww
  125. return _internal_noise_4d_unskewed_base(seed, Vec4{xr, yr, zr, wr})
  126. }
  127. /*
  128. 4D OpenSimplex2 noise, with XYZ oriented like `noise_3d_fallback`
  129. and W for an extra degree of freedom. W repeats eventually.
  130. Recommended for time-varied animations which texture a 3D object (W=time)
  131. where there isn't a clear distinction between horizontal and vertical
  132. */
  133. noise_4d_improve_xyz :: proc(seed: i64, coord: Vec4) -> (value: f32) {
  134. xyz := coord.x + coord.y + coord.z
  135. ww := coord.w * 0.2236067977499788
  136. s2 := xyz * -0.16666666666666666 + ww
  137. skewed := Vec4{coord.x + s2, coord.y + s2, coord.z + s2, -0.5 * xyz + ww}
  138. return _internal_noise_4d_unskewed_base(seed, skewed)
  139. }
  140. /*
  141. 4D OpenSimplex2 noise, fallback lattice orientation.
  142. */
  143. noise_4d_fallback :: proc(seed: i64, coord: Vec4) -> (value: f32) {
  144. // Get points for A4 lattice
  145. skew := f64(SKEW_4D) * (coord.x + coord.y + coord.z + coord.w)
  146. return _internal_noise_4d_unskewed_base(seed, coord + skew)
  147. }