DiffractionGrating - 图1

    1. // Modified 3/03/22 by Saul Espinosa for Redshift3d
    2. // Psuedo Diffraction Grating Uber Shader using 3 shifted RGB Microfacets
    3. shader DiffractionGrating
    4. [[ string help = "Psuedo Diffraction Grating Material",
    5. string label = "Diffraction Grating" ]]
    6. (
    7. color diffuse_color = 0.025
    8. [[
    9. string page = "Diffuse",
    10. string label = "Diffuse Color"
    11. ]],
    12. float diffuse_weight = 1.0
    13. [[
    14. string label = "Diffuse Weight" ,
    15. string page = "Diffuse",
    16. float min = 0, float max = 1
    17. ]],
    18. float diffuse_roughness = 0.0
    19. [[
    20. string label = "Diffuse Roughness" ,
    21. string page = "Diffuse",
    22. float min = 0, float max = 1
    23. ]],
    24. // Refraction Layer
    25. color refract_color = color(1.0)
    26. [[
    27. string label = "Refraction Color",
    28. string page = "Refraction",
    29. ]],
    30. float refract_weight = 0.0
    31. [[
    32. string label = "Refraction Weight" ,
    33. string page = "Refraction",
    34. float min = 0, float max = 1
    35. ]],
    36. float refract_roughness = 0.0
    37. [[
    38. string label = "Refraction Roughness" ,
    39. string page = "Refraction",
    40. float min = 0, float max = 1
    41. ]],
    42. float refract_IOR = 1.5
    43. [[
    44. string label = "Refraction IOR" ,
    45. string page = "Refraction",
    46. float min = 0, float max = 25
    47. ]],
    48. // Diffraction
    49. float diffraction_weight = 1.0
    50. [[
    51. string label = "Diffraction Weight" ,
    52. string page = "Diffraction",
    53. float min = 0, float max = 1
    54. ]],
    55. float roughness = 0.5
    56. [[
    57. string label = "Roughness" ,
    58. string page = "Diffraction",
    59. float min = 0, float max = 1
    60. ]],
    61. float separation = 0.9
    62. [[
    63. string label = "Separation" ,
    64. string page = "Diffraction",
    65. float min = 0, float max = 1
    66. ]],
    67. float anisotropy = 0.45
    68. [[
    69. string label = "Anisotropy" ,
    70. string page = "Diffraction",
    71. float min = -1, float max = 1
    72. ]],
    73. float rotation = 0.3
    74. [[
    75. string label = "Rotation" ,
    76. string page = "Diffraction",
    77. float min = 0, float max = 1
    78. ]],
    79. float IOR = 1.6
    80. [[
    81. string page = "Diffraction",
    82. string label = "Diffraction IOR",
    83. float min = 1,
    84. float max = 5
    85. ]],
    86. // Coating Layer
    87. color coat_color = color(1.0)
    88. [[
    89. string label = "Coat Color",
    90. string page = "Coat",
    91. ]],
    92. float coat_weight = 0.0
    93. [[
    94. string label = "Coat Weight" ,
    95. string page = "Coat",
    96. float min = 0, float max = 1
    97. ]],
    98. float coat_roughness = 0.0
    99. [[
    100. string label = "Coat Roughness" ,
    101. string page = "Coat",
    102. float min = 0, float max = 1
    103. ]],
    104. float coat_IOR = 1.52
    105. [[
    106. string label = "Coat IOR" ,
    107. string page = "Coat",
    108. float min = 0, float max = 25
    109. ]],
    110. normal Tangent = normalize(dPdu),
    111. // [[
    112. // string widget = "null"
    113. // ]],
    114. output closure color outColor = 0.0
    115. )
    116. {
    117. vector Normal = normalize(N);
    118. vector T = Tangent;
    119. float a = (anisotropy + 1.0) / 2.0;
    120. float delta = mix(roughness * separation, 0.001, abs(anisotropy));
    121. vector rot = rotate(T, (rotation * M_2PI), Normal);
    122. float x_alpha = mix(0.001, roughness * 2, a);
    123. float y_alpha = mix(roughness * 2, 0.001, a);
    124. closure color diffuseBRDF = diffuse_color * diffuse_weight * oren_nayar(Normal, diffuse_roughness);
    125. closure color refractionBSDF = refract_color * refract_weight * microfacet("ggx", Normal, refract_roughness, refract_IOR, 1);
    126. closure color microfacetR = microfacet("ggx", Normal, delta - rot, x_alpha, y_alpha, IOR, 0) * color(1,0,0);
    127. closure color microfacetG = microfacet("ggx", Normal, rot, x_alpha, y_alpha, IOR, 0) * color(0,1,0);
    128. closure color microfacetB = microfacet("ggx", Normal, delta + rot, x_alpha, y_alpha, IOR, 0) * color(0,0,1);
    129. closure color coat_layer = coat_color * coat_weight * microfacet("ggx", Normal, coat_roughness, coat_IOR, 0);
    130. outColor = diffuseBRDF + refractionBSDF + (diffraction_weight * (microfacetR + microfacetG + microfacetB)) + coat_layer;
    131. }