-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3d_sphere_with_craters.lua
233 lines (194 loc) · 5.06 KB
/
3d_sphere_with_craters.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
-- 3d sphere with craters/spherical height maps
function prepare()
--noise precalc
toRad = 180/math.pi
radius = get_slider_input(RADIUS)
p = {}
math.randomseed( get_intslider_input(SEED) )
for i=0,255 do
p[i] = math.random(255)
p[256+i] = p[i]
end
-- noise constants
ROUGHNESS_THRESHOLD = 0.00001
REMAINDER_THRESHOLD = 0.00001
-- noise input values
local details = get_slider_input(DETAILS) * 10 + 1
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) * (1.0 - ROUGHNESS_THRESHOLD)
OCTAVES = {}
local cell_size = (0.01 + NOISE_SIZE * 0.99) * 10
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
--other
bumpiness = get_slider_input(BUMPINESS)
unbump = 1.0-bumpiness
crater_roughness = get_slider_input(CRATER_ROUGHNESS)* 0.8
crater_overlap = 1.0 - (get_slider_input(CRATER_OVERLAP)* 0.9)
max_crater_size = get_slider_input(MAX_CRATER_SIZE)* 0.2
--make craters
cx = {}
cy = {}
cz = {}
ra = {}
rsq = {}
ckill = {}
count = get_intslider_input(CRATER_COUNT)
max_crater = 0.15
local dead = 0
for i=1,count do
dead = 1
while(dead == 1) do
local x = 0
local y = 0
local z = 0
while x == 0 and y == 0 and z == 0 do
x = -1.0 + (math.random()*2.0)
y = -1.0 + (math.random()*2.0)
z = -1.0 + (math.random()*1.0)
end
local len = math.sqrt((x*x)+(y*y)+(z*z))
cx[i] = x/len
cy[i] = y/len
cz[i] = z/len
ra[i] = 0.01+(math.random()*max_crater_size)
rsq[i] = ra[i]*ra[i]
ckill[i] = 0
--too close to another?
if i > 1 then
for j = 1, i-1 do
local xd = cx[i] - cx[j]
local yd = cy[i] - cy[j]
local zd = cz[i] - cz[j]
local d = (xd*xd)+(yd*yd)+(zd*zd)
local d2 = (ra[i]+ra[j])*crater_overlap
if d < d2*d2 then
ckill[i] = 1
break
end
end
end
dead = ckill[i]
end
end
end;
function get_sample(x, y)
-- image generation
--[[
--]]
-- sphere map block
local px = (x*2.0) - 1.0
local py = (y*2.0) - 1.0
px = px/radius
py = py/radius
local len = math.sqrt((px*px)+(py*py))
if len > 1.0 then return 0,0,0,0 end
local z = -math.sqrt(1.0 - ((px*px)+(py*py)))
--[[
-- spherical map block
local x = x * aspect * math.pi
local y = y * math.pi
px = math.cos(x) * math.sin(y)
py = math.sin(x) * math.sin(y)
z = math.cos(y)
-- end
--]]
local n = 0
local octave_index
for octave_index = 1, OCTAVES_COUNT do
local size = OCTAVES[octave_index][1]
local opacity = OCTAVES[octave_index][2]
local noise_z = octave_index
n = n + (opacity * noise(px*size,py*size,(z*size)+noise_z))
end
n = (n+1.0)/2.0
n = get_sample_curve(x, y, n, HEIGHT_CURVE)
local n1 = n
local crater = get_sample_curve(x,y, 0, CRATER_CURVE)
local base_height = crater
for i=1,count do
local r = ra[i] - (crater_roughness*ra[i]*n1)
local xd = px - cx[i]
local yd = py - cy[i]
local zd = z - cz[i]
local d = (xd*xd)+(yd*yd)+(zd*zd)
if d < r*r then
d = 1.0 - (math.sqrt(d)/r)
local crater2 = get_sample_curve(x, y, d, CRATER_CURVE)-- * (r/max_crater)
if crater2 < base_height then
crater = crater2
else
crater = crater+(crater2-base_height)
end
end
end
n = (crater * 0.5) + (n*0.5)
n1 = n
n = -z*unbump+(n*bumpiness)
return n,n,n,n1
end;
function noise(x, y, z)
local X = math.floor(x) % 256
local Y = math.floor(y) % 256
local Z = math.floor(z) % 256
x = x - math.floor(x)
y = y - math.floor(y)
z = z - math.floor(z)
local u = fade(x)
local v = fade(y)
local w = fade(z)
A = p[X ]+Y
AA = p[A]+Z
AB = p[A+1]+Z
B = p[X+1]+Y
BA = p[B]+Z
BB = p[B+1]+Z
return lerp(w, lerp(v, lerp(u, grad(p[AA ], x , y , z ),
grad(p[BA ], x-1, y , z )),
lerp(u, grad(p[AB ], x , y-1, z ),
grad(p[BB ], x-1, y-1, z ))),
lerp(v, lerp(u, grad(p[AA+1], x , y , z-1 ),
grad(p[BA+1], x-1, y , z-1 )),
lerp(u, grad(p[AB+1], x , y-1, z-1 ),
grad(p[BB+1], x-1, y-1, z-1 )))
)
end;
function fade(t)
return t * t * t * (t * (t * 6 - 15) + 10)
end;
function lerp(t,a,b)
return a + t * (b - a)
end;
function grad(hash,x,y,z)
local h = hash % 16
local u
local v
if (h<8) then u = x else u = y end
if (h<4) then v = y elseif (h==12 or h==14) then v=x else v=z end
local r
if ((h%2) == 0) then r=u else r=-u end
if ((h%4) == 0) then r=r+v else r=r-v end
return r
end;