-
Notifications
You must be signed in to change notification settings - Fork 6
/
Noise - OrganicNoise.osl
584 lines (518 loc) · 13.9 KB
/
Noise - OrganicNoise.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
// Organic Noise Shader - Caustics, bubbly, and weird noises.
// Organic Noise by Philippe Groarke
// Copyright 2022 Philippe Groarke, All rights reserved. This file is licensed under Apache 2.0 license
// https://github.com/ADN-DevTech/3dsMax-OSL-Shaders/blob/master/LICENSE.txt
#include "C:\Users\groarkp\code\3dsmax-plugins\OSL\FeaOSL\include\fea_widget.osl"
#include "C:\Users\groarkp\code\3dsmax-plugins\OSL\FeaOSL\include\fea_color.osl"
#include "C:\Users\groarkp\code\3dsmax-plugins\OSL\FeaOSL\include\fea_functional.osl"
#include "C:\Users\groarkp\code\3dsmax-plugins\OSL\FeaOSL\include\fea_interpolate.osl"
#define noise_t int
// Simplex:0
// Fractal:1
// Fractal Lines:2
// Fractal Inverse:3
#define custom_noise_opts \
"Simplex:0" \
"|Fractal Light:1" \
"|Fractal Dark:2" \
"|Fractal Lines:3"
float custom_noise(noise_t noise_type, vector coord, float phase) {
// Minimize calls to noise.
float ret = noise("simplex", coord, phase);
if (noise_type == 0) {
// simplex
ret = (ret + 1) * 0.5;
} else if (noise_type == 1) {
// fractal light
ret = 1 - fabs(ret);
} else if (noise_type == 2) {
// fractal dark
ret = fabs(ret);
} else if (noise_type == 3) {
// fractal lines
ret = 1 - fabs(ret);
if (ret <= 0.99) {
ret = 0;
}
}
return ret;
}
// Returns a 3x3 matrix of noise values around
// center coords x,y.
matrix noise_3x3(noise_t noise_type, vector coord, float phase, vector xmove, vector ymove) {
matrix ret = 0;
// vector coord = vector(c[0], c[1], 0);
// topleft
{
point p = coord - xmove + ymove;
ret[0][0] = custom_noise(noise_type, p, phase);
}
// topcenter
{
point p = coord + ymove;
ret[0][1] = custom_noise(noise_type, p, phase);
}
// topright
{
point p = coord + xmove + ymove;
ret[0][2] = custom_noise(noise_type, p, phase);
}
// middleleft
{
point p = coord - xmove;
ret[1][0] = custom_noise(noise_type, p, phase);
}
// middlecenter
{
point p = coord;
ret[1][1] = custom_noise(noise_type, p, phase);
}
// middleright
{
point p = coord + xmove;
ret[1][2] = custom_noise(noise_type, p, phase);
}
// bottomleft
{
point p = coord - xmove - ymove;
ret[2][0] = custom_noise(noise_type, p, phase);
}
// bottomcenter
{
point p = coord - ymove;
ret[2][1] = custom_noise(noise_type, p, phase);
}
// bottomright
{
point p = coord + xmove - ymove;
ret[2][2] = custom_noise(noise_type, p, phase);
}
return ret;
}
float flatten_top(float mag, float thresh) {
return mag < thresh ? thresh : mag;
}
float emphasize_top(float mag, float thresh) {
// Also flip the threshold for UI purposes.
return mag > 1 - thresh ? 1 - thresh : mag;
}
shader OrganicNoise
[[
string label = "Noise - Organic Noise",
string help =
"<h3>Organic Noise</h3>"
"Modulates and filters OSL noises to produce organic looking noises. "
"Garden variety of caustic, fleshy and abstract noises.",
]]
(
int noise_type = 0
[[
string widget = "mapper",
string label = "Noise Type",
string options =
custom_noise_opts
,
string help = "The source noise type. 'Fractal lines' doesn't play well with most options, but is there because why not.",
int connectable = 0,
]],
int noise_effect = 0
[[
string widget = "mapper",
string label = "Noise Effect",
string options =
"None:0"
"|Exponentize:1"
"|Logarithmize:2"
"|Sharpen:3"
"|Blur:4"
"|Emboss:5"
"|Outline:6"
,
string help = "Applies an extra effect on your base noise.",
int connectable = 0,
]],
int line_mode = 0
[[
string widget = "mapper",
string label = "Lines",
string options =
fea_blend_opts
,
string help = "Creates lines distorted by noise.",
int connectable = 0,
string packName = "Lines / Spacing",
]],
float line_spacing = 1.0
[[
string label = "Line Spacing",
string help = "Space between lines.",
float min = 0.00001,
int connectable = 0,
string packName = "Lines / Spacing",
int widgetWidth = FEA_RPACK_W,
]],
int camo_mode = 0
[[
string widget = "mapper",
string label = "Camo",
string options =
fea_blend_opts
,
string help = "Gives a 'camouflage' look to the noise.",
int connectable = 0,
string packName = "Camo / Angle",
]],
float camo_angle = 0
[[
string label = "Camo Seed",
string help = "An angle used to tweak 'camo', 0 to 360 degrees.",
float min = 0,
float max = 1,
int connectable = 0,
string packName = "Camo / Angle",
int widgetWidth = FEA_RPACK_W,
]],
int bubbles_mode = 0
[[
string widget = "mapper",
string label = "Bubbles",
string options =
fea_blend_opts
,
string help = "Gives a 'bubbly' thicker look to the noise.",
int connectable = 0,
string packName = "Bubbles / Strength",
]],
float bubbles_strength = 1.0
[[
string label = "Bubbles Strength",
string help = "Affects bubbles strength.",
float min = 0,
float max = 1,
int connectable = 0,
string packName = "Bubbles / Strength",
int widgetWidth = FEA_RPACK_W,
]],
int crease_mode = 0
[[
string widget = "mapper",
string label = "Crease",
string options =
fea_blend_opts
,
string help = "Filters the noise according to a direction, resulting in dark lines with peak 'creases'.",
int connectable = 0,
string packName = "Crease / Angle",
]],
float crease_angle = 0.0
[[
string label = "Crease Direction",
string help = "Affects orientation of creases.",
float min = 0,
float max = 1,
int connectable = 0,
string packName = "Crease / Angle",
int widgetWidth = FEA_RPACK_W,
]],
FEA_SPACER(1),
int over_sampling = 0
[[
string widget = "mapper",
string label = "Over-Sampling",
string options =
"None:0"
"|Rings:1"
"|Pinch:2",
string help = "The oversampling affects the noise coordinate used to acquire the noise. From a visual perspective, 'rings' creates circular patterns in darker areas. Pinch creates crease lines directed towards peaks.",
int connectable = 0,
string packName = "Over-Sampling / Strength",
]],
float oversampling_strength = 1
[[
string label = "Over-Sampling Strength",
string help = "Increase or decrease the over-sampling amount.",
string packName = "Over-Sampling / Strength",
int connectable = 0,
int widgetWidth = FEA_RPACK_W,
]],
int oversampling_noise_type = -1
[[
string widget = "mapper",
string label = "Over-Sampling Noise",
string options =
"Same:-1|"
custom_noise_opts
,
string help = "The type of noise to over-sample. By default, same type as 'Noise Type'.",
int connectable = 0,
string packName = "Over-Sampling Noise / Blend",
]],
int overampling_blend = 13
[[
string widget = "mapper",
string label = "Over-Sampling Blend",
string options =
fea_blend_opts
,
string help = "How to blend the over-sampled noise.",
int connectable = 0,
string packName = "Over-Sampling Noise / Blend",
]],
FEA_SPACER(2),
int girth_mode = 0
[[
string widget = "mapper",
string label = "Girth",
string options =
"None:0"
"|Bubbly:1"
"|Caustics:2",
string help = "Bubbly expands the noise, caustics squeezes it.\nNote : Caustics don't play well with Bubbles noise mode.",
int connectable = 0,
string packName = "Girth / Strength",
]],
float girth_strength = 1
[[
string label = "Girth Strength",
string help = "Increase or decrease the girth multiplier or divider.",
string packName = "Girth / Strength",
float min = 0.001,
int connectable = 0,
int widgetWidth = FEA_RPACK_W,
]],
int girth_threshold = 0
[[
string widget = "mapper",
string label = "Girth-Threshold",
string options =
"None:0"
"|Flatten Top:1"
"|Emphasize Top:2",
string help = "Limits the girth above/below a certain value. Does nothing if no girth.",
string packName = "Girth-Threshold / Strength",
int connectable = 0,
]],
float girth_threshold_strength = 0.5
[[
string label = "Girth-Threshold Strength",
string help = "The girth threshold limit value.",
string packName = "Girth-Threshold / Strength",
int connectable = 0,
int widgetWidth = FEA_RPACK_W,
float min = 0,
float max = 0.9,
]],
FEA_SPACER(6),
int step_mode = 0
[[
string widget = "mapper",
string label = "Step",
string options =
fea_blend_opts
,
string help = "Creates 'level sets' or topological layers.",
int connectable = 0,
string packName = "Step / Amount",
]],
int step_amount = 10
[[
string label = "Step Amount",
string help = "The number of steps to create.",
int min = 2,
int connectable = 0,
string packName = "Step / Amount",
int widgetWidth = FEA_RPACK_W,
]],
float interpolate = 0
[[
string label = "Interpolate Output",
string help = "Interpolates final result. Positive is exponential, negative is logarithmic and '0' is linear interpolation (none).",
int connectable = 0,
string packName = "Interpolate / Clamp",
]],
int clamp_out = 1
[[
string label = "Clamp Output",
string help = "Clamps the output value between [0..1].",
string widget = "checkBox",
int connectable = 0,
string packName = "Interpolate / Clamp",
]],
FEA_SPACER(3),
float sampling_dist = 1
[[
string label = "Sampling Distance",
string help = "The 'search range' of the neighbours. Low distances give a 'cell' look. Higher sampling distances reduce tiling.",
float min = 0.05,
float max = 100,
int connectable = 0,
]],
float in_phase = 0
[[
string label = "Phase",
string help = "The 4th coordinate of the noise. You can animate with scene time for movement. "
"For interesting results and 'edge distortion', feed another noise to this parameter.",
string packName = "Phase / Perturb",
]],
int in_perturb_phase = 0
[[
string label = "Perturb Phase",
string help = "Applies some randomness to the phase itself. Can lead to interesting animations or distortions.",
int connectable = 0,
string widget = "checkBox",
string packName = "Phase / Perturb",
]],
float uv_scale = 0.25
[[
string label = "UV Scale",
string help = "Scale your UVWs up or down. For more betterer transformations, use UVW Transform node.",
float min = 0.00001,
int connectable = 0,
]],
point uvw = transform("object", P)
[[
string label = "UVW",
string help = "Transformed UVW coordinates. Animate 'w' with scene time for interesting results.",
]],
output float Out = 0
)
{
float epsilon = 0.00001;
float muv_scale = uv_scale;
point orig_coord = uvw / muv_scale;
point coord = orig_coord;
vector grad = 0;
matrix sample_area;
float noise_v = 0;
float sample_distance = 0.05;
float phase = in_phase;
float entropy = fea_fbm(3, coord, phase);
if (in_perturb_phase) {
phase += entropy;
}
{
// Gets wonky at very low sampling distances.
sample_distance = sampling_dist;
// Low sampling distances give a "cell" look, so we can't push it too
// far with the entropy. The higher the sampling dist, the more random.
sample_distance += entropy * sample_distance;
// Add some perturberance if enabled.
vector xmove;
vector ymove;
if (in_perturb_phase) {
xmove = vector(sample_distance, 0, phase);
ymove = vector(0, sample_distance, phase);
} else {
xmove = vector(sample_distance, 0, 0);
ymove = vector(0, sample_distance, 0);
}
// Compute a 3x3 matrix of surrounding samples to the currently
// rendered point.
sample_area = noise_3x3(noise_type, coord, phase, xmove, ymove);
noise_v = sample_area[1][1];
// Compute gradient of noise.
grad = fea_sobel_gradient_2d(sample_area);
}
if (noise_effect == 1) {
// Exponent
noise_v = fea_interp(10, noise_v);
} else if (noise_effect == 2) {
// Logarithm
noise_v = fea_interp(-10, noise_v);
} else if (noise_effect == 3) {
// sharpen
noise_v = fea_sharpen_2d(sample_area);
} else if (noise_effect == 4) {
// blur
noise_v = fea_blur_2d(sample_area);
} else if (noise_effect == 5) {
// emboss
noise_v = fea_emboss_2d(sample_area);
} else if (noise_effect == 6) {
// outline
noise_v = fea_outline_2d(sample_area);
}
float mag = length(grad);
float dir = atan2(grad[1], grad[0]);
// Lines
if (line_mode != 0) {
vector new_coord = coord + grad;
if (in_perturb_phase) {
new_coord[2] = phase;
}
float lspace = 1.0 / (line_spacing * 0.5);
float val = abs(sin((new_coord[0] + new_coord[1] + new_coord[2]) * lspace));
noise_v = fea_blend(line_mode, noise_v, val);
}
// Camo
if (camo_mode != 0) {
float camo_dir = fea_rad_to_per(dir + M_PI) + camo_angle;
camo_dir = fmod(camo_dir, 1);
noise_v = fea_blend(camo_mode, noise_v, camo_dir);
}
// Bubbles
noise_v = fea_blend(bubbles_mode, noise_v, mag * bubbles_strength);
// Creases
if (crease_mode != 0) {
float s = fea_per_to_rad(crease_angle);
vector axis = fea_angle_to_vec(s);
float val = abs(dot(normalize(grad), axis));
noise_v = fea_blend(crease_mode, noise_v, val);
}
// Over-sampling.
if (over_sampling == 1) {
// ring
coord += grad * oversampling_strength;
} else if (over_sampling == 2) {
// pinch
coord += normalize(grad) * oversampling_strength;
}
if (over_sampling != 0) {
int os_noise_type = noise_type;
if (oversampling_noise_type != -1) {
os_noise_type = oversampling_noise_type;
}
float os_noise = custom_noise(os_noise_type, coord, phase);
noise_v = fea_blend(overampling_blend, noise_v, os_noise);
}
// Girth
// The threshold acts invertly dependending on girth,
// flip it so UI makes sense.
if (girth_threshold == 1) {
// flatten
if (girth_mode == 2) {
mag = flatten_top(mag, girth_threshold_strength);
} else {
mag = emphasize_top(mag, girth_threshold_strength);
}
} else if (girth_threshold == 2) {
// emphasize
if (girth_mode == 2) {
mag = emphasize_top(mag, girth_threshold_strength);
} else {
mag = flatten_top(mag, girth_threshold_strength);
}
}
if (girth_mode == 1) {
mag *= girth_strength;
noise_v *= mag;
} else if (girth_mode == 2) {
// Caustics.
mag *= 10;
mag *= (1.0 / girth_strength);
noise_v /= mag;
}
// Step
if (step_mode != 0) {
noise_v = fea_fancy_step(step_mode, step_amount, noise_v);
}
// Interpolate
if (interpolate != 0) {
noise_v = fea_interp(interpolate, noise_v);
}
// Clamp
if (clamp_out) {
noise_v = clamp(noise_v, 0, 1);
}
Out = noise_v;
}