-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathff_api_3d_perlin_spherical_map_v.0.0.4.lua
336 lines (294 loc) · 9.17 KB
/
ff_api_3d_perlin_spherical_map_v.0.0.4.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
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
-- 3d perlin spherical map v.0.0.4 - improved perlin noise, distortion power, get_perlin_octaves
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_intslider_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 rc, gc, bc, ac = 0, 0, 0, 0
rd, gd, bd, ad = 0, 0, 0, 0
rs, gs, bs, as = 0, 0, 0, 0
local rr, gr, br, ar = get_sample_map(x, y, ROUGHNESS)
local rc, gc, bc, ac = get_sample_map(x, y, CONTRAST) -- * 2 - 1
local r1, g1, b1, a1 = get_sample_map(x, y, HIGH)
local r2, g2, b2, a2 = get_sample_map(x, y, LOW)
local r3, g3, b3, a3 = get_sample_map(x, y, OVERLAY)
local ros, gos, bos, osa = get_sample_map(x, y, OFFSET)
rd, gd, bd, ad = get_sample_map(x, y, DISTORTION)
rs, gs, bs, as = get_sample_map(x, y, SCALE)
if rs > 100 then rs = 100 end
if gs > 100 then gs = 100 end
if bs > 100 then bs = 100 end
if as > 100 then as = 100 end
rd = rd * power
gd = gd * power
bd = bd * power
-- 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
nx = nx * (rs * as) + (ros * power * 0.1)
ny = ny * (gs * as) + (gos* power * 0.1)
nz = nz * (bs * as) + (bos* 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
nr = gen_perlin_octaves(nr, rr, rc, rd, nx_r, ny_r, nz_r)
ng = gen_perlin_octaves(ng, gr, gc, gd, nx_g, ny_g, nz_g)
nb = gen_perlin_octaves(nb, br, bc, bd, nx_b, ny_b, nz_b)
h,s,l = fromrgb(nr, ng, nb)
-- input curves
l = get_sample_curve(x, y, l, 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 == 1 then
-- map = true
-- channel = r
-- blends forground HIGH and background LOW
r, g, b, a = blend_normal(r2, g2, b2, a2, r1, g1, b1, a1, nr, hdr)
return r, g, b, a
elseif mode == 2 then
-- map = true
-- channel = g
-- blends forground HIGH and background LOW
r, g, b, a = blend_normal(r2, g2, b2, a2, r1, g1, b1, a1, ng, hdr)
return r, g, b, a
elseif mode == 3 then
-- map = true
-- channel = b
-- blends forground HIGH and background LOW
r, g, b, a = blend_normal(r2, g2, b2, a2, r1, g1, b1, a1, nb, hdr)
return r, g, b, a
elseif mode == 4 then
-- map = true
-- channel = l -- (luminance)
-- blends forground HIGH and background LOW
r, g, b, a = blend_normal(r2, g2, b2, a2, r1, g1, b1, a1, l, hdr)
return r, g, b, a
-- return nr, ng, nb, 1
elseif mode == 5 then
-- map = true
-- planet = true
-- blends clouds HIGH and surface LOW plus shaded with atmosphere in color fresnel overlay
r, g, b, a = blend_normal(r2, g2, b2, a2, r1, g1, b1, a1, 1, hdr)
-- atmosphere = true
r, g, b, a = blend_normal(r, g, b, a, r3, g3, b3, a3, 0.8, hdr)
-- blends in clouds overlay
r, g, b, a = blend_linear_dodge(r, g, b, a, r1, g1, b1, a1, 0.1, hdr)
return r, g, b, a
else
-- map = true
-- rgban = true
return nr, ng, nb, 1
end
-- debug
-- return rc, gc, bc, ac
end;
function gen_perlin_octaves(ch_noise, ch_roughness, ch_contrast, ch_distortion, ch_noise_x, ch_noise_y, ch_noise_z)
local distortion = 0
ch_roughness = set_ch_roughness(ch_roughness)
ch_contrast = ch_contrast * 2 - 1
factor = (259 * (ch_contrast + 1)) / (1 * (259 - ch_contrast))
NOISE_SIZE = (((rs + gs + bs + as) * 0.25) ^ 2)
OCTAVES = {}
local cell_size = (0.01 + NOISE_SIZE * 0.99) * grain
local scale = ch_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 * ch_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
for octave_index = 1, OCTAVES_COUNT do
local size = OCTAVES[octave_index][1]
local opacity = OCTAVES[octave_index][2]
distortion = opacity * perlin:noise(rd + ch_noise_x * size,gd + ch_noise_y * size, bd + ch_noise_z * size)
ch_noise = ch_noise + (opacity * perlin:noise(ch_noise_x * size,ch_noise_y * size, ch_noise_z * size )+ distortion)
end
-- contrast adjustments
ch_noise = (ch_noise + 1.0) * 0.5
ch_noise = truncate(factor * (ch_noise - 0.5) + 0.5)
return ch_noise
end;
function set_ch_roughness(ch_roughness)
ch_roughness = ROUGHNESS_THRESHOLD + ch_roughness * (1.0 - ROUGHNESS_THRESHOLD)
return ch_roughness
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;