Flakes - 图1

    1. // Make glint reflections, ice, snow, and car shaders for example.
    2. // Flakes.osl, by Mads Drøschler
    3. // Modified: 2021-02-31
    4. // License MIT Copyright Mads Drøschler
    5. // https://github.com/gkmotu/OSL-Shaders
    6. // Modified: 2021-03-01 by Saul Espinosa for Redshift 3D. Added Metadata + Mapper Menu for Coordinate Space
    7. vector TileHash ( output vector x, int tileSize ) {
    8. x = mod(x,tileSize);
    9. return noise("hash",x);
    10. }
    11. vector ID(vector x, int seed,int Tiling,int tileSize)
    12. {
    13. vector n = floor(x);
    14. vector f = x-floor(x);
    15. vector m = vector(8);
    16. vector o;
    17. for( int k=-1; k<=1; k++ ){
    18. for( int j=-1; j<=1; j++ ){
    19. for( int i=-1; i<=1; i++ ){
    20. vector g = vector( float(i), float(j),float(k) );
    21. Tiling? o = TileHash(n+g,tileSize) : o = noise("hash",n+g,seed);
    22. vector r = g - f + (0.5+0.5*sin(M_2PI*o));
    23. float s = dot(r, r);
    24. s<m[0] ? m = vector(s, o[0], o[1]):0;
    25. }}}
    26. return vector( sqrt(m[0]), m[1]+m[2], m[1]+m[0]);
    27. }
    28. float IDs ( output float x, int seed )
    29. {
    30. x = -1000 + x * 2000;
    31. x = int(trunc(x));
    32. float x_ratio = noise("cell", vector(abs(x), abs(seed), 11));
    33. return mix(0, 1, x_ratio);
    34. }
    35. shader Flakes
    36. [[
    37. string help = "Use the Flakes shader to create small glints in surfaces",
    38. string label = "Flakes"
    39. ]]
    40. (
    41. // Inputs
    42. float scale = 0.2
    43. [[
    44. int connectable = 0,
    45. float min = 0.0,
    46. float max = 25.0,
    47. string help = "<ul><br><li>Scale the IDs up and down. ( This is disabled when Seamless Tiling is enabled )<br></li>"
    48. "<li>Use the Tile Size instead when Seamless Tiling is enabled.</li></ul>"
    49. ]],
    50. float Density = 1.0
    51. [[
    52. int connectable = 0,
    53. float min = 0.0,
    54. float max = 1.0,
    55. string help = "<ul><br><li>0.0 will print 0% IDs<br></li>"
    56. "<li>0.5 will print 50% of the IDs<br></li>"
    57. "<li>1.0 will cover the whole surface with ID's</li></ul>"
    58. ]],
    59. float Randomize = 0.5
    60. [[
    61. int connectable = 0,
    62. float min = 0.0,
    63. float max = 1.0,
    64. string help = "<ul><br><br>A value between 0-1 that will rotate the IDs<br>"
    65. "<li>If value is 0, the IDs will point straight up from the surface ( no glints )<br></li>"
    66. "<li>If value is 0.5, the IDs will be rotated randomly around 180 deg. ( mild dispersion )<br></li>"
    67. "<li>If value is 1, the IDs will be rotated randomly around 360 deg.( Very scattered dispersion )</li></ul>"
    68. ]],
    69. int seed = 42
    70. [[
    71. int connectable = 0,
    72. int min = 0,
    73. int max = 100,
    74. string help = "<br><br>Reshuffle the layout of the IDs"
    75. ]],
    76. int Tiling = 0
    77. [[
    78. string widget = "checkBox",
    79. string label = "Seamless Tiling",
    80. int connectable = 0,
    81. string help = "<ul><br><br><li>Plug the Flake Shader directly into the Physical Materials diffuse slot</li>"
    82. "<br><li>Make a square plane, 100x100/250x250/etc.</li>"
    83. "<br><li>Assign the material to the plane.</li>"
    84. "<br><li>Enable Seamless Tiling.</li>"
    85. "<br><li>Adjust the size of the texure details with the Tile Size parameter.</li>"
    86. "<br><li>Bake an Arnold Albedo texture to .exr and convert it to .tx with mipmapping enabled, for optimal performance.</li><br>"
    87. "<br>You now have a Seamless Tiling OSL Flake bitmap.</ul>"
    88. ]],
    89. int tileSize = 10
    90. [[
    91. int min = 10,
    92. int max = 50,
    93. int connectable = 0,
    94. string help = "<br><br>Increase value to sample more IDs on the seamless tile"
    95. ]],
    96. int CSpace = 0
    97. [[
    98. string label = "Coord Space",
    99. string widget = "mapper",
    100. string options = "object:0|world:1|UV:3",
    101. string help = "<ul><br><li>Object</li><br>"
    102. "<li>World</li><br>"
    103. "<li>UV (Texture)</li></ul>"
    104. ]],
    105. // Output
    106. output normal Out = 0.0,
    107. output float A = 0,
    108. )
    109. {
    110. // Coord Space
    111. string mode;
    112. point pos,s = P;
    113. vector t = vector(u,v,0);
    114. CSpace == 0 ? mode = "object" : mode = "world";
    115. CSpace == 2 ? pos = transform("object",t) : pos = transform(mode,s);
    116. Tiling ? pos = transform("object",t):0;
    117. // Scale
    118. !Tiling ? pos = pos : pos = pos*tileSize;
    119. float sc = scale;
    120. !Tiling ? sc = sc : sc = 1.0;
    121. // Point
    122. point p = (pos)/sc;
    123. // Base Normal
    124. vector Base_Normal = vector(0.5,0.5,1.0);
    125. // Fetch global ID
    126. vector k = ID( p,seed,Tiling,tileSize );
    127. // Turn
    128. float x = 0.5 + 0.5 * cos( k[1]*M_2PI );
    129. float y = 0.5 + 0.5 * sin( k[1]*M_2PI );
    130. float z = 0.5 + 0.5 * tan( k[1]*M_2PI );
    131. // Normalize
    132. Out = normalize(vector(x,y,z)*2-1)*0.5+0.5;
    133. // Fetch xyz IDs
    134. Out[0] = IDs(Out[0],4);
    135. Out[1] = IDs(Out[1],4);
    136. Out[2] = IDs(Out[2],1);
    137. // Fit z to 0.5 - 1.0
    138. Out[2] = 0.5 + 0.5 *Out[2];
    139. // Density
    140. float h = -1000 + Out[0]*2000;
    141. h = int(trunc(h));
    142. float Rehash = noise("cell", vector(abs(h), abs(16), 11));
    143. Rehash = mix(0, 1, Rehash);
    144. Density > Rehash ? Out = Out : Out = Base_Normal;
    145. // A
    146. Out[0] > 0.5 or Out[0] < 0.5 ? A = 1 : A = 0;
    147. // Randomize
    148. Out = mix(Base_Normal,Out,Randomize);
    149. //Out = Out*2-1;
    150. }