node_voronoi_texture.osl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #include "stdosl.h"
  17. #include "node_texture.h"
  18. void voronoi_m(point p, string metric, float e, float da[4], point pa[4])
  19. {
  20. /* Compute the distance to and the position of the four closest neighbors to p.
  21. *
  22. * The neighbors are randomly placed, 1 each in a 3x3x3 grid (Worley pattern).
  23. * The distances and points are returned in ascending order, i.e. da[0] and pa[0] will
  24. * contain the distance to the closest point and its coordinates respectively.
  25. */
  26. int xx, yy, zz, xi, yi, zi;
  27. xi = (int)floor(p[0]);
  28. yi = (int)floor(p[1]);
  29. zi = (int)floor(p[2]);
  30. da[0] = 1e10;
  31. da[1] = 1e10;
  32. da[2] = 1e10;
  33. da[3] = 1e10;
  34. for (xx = xi - 1; xx <= xi + 1; xx++) {
  35. for (yy = yi - 1; yy <= yi + 1; yy++) {
  36. for (zz = zi - 1; zz <= zi + 1; zz++) {
  37. point ip = point(xx, yy, zz);
  38. point vp = (point)cellnoise_color(ip);
  39. point pd = p - (vp + ip);
  40. float d = 0.0;
  41. if (metric == "distance") {
  42. d = dot(pd, pd);
  43. }
  44. else if (metric == "manhattan") {
  45. d = fabs(pd[0]) + fabs(pd[1]) + fabs(pd[2]);
  46. }
  47. else if (metric == "chebychev") {
  48. d = max(fabs(pd[0]), max(fabs(pd[1]), fabs(pd[2])));
  49. }
  50. else if (metric == "minkowski") {
  51. d = pow(pow(fabs(pd[0]), e) + pow(fabs(pd[1]), e) + pow(fabs(pd[2]), e), 1.0 / e);
  52. }
  53. vp += point(xx, yy, zz);
  54. if (d < da[0]) {
  55. da[3] = da[2];
  56. da[2] = da[1];
  57. da[1] = da[0];
  58. da[0] = d;
  59. pa[3] = pa[2];
  60. pa[2] = pa[1];
  61. pa[1] = pa[0];
  62. pa[0] = vp;
  63. }
  64. else if (d < da[1]) {
  65. da[3] = da[2];
  66. da[2] = da[1];
  67. da[1] = d;
  68. pa[3] = pa[2];
  69. pa[2] = pa[1];
  70. pa[1] = vp;
  71. }
  72. else if (d < da[2]) {
  73. da[3] = da[2];
  74. da[2] = d;
  75. pa[3] = pa[2];
  76. pa[2] = vp;
  77. }
  78. else if (d < da[3]) {
  79. da[3] = d;
  80. pa[3] = vp;
  81. }
  82. }
  83. }
  84. }
  85. }
  86. /* Voronoi */
  87. shader node_voronoi_texture(
  88. int use_mapping = 0,
  89. matrix mapping = matrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  90. string coloring = "intensity",
  91. string metric = "distance",
  92. string feature = "F1",
  93. float Exponent = 1.0,
  94. float Scale = 5.0,
  95. point Vector = P,
  96. output float Fac = 0.0,
  97. output color Color = 0.0)
  98. {
  99. point p = Vector;
  100. if (use_mapping)
  101. p = transform(mapping, p);
  102. /* compute distance and point coordinate of 4 nearest neighbours */
  103. float da[4];
  104. point pa[4];
  105. /* compute distance and point coordinate of 4 nearest neighbours */
  106. voronoi_m(p * Scale, metric, Exponent, da, pa);
  107. if (coloring == "intensity") {
  108. /* Intensity output */
  109. if (feature == "F1") {
  110. Fac = fabs(da[0]);
  111. }
  112. else if (feature == "F2") {
  113. Fac = fabs(da[1]);
  114. }
  115. else if (feature == "F3") {
  116. Fac = fabs(da[2]);
  117. }
  118. else if (feature == "F4") {
  119. Fac = fabs(da[3]);
  120. }
  121. else if (feature == "F2F1") {
  122. Fac = fabs(da[1] - da[0]);
  123. }
  124. Color = color(Fac);
  125. }
  126. else {
  127. /* Color output */
  128. if (feature == "F1") {
  129. Color = pa[0];
  130. }
  131. else if (feature == "F2") {
  132. Color = pa[1];
  133. }
  134. else if (feature == "F3") {
  135. Color = pa[2];
  136. }
  137. else if (feature == "F4") {
  138. Color = pa[3];
  139. }
  140. else if (feature == "F2F1") {
  141. Color = fabs(pa[1] - pa[0]);
  142. }
  143. Color = cellnoise_color(Color);
  144. Fac = (Color[0] + Color[1] + Color[2]) * (1.0 / 3.0);
  145. }
  146. }