Dots - 图1

    1. // Random circles (or spheres, really) with outputs
    2. // for bump, random factor, etc. Can be made to make
    3. // very cool random candy cover, paint flakes...
    4. //
    5. // Dots.osl, by Zap Andersson
    6. // Modified: 2019-11-10
    7. // Copyright 2019 Autodesk Inc, All rights reserved. This file is licensed under Apache 2.0 license
    8. // https://github.com/ADN-DevTech/3dsMax-OSL-Shaders/blob/master/LICENSE.txt
    9. // Modified: 2020-1-15 by Saul Espinosa for Redshift 3D
    10. shader Candy
    11. [[ string help="Random circles with random colors and a tunable bump output. For random dots or candy sprinkles.",
    12. string category = "Textures",
    13. string label = "Random Dots"
    14. ]]
    15. (
    16. // For convenience, default to object space
    17. // You can connect any other coordinate space
    18. // as input anyway...
    19. point UVW = transform("object", P) [[ string help = "The position input, defaulting to object space. Connect an UVW shader for other alternatives." ]],
    20. float Scale = 0.01 [[float min = 0, float max = 25]],
    21. float Radius = 0.5 [[ float min=0.0, float max=2.0 ]],
    22. int RandomOverlap = 0 [[ string widget = "checkBox" ]],
    23. float BumpAmount = 1.0 [[float min = 0, float max = 5]],
    24. float BumpShape = 1.0 [[float min = 0, float max = 25]],
    25. output color Col = 0,
    26. output float Fac = 0,
    27. output float Bump = 0,
    28. output float Dist = 0,
    29. output float Rnd = 0
    30. )
    31. {
    32. point pnt = UVW / Scale;
    33. float pri = -1;
    34. // Standard method for randomization:
    35. // Go through a 3x3x3 grid that we offset
    36. for (float x = -1; x <= 1; x++)
    37. {
    38. for (float y = -1; y <= 1; y++)
    39. {
    40. for(float z = -1; z <= 1; z++)
    41. {
    42. // Point that sources all our randomization
    43. // We run these through cell noises to get
    44. // random values
    45. // MAXX-44914 fix: Adds the 0.001 offset and floor()
    46. point rndpoint = floor(pnt) + point(x, y, z) + 0.001;
    47. // Compute a center
    48. point dotcenter = rndpoint + noise("cell", rndpoint, 1);
    49. float dist = distance(dotcenter, pnt);
    50. // Randomize the priority, all they all look "stacked" in
    51. // the same direction if they overlap
    52. float priority = noise("cell", rndpoint, 2);
    53. // If within the radius, and priority is higher
    54. if (dist < Radius && priority > pri)
    55. {
    56. pri = priority;
    57. Col = noise("cell", rndpoint, 3);
    58. Dist = dist / Radius;
    59. Bump = pow(sin((1.0 - Dist) * 1.58), BumpShape) * BumpAmount;
    60. Fac = 1.0;
    61. Rnd = priority;
    62. // If we exit early, shader is more efficient,
    63. // but any "overlap" becomes completely sorted
    64. // in an X/Y/Z order and looks fake.
    65. if (!RandomOverlap)
    66. return;
    67. }
    68. }
    69. }
    70. }
    71. }