WoodGrain - 图1

    1. /*
    2. * WoodGrain.osl by Michel J. Anders (c)2012
    3. * from https://github.com/sambler/osl-shaders
    4. *
    5. * license: cc-by-sa
    6. *
    7. * original script from -
    8. * http://blenderthings.blogspot.com.au/2013/01/an-osl-wood-shader-for-blender-cycles.html
    9. * Modified by Saul Espinosa for Redshift 3D 2020-1-11
    10. */
    11. // for the original renderman shader, check http://www.larrygritz.com/arman/materials.html
    12. // adapted from larry gritz advanced renderman patterns.h
    13. float smoothpulse (float e0, float e1, float e2, float e3, float x)
    14. {
    15. return smoothstep(e0,e1,x) - smoothstep(e2,e3,x);
    16. }
    17. /* A pulse train of smoothsteps: a signal that repeats with a given
    18. * period, and is 0 when 0 <= mod(x/period,1) < edge, and 1 when
    19. * mod(x/period,1) > edge.
    20. */
    21. float smoothpulsetrain (float e0, float e1, float e2, float e3, float period, float x)
    22. {
    23. return smoothpulse (e0, e1, e2, e3, mod(x,period));
    24. }
    25. // adapted from larry gritz advanced renderman noises.h
    26. /* fractional Brownian motion
    27. * Inputs:
    28. * p position
    29. * octaves max # of octaves to calculate
    30. * lacunarity frequency spacing between successive octaves
    31. * gain scaling factor between successive octaves
    32. */
    33. /* A vector-valued antialiased fBm. */
    34. vector vfBm (point p, float octaves, float lacunarity, float gain)
    35. {
    36. float amp = 1;
    37. point pp = p;
    38. vector sum = 0;
    39. float i;
    40. for (i = 0; i < octaves; i += 1) {
    41. vector d = snoise(pp);
    42. sum += amp * d;
    43. amp *= gain;
    44. pp *= lacunarity;
    45. }
    46. return sum;
    47. }
    48. // adapted from larry gritz oak.sl and oak.h
    49. // original comments between /* ... */
    50. // my comments start with //
    51. // note that I dropped the whole filterwidth stuff, partly
    52. // because I don't think it necessary in Blender Cycles, partly
    53. // because the derivatives and area() function doesn't seem to work (yet)
    54. // all specialized snoise defines are replaced by snoise() function calls
    55. float oaktexture (point Pshad,
    56. float dPshad,
    57. float ringfreq,
    58. float ringunevenness,
    59. float grainfreq,
    60. float ringnoise,
    61. float ringnoisefreq,
    62. float trunkwobble,
    63. float trunkwobblefreq,
    64. float angularwobble,
    65. float angularwobblefreq,
    66. float ringy,
    67. float grainy)
    68. {
    69. /* We shade based on Pshad, but we add several layers of warping: */
    70. /* Some general warping of the domain */
    71. vector offset = vfBm(Pshad*ringnoisefreq, 2, 4, 0.5);
    72. point Pring = Pshad + ringnoise*offset;
    73. /* The trunk isn't totally steady xy as you go up in z */
    74. vector d = snoise(Pshad[2]*trunkwobblefreq) ;
    75. Pring += trunkwobble * d * vector(1,1,0);
    76. /* Calculate the radius from the center. */
    77. float r = hypot(Pring[0], Pring[1]) * ringfreq;
    78. /* Add some noise around the trunk */
    79. r += angularwobble * smoothstep(0,5,r)
    80. * snoise (angularwobblefreq*(Pring)*vector(1,1,0.1));
    81. /* Now add some noise so all rings are not equal width */
    82. r += ringunevenness*snoise(r);
    83. float inring = smoothpulsetrain (0.1, 0.55, 0.7, 0.95, 1, r);
    84. point Pgrain = Pshad*grainfreq*vector(1,1,0.05);
    85. float dPgrain = dPshad; //dropped filterwidthp(Pgrain);
    86. float grain = 0;
    87. float i, amp=1;
    88. for (i = 0; i < 2; i += 1) {
    89. float grain1valid = 1-smoothstep(0.2,0.6,dPgrain);
    90. if (grain1valid > 0) {
    91. float g = grain1valid * snoise (Pgrain);
    92. g *= (0.3 + 0.7*inring);
    93. g = pow(clamp(0.8 - (g),0,1),2);
    94. g = grainy * smoothstep (0.5, 1, g);
    95. if (i == 0)
    96. inring *= (1-0.4*grain1valid);
    97. grain = max (grain, g);
    98. }
    99. Pgrain *= 2;
    100. dPgrain *= 2;
    101. amp *= 0.5;
    102. }
    103. return mix (inring*ringy, 1, grain);
    104. }
    105. // larry gritz' original shader was a closure but this shader
    106. // provides different outputs that you can plug into your own
    107. // closures/shaders
    108. surface oak
    109. [[ string help = "Procedural Wood Grain Shader",
    110. string label = "Wood Grain" ]]
    111. (
    112. point Vector = P,
    113. color LightWood = color(0.5, 0.2, 0.067),
    114. color DarkWood = color(0.15, 0.077, 0.028),
    115. float Sharpness = 0.01
    116. [[
    117. float min = 0, float max = 1
    118. ]],
    119. float Ringy = 1.0
    120. [[
    121. float min = 0, float max = 5
    122. ]],
    123. float RingFreq = 8.0
    124. [[
    125. float min = 0, float max = 50
    126. ]],
    127. float RingUnevenness = 0.5
    128. [[
    129. float min = 0, float max = 50
    130. ]],
    131. float RingNoise = 0.02
    132. [[
    133. float min = 0, float max = 1
    134. ]],
    135. float RingNoiseFreq = 1.0
    136. [[
    137. float min = 0, float max = 1
    138. ]],
    139. float Grainy = 0.5
    140. [[
    141. float min = 0, float max = 1
    142. ]],
    143. float GrainFreq = 25.0
    144. [[
    145. float min = 0, float max = 50
    146. ]],
    147. float TrunkWobble = 0.15
    148. [[
    149. float min = 0, float max = 50
    150. ]],
    151. float TrunkWobbleFreq = 0.025
    152. [[
    153. float min = 0, float max = 1
    154. ]],
    155. float AngularWobble = 1.0
    156. [[
    157. float min = 0, float max = 1
    158. ]],
    159. float AngularWobbleFreq = 1.5
    160. [[
    161. float min = 0, float max = 5
    162. ]],
    163. output color Color = 0,
    164. output float Specular = 0.1,
    165. output float Roughness = 0.1,
    166. output float Displacement = 0.0 )
    167. {
    168. float wood = oaktexture (Vector, Sharpness, RingFreq, RingUnevenness, GrainFreq,
    169. RingNoise, RingNoiseFreq, TrunkWobble, TrunkWobbleFreq,
    170. AngularWobble, AngularWobbleFreq, Ringy, Grainy);
    171. Color = mix (LightWood, DarkWood, wood);
    172. Displacement = -wood; // lightwood = 0, darkwood is deeper/lower = -1
    173. Specular = 0.1*(1.0-0.5*wood); // darkwood is less specular
    174. Roughness = 0.1+0.1*wood; // and rougher
    175. }