1. // A simple Halftone shader
    2. // Halftone.osl, by Zap Andersson
    3. // Modified: 2019-11-25
    4. // Modified: 202-1-11 by Saul Espinosa for Redshift 3D
    5. // Copyright 2019 Autodesk Inc, All rights reserved. This file is licensed under Apache 2.0 license
    6. // https://github.com/ADN-DevTech/3dsMax-OSL-Shaders/blob/master/LICENSE.txt
    7. shader HalftoneDots
    8. [[ string help="Halftone Dots (by default i screen space)<br>"
    9. "Works well together with toon shaders.",
    10. string category = "Textures",
    11. string label = "Halftone Dots" ]]
    12. (
    13. // Inputs
    14. vector UVW = transform("raster", P)
    15. [[ string help = "The input coordinate. If not connected, uses screen pixel space" ]],
    16. vector Scale = vector(8.0,8.0,8.0)
    17. [[ string help = "This size of the dots. In the default screen space mapping, this is in pixels." ]],
    18. float Angle = 45.0
    19. [[ string help = "Angle of the Halftoning" ,
    20. float min = -180, float max = 180
    21. ]],
    22. color InputValue = 0.25
    23. [[ string help = "The input value defining the size of the dots to yield the right halftone density. " ]],
    24. int U_Input = 0
    25. [[ string help = "If checked, adds the U coordinate as the input value. This works "
    26. "well when connected in a 'base_tonemap' input of an Arnold Toon shader.",
    27. string widget = "checkBox" ]],
    28. float Fuzz = 0.1 [[ float min = 0.0, float max = 2.0 ]],
    29. color BlackDots = 0.0
    30. [[ string help = "The color of the dots" ]],
    31. color WhiteDots = 1.0
    32. [[ string help = "The color of the space between the dots" ]],
    33. // Outputs
    34. output color Out = 0.0
    35. )
    36. {
    37. vector pos = rotate(UVW, radians(Angle), 0.0, vector(0.0,0.0,1.0));
    38. pos = vector(pos[0] / Scale[0], pos[1] / Scale[1], 0.0);
    39. if (Scale[0] == 0.0)
    40. pos[0] = 0.5;
    41. if (Scale[1] == 0.0)
    42. pos[1] = 0.5;
    43. color inputV = InputValue;
    44. if (U_Input)
    45. inputV += u;
    46. pos = pos - floor(pos);
    47. float dist = 1.0 - (distance(vector(0.5,0.5,0.0), pos) / (sqrt(2.0) / 2.5));
    48. color factor = smoothstep(-Fuzz, 0.0, inputV - sqrt(max(0,dist)));
    49. Out = mix(BlackDots, WhiteDots, factor);
    50. }