771 lines
25 KiB
Luau
771 lines
25 KiB
Luau
--!strict
|
|
-- codecs/model/Vrml/Geometry: VRML97 / X3D geometry nodes to Scene primitives. Written from the specifications; no
|
|
-- third-party code. IndexedFaceSet (coord / normal / color / texCoord with their own indices, per-vertex or per-face
|
|
-- normals and colours, ccw, convex polygons fan-triangulated or kept as "polygons"), the X3D triangle / quad sets
|
|
-- (IndexedTriangleSet, TriangleSet, IndexedTriangleFanSet, TriangleFanSet, IndexedTriangleStripSet,
|
|
-- TriangleStripSet, IndexedQuadSet, QuadSet), ElevationGrid, Extrusion (spine-aligned cross sections, scale,
|
|
-- orientation, caps), IndexedLineSet / LineSet ("lines"), PointSet ("points"), and the primitives Box, Sphere, Cone,
|
|
-- Cylinder tessellated with normals and the specification's texture mapping. Texture coordinates get t flipped to
|
|
-- glTF's top-left origin unless flipV = false. Never throws: returns nil, message for bad data.
|
|
local Types = require("@formats/Types")
|
|
local Fields = require("@formats/codecs/model/Vrml/Fields")
|
|
|
|
local Geometry = {}
|
|
|
|
local S = Types.Scene
|
|
local nums, num, vec, bool, node = Fields.nums, Fields.num, Fields.vec, Fields.bool, Fields.node
|
|
local sqrt, sin, cos, pi = math.sqrt, math.sin, math.cos, math.pi
|
|
|
|
export type Options = { flipV: boolean?, triangulate: boolean?, segments: number?, maxVertices: number? }
|
|
|
|
local function f32(list: { number }, comps: number, count: number): Types.Accessor
|
|
local a = S.accessor(nil, "f32", comps, count)
|
|
local d = a.data
|
|
for i = 1, math.min(#list, comps * count) do
|
|
buffer.writef32(d, (i - 1) * 4, list[i])
|
|
end
|
|
return a
|
|
end
|
|
|
|
local function u32(list: { number }): Types.Accessor
|
|
local a = S.accessor(nil, "u32", 1, #list)
|
|
for i, v in list do
|
|
buffer.writeu32(a.data, (i - 1) * 4, v)
|
|
end
|
|
return a
|
|
end
|
|
|
|
-- the colour node's values and component count (Color: 3, ColorRGBA: 4)
|
|
local function colors(n: any): ({ number }?, number)
|
|
local c = node(n, "color")
|
|
if not c then
|
|
return nil, 3
|
|
end
|
|
local comps = if c.type == "ColorRGBA" then 4 else 3
|
|
return nums(c, "color") or {}, comps
|
|
end
|
|
|
|
local function points(n: any, field: string, sub: string): { number }?
|
|
local c = node(n, field)
|
|
return if c then nums(c, sub) or {} else nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ polygon meshes
|
|
-- Mesh input: faces as corner lists of coordinate indices (0-based) plus, per attribute, a per-corner index list (nil:
|
|
-- the coordinate index) or per-face indices.
|
|
type PolyIn = {
|
|
P: { number }, -- coordinates, 3 per point
|
|
corners: { number }, -- coordinate index per corner, face after face
|
|
sizes: { number }, -- corners per face
|
|
N: { number }?, nIdx: { number }?, nFace: boolean?, -- normals; per-corner or per-face indices (nil: identity)
|
|
C: { number }?, cComps: number?, cIdx: { number }?, cFace: boolean?,
|
|
T: { number }?, tIdx: { number }?,
|
|
ccw: boolean,
|
|
}
|
|
|
|
local function polyMesh(m: PolyIn, o: Options, maxV: number): (any, string?)
|
|
local P, corners, sizes = m.P, m.corners, m.sizes
|
|
local np = #P // 3
|
|
local N, C, T = m.N, m.C, m.T
|
|
local cc = m.cComps or 3
|
|
-- the attribute index of corner k (1-based) in face f (1-based)
|
|
local function idx(list: { number }?, perFace: boolean?, f: number, k: number, c: number): number
|
|
if perFace then
|
|
return if list and #list > 0 then (list[f] or 0) else f - 1
|
|
end
|
|
return if list and #list > 0 then (list[k] or c) else c
|
|
end
|
|
local simple = (N == nil or (not m.nFace and (m.nIdx == nil or #m.nIdx == 0))) and (C == nil or (not m.cFace and (m.cIdx == nil or #m.cIdx == 0))) and (T == nil or m.tIdx == nil or #m.tIdx == 0)
|
|
local vkey: { [string]: number } = {}
|
|
local vsrc: { { number } } = {} -- per output vertex: coord, normal, colour, texcoord indices
|
|
local out: { number } = {}
|
|
local outSizes: { number } = {}
|
|
local k = 0
|
|
for f, size in sizes do
|
|
local face: { number } = {}
|
|
local valid = size >= 3
|
|
for q = 1, size do
|
|
k += 1
|
|
local c = corners[k]
|
|
if c == nil or c < 0 or c >= np then
|
|
valid = false
|
|
end
|
|
face[q] = k
|
|
end
|
|
if valid then
|
|
local verts: { number } = {}
|
|
for q, kk in face do
|
|
local c = corners[kk]
|
|
if simple then
|
|
verts[q] = c
|
|
else
|
|
local ni = if N then idx(m.nIdx, m.nFace, f, kk, c) else -1
|
|
local ci = if C then idx(m.cIdx, m.cFace, f, kk, c) else -1
|
|
local ti = if T then idx(m.tIdx, false, f, kk, c) else -1
|
|
local key = c .. ":" .. ni .. ":" .. ci .. ":" .. ti
|
|
local v = vkey[key]
|
|
if not v then
|
|
v = #vsrc
|
|
vsrc[v + 1] = { c, ni, ci, ti }
|
|
vkey[key] = v
|
|
if v >= maxV then
|
|
return nil, "limit: more than " .. maxV .. " vertices"
|
|
end
|
|
end
|
|
verts[q] = v
|
|
end
|
|
end
|
|
if not m.ccw then
|
|
for a = 2, (size + 1) // 2 do
|
|
verts[a], verts[size + 2 - a] = verts[size + 2 - a], verts[a]
|
|
end
|
|
end
|
|
if o.triangulate == false then
|
|
table.insert(outSizes, size)
|
|
for _, v in verts do
|
|
table.insert(out, v)
|
|
end
|
|
else
|
|
for a = 2, size - 1 do
|
|
table.insert(out, verts[1])
|
|
table.insert(out, verts[a])
|
|
table.insert(out, verts[a + 1])
|
|
end
|
|
end
|
|
end
|
|
end
|
|
local nv = if simple then np else #vsrc
|
|
if nv > maxV then
|
|
return nil, "limit: more than " .. maxV .. " vertices"
|
|
end
|
|
local flipV = o.flipV ~= false
|
|
local pos = S.accessor(nil, "f32", 3, nv)
|
|
local nrm = if N then S.accessor(nil, "f32", 3, nv) else nil
|
|
local col = if C then S.accessor(nil, "f32", cc, nv) else nil
|
|
local tex = if T then S.accessor(nil, "f32", 2, nv) else nil
|
|
local wf = buffer.writef32
|
|
for v = 0, nv - 1 do
|
|
local s = if simple then nil else vsrc[v + 1]
|
|
local c = if s then s[1] else v
|
|
for q = 0, 2 do
|
|
wf(pos.data, (v * 3 + q) * 4, P[c * 3 + q + 1] or 0)
|
|
end
|
|
if nrm and N then
|
|
local ni = if s then s[2] else v
|
|
for q = 0, 2 do
|
|
wf(nrm.data, (v * 3 + q) * 4, N[ni * 3 + q + 1] or 0)
|
|
end
|
|
end
|
|
if col and C then
|
|
local ci = if s then s[3] else v
|
|
for q = 0, cc - 1 do
|
|
wf(col.data, (v * cc + q) * 4, C[ci * cc + q + 1] or 1)
|
|
end
|
|
end
|
|
if tex and T then
|
|
local ti = if s then s[4] else v
|
|
local tt = T[ti * 2 + 2] or 0
|
|
wf(tex.data, v * 8, T[ti * 2 + 1] or 0)
|
|
wf(tex.data, v * 8 + 4, if flipV then 1 - tt else tt)
|
|
end
|
|
end
|
|
local attrs: { [string]: Types.Accessor } = { POSITION = pos }
|
|
attrs.NORMAL, attrs.COLOR_0, attrs.TEXCOORD_0 = nrm, col, tex
|
|
local prim: Types.Primitive = { attributes = attrs, indices = u32(out), mode = "triangles" }
|
|
if o.triangulate == false then
|
|
prim.mode = "polygons"
|
|
prim.faces = u32(outSizes)
|
|
end
|
|
return prim
|
|
end
|
|
|
|
-- split a -1 separated index list into corners + sizes
|
|
local function split(list: { number }): ({ number }, { number })
|
|
local corners, sizes = {}, {}
|
|
local run = 0
|
|
for _, x in list do
|
|
if x < 0 then
|
|
if run > 0 then
|
|
table.insert(sizes, run)
|
|
end
|
|
run = 0
|
|
else
|
|
table.insert(corners, x)
|
|
run += 1
|
|
end
|
|
end
|
|
if run > 0 then
|
|
table.insert(sizes, run)
|
|
end
|
|
return corners, sizes
|
|
end
|
|
|
|
-- per-face index lists keep only the entries of faces (the -1 separators of per-corner lists match the corners)
|
|
local function cornerList(list: { number }?): { number }?
|
|
if not list or #list == 0 then
|
|
return nil
|
|
end
|
|
local out = {}
|
|
for _, x in list do
|
|
if x >= 0 then
|
|
table.insert(out, x)
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
local function faceSet(n: any, o: Options, maxV: number): (Types.Primitive?, string?)
|
|
local P = points(n, "coord", "point") or {}
|
|
local corners, sizes = split(nums(n, "coordIndex") or {})
|
|
local nPer, cPer = bool(n, "normalPerVertex", true), bool(n, "colorPerVertex", true)
|
|
local C, cc = colors(n)
|
|
local tc = node(n, "texCoord")
|
|
local m: PolyIn = {
|
|
P = P, corners = corners, sizes = sizes, ccw = bool(n, "ccw", true),
|
|
N = points(n, "normal", "vector"), nFace = not nPer, nIdx = if nPer then cornerList(nums(n, "normalIndex")) else nums(n, "normalIndex"),
|
|
C = C, cComps = cc, cFace = not cPer, cIdx = if cPer then cornerList(nums(n, "colorIndex")) else nums(n, "colorIndex"),
|
|
T = if tc and tc.type ~= "TextureCoordinateGenerator" then (nums(if tc.type == "MultiTextureCoordinate" then (node(tc, "texCoord") or tc) else tc, "point") or {}) else nil,
|
|
tIdx = cornerList(nums(n, "texCoordIndex")),
|
|
}
|
|
return polyMesh(m, o, maxV)
|
|
end
|
|
|
|
-- the X3D triangle / quad sets: faces from index (or vertex order) and fan / strip counts
|
|
local function triSet(n: any, o: Options, maxV: number): (Types.Primitive?, string?)
|
|
local t = n.type
|
|
local P = points(n, "coord", "point") or {}
|
|
local np = #P // 3
|
|
local index = nums(n, "index")
|
|
local seq: { number }
|
|
if string.sub(t, 1, 7) == "Indexed" then
|
|
seq = index or {}
|
|
else
|
|
seq = {}
|
|
for i = 0, np - 1 do
|
|
seq[i + 1] = i
|
|
end
|
|
end
|
|
local corners, sizes = {}, {}
|
|
local function tri(a: number, b: number, c: number)
|
|
table.insert(corners, a)
|
|
table.insert(corners, b)
|
|
table.insert(corners, c)
|
|
table.insert(sizes, 3)
|
|
end
|
|
-- runs: -1 separated (indexed fans / strips) or fanCount / stripCount (plain fans / strips)
|
|
local function runs(): { { number } }
|
|
local out = {}
|
|
local counts = nums(n, if string.find(t, "Fan", 1, true) then "fanCount" else "stripCount")
|
|
if string.sub(t, 1, 7) == "Indexed" or not counts then
|
|
local cur = {}
|
|
for _, x in seq do
|
|
if x < 0 then
|
|
table.insert(out, cur)
|
|
cur = {}
|
|
else
|
|
table.insert(cur, x)
|
|
end
|
|
end
|
|
table.insert(out, cur)
|
|
else
|
|
local at = 1
|
|
for _, c in counts do
|
|
local cur = {}
|
|
for q = 0, c - 1 do
|
|
cur[q + 1] = seq[at + q]
|
|
end
|
|
at += c
|
|
table.insert(out, cur)
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
if t == "IndexedTriangleSet" or t == "TriangleSet" then
|
|
for i = 1, #seq - 2, 3 do
|
|
tri(seq[i], seq[i + 1], seq[i + 2])
|
|
end
|
|
elseif t == "IndexedQuadSet" or t == "QuadSet" then
|
|
for i = 1, #seq - 3, 4 do
|
|
for q = 0, 3 do
|
|
table.insert(corners, seq[i + q])
|
|
end
|
|
table.insert(sizes, 4)
|
|
end
|
|
elseif string.find(t, "Fan", 1, true) then
|
|
for _, r in runs() do
|
|
for i = 2, #r - 1 do
|
|
tri(r[1], r[i], r[i + 1])
|
|
end
|
|
end
|
|
else
|
|
for _, r in runs() do
|
|
for i = 1, #r - 2 do
|
|
if i % 2 == 1 then
|
|
tri(r[i], r[i + 1], r[i + 2])
|
|
else
|
|
tri(r[i + 1], r[i], r[i + 2])
|
|
end
|
|
end
|
|
end
|
|
end
|
|
local nPer, cPer = bool(n, "normalPerVertex", true), bool(n, "colorPerVertex", true)
|
|
local C, cc = colors(n)
|
|
local tc = node(n, "texCoord")
|
|
local m: PolyIn = {
|
|
P = P, corners = corners, sizes = sizes, ccw = bool(n, "ccw", true),
|
|
N = points(n, "normal", "vector"), nFace = not nPer, C = C, cComps = cc, cFace = not cPer,
|
|
T = if tc and tc.type == "TextureCoordinate" then nums(tc, "point") or {} else nil,
|
|
}
|
|
return polyMesh(m, o, maxV)
|
|
end
|
|
|
|
local function elevationGrid(n: any, o: Options, maxV: number): (Types.Primitive?, string?)
|
|
local nx, nz = math.floor(num(n, "xDimension", 0)), math.floor(num(n, "zDimension", 0))
|
|
local dx, dz = num(n, "xSpacing", 1), num(n, "zSpacing", 1)
|
|
local h: { number } = nums(n, "height") or {}
|
|
if nx < 2 or nz < 2 then
|
|
return nil, nil
|
|
end
|
|
if nx * nz > maxV then
|
|
return nil, "limit: elevation grid of " .. nx .. " x " .. nz
|
|
end
|
|
local P: { number }, T: { number }, corners: { number }, sizes: { number } = {}, {}, {}, {}
|
|
for z = 0, nz - 1 do
|
|
for x = 0, nx - 1 do
|
|
table.insert(P, x * dx)
|
|
table.insert(P, h[z * nx + x + 1] or 0)
|
|
table.insert(P, z * dz)
|
|
table.insert(T, x / (nx - 1))
|
|
table.insert(T, z / (nz - 1))
|
|
end
|
|
end
|
|
for z = 0, nz - 2 do
|
|
for x = 0, nx - 2 do
|
|
local a = z * nx + x
|
|
-- counter-clockwise seen from +y: a, a + nx, a + nx + 1, a + 1
|
|
for _, c in { a, a + nx, a + nx + 1, a + 1 } do
|
|
table.insert(corners, c)
|
|
end
|
|
table.insert(sizes, 4)
|
|
end
|
|
end
|
|
local C, cc = colors(n)
|
|
local tc = node(n, "texCoord")
|
|
local m: PolyIn = {
|
|
P = P, corners = corners, sizes = sizes, ccw = bool(n, "ccw", true),
|
|
N = points(n, "normal", "vector"), nFace = not bool(n, "normalPerVertex", true), C = C, cComps = cc,
|
|
cFace = not bool(n, "colorPerVertex", true), T = if tc then nums(tc, "point") or {} else T,
|
|
}
|
|
return polyMesh(m, o, maxV)
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ Extrusion
|
|
local function cross(a: { number }, b: { number }): { number }
|
|
return { a[2] * b[3] - a[3] * b[2], a[3] * b[1] - a[1] * b[3], a[1] * b[2] - a[2] * b[1] }
|
|
end
|
|
local function sub3(a: { number }, b: { number }): { number }
|
|
return { a[1] - b[1], a[2] - b[2], a[3] - b[3] }
|
|
end
|
|
local function norm(a: { number }): ({ number }, number)
|
|
local l = sqrt(a[1] * a[1] + a[2] * a[2] + a[3] * a[3])
|
|
return if l > 1e-12 then { a[1] / l, a[2] / l, a[3] / l } else { 0, 0, 0 }, l
|
|
end
|
|
-- rotate v about the unit axis k by angle a (Rodrigues)
|
|
local function rotate(v: { number }, k: { number }, a: number): { number }
|
|
local c, s = cos(a), sin(a)
|
|
local d = (k[1] * v[1] + k[2] * v[2] + k[3] * v[3]) * (1 - c)
|
|
local kv = cross(k, v)
|
|
return { v[1] * c + kv[1] * s + k[1] * d, v[2] * c + kv[2] * s + k[2] * d, v[3] * c + kv[3] * s + k[3] * d }
|
|
end
|
|
|
|
local function extrusion(n: any, o: Options, maxV: number): (Types.Primitive?, string?)
|
|
local sp = nums(n, "spine") or { 0, 0, 0, 0, 1, 0 }
|
|
local cs = nums(n, "crossSection") or { 1, 1, 1, -1, -1, -1, -1, 1, 1, 1 }
|
|
local sc = nums(n, "scale") or { 1, 1 }
|
|
local ori = nums(n, "orientation") or { 0, 0, 1, 0 }
|
|
local ns, nc = #sp // 3, #cs // 2
|
|
if ns < 2 or nc < 2 then
|
|
return nil, nil
|
|
end
|
|
if ns * nc > maxV then
|
|
return nil, "limit: extrusion of " .. ns .. " x " .. nc
|
|
end
|
|
local spine = {}
|
|
for i = 1, ns do
|
|
spine[i] = { sp[i * 3 - 2], sp[i * 3 - 1], sp[i * 3] }
|
|
end
|
|
local function same(a: { number }, b: { number }): boolean
|
|
return math.abs(a[1] - b[1]) < 1e-9 and math.abs(a[2] - b[2]) < 1e-9 and math.abs(a[3] - b[3]) < 1e-9
|
|
end
|
|
local closedSpine = same(spine[1], spine[ns])
|
|
local closedCs = math.abs(cs[1] - cs[nc * 2 - 1]) < 1e-9 and math.abs(cs[2] - cs[nc * 2]) < 1e-9
|
|
-- spine-aligned cross-section planes: Y along the spine, Z from the bend, X = Y x Z
|
|
local Ys: { { number } }, Zs: { [number]: { number } } = {}, {}
|
|
for i = 1, ns do
|
|
local prev = if i > 1 then spine[i - 1] elseif closedSpine then spine[ns - 1] else nil
|
|
local nxt = if i < ns then spine[i + 1] elseif closedSpine then spine[2] else nil
|
|
local y = sub3(nxt or spine[i], prev or spine[i])
|
|
Ys[i] = norm(y)
|
|
if prev and nxt then
|
|
local z, l = norm(cross(sub3(nxt, spine[i]), sub3(prev, spine[i])))
|
|
Zs[i] = if l > 1e-12 then z else nil
|
|
end
|
|
end
|
|
-- missing Z: copy a neighbour's; a straight spine rotates (0, 0, 1) like (0, 1, 0) onto the spine direction
|
|
local firstZ = nil
|
|
for i = 1, ns do
|
|
if Zs[i] then
|
|
firstZ = Zs[i]
|
|
break
|
|
end
|
|
end
|
|
for i = 1, ns do
|
|
if not Ys[i] or (Ys[i][1] == 0 and Ys[i][2] == 0 and Ys[i][3] == 0) then
|
|
Ys[i] = Ys[i - 1] or { 0, 1, 0 }
|
|
end
|
|
end
|
|
for i = 1, ns do
|
|
if not Zs[i] then
|
|
if firstZ then
|
|
Zs[i] = Zs[i - 1] or firstZ
|
|
else
|
|
local y = Ys[i]
|
|
local axis, l = norm(cross({ 0, 1, 0 }, y))
|
|
local ang = math.acos(math.clamp(y[2], -1, 1))
|
|
Zs[i] = if l > 1e-12 then rotate({ 0, 0, 1 }, axis, ang) elseif y[2] < 0 then { 0, 0, -1 } else { 0, 0, 1 }
|
|
end
|
|
end
|
|
if i > 1 and Zs[i][1] * Zs[i - 1][1] + Zs[i][2] * Zs[i - 1][2] + Zs[i][3] * Zs[i - 1][3] < 0 then
|
|
Zs[i] = { -Zs[i][1], -Zs[i][2], -Zs[i][3] }
|
|
end
|
|
end
|
|
local nr = if closedSpine then ns - 1 else ns
|
|
local nring = if closedCs then nc - 1 else nc
|
|
local P = {}
|
|
for i = 1, nr do
|
|
local y, z = Ys[i], Zs[i]
|
|
local x = cross(y, z)
|
|
local si = if #sc >= i * 2 then i else 1
|
|
local sx, sz = sc[si * 2 - 1] or 1, sc[si * 2] or 1
|
|
local oi = if #ori >= i * 4 then i else 1
|
|
local ax, ang = norm({ ori[oi * 4 - 3] or 0, ori[oi * 4 - 2] or 0, ori[oi * 4 - 1] or 1 }), ori[oi * 4] or 0
|
|
for j = 1, nring do
|
|
local v = { cs[j * 2 - 1] * sx, 0, cs[j * 2] * sz }
|
|
if ang ~= 0 then
|
|
v = rotate(v, ax, ang)
|
|
end
|
|
local s = spine[i]
|
|
table.insert(P, s[1] + x[1] * v[1] + y[1] * v[2] + z[1] * v[3])
|
|
table.insert(P, s[2] + x[2] * v[1] + y[2] * v[2] + z[2] * v[3])
|
|
table.insert(P, s[3] + x[3] * v[1] + y[3] * v[2] + z[3] * v[3])
|
|
end
|
|
end
|
|
local corners, sizes = {}, {}
|
|
local function at(i: number, j: number): number
|
|
return ((i - 1) % nr) * nring + (j - 1) % nring
|
|
end
|
|
for i = 1, ns - 1 do
|
|
for j = 1, nc - 1 do
|
|
for _, c in { at(i, j), at(i + 1, j), at(i + 1, j + 1), at(i, j + 1) } do
|
|
table.insert(corners, c)
|
|
end
|
|
table.insert(sizes, 4)
|
|
end
|
|
end
|
|
if not closedSpine then
|
|
if bool(n, "beginCap", true) then
|
|
for j = 1, nring do
|
|
table.insert(corners, at(1, j))
|
|
end
|
|
table.insert(sizes, nring)
|
|
end
|
|
if bool(n, "endCap", true) then
|
|
for j = nring, 1, -1 do
|
|
table.insert(corners, at(ns, j))
|
|
end
|
|
table.insert(sizes, nring)
|
|
end
|
|
end
|
|
return polyMesh({ P = P, corners = corners, sizes = sizes, ccw = bool(n, "ccw", true) }, o, maxV)
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ lines, points
|
|
local function lines(n: any, maxV: number): (any, string?)
|
|
local P = points(n, "coord", "point") or {}
|
|
local np = #P // 3
|
|
local C, cc = colors(n)
|
|
local perVertex = bool(n, "colorPerVertex", true)
|
|
local runs: { { number } } = {}
|
|
if n.type == "LineSet" then
|
|
local at = 0
|
|
for _, c in nums(n, "vertexCount") or {} do
|
|
local r = {}
|
|
for q = 0, c - 1 do
|
|
r[q + 1] = at + q
|
|
end
|
|
at += c
|
|
table.insert(runs, r)
|
|
end
|
|
else
|
|
local corners, sizes = split(nums(n, "coordIndex") or {})
|
|
local k = 0
|
|
for _, s in sizes do
|
|
local r = {}
|
|
for q = 1, s do
|
|
r[q] = corners[k + q]
|
|
end
|
|
k += s
|
|
table.insert(runs, r)
|
|
end
|
|
end
|
|
local cIdx = nums(n, "colorIndex") or {}
|
|
local cList = if perVertex then cornerList(cIdx) else nil
|
|
local vkey: { [string]: number } = {}
|
|
local src: { { number } } = {}
|
|
local out: { number } = {}
|
|
local k = 0
|
|
for li, r in runs do
|
|
local prev: number? = nil
|
|
for _, c in r do
|
|
k += 1
|
|
if c == nil or c < 0 or c >= np then
|
|
prev = nil
|
|
continue
|
|
end
|
|
local ci = if not C then -1 elseif perVertex then (if cList then cList[k] or c else c) else (cIdx[li] or li - 1)
|
|
local key = c .. ":" .. ci
|
|
local v = vkey[key]
|
|
if not v then
|
|
v = #src
|
|
src[v + 1] = { c, ci }
|
|
vkey[key] = v
|
|
end
|
|
if prev then
|
|
table.insert(out, prev)
|
|
table.insert(out, v)
|
|
end
|
|
prev = v
|
|
end
|
|
end
|
|
if #src > maxV then
|
|
return nil, "limit: more than " .. maxV .. " vertices"
|
|
end
|
|
local pos = S.accessor(nil, "f32", 3, #src)
|
|
local col = if C then S.accessor(nil, "f32", cc, #src) else nil
|
|
for v, s in src do
|
|
for q = 0, 2 do
|
|
buffer.writef32(pos.data, ((v - 1) * 3 + q) * 4, P[s[1] * 3 + q + 1])
|
|
end
|
|
if col and C then
|
|
for q = 0, cc - 1 do
|
|
buffer.writef32(col.data, ((v - 1) * cc + q) * 4, C[s[2] * cc + q + 1] or 1)
|
|
end
|
|
end
|
|
end
|
|
return { attributes = { POSITION = pos, COLOR_0 = col } :: any, indices = u32(out), mode = "lines" }
|
|
end
|
|
|
|
local function pointSet(n: any, maxV: number): (any, string?)
|
|
local P = points(n, "coord", "point") or {}
|
|
local np = #P // 3
|
|
if np > maxV then
|
|
return nil, "limit: more than " .. maxV .. " points"
|
|
end
|
|
local C, cc = colors(n)
|
|
local attrs: { [string]: Types.Accessor } = { POSITION = f32(P, 3, np) }
|
|
if C then
|
|
local col = S.accessor(nil, "f32", cc, np)
|
|
for i = 0, np * cc - 1 do
|
|
buffer.writef32(col.data, i * 4, C[i + 1] or 1)
|
|
end
|
|
attrs.COLOR_0 = col
|
|
end
|
|
return { attributes = attrs, mode = "points" }
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ primitives
|
|
-- a primitive from flat arrays (positions, normals, uvs with the specification's bottom-left origin, triangles)
|
|
local function direct(P: { number }, N: { number }, T: { number }, I: { number }, o: Options): Types.Primitive
|
|
local nv = #P // 3
|
|
if o.flipV ~= false then
|
|
for i = 2, #T, 2 do
|
|
T[i] = 1 - T[i]
|
|
end
|
|
end
|
|
return {
|
|
attributes = { POSITION = f32(P, 3, nv), NORMAL = f32(N, 3, nv), TEXCOORD_0 = f32(T, 2, nv) },
|
|
indices = u32(I),
|
|
mode = "triangles",
|
|
}
|
|
end
|
|
|
|
local function box(n: any, o: Options): Types.Primitive
|
|
local s = vec(n, "size", { 2, 2, 2 })
|
|
local hx, hy, hz = s[1] / 2, s[2] / 2, s[3] / 2
|
|
local P, N, T, I = {}, {}, {}, {}
|
|
-- faces: normal, u axis, v axis (the specification's texture orientation)
|
|
local FACES = {
|
|
{ { 0, 0, 1 }, { 1, 0, 0 }, { 0, 1, 0 } }, { { 0, 0, -1 }, { -1, 0, 0 }, { 0, 1, 0 } },
|
|
{ { 1, 0, 0 }, { 0, 0, -1 }, { 0, 1, 0 } }, { { -1, 0, 0 }, { 0, 0, 1 }, { 0, 1, 0 } },
|
|
{ { 0, 1, 0 }, { 1, 0, 0 }, { 0, 0, -1 } }, { { 0, -1, 0 }, { 1, 0, 0 }, { 0, 0, 1 } },
|
|
}
|
|
for f, face in FACES do
|
|
local nn, u, v = face[1], face[2], face[3]
|
|
for _, c in { { -1, -1 }, { 1, -1 }, { 1, 1 }, { -1, 1 } } do
|
|
for q = 1, 3 do
|
|
local h = if q == 1 then hx elseif q == 2 then hy else hz
|
|
table.insert(P, (nn[q] + u[q] * c[1] + v[q] * c[2]) * h)
|
|
table.insert(N, nn[q])
|
|
end
|
|
table.insert(T, (c[1] + 1) / 2)
|
|
table.insert(T, (c[2] + 1) / 2)
|
|
end
|
|
local b = (f - 1) * 4
|
|
for _, x in { 0, 1, 2, 0, 2, 3 } do
|
|
table.insert(I, b + x)
|
|
end
|
|
end
|
|
return direct(P, N, T, I, o)
|
|
end
|
|
|
|
-- a ring of segs + 1 vertices (the seam doubled for the texture) at height y, radius r; s starts at the back (-z)
|
|
-- and runs counter-clockwise seen from +y
|
|
local function ring(P: { number }, N: { number }?, T: { number }?, segs: number, y: number, r: number, t: number, ny: number?, nr: number?)
|
|
for i = 0, segs do
|
|
local a = 2 * pi * i / segs
|
|
local x, z = -sin(a), -cos(a)
|
|
table.insert(P, x * r)
|
|
table.insert(P, y)
|
|
table.insert(P, z * r)
|
|
if N then
|
|
table.insert(N, x * (nr or 1))
|
|
table.insert(N, ny or 0)
|
|
table.insert(N, z * (nr or 1))
|
|
end
|
|
if T then
|
|
table.insert(T, i / segs)
|
|
table.insert(T, t)
|
|
end
|
|
end
|
|
end
|
|
|
|
-- a cap disc at height y facing ny (+1 / -1)
|
|
local function cap(P: { number }, N: { number }, T: { number }, I: { number }, segs: number, y: number, r: number, ny: number)
|
|
local b = #P // 3
|
|
for i = 0, segs - 1 do
|
|
local a = 2 * pi * i / segs
|
|
local x, z = -sin(a), -cos(a)
|
|
table.insert(P, x * r)
|
|
table.insert(P, y)
|
|
table.insert(P, z * r)
|
|
table.insert(N, 0)
|
|
table.insert(N, ny)
|
|
table.insert(N, 0)
|
|
-- the specification maps the cap like a top view: s = x, t = -z (top) or z (bottom)
|
|
table.insert(T, x / 2 + 0.5)
|
|
table.insert(T, (if ny > 0 then -z else z) / 2 + 0.5)
|
|
end
|
|
for i = 1, segs - 2 do
|
|
-- the ring runs counter-clockwise seen from +y: in order for the top, reversed for the bottom
|
|
if ny > 0 then
|
|
table.insert(I, b)
|
|
table.insert(I, b + i)
|
|
table.insert(I, b + i + 1)
|
|
else
|
|
table.insert(I, b)
|
|
table.insert(I, b + i + 1)
|
|
table.insert(I, b + i)
|
|
end
|
|
end
|
|
end
|
|
|
|
-- quads between two rings of segs + 1 vertices starting at a (lower) and b (upper)
|
|
local function band(I: { number }, a: number, b: number, segs: number)
|
|
for i = 0, segs - 1 do
|
|
table.insert(I, a + i)
|
|
table.insert(I, a + i + 1)
|
|
table.insert(I, b + i + 1)
|
|
table.insert(I, a + i)
|
|
table.insert(I, b + i + 1)
|
|
table.insert(I, b + i)
|
|
end
|
|
end
|
|
|
|
local function sphere(n: any, o: Options, segs: number): Types.Primitive
|
|
local r = num(n, "radius", 1)
|
|
local rings = math.max(2, segs // 2)
|
|
local P, N, T, I = {}, {}, {}, {}
|
|
for j = 0, rings do
|
|
local phi = pi * j / rings -- from the bottom
|
|
local y, rr = -cos(phi), sin(phi)
|
|
ring(P, N, T, segs, y * r, rr * r, j / rings, y, rr)
|
|
end
|
|
for j = 0, rings - 1 do
|
|
band(I, j * (segs + 1), (j + 1) * (segs + 1), segs)
|
|
end
|
|
return direct(P, N, T, I, o)
|
|
end
|
|
|
|
local function cylinder(n: any, o: Options, segs: number, cone: boolean): Types.Primitive
|
|
local r = num(n, if cone then "bottomRadius" else "radius", 1)
|
|
local h = num(n, "height", 2)
|
|
local P, N, T, I = {}, {}, {}, {}
|
|
if bool(n, "side", true) then
|
|
-- side normals: a cone's lean outwards by its slope
|
|
local l = sqrt(r * r + h * h)
|
|
local ny, nr = if cone and l > 0 then r / l else 0, if cone and l > 0 then h / l else 1
|
|
ring(P, N, T, segs, -h / 2, r, 0, ny, nr)
|
|
ring(P, N, T, segs, h / 2, if cone then 0 else r, 1, ny, nr)
|
|
band(I, 0, segs + 1, segs)
|
|
end
|
|
if not cone and bool(n, "top", true) then
|
|
cap(P, N, T, I, segs, h / 2, r, 1)
|
|
end
|
|
if bool(n, "bottom", true) then
|
|
cap(P, N, T, I, segs, -h / 2, r, -1)
|
|
end
|
|
return direct(P, N, T, I, o)
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ dispatch
|
|
-- primitive(geometryNode, opts) -> Primitive | nil, message | nil, nil (a node type without geometry here);
|
|
-- the second value of a success is the node's solid flag
|
|
function Geometry.primitive(n: any, o: Options): (Types.Primitive?, any)
|
|
local t = n.type
|
|
local maxV = o.maxVertices or 2 ^ 24
|
|
local segs = math.clamp(math.floor(o.segments or 32), 3, 1024)
|
|
local solid = bool(n, "solid", true)
|
|
local prim: Types.Primitive?, err: string? = nil, nil
|
|
if t == "IndexedFaceSet" then
|
|
prim, err = faceSet(n, o, maxV)
|
|
elseif t == "IndexedTriangleSet" or t == "TriangleSet" or t == "IndexedTriangleFanSet" or t == "TriangleFanSet" or t == "IndexedTriangleStripSet" or t == "TriangleStripSet" or t == "IndexedQuadSet" or t == "QuadSet" then
|
|
prim, err = triSet(n, o, maxV)
|
|
elseif t == "ElevationGrid" then
|
|
prim, err = elevationGrid(n, o, maxV)
|
|
elseif t == "Extrusion" then
|
|
prim, err = extrusion(n, o, maxV)
|
|
elseif t == "IndexedLineSet" or t == "LineSet" then
|
|
prim, err = lines(n, maxV)
|
|
solid = false
|
|
elseif t == "PointSet" then
|
|
prim, err = pointSet(n, maxV)
|
|
solid = false
|
|
elseif t == "Box" then
|
|
prim = box(n, o)
|
|
elseif t == "Sphere" then
|
|
prim = sphere(n, o, segs)
|
|
elseif t == "Cylinder" then
|
|
prim = cylinder(n, o, segs, false)
|
|
elseif t == "Cone" then
|
|
prim = cylinder(n, o, segs, true)
|
|
else
|
|
return nil, nil
|
|
end
|
|
if not prim then
|
|
return nil, err
|
|
end
|
|
return prim, solid
|
|
end
|
|
|
|
return Geometry
|