-
Notifications
You must be signed in to change notification settings - Fork 5
/
Noise - TilingSimplex.osl
179 lines (155 loc) · 4.6 KB
/
Noise - TilingSimplex.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
// TilingSimplex Shader
// TilingSimplex 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_constants.osl"
#include "C:\Users\groarkp\code\3dsmax-plugins\OSL\FeaOSL\include\fea_tsimplex.osl"
#include "C:\Users\groarkp\code\3dsmax-plugins\OSL\FeaOSL\include\fea_widget_phase.osl"
#include "C:\Users\groarkp\code\3dsmax-plugins\OSL\FeaOSL\include\fea_widget_uvw.osl"
#include "C:\Users\groarkp\code\3dsmax-plugins\OSL\FeaOSL\include\fea_shape.osl"
#include "C:\Users\groarkp\code\3dsmax-plugins\OSL\FeaOSL\include\fea_interpolate.osl"
// TODO : Debug why gradient is so far out-of-range of expected magnitude
// of 2 (for -1,1) and 1 (for 0,1).
shader TilingSimplex
[[
string label = "Noise - Tiling Simplex",
string help =
"<h3>Tiling Simplex</h3>"
"A spacially and temporally tiling simplex noise.<br>"
"Port of Stefan Gustavson's and Ian McEwan's GLSL implementation."
,
]]
(
int in_fbm_octaves = 3
[[
string label = "Octaves",
string help = "How many octaves used for the fractal brownian motion (clouds).",
int min = 1,
int max = 20,
int connectable = 0,
// string packName = "Octaves / Lacunarity",
]],
float in_lacunarity = 1.8
[[
string label = "Lacunarity",
string help = "The frequency growth factor used by the fractalization.",
float min = 0.01,
int connectable = 0,
// string packName = "Octaves / Lacunarity",
]],
vector in_tiling = 0.0
[[
string label = "Tile Size",
string help = "These are the x, y, z periods, used to tile the noise. [0, 0, 0] disables tiling (infinite noise).",
float max = 289.0,
int connectable = 0,
]],
FEA_SPACER(0),
float in_low_clamp = 0.0
[[
string label = "Low Value",
string help = "The minimum value of the noise. Hard clamped.",
float min = 0.0,
float max = 1.0,
// string packName = "High / Low Clamp",
]],
float in_high_clamp = 1.0
[[
string label = "High Value",
string help = "The maximum value of the noise. Hard clamped.",
float min = 0.0,
float max = 1.0,
// string packName = "High / Low Clamp",
]],
color in_low_color = 0
[[
string label = "Low Color",
string help = "The dark color (noise == 0).",
]],
color in_high_color = 1
[[
string label = "High Color",
string help = "The light color (noise == 1).",
]],
FEA_WGT_PHASE_INPUTS,
FEA_WGT_UVW_INPUTS,
output color out_col = 0
[[
string label = "Out (color)"
]],
output float out_float = 0
[[
string label = "Out (float)"
]],
output color out_normal = 0
[[
string label = "Normal (color)"
]],
output vector out_gradient = 0
[[
string label = "Gradient (vector)"
]],
output vector out_second_deriv = 0
[[
string label = "2nd Derivative (vector)"
]],
output vector out_second_deriv2 = 0
[[
string label = "2nd Derivative 2 (vector)"
]]
)
{
fea_wgt_uvw_ret uvw_data = FEA_WGT_GET_UVW;
point coord = uvw_data.coord;
float phase = FEA_WGT_GET_PHASE;
// fea_tsimplex_ret ret = fea_utsimplex_fbm(coord, in_fbm_octaves, in_lacunarity, in_tiling, phase);
fea_tsimplex_ret ret = fea_utsimplex(coord, in_tiling, phase);
ret.value = fea_lerp(ret.value, 0.0, 1.0, in_low_clamp, in_high_clamp);
out_col = mix(in_low_color, in_high_color, ret.value);
out_float = ret.value;
out_gradient = ret.gradient;
out_second_deriv = ret.dg;
out_second_deriv2 = ret.dg2;
vector m_tsimplex_normal(string input_space, vector scale, vector gradient) {
vector zz = normalize(N); // Fix shader imprecision.
vector xx = normalize(gradient);
vector yy = cross(zz, xx);
zz = cross(xx, yy);
matrix m = matrix(
xx[0], xx[1], xx[2], 0,
yy[0], yy[1], yy[2], 0,
zz[0], zz[1], zz[2], 0,
0, 0, 0, 1
);
return transform(m, normal(0,0,1));
// // vector up = fea_transform("world", "tangent", N);
// vector up = N;
// vector grad = gradient * scale;
// grad = fea_transform(input_space, "world", grad);
// vector surf_grad = grad - up * dot(grad, up);
// return fea_transform("world", "tangent", normalize(up + surf_grad));
}
// Compute normal using surface gradient method.
vector n = m_tsimplex_normal(uvw_data.coord_space, uvw_data.scale,
ret.gradient);
// Normalize to expected rgb color map [-1,1] -> [0,1].
out_normal = n * 0.5 + 0.5;
#if 0
float mag = length(ret.gradient * uvw_data.scale);
if (mag >= 1.0) {
out_col = FEA_GREEN;
}
if (mag >= 2.0) {
out_col = FEA_BLUE;
}
if (mag >= 4.0) {
out_col = FEA_RED;
}
if (mag >= 5.0) {
out_col = color(0);
}
if (mag >= 6.9) {
out_col = color(1);
}
#endif
}