node_texture.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright 2011-2013 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* Voronoi / Worley like */
  17. color cellnoise_color(point p)
  18. {
  19. float r = cellnoise(p);
  20. float g = cellnoise(point(p[1], p[0], p[2]));
  21. float b = cellnoise(point(p[1], p[2], p[0]));
  22. return color(r, g, b);
  23. }
  24. void voronoi(point p, float e, float da[4], point pa[4])
  25. {
  26. /* returns distances in da and point coords in pa */
  27. int xx, yy, zz, xi, yi, zi;
  28. xi = (int)floor(p[0]);
  29. yi = (int)floor(p[1]);
  30. zi = (int)floor(p[2]);
  31. da[0] = 1e10;
  32. da[1] = 1e10;
  33. da[2] = 1e10;
  34. da[3] = 1e10;
  35. for (xx = xi - 1; xx <= xi + 1; xx++) {
  36. for (yy = yi - 1; yy <= yi + 1; yy++) {
  37. for (zz = zi - 1; zz <= zi + 1; zz++) {
  38. point ip = point(xx, yy, zz);
  39. point vp = (point)cellnoise_color(ip);
  40. point pd = p - (vp + ip);
  41. float d = dot(pd, pd);
  42. vp += point(xx, yy, zz);
  43. if (d < da[0]) {
  44. da[3] = da[2];
  45. da[2] = da[1];
  46. da[1] = da[0];
  47. da[0] = d;
  48. pa[3] = pa[2];
  49. pa[2] = pa[1];
  50. pa[1] = pa[0];
  51. pa[0] = vp;
  52. }
  53. else if (d < da[1]) {
  54. da[3] = da[2];
  55. da[2] = da[1];
  56. da[1] = d;
  57. pa[3] = pa[2];
  58. pa[2] = pa[1];
  59. pa[1] = vp;
  60. }
  61. else if (d < da[2]) {
  62. da[3] = da[2];
  63. da[2] = d;
  64. pa[3] = pa[2];
  65. pa[2] = vp;
  66. }
  67. else if (d < da[3]) {
  68. da[3] = d;
  69. pa[3] = vp;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. /* Noise Bases */
  76. float safe_noise(point p, string type)
  77. {
  78. float f = 0.0;
  79. /* Perlin noise in range -1..1 */
  80. if (type == "signed")
  81. f = noise("perlin", p);
  82. /* Perlin noise in range 0..1 */
  83. else
  84. f = noise(p);
  85. /* can happen for big coordinates, things even out to 0.5 then anyway */
  86. if (!isfinite(f))
  87. return 0.5;
  88. return f;
  89. }
  90. /* Turbulence */
  91. float noise_turbulence(point p, float details, int hard)
  92. {
  93. float fscale = 1.0;
  94. float amp = 1.0;
  95. float sum = 0.0;
  96. int i, n;
  97. float octaves = clamp(details, 0.0, 16.0);
  98. n = (int)octaves;
  99. for (i = 0; i <= n; i++) {
  100. float t = safe_noise(fscale * p, "unsigned");
  101. if (hard)
  102. t = fabs(2.0 * t - 1.0);
  103. sum += t * amp;
  104. amp *= 0.5;
  105. fscale *= 2.0;
  106. }
  107. float rmd = octaves - floor(octaves);
  108. if (rmd != 0.0) {
  109. float t = safe_noise(fscale * p, "unsigned");
  110. if (hard)
  111. t = fabs(2.0 * t - 1.0);
  112. float sum2 = sum + t * amp;
  113. sum *= ((float)(1 << n) / (float)((1 << (n + 1)) - 1));
  114. sum2 *= ((float)(1 << (n + 1)) / (float)((1 << (n + 2)) - 1));
  115. return (1.0 - rmd) * sum + rmd * sum2;
  116. }
  117. else {
  118. sum *= ((float)(1 << n) / (float)((1 << (n + 1)) - 1));
  119. return sum;
  120. }
  121. }
  122. /* Utility */
  123. float nonzero(float f, float eps)
  124. {
  125. float r;
  126. if (abs(f) < eps)
  127. r = sign(f) * eps;
  128. else
  129. r = f;
  130. return r;
  131. }