--!strict -- model/smd/Write: a Scene to Valve SMD text (version 1). Bones: the joints of the first skin (parents from the node -- tree: the nearest ancestor that is also a joint), or one "root" bone for a static scene; "skeleton": the bind pose -- (each joint node's TRS: rotation as Euler radians, R = Rz Ry Rx), or, with opts.animation = i, that animation sampled -- at its key times (translation / scale linearly, rotation by normalised linear interpolation); "triangles": every -- triangle of the scene in world space (codecs/model/MeshFlatten), one material line per triangle (the material -- name, else "default"), vertex lines with the Source links (opts.links ~= false; GoldSrc style otherwise: the -- strongest joint as the parent bone only). Animation SMDs (opts.animation set) carry no triangles unless -- opts.triangles is true. local Types = require("@formats/Types") local Mat4 = require("@formats/codecs/model/Mat4") local Flatten = require("@formats/codecs/model/MeshFlatten") local Write = {} export type Options = { animation: number?, links: boolean?, triangles: boolean? } local fmt = string.format local function num(v: number): string if v ~= v or v == math.huge or v == -math.huge then return "0" end local s = fmt("%.6f", v) if s == "-0.000000" then return "0.000000" end return s end -- quaternion -> x, y, z Euler radians with R = Rz(z) Ry(y) Rx(x) local function angles(q: { number }): (number, number, number) local m = Mat4.fromQuat(q) local r31, r32, r33, r21, r11 = m[3], m[7], m[11], m[2], m[1] local y = math.asin(math.clamp(-r31, -1, 1)) local x, z if math.abs(r31) < 0.9999999 then x = math.atan2(r32, r33) z = math.atan2(r21, r11) else x = math.atan2(-m[10], m[6]) z = 0 end return x, y, z end local function lerp(a: { number }, b: { number }, t: number): { number } local out: { number } = {} for i = 1, #a do out[i] = a[i] + (b[i] - a[i]) * t end return out end -- the value of channel ch at time tm (nil when the channel has no keys) local function sample(ch: Types.Channel, tm: number): { number }? local input, output = ch.input, ch.output local n = input.count if n == 0 then return nil end local comps = if ch.path == "rotation" then 4 else 3 local function key(k: number): { number } local out: { number } = {} for c = 0, comps - 1 do out[c + 1] = buffer.readf32(output.data, (k * comps + c) * 4) end return out end local times = input.data if tm <= buffer.readf32(times, 0) then return key(0) end for k = 1, n - 1 do local t1 = buffer.readf32(times, k * 4) if tm <= t1 then local t0 = buffer.readf32(times, (k - 1) * 4) local f = if ch.interpolation == "STEP" or t1 <= t0 then 0 else (tm - t0) / (t1 - t0) local a, b = key(k - 1), key(k) if comps == 4 then if a[1] * b[1] + a[2] * b[2] + a[3] * b[3] + a[4] * b[4] < 0 then b = { -b[1], -b[2], -b[3], -b[4] } end local q = lerp(a, b, f) local l = math.sqrt(q[1] ^ 2 + q[2] ^ 2 + q[3] ^ 2 + q[4] ^ 2) return if l > 0 then { q[1] / l, q[2] / l, q[3] / l, q[4] / l } else a end return lerp(a, b, f) end end return key(n - 1) end -- write(scene, opts) -> text | nil, message function Write.write(scene: Types.Scene, opts: Options?): (string?, string?) if type(scene) ~= "table" or scene.kind ~= "scene" then return nil, "expected a Scene" end local out: { string } = { "version 1", "nodes" } local skin = scene.skins and scene.skins[1] local joints: { number } = if skin then skin.joints else {} local jointOf: { [number]: number } = {} -- node index -> bone id (0-based) for k, nd in joints do jointOf[nd] = k - 1 end local parentOf: { [number]: number } = {} for ni, nd in scene.nodes do for _, c in nd.children do parentOf[c] = ni end end if #joints == 0 then table.insert(out, '0 "root" -1') else for k, nd in joints do local p = parentOf[nd] while p and jointOf[p] == nil do p = parentOf[p] end local name = scene.nodes[nd] and scene.nodes[nd].name or ("bone" .. (k - 1)) table.insert(out, fmt('%d "%s" %d', k - 1, string.gsub(name, '"', "'"), if p then jointOf[p] else -1)) end end table.insert(out, "end") table.insert(out, "skeleton") local function pose(tr: { number }?, rot: { number }?): string local t = tr or { 0, 0, 0 } local x, y, z = angles(rot or { 0, 0, 0, 1 }) return table.concat({ num(t[1]), num(t[2]), num(t[3]), num(x), num(y), num(z) }, " ") end local animIndex = opts and opts.animation local anim = animIndex and scene.animations[animIndex] if animIndex and not anim then return nil, "no animation " .. tostring(animIndex) end if anim then local keyset: { [number]: boolean } = {} for _, ch in anim.channels do for k = 0, ch.input.count - 1 do keyset[buffer.readf32(ch.input.data, k * 4)] = true end end local keys: { number } = {} for tm in keyset do table.insert(keys, tm) end table.sort(keys) local byNode: { [number]: { [string]: Types.Channel } } = {} for _, ch in anim.channels do local m = byNode[ch.node] or {} byNode[ch.node] = m m[ch.path] = ch end for f, tm in keys do table.insert(out, "time " .. (f - 1)) for k, nd in joints do local node = scene.nodes[nd] local chs: { [string]: Types.Channel } = byNode[nd] or {} local tch, rch = chs.translation, chs.rotation local tr = if tch then sample(tch, tm) else node.translation local rot = if rch then sample(rch, tm) else node.rotation table.insert(out, (k - 1) .. " " .. pose(tr, rot)) end end else table.insert(out, "time 0") if #joints == 0 then table.insert(out, "0 " .. pose(nil, nil)) else for k, nd in joints do local node = scene.nodes[nd] local tr, rot = node.translation, node.rotation if node.matrix then local t, r = Mat4.decompose(node.matrix) tr, rot = t, r end table.insert(out, (k - 1) .. " " .. pose(tr, rot)) end end end table.insert(out, "end") -- triangles if not anim or (opts and opts.triangles) then local m, err = Flatten.merge(scene, function(name: string): boolean return name == "NORMAL" or name == "TEXCOORD_0" or name == "JOINTS_0" or name == "WEIGHTS_0" end, { triangulate = true }) if not m then return nil, err end local A = m.attrs local P, N, UV, J, W = A.POSITION, A.NORMAL, A.TEXCOORD_0, A.JOINTS_0, A.WEIGHTS_0 local links = not (opts and opts.links == false) local nbones = math.max(1, #joints) if #m.corners > 0 then table.insert(out, "triangles") for _, part in m.parts do local mat = part.prim.material and scene.materials[part.prim.material] local mname = if mat and mat.name and mat.name ~= "" then mat.name else "default" for f = part.firstFace, part.firstFace + part.faceCount - 1 do table.insert(out, mname) for c = 0, 2 do local v = m.corners[f * 3 + c + 1] local px, py, pz = P.values[v * 3 + 1], P.values[v * 3 + 2], P.values[v * 3 + 3] local nx, ny, nz = 0, 0, 1 if N then nx, ny, nz = N.values[v * 3 + 1], N.values[v * 3 + 2], N.values[v * 3 + 3] end local u, w = 0, 0 if UV then local k = UV.components u, w = UV.values[v * k + 1], UV.values[v * k + 2] end local parent = 0 local lk: { string } = {} if J and W and #joints > 0 then local jk, wk = J.components, W.components local best, bestW = 0, -1 local count = 0 for c2 = 1, math.min(4, jk, wk) do local jw = W.values[v * wk + c2] local jj = math.clamp(math.floor(J.values[v * jk + c2] or 0), 0, nbones - 1) if jw and jw > 0 then count += 1 table.insert(lk, fmt("%d %s", jj, num(jw))) if jw > bestW then best, bestW = jj, jw end end end parent = best if count > 0 then table.insert(lk, 1, tostring(count)) end end local line = fmt("%d %s %s %s %s %s %s %s %s", parent, num(px), num(py), num(pz), num(nx), num(ny), num(nz), num(u), num(1 - w)) if links and #lk > 0 then line ..= " " .. table.concat(lk, " ") end table.insert(out, line) end end end table.insert(out, "end") end end table.insert(out, "") return table.concat(out, "\n"), nil end return Write