node_brick_texture.osl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /* Brick */
  19. float brick_noise(int ns) /* fast integer noise */
  20. {
  21. int nn;
  22. int n = (ns + 1013) & 2147483647;
  23. n = (n >> 13) ^ n;
  24. nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 2147483647;
  25. return 0.5 * ((float)nn / 1073741824.0);
  26. }
  27. float brick(point p,
  28. float mortar_size,
  29. float mortar_smooth,
  30. float bias,
  31. float BrickWidth,
  32. float row_height,
  33. float offset_amount,
  34. int offset_frequency,
  35. float squash_amount,
  36. int squash_frequency,
  37. output float tint)
  38. {
  39. int bricknum, rownum;
  40. float offset = 0.0;
  41. float brick_width = BrickWidth;
  42. float x, y;
  43. rownum = (int)floor(p[1] / row_height);
  44. if (offset_frequency && squash_frequency) {
  45. brick_width *= (rownum % squash_frequency) ? 1.0 : squash_amount; /* squash */
  46. offset = (rownum % offset_frequency) ? 0.0 : (brick_width * offset_amount); /* offset */
  47. }
  48. bricknum = (int)floor((p[0] + offset) / brick_width);
  49. x = (p[0] + offset) - brick_width * bricknum;
  50. y = p[1] - row_height * rownum;
  51. tint = clamp((brick_noise((rownum << 16) + (bricknum & 65535)) + bias), 0.0, 1.0);
  52. float min_dist = min(min(x, y), min(brick_width - x, row_height - y));
  53. if (min_dist >= mortar_size) {
  54. return 0.0;
  55. }
  56. else if (mortar_smooth == 0.0) {
  57. return 1.0;
  58. }
  59. else {
  60. min_dist = 1.0 - min_dist / mortar_size;
  61. return smoothstep(0.0, mortar_smooth, min_dist);
  62. }
  63. }
  64. shader node_brick_texture(int use_mapping = 0,
  65. matrix mapping = matrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
  66. float offset = 0.5,
  67. int offset_frequency = 2,
  68. float squash = 1.0,
  69. int squash_frequency = 1,
  70. point Vector = P,
  71. color Color1 = 0.2,
  72. color Color2 = 0.8,
  73. color Mortar = 0.0,
  74. float Scale = 5.0,
  75. float MortarSize = 0.02,
  76. float MortarSmooth = 0.0,
  77. float Bias = 0.0,
  78. float BrickWidth = 0.5,
  79. float RowHeight = 0.25,
  80. output float Fac = 0.0,
  81. output color Color = 0.2)
  82. {
  83. point p = Vector;
  84. if (use_mapping)
  85. p = transform(mapping, p);
  86. float tint = 0.0;
  87. color Col = Color1;
  88. Fac = brick(p * Scale,
  89. MortarSize,
  90. MortarSmooth,
  91. Bias,
  92. BrickWidth,
  93. RowHeight,
  94. offset,
  95. offset_frequency,
  96. squash,
  97. squash_frequency,
  98. tint);
  99. if (Fac != 1.0) {
  100. float facm = 1.0 - tint;
  101. Col = facm * Color1 + tint * Color2;
  102. }
  103. Color = mix(Col, Mortar, Fac);
  104. }