model formats (mca..x3d): comments are specs only

This commit is contained in:
4DBug committed 2026-11-01 01:54:32 -05:00
1 parent 0bd7b87d2b
commit b952c10222
49 files changed
+310 -936

No files matched your search

+4 -12
View File
@@ -1,12 +1,7 @@
--!strict
-- model/smd/Read: Valve SMD text (GoldSrc / Source studiomdl input: version 1) to a Scene. Sections "nodes" (id,
-- "name", parent), "skeleton" ("time n" blocks of "bone px py pz rx ry rz", Euler radians, R = Rz Ry Rx; a bone
-- missing from a later frame keeps its previous pose), "triangles" (a material line, then three vertex lines "bone px
-- py pz nx ny nz u v [links (bone weight)...]": model-space positions, v up), "vertexanimation" (skipped, counted).
-- Bones -> joint nodes and a Skin (bind pose = the first skeleton frame), triangles -> one primitive per material
-- (welded vertices, counter-clockwise as stored, TEXCOORD_0 v flipped to top-left, JOINTS_0 / WEIGHTS_0: the links,
-- the rest of the weight to the parent bone; GoldSrc lines without links: the parent bone), skeleton frames beyond
-- the first -> one animation (opts.fps, default 30). An animation SMD (no triangles) is a skeleton + its animation.
-- model/smd/Read: sections "nodes" (id, "name", parent), "skeleton" ("time n", "bone px py pz rx ry rz", Euler radians,
-- R = Rz Ry Rx; a bone missing from a later frame keeps its pose), "triangles" (a material line, then "bone px py pz
-- nx ny nz u v [links (bone weight)...]", model space, v up; the rest of the weight goes to the parent bone).
local Types = require("@rosetta/Types")
local Rig = require("@rosetta/codecs/model/Rig")
@@ -20,7 +15,6 @@ export type Options = { fps: number?, name: string?, maxVertices: number? }
type Bone = { id: number, name: string, parent: number }
local ZERO: { number } = { 0, 0, 0, 0, 0, 0 }
-- read(text, opts) -> Scene | nil, message
function Read.read(text: string, opts: Options?): (Types.Scene?, string?)
local lines: { string } = {}
for line in string.gmatch(text, "[^\r\n]*") do
@@ -177,7 +171,7 @@ function Read.read(text: string, opts: Options?): (Types.Scene?, string?)
local root: Types.Node = { name = opts and opts.name or "smd", children = {} }
table.insert(scene.nodes, root)
scene.roots[1] = 1
-- skeleton (bind pose: the first frame)
-- bind pose: the first frame
local joints: { Rig.Joint } = {}
local bind: { [number]: { number } } = frames[1] or {}
for k, bn in bones do
@@ -186,7 +180,6 @@ function Read.read(text: string, opts: Options?): (Types.Scene?, string?)
joints[k] = { name = bn.name, parent = parent, translation = { v[1], v[2], v[3] }, rotation = Rig.angles(v[4], v[5], v[6]) }
end
local jointNodes, skinIndex = Rig.skeleton(scene, joints, "local", 1)
-- meshes
if #batches > 0 then
local mesh: Types.Mesh = { primitives = {} }
for _, b in batches do
@@ -244,7 +237,6 @@ function Read.read(text: string, opts: Options?): (Types.Scene?, string?)
table.insert(scene.nodes, { name = "mesh", children = {}, mesh = #scene.meshes, skin = if #bones > 0 then skinIndex else nil })
table.insert(root.children, #scene.nodes)
end
-- animation
if #frames > 1 or (#frames == 1 and #batches == 0) then
local fps = opts and opts.fps or 30
local t: { number } = {}
+2 -11
View File
@@ -1,12 +1,6 @@
--!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.
-- model/smd/Write: Scene -> SMD version 1: the first skin's joints (or one "root" bone), the bind pose or a sampled
-- animation, world-space triangles with Source links (GoldSrc style: the strongest joint as the parent bone only).
local Types = require("@rosetta/Types")
local Mat4 = require("@rosetta/codecs/model/Mat4")
local Flatten = require("@rosetta/codecs/model/MeshFlatten")
@@ -52,7 +46,6 @@ local function lerp(a: { number }, b: { number }, t: number): { number }
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
@@ -91,7 +84,6 @@ local function sample(ch: Types.Channel, tm: number): { number }?
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"
@@ -179,7 +171,6 @@ function Write.write(scene: Types.Scene, opts: Options?): (string?, string?)
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"
+4 -8
View File
@@ -1,10 +1,6 @@
--!strict
-- model/smd: Valve SMD (studiomdl data, GoldSrc and Source: "version 1" text), read and write. Written from the
-- Valve Developer Community "SMD file format" page; no code taken. Read: model/smd/Read (bones -> joint nodes + a
-- Skin, triangles -> a primitive per material with JOINTS_0 / WEIGHTS_0, skeleton frames -> an animation; animation
-- SMDs too). Write: model/smd/Write (the first skin's joints or one root bone, the bind pose or opts.animation,
-- world-space triangles with Source weight links). Units as stored (inches in Valve's games), z up.
-- Not supported: vertex animation (flex) sections (skipped, counted in meta.source.vertexAnimations).
-- model/smd: Valve SMD (studiomdl "version 1" text, GoldSrc and Source), from the Valve Developer Community "SMD file
-- format" page; no code taken. Units as stored (inches in Valve's games), z up. Not supported: vertex animation (flex).
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
@@ -26,8 +22,8 @@ local Smd = {
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
fps: number?, -- skeleton frames per second (default 30)
name: string?, -- the root node / animation name (the api passes the file name)
fps: number?, -- default 30
name: string?,
maxVertices: number?, -- default 2^24
}
export type EncodeOptions = Write.Options