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