-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathff_api_3d_perlin_spherical_map_v.0.0.3.lua
304 lines (264 loc) · 8.09 KB
/
ff_api_3d_perlin_spherical_map_v.0.0.3.lua
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
-- 3d perlin spherical map v.0.0.3 - improved perlin noise, distortion power
function prepare()
-- constants
ROUGHNESS_THRESHOLD = 0.00001
REMAINDER_THRESHOLD = 0.00001
aspect = OUTPUT_HEIGHT / OUTPUT_WIDTH * 2
-- input values
details = get_slider_input(DETAILS) * 10 + 0.0001
grain = (get_slider_input(GRAIN) * 5) + 0.0001
power = get_slider_input(POWER) * 10
OCTAVES_COUNT = math.floor(details)
-- noise block
--[[
https://gist.githubusercontent.com/kymckay/25758d37f8e3872e1636d90ad41fe2ed/raw/1c647169a6729713f8987506b2e5c75a23b14969/perlin.lua
Implemented as described here:
http://flafla2.github.io/2014/08/09/perlinnoise.html
originally an external, FF requires internal script exclusively.
block only functions inside prepare(), appended to the end it throws a nil global value 'perlin' error.
]]--
perlin = {}
perlin.p = {}
math.randomseed(get_intslider_input(SEED))
for i = 0, 255 do
perlin.p[i] = math.random(255)
perlin.p[256 + i] = perlin.p[i]
end
-- return range: [ - 1, 1]
function perlin:noise(x, y, z)
y = y or 0
z = z or 0
-- calculate the "unit cube" that the point asked will be located in
local xi = bit32.band(math.floor(x), 255)
local yi = bit32.band(math.floor(y), 255)
local zi = bit32.band(math.floor(z), 255)
-- next we calculate the location (from 0 to 1) in that cube
x = x - math.floor(x)
y = y - math.floor(y)
z = z - math.floor(z)
-- we also fade the location to smooth the result
local u = self.fade(x)
local v = self.fade(y)
local w = self.fade(z)
-- hash all 8 unit cube coordinates surrounding input coordinate
local p = self.p
local A, AA, AB, AAA, ABA, AAB, ABB, B, BA, BB, BAA, BBA, BAB, BBB
A = p[xi ] + yi
AA = p[A ] + zi
AB = p[A + 1 ] + zi
AAA = p[ AA ]
ABA = p[ AB ]
AAB = p[ AA + 1 ]
ABB = p[ AB + 1 ]
B = p[xi + 1] + yi
BA = p[B ] + zi
BB = p[B + 1 ] + zi
BAA = p[ BA ]
BBA = p[ BB ]
BAB = p[ BA + 1 ]
BBB = p[ BB + 1 ]
-- take the weighted average between all 8 unit cube coordinates
return self.lerp(w,
self.lerp(v,
self.lerp(u,
self:grad(AAA, x, y, z),
self:grad(BAA, x - 1, y, z)
),
self.lerp(u,
self:grad(ABA, x, y - 1, z),
self:grad(BBA, x - 1, y - 1, z)
)
),
self.lerp(v,
self.lerp(u,
self:grad(AAB, x, y, z - 1), self:grad(BAB, x - 1, y, z - 1)
),
self.lerp(u,
self:grad(ABB, x, y - 1, z - 1), self:grad(BBB, x - 1, y - 1, z - 1)
)
)
)
end
--[[
gradient function finds dot product between pseudorandom gradient vector
and the vector from input coordinate to a unit cube vertex.
]]--
perlin.dot_product = {
[0x0] = function(x, y, z) return x + y end,
[0x1] = function(x, y, z) return -x + y end,
[0x2] = function(x, y, z) return x - y end,
[0x3] = function(x, y, z) return -x - y end,
[0x4] = function(x, y, z) return x + z end,
[0x5] = function(x, y, z) return -x + z end,
[0x6] = function(x, y, z) return x - z end,
[0x7] = function(x, y, z) return -x - z end,
[0x8] = function(x, y, z) return y + z end,
[0x9] = function(x, y, z) return -y + z end,
[0xA] = function(x, y, z) return y - z end,
[0xB] = function(x, y, z) return -y - z end,
[0xC] = function(x, y, z) return y + x end,
[0xD] = function(x, y, z) return -y + z end,
[0xE] = function(x, y, z) return y - x end,
[0xF] = function(x, y, z) return -y - z end
}
function perlin:grad(hash, x, y, z)
return self.dot_product[bit32.band(hash, 0xF)](x, y, z)
end
-- fade function is used to smooth final output
function perlin.fade(t)
return t * t * t * (t * (t * 6 - 15) + 10)
end
function perlin.lerp(t, a, b)
return a + t * (b - a)
end
-- end perlin
-- perlin octaves initialization
remainder = details - OCTAVES_COUNT
if (remainder > REMAINDER_THRESHOLD) then
OCTAVES_COUNT = OCTAVES_COUNT + 1
end
-- end noise block
-- mode block
mode = get_checkbox_input(MODE)
if (get_checkbox_input(HDR)) then
hdr = true
else
hdr = false
end
-- end
end;
function get_sample(x, y)
-- key variables
local nr, ng, nb = 0, 0, 0
local nx, ny, nz = 0, 0, 0
local nx_r, nx_g, nx_b = 0, 0, 0
local ny_r, ny_g, ny_b = 0, 0, 0
local nz_r, nz_g, nz_b = 0, 0, 0
local dr, dg, db, da = 0, 0, 0, 0
local dx, dy, dz, da = 0, 0, 0, 0
local sx, sy, sz, sa = 0, 0, 0, 0
-- image generation
-- input maps
roughness = ROUGHNESS_THRESHOLD + get_sample_grayscale(x, y, ROUGHNESS) *
(1.0 - ROUGHNESS_THRESHOLD)
local contrast = (get_sample_grayscale(x, y, CONTRAST) * 2) - 1
local factor = (259 * (contrast + 1)) / (1 * (259 - contrast))
local r1, g1, b1, a1 = get_sample_map(x, y, HIGH)
local r2, g2, b2, a2 = get_sample_map(x, y, LOW)
local dx, dy, dz, da = get_sample_map(x, y, DISTORTION)
local osx, osy, osz, osa = get_sample_map(x, y, OFFSET)
local sx, sy, sz, sa = get_sample_map(x, y, SCALE)
if sx > 100 then sx = 100 end
if sy > 100 then sy = 100 end
if sz > 100 then sz = 100 end
if sa > 100 then sa = 100 end
dx = dx * power
dy = dy * power
dz = dz * power
-- end
-- spherical map block
local x = x * aspect * math.pi
local y = y * math.pi
nx = math.cos(x) * math.sin(y)
ny = math.sin(x) * math.sin(y)
nz = math.cos(y)
-- end
-- noise generation
NOISE_SIZE = (((sx + sy + sz + sa) * 0.25) ^ 2)
OCTAVES = {}
local cell_size = (0.01 + NOISE_SIZE * 0.99) * grain
local scale = roughness
local octave_index
for octave_index = 1, OCTAVES_COUNT do
if (scale < ROUGHNESS_THRESHOLD) then
OCTAVES_COUNT = octave_index - 1
break
end
OCTAVES[octave_index] = {cell_size, scale}
cell_size = cell_size * 2.0
scale = scale * roughness
end
if (remainder >= 0.001) then
OCTAVES[OCTAVES_COUNT][2] = OCTAVES[OCTAVES_COUNT][2] * remainder
end
NORM_FACTOR = 0
for octave_index = 1, OCTAVES_COUNT do
NORM_FACTOR = NORM_FACTOR + OCTAVES[octave_index][2] ^ 2
end
NORM_FACTOR = 1 / math.sqrt(NORM_FACTOR)
local octave_index
-- scale and offset applied
nx = nx * (sx * sa) + (osx * power * 0.1)
ny = ny * (sy * sa) + (osy * power * 0.1)
nz = nz * (sz * sa) + (osz * power * 0.1)
nx_r = nx + 1
ny_r = ny + 1
nz_r = nz + 1
nx_g = nx + 2
ny_g = ny + 2
nz_g = nz + 2
nx_b = nx + 3
ny_b = ny + 3
nz_b = nz + 3
for octave_index = 1, OCTAVES_COUNT do
local size = OCTAVES[octave_index][1]
local opacity = OCTAVES[octave_index][2]
-- distortion noise built
dr = (opacity * perlin:noise(dx + nx_r * size, dy + ny_r * size, dz + nz_r * size))
dg = (opacity * perlin:noise(dx + nx_g * size, dy + ny_g * size, dz + nz_g * size))
db = (opacity * perlin:noise(dx + nx_b * size, dy + ny_b * size, dz + nz_b * size))
-- distortion noise applied (multiplier affects contrast)
nr = nr + (opacity * perlin:noise(nx_r * size, ny_r * size, nz_r * size ) + (dr)) -- * dx))
ng = ng + (opacity * perlin:noise(nx_g * size, ny_g * size, nz_g * size) + (dg)) -- * dy))
nb = nb + (opacity * perlin:noise(nx_b * size, ny_b * size, nz_b * size) + (db)) -- * dz))
end
-- contrast adjustments
nr = (nr + 1.0) * 0.5
ng = (ng + 1.0) * 0.5
nb = (nb + 1.0) * 0.5
nr = truncate(factor * (nr - 0.5) + 0.5)
ng = truncate(factor * (ng - 0.5) + 0.5)
nb = truncate(factor * (nb - 0.5) + 0.5)
-- input curves
pr = nr
pr = get_sample_curve(x, y, pr, PROFILE)
nr = get_sample_curve(x, y, nr, PROFILE)
ng = get_sample_curve(x, y, ng, PROFILE)
nb = get_sample_curve(x, y, nb, PROFILE)
-- return conditions
if mode then
-- rgban = true
return nr, ng, nb, 1
else
-- map = true
-- blends forground HIGH and background LOW
r, g, b, a = blend_normal(r2, g2, b2, a2, r1, g1, b1, a1, pr, hdr)
return r, g, b, a
end
-- debug
end;
function fromrgb(r, g, b)
local max, min = math.max(r, g, b), math.min(r, g, b)
local h, s, l
l = (max + min) / 2
if max == min then
h, s = 0, 0 -- achromatic
else
local d = max - min
local s
if l > 0.5 then s = d / (2 - max - min) else s = d / (max + min) end
if max == r then
h = (g - b) / d
if g < b then h = h + 6 end
elseif max == g then h = (b - r) / d + 2
elseif max == b then h = (r - g) / d + 4
end
h = h / 6
end
return h, s, l or 1
end
function truncate(value)
if value <= 0 then value = 0 end
if value >= 1 then value = 1 end
return value
end;