-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathff_api_perlin_noise_octaves.lua
114 lines (102 loc) · 3.18 KB
/
ff_api_perlin_noise_octaves.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
-- perlin noise - get_perlin_octaves(x,y,z,channel)
function prepare()
-- constants
AMPLITUDE_CORRECTION_FACTOR = 1.731628995
ROUGHNESS_THRESHOLD = 0.00001
REMAINDER_THRESHOLD = 0.00001
aspect = OUTPUT_HEIGHT / OUTPUT_WIDTH * 2
-- input values
local details = get_slider_input(DETAILS) * 25
OCTAVES_COUNT = math.floor(details)
NOISE_SIZE = get_slider_input(SCALE)
local remainder = details - OCTAVES_COUNT
if (remainder > REMAINDER_THRESHOLD) then
OCTAVES_COUNT = OCTAVES_COUNT + 1
end
local roughness = ROUGHNESS_THRESHOLD +
(get_slider_input(ROUGHNESS) + 0.25) * (1.0 - ROUGHNESS_THRESHOLD)
OCTAVES = {}
local cell_size = (0.01 + NOISE_SIZE * 0.99) * 1000
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 * 0.5
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)
set_perlin_noise_seed(get_intslider_input(NOISE_VARIATION))
end;
function get_sample(x, y)
local r, g, b, a = get_sample_map(x, y, BACKGROUND)
local noise_r, noise_g, noise_b, noise_a = get_sample_map(x, y, NOISE)
-- local contrast = (get_sample_grayscale(x, y, CONTRAST) * 2) - 1
-- local factor = (259 * (contrast + 1)) / (1 * (259 - contrast))
local z = 0
local x = x * aspect * math.pi
local y = y * math.pi
local nx = math.cos(x) * math.sin(y)
local ny = math.sin(x) * math.sin(y)
local nz = math.cos(y)
-- local r = get_perlin_octaves(x,y,z,1)
-- local g = get_perlin_octaves(x,y,z,2)
-- local b = get_perlin_octaves(x,y,z,3)
local alpha = get_perlin_octaves(nx,ny,nz)
-- r = truncate(factor * (r - 0.5) + 0.5)
-- g = truncate(factor * (g - 0.5) + 0.5)
-- b = truncate(factor * (b - 0.5) + 0.5)
-- alpha = truncate(factor * (alpha - 0.5) + 0.5)
-- return r, g, b, a
-- return v
r, g, b, a = blend_normal(r, g, b, a, noise_r, noise_g, noise_b, noise_a, alpha)
return r, g, b, a
end;
function get_perlin_octaves(x,y,z,channel)
local cx = 0
local cy = 0
local cz = 0
if channel == 1 then
cx = x
cy = y
cz = z
elseif channel == 2 then
cx = x+(OUTPUT_WIDTH*0.65)
cy = y+(OUTPUT_HEIGHT*0.35)
cz = z+(OUTPUT_WIDTH*0.25)
elseif channel == 3 then
cx = x+(OUTPUT_WIDTH*0.35)
cy = y+(OUTPUT_HEIGHT*0.65)
cz = z+(OUTPUT_WIDTH*0.5)
else
cx = x+(OUTPUT_WIDTH*0.5)
cy = y+(OUTPUT_HEIGHT*0.5)
cz = z+(OUTPUT_WIDTH*0.75)
end;
local alpha = 0
local octave_index
for octave_index = 1, OCTAVES_COUNT do
size = OCTAVES[octave_index][1]
local opacity = OCTAVES[octave_index][2]
-- alpha = opacity * (get_perlin_noise(cx, cy, cz, size))
alpha = alpha + opacity * (2*get_perlin_noise(cx, cy, cz, size) - 1)
end
alpha = (alpha * NORM_FACTOR + AMPLITUDE_CORRECTION_FACTOR) *
(0.5 / AMPLITUDE_CORRECTION_FACTOR)
return alpha
end;
-- function truncate(value)
-- if value <= 0 then value = 0 end
-- if value >= 1 then value = 1 end
-- return value
-- end;