-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathDiffraction.osl
More file actions
279 lines (257 loc) · 9.35 KB
/
Diffraction.osl
File metadata and controls
279 lines (257 loc) · 9.35 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Diffraction Grating Shader - Emulate diffraction based on a grating pattern
/*
Copyright (c) 2025 Edward Darby Edelen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
float normal_sine(vector NN) {
float cosine = dot(I, NN);
return sqrt(1.0 - cosine * cosine);
}
float wavelength_stratified(float w_min, float w_max, int order, int strata) {
float range = w_max - w_min;
float index = floor(noise("hash", P, time + order * M_SQRT2) * strata);
float width = range / strata;
float rnd = noise("hash", P, time + order * M_PI + index);
return w_min + width * (index + rnd);
}
shader DiffractionGrating
(
color base_color = 0.8
[[
string label = "Color",
string page = "Base",
]],
float base_weight = 1.0
[[
string label = "Weight",
string page = "Base",
float min = 0,
float max = 1
]],
float base_rough = 0.0
[[
string label = "Roughness",
string page = "Base",
float min = 0,
float max = 1
]],
float base_metal = 1.0
[[
string label = "Metalness",
string page = "Base",
float min = 0,
float max = 1
]],
float microfacet_weight = 1
[[
string label = "Weight",
string page = "Microfacet",
float min = 0,
float max = 1
]],
float refraction_weight = 0.0
[[
string label = "Refraction",
string page = "Microfacet",
float min = 0,
float max = 1
]],
float microfacet_roughness = 0.2
[[
string label = "Roughness",
string page = "Microfacet",
float min = 0,
float max = 1
]],
float microfacet_anisotropy = 0.5
[[
string label = "Anisotropy",
string page = "Microfacet",
float min = -1,
float max = 1
]],
float microfacet_rotation = 0.0
[[
string label = "Anisotropy Rotation",
string page = "Microfacet",
float min = 0,
float max = 1
]],
float IOR=1.6
[[
string label = "IOR",
string page = "Microfacet",
float slidermin = 0,
float slidermax = 5,
float min = 0,
float max = 50
]],
color diffraction_color = 1.0
[[
string label = "Color",
string page = "Diffraction"
]],
float diffraction_weight = 1.0
[[
string label = "Weight",
string page = "Diffraction",
float min = 0,
float max = 1
]],
int diffraction_orders = 6
[[
string label = "Orders",
string page = "Diffraction",
int min = 1,
int max = 12
]],
float diffraction_extra_roughness = 0.2
[[
string label = "Extra Roughness",
string page = "Diffraction",
float min = -1,
float max = 1
]],
float diffraction_roughness_falloff = -0.5
[[
string label = "Roughness Falloff",
string page = "Diffraction",
float min = -1,
float max = 1
]],
float diffraction_falloff = 0.0
[[
string label = "Falloff",
string page = "Diffraction",
float min = 0,
float max = 1
]],
float Distance = 1200
[[
string label = "Distance",
string page = "Diffraction",
float min = 200,
float max = 8000
]],
float grating_width = 0.1
[[
string label = "Fill Ratio",
string page = "Diffraction",
float min = 0.01,
float max = 1
]],
float rotation = 0.0
[[
string label = "Rotation",
string page = "Diffraction",
float min = 0,
float max = 1
]],
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.005
[[
string label = "Coat Roughness" ,
string page = "Coat",
float min = 0, float max = 1
]],
float coat_IOR = 1.33
[[
string label = "Coat IOR" ,
string page = "Coat",
float slidermin = 0, float slidermax = 5,
float min = 0, float max = 50
]],
normal Normal = N
[[
string label = "Normal",
string page = "Extra"
]],
normal Tangent = normalize(dPdu)
[[
string label = "Tangent",
string page = "Extra"
]],
output closure color BRDF = 0
)
{
closure color refl = 0;
closure color refr = 0;
closure color metal = 0;
closure color zero = 0;
float epsilon = 0.000001;
// Change the angle of the diffraction grating
normal Tg = rotate(Tangent, rotation*M_2PI, Normal);
normal Ta = rotate(Tangent, (rotation + microfacet_rotation)*M_2PI, Normal);
float base_ior = mix(IOR, IOR / coat_IOR, coat_weight);
base_ior = select(1 / base_ior, base_ior, int(base_ior));
float mr = pow(microfacet_roughness, 2);
float cr = pow(coat_roughness, 2);
float dr = pow(clamp(microfacet_roughness + diffraction_extra_roughness, 0, 1), 2);
float base_roughness = mix(mr, pow(min(1, pow(mr,4) + 2*pow(cr, 4)), 0.25), coat_weight);
vector roughness = vector(base_roughness, dr, 0);
for(int m = -diffraction_orders; m <= diffraction_orders; m++){
color Ct = color(1);
normal Nw = Normal;
float w = wavelength_stratified(300, 800, m, 5);
float intensity = 1.0;
// Calculate diffraction
if(m != 0){
float sine = normal_sine(Normal);
float sine_offset = sine + m * w / Distance;
// No real solution, continue to next order
if(abs(sine_offset) > 1) continue;
float beta = (M_PI * Distance * grating_width * sine) / w;
Nw = rotate(Normal, 0.5 * (asin(sine_offset) - asin(sine)), Tg);
Ct = wavelength_color(w) * diffraction_color * diffraction_weight;
// Reduce intensity of higher orders based on width of slits and falloff
intensity = select(1.0, pow(sin(beta)/beta, 2), beta) * pow(1 - diffraction_falloff, (abs(m) - 1));
metal += (intensity * Ct) * microfacet("ggx", Nw, roughness.y * pow(1 - diffraction_roughness_falloff, (abs(m) - 1)), 0, 0);
refl += (intensity * Ct) * microfacet("ggx", Nw, roughness.y * pow(1 - diffraction_roughness_falloff, (abs(m) - 1)), base_ior, 0);
refr += (intensity * Ct) * microfacet("beckmann", Nw, roughness.y * pow(1 - diffraction_roughness_falloff, (abs(m) - 1)), base_ior, 1);
}
else{
float alpha_x = mix(2 * roughness.x, 0, 0.5 * (clamp(microfacet_anisotropy, epsilon - 1, 1 - epsilon) + 1));
float alpha_y = mix(0, 2 * roughness.x, 0.5 * (clamp(microfacet_anisotropy, epsilon - 1, 1 - epsilon) + 1));
metal += mix(zero, (intensity * Ct) * microfacet("ggx", Nw, Ta, alpha_x, alpha_y, 0, 0), microfacet_weight);
refl += mix(zero, (intensity * Ct) * microfacet("ggx", Nw, Ta, alpha_x, alpha_y, base_ior, 0), microfacet_weight);
refr += mix(zero, (intensity * Ct) * microfacet("beckmann", Nw, Ta, alpha_x, alpha_y, base_ior, 1), microfacet_weight);
}
}
color weighted_coat_color = mix(color(1.0), coat_color, coat_weight);
float Kr = 0;
float Kt = 0;
vector R = vector(0);
vector T = vector(0);
fresnel(I, N, 1 / base_ior, Kr, Kt, R, T);
refr *= mix(1, Kt, coat_weight);
refl *= mix(1, Kt, coat_weight);
float mod_refraction_weight = mix(refraction_weight, 0, base_metal);
BRDF = weighted_coat_color * mix(refl, refl + refr, mod_refraction_weight);
// Add diffuse and clearcoat
BRDF += weighted_coat_color * mix(base_color * base_weight * mix(oren_nayar(N, base_rough), metal, base_metal), zero, mod_refraction_weight);
BRDF += coat_weight * microfacet("ggx", N, 0.005, coat_IOR, 0);
}