more lua support, fixed scene 3

This commit is contained in:
2021-04-30 23:29:40 +02:00
parent 694fdf8e2b
commit fcde73c1ef
19 changed files with 211 additions and 95 deletions
+5 -28
View File
@@ -1,28 +1,20 @@
local unpack = table.unpack or _G.unpack
math.randomseed(os.time())
local function randab(a, b)
return math.random() * (b-a) + a
end
util.seed()
local function randtable(n, a, b)
local t = {}
for i=1, n do
t[i] = randab(a, b)
t[i] = util.randab(a, b)
end
return t
end
local function choice(list)
return list[math.random(1, #list)]
end
local function randomsphere()
local pos = vec3.new(unpack(randtable(3, -2, 2)))
local radius = randab(0, 0.5)
local radius = util.randab(0, 0.5)
local emission = colorvec.new(unpack(randtable(4, 0, 1)))
local reflection = colormat.new(unpack(randtable(16, 0, 1)))
local surfacetype = choice({surfacetype.DIFFUSE, surfacetype.REFLECTIVE, surfacetype.STOP})
local surfacetype = util.choice({surfacetype.DIFFUSE, surfacetype.REFLECTIVE, surfacetype.STOP})
return obj.withmaterial(
obj.sphere(pos, radius),
@@ -30,23 +22,8 @@ local function randomsphere()
)
end
local function union(objs)
local n = #objs
if n == 1 then return objs[1] end
local mid = n//2
local left, right = {}, {}
for i=1, mid do
left[i] = objs[i]
end
for i=mid+1, n do
right[i-mid] = objs[i]
end
return obj.union(union(left), union(right))
end
local spheres = {}
for i=1, 100 do
spheres[i] = randomsphere()
end
return union(spheres)
return util.union(spheres)
+11 -34
View File
@@ -1,30 +1,22 @@
local unpack = table.unpack or _G.unpack
math.randomseed(os.time())
util.seed()
local SCALE = 0.3
local N_EACH = 25
local function randab(a, b)
return math.random() * (b-a) + a
end
local function randtable(n, a, b)
local t = {}
for i=1, n do
t[i] = randab(a, b)
t[i] = util.randab(a, b)
end
return t
end
local function choice(list)
return list[math.random(1, #list)]
end
local MATERIALS = {surfacetype.DIFFUSE, surfacetype.REFLECTIVE, surfacetype.STOP}
local function randommaterial()
local emission = colorvec.new(unpack(randtable(4, 0, 1)))
local reflection = colormat.new(unpack(randtable(16, 0, 1)))
local surfacetype = choice(MATERIALS)
local surfacetype = util.choice(MATERIALS)
return material.new(emission, reflection, surfacetype)
end
@@ -62,20 +54,20 @@ local ORIENTATIONS = {
)
}
local function randomorientation()
return choice(ORIENTATIONS)
return util.choice(ORIENTATIONS)
end
local function randomsphere()
local pos = vec3.new(unpack(randtable(3, -2, 2)))
local radius = randab(0, SCALE)
local radius = util.randab(0, SCALE)
return obj.sphere(pos, radius)
end
local function randomtorus()
local pos = vec3.new(unpack(randtable(3, -2, 2)))
local radius = randab(0, SCALE)
local thickness = randab(radius/4, radius)
local radius = util.randab(0, SCALE)
local thickness = util.randab(radius/4, radius)
local orientation = randomorientation()
return obj.affinetransform(obj.torus(pos, radius, thickness), orientation, vec3.O)
@@ -83,15 +75,15 @@ end
local function randomcuboid()
local pos = vec3.new(unpack(randtable(3, -2, 2)))
local radius = randab(0, SCALE)
local radius = util.randab(0, SCALE)
return obj.cuboid(pos, vec3.new(radius, radius, radius))
end
local function randomcylinder()
local pos = vec3.new(unpack(randtable(3, -2, 2)))
local radius = randab(0, SCALE)
local height = randab(0, SCALE)
local radius = util.randab(0, SCALE)
local height = util.randab(0, SCALE)
local orientation = randomorientation()
return obj.affinetransform(
@@ -104,25 +96,10 @@ local function randomcylinder()
)
end
local function union(objs)
local n = #objs
if n == 1 then return objs[1] end
local mid = n//2
local left, right = {}, {}
for i=1, mid do
left[i] = objs[i]
end
for i=mid+1, n do
right[i-mid] = objs[i]
end
return obj.union(union(left), union(right))
end
local objects = {}
for i=1, N_EACH do table.insert(objects, randomsphere()) end
for i=1, N_EACH do table.insert(objects, randomtorus()) end
for i=1, N_EACH do table.insert(objects, randomcuboid()) end
for i=1, N_EACH do table.insert(objects, randomcylinder()) end
for i, object in ipairs(objects) do objects[i] = obj.withmaterial(object, randommaterial()) end
return union(objects)
return util.union(objects)