model formats (mca..x3d): comments are specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
b952c10222
49 files changed
+310
-936
No files matched your search
@@ -1,15 +1,7 @@
|
||||
--!strict
|
||||
-- model/ms3d/Read: MilkShape 3D 1.8 (.ms3d, version 3 / 4) -> Types.Scene. The layout from MilkShape's ms3dspec.h
|
||||
-- (chUmbaLum soft): vertices (position, a bone), triangles (three vertex indices, per-corner normals and uvs, a
|
||||
-- smoothing group, a group), groups (named triangle lists with a material), materials (Phong colours, shininess
|
||||
-- 0..128, transparency, texture and alpha map file names), the animation header (fps, current time, frames) and
|
||||
-- joints (name, parent name, local Euler XYZ rotation in radians and position, rotation / position keys in seconds
|
||||
-- relative to that rest pose); then the optional sub-versioned blocks: comments (groups, materials, joints, model),
|
||||
-- vertex extras (three more bones and weights in percent: subversion 1-3, with one or two extra u32s from 2 / 3),
|
||||
-- joint extras (colours) and model extras (joint size, transparency mode, alpha reference).
|
||||
-- Mapping: one node + mesh + primitive per group; joints -> nodes in their hierarchy, a skin (inverse bind matrices
|
||||
-- of the rest pose; JOINTS_0 / WEIGHTS_0 when a vertex has a bone) and one animation (rest * key rotations, rest +
|
||||
-- key positions). Never throws on bad data: returns nil, message, offset.
|
||||
-- model/ms3d/Read: the ms3dspec.h layout: vertices, triangles, groups, materials (shininess 0..128), the animation
|
||||
-- header, joints (local Euler XYZ radians, keys in seconds relative to the rest pose), then the sub-versioned comments,
|
||||
-- vertex extras (weights in percent, subversion 1-3), joint extras (colours) and model extras. Never throws.
|
||||
local Types = require("@rosetta/Types")
|
||||
local Mat4 = require("@rosetta/codecs/model/Mat4")
|
||||
|
||||
@@ -62,7 +54,6 @@ type Joint = {
|
||||
rotKeys: { { number } }, posKeys: { { number } }, comment: string?, color: { number }?,
|
||||
}
|
||||
|
||||
-- scene(b, base, limit, opts) -> Scene | nil, message, offset
|
||||
function Read.scene(b: buffer, base: number, limit: number, opts: Options?): (Types.Scene?, string?, number?)
|
||||
local p = base
|
||||
local function need(n: number): boolean
|
||||
@@ -77,7 +68,6 @@ function Read.scene(b: buffer, base: number, limit: number, opts: Options?): (Ty
|
||||
end
|
||||
p += 14
|
||||
local maxV = opts and opts.maxVertices or 2 ^ 20
|
||||
-- vertices
|
||||
if not need(2) then
|
||||
return nil, "truncated vertex count", p
|
||||
end
|
||||
@@ -92,7 +82,6 @@ function Read.scene(b: buffer, base: number, limit: number, opts: Options?): (Ty
|
||||
verts[i] = { pos = { readf32(b, q + 1), readf32(b, q + 5), readf32(b, q + 9) }, bones = { readi8(b, q + 13), -1, -1, -1 }, weights = { 1, 0, 0, 0 } }
|
||||
end
|
||||
p += nv * 15
|
||||
-- triangles
|
||||
if not need(2) then
|
||||
return nil, "truncated triangle count", p
|
||||
end
|
||||
@@ -106,7 +95,6 @@ function Read.scene(b: buffer, base: number, limit: number, opts: Options?): (Ty
|
||||
end
|
||||
local triAt = p
|
||||
p += nt * 70
|
||||
-- groups
|
||||
if not need(2) then
|
||||
return nil, "truncated group count", p
|
||||
end
|
||||
@@ -135,7 +123,6 @@ function Read.scene(b: buffer, base: number, limit: number, opts: Options?): (Ty
|
||||
p += 1
|
||||
table.insert(groups, g)
|
||||
end
|
||||
-- materials
|
||||
if not need(2) then
|
||||
return nil, "truncated material count", p
|
||||
end
|
||||
@@ -154,7 +141,7 @@ function Read.scene(b: buffer, base: number, limit: number, opts: Options?): (Ty
|
||||
})
|
||||
end
|
||||
p += nm * 361
|
||||
-- animation header and joints (version 3 files may end here)
|
||||
-- version 3 files may end here
|
||||
local fps, current, frames = 24, 0, 0
|
||||
local joints: { Joint } = {}
|
||||
if need(12) then
|
||||
@@ -189,7 +176,6 @@ function Read.scene(b: buffer, base: number, limit: number, opts: Options?): (Ty
|
||||
table.insert(joints, j)
|
||||
end
|
||||
end
|
||||
-- sub-versioned blocks
|
||||
local source: { [string]: any } = { format = "ms3d", version = version, fps = fps, currentTime = current, frames = frames }
|
||||
local function comments(list: { any }): string?
|
||||
if not need(4) then
|
||||
@@ -283,7 +269,6 @@ function Read.build(b: buffer, triAt: number, verts: { Vertex }, groups: { any }
|
||||
local scene = S.new()
|
||||
scene.meta.up = "y"
|
||||
scene.meta.source = source :: any
|
||||
-- materials, textures
|
||||
for _, m in mats do
|
||||
local mat = S.material(m.name)
|
||||
local d = m.diffuse
|
||||
@@ -305,7 +290,6 @@ function Read.build(b: buffer, triAt: number, verts: { Vertex }, groups: { any }
|
||||
}
|
||||
table.insert(scene.materials, mat)
|
||||
end
|
||||
-- joints: nodes, rest pose, skin
|
||||
local nj = #joints
|
||||
local jointNode: { [number]: number } = {}
|
||||
local byName: { [string]: number } = {}
|
||||
@@ -355,7 +339,6 @@ function Read.build(b: buffer, triAt: number, verts: { Vertex }, groups: { any }
|
||||
end
|
||||
scene.skins[1] = { joints = list, inverseBindMatrices = ibm }
|
||||
end
|
||||
-- groups: one node + mesh each; vertices unwelded per (vertex, normal, uv) corner
|
||||
local nv = #verts
|
||||
for gi, g in groups do
|
||||
local remap: { [string]: number } = {}
|
||||
@@ -365,7 +348,6 @@ function Read.build(b: buffer, triAt: number, verts: { Vertex }, groups: { any }
|
||||
local count = 0
|
||||
for _, ti in g.tris do
|
||||
local q = triAt + ti * 70
|
||||
-- a triangle with an out-of-range vertex is dropped
|
||||
if readu16(b, q + 2) >= nv or readu16(b, q + 4) >= nv or readu16(b, q + 6) >= nv then
|
||||
continue
|
||||
end
|
||||
@@ -382,7 +364,6 @@ function Read.build(b: buffer, triAt: number, verts: { Vertex }, groups: { any }
|
||||
table.move(v.pos, 1, 3, o * 3 + 1, P)
|
||||
N[o * 3 + 1], N[o * 3 + 2], N[o * 3 + 3] = readf32(b, q + 8 + k * 12), readf32(b, q + 12 + k * 12), readf32(b, q + 16 + k * 12)
|
||||
U[o * 2 + 1], U[o * 2 + 2] = readf32(b, q + 44 + k * 4), readf32(b, q + 56 + k * 4)
|
||||
-- influences: the valid bones, weights normalized
|
||||
local sum = 0
|
||||
for c = 1, 4 do
|
||||
if v.bones[c] >= 0 then
|
||||
@@ -429,7 +410,6 @@ function Read.build(b: buffer, triAt: number, verts: { Vertex }, groups: { any }
|
||||
table.insert(scene.nodes, node)
|
||||
table.insert(scene.roots, #scene.nodes)
|
||||
end
|
||||
-- the animation: rest * key rotations, rest + key positions
|
||||
local channels: { Types.Channel } = {}
|
||||
for i, j in joints do
|
||||
if #j.rotKeys > 0 then
|
||||
|
||||
Reference in new issue
Block a user