56 lines
1.3 KiB
Luau
56 lines
1.3 KiB
Luau
--!strict
|
|
--!native
|
|
-- model/vox/Grid: XYZI records to and from a dense grid (cell x + sx * (y + sy * z); index 0 = empty). Never throws.
|
|
local Grid = {}
|
|
|
|
local readu8, writeu8 = buffer.readu8, buffer.writeu8
|
|
|
|
function Grid.fill(dst: buffer, sx: number, sy: number, sz: number, src: buffer, off: number, n: number): number
|
|
local skipped = 0
|
|
local sxy = sx * sy
|
|
for k = 0, n - 1 do
|
|
local p = off + k * 4
|
|
local x, y, z, i = readu8(src, p), readu8(src, p + 1), readu8(src, p + 2), readu8(src, p + 3)
|
|
if x < sx and y < sy and z < sz and i ~= 0 then
|
|
writeu8(dst, x + sx * y + sxy * z, i)
|
|
else
|
|
skipped += 1
|
|
end
|
|
end
|
|
return skipped
|
|
end
|
|
|
|
function Grid.count(data: buffer, cells: number): number
|
|
local n = 0
|
|
for k = 0, cells - 1 do
|
|
if readu8(data, k) ~= 0 then
|
|
n += 1
|
|
end
|
|
end
|
|
return n
|
|
end
|
|
|
|
-- one XYZI record per non-empty cell (x fastest; sx, sy, sz <= 256)
|
|
function Grid.write(dst: buffer, at: number, data: buffer, sx: number, sy: number, sz: number): number
|
|
local p = at
|
|
local k = 0
|
|
for z = 0, sz - 1 do
|
|
for y = 0, sy - 1 do
|
|
for x = 0, sx - 1 do
|
|
local i = readu8(data, k)
|
|
if i ~= 0 then
|
|
writeu8(dst, p, x)
|
|
writeu8(dst, p + 1, y)
|
|
writeu8(dst, p + 2, z)
|
|
writeu8(dst, p + 3, i)
|
|
p += 4
|
|
end
|
|
k += 1
|
|
end
|
|
end
|
|
end
|
|
return p
|
|
end
|
|
|
|
return Grid
|