model formats (3ds..lwo): comments are specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
3bddd582bc
35 files changed
+233
-697
No files matched your search
@@ -1,8 +1,5 @@
|
||||
--!strict
|
||||
-- model/b3d/Read: the BB3D chunk tree -> Types.Scene (see model/b3d for what is read and how it is mapped). Chunks are
|
||||
-- walked with containers/Chunks (4-byte id, u32 little-endian size, no padding); fields are read through core/Bytes
|
||||
-- readers over each chunk body, so a short body sets the reader's sticky error instead of reading past the end.
|
||||
-- Returns nil, message[, kind] on failure (the contract turns it into an Err with Err.from).
|
||||
-- model/b3d/Read: the BB3D chunk tree (4-byte id, u32 little-endian size, no padding) -> Types.Scene.
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Chunks = require("@rosetta/containers/Chunks")
|
||||
@@ -26,8 +23,8 @@ type MeshRec = { mesh: number, node: number, count: number, joints: { { number }
|
||||
type Bone = { node: number, target: MeshRec?, list: { number } }
|
||||
type Keys = { t: { [number]: { number } }, s: { [number]: { number } }, r: { [number]: { number } } }
|
||||
|
||||
-- the file's left-handed Y-up values in glTF's right-handed frame: z mirrored; quaternions as w x y z, conjugated
|
||||
-- (Blitz3D stores the inverse rotation, as assimp's reader reads them) then mirrored
|
||||
-- Blitz3D is left-handed Y up: z mirrored; quaternions are w x y z, conjugated (Blitz3D stores the inverse
|
||||
-- rotation, as assimp reads them) then mirrored
|
||||
local function vec(r: Bytes.Reader): { number }
|
||||
local x, y, z = r:f32le(), r:f32le(), r:f32le()
|
||||
return { x, y, -z }
|
||||
@@ -54,7 +51,6 @@ local function fetcher(opts: Options?): (string) -> buffer?
|
||||
end
|
||||
end
|
||||
|
||||
-- scene(r, opts) -> Scene | nil, message, kind
|
||||
function Read.scene(src: Bytes.Reader, opts: Options?): (Types.Scene?, string?, string?)
|
||||
local buf = src.buf
|
||||
local maxVertices = opts and opts.maxVertices or 2 ^ 24
|
||||
@@ -77,7 +73,7 @@ function Read.scene(src: Bytes.Reader, opts: Options?): (Types.Scene?, string?,
|
||||
local scene = Types.Scene.new()
|
||||
local texs: { TexInfo } = {}
|
||||
local brushes: { Brush } = {}
|
||||
local materialOf: { [number]: number } = {} -- brush index (0-based) -> material
|
||||
local materialOf: { [number]: number } = {}
|
||||
local meshes: { MeshRec } = {}
|
||||
local bones: { Bone } = {}
|
||||
local nodeKeys: { [number]: Keys } = {}
|
||||
@@ -141,7 +137,6 @@ function Read.scene(src: Bytes.Reader, opts: Options?): (Types.Scene?, string?,
|
||||
end
|
||||
end
|
||||
|
||||
-- the material of brush id (0-based) | nil for -1 / out of range
|
||||
local function material(id: number): number?
|
||||
if id < 0 or id >= #brushes then
|
||||
return nil
|
||||
@@ -290,7 +285,6 @@ function Read.scene(src: Bytes.Reader, opts: Options?): (Types.Scene?, string?,
|
||||
fail(string.format("triangle %d: vertex index out of range", i))
|
||||
return nil
|
||||
end
|
||||
-- mirrored z: reverse the winding to keep the faces front-facing
|
||||
writeu32(idx, i * 12, a)
|
||||
writeu32(idx, i * 12 + 4, cc)
|
||||
writeu32(idx, i * 12 + 8, b)
|
||||
@@ -302,7 +296,7 @@ function Read.scene(src: Bytes.Reader, opts: Options?): (Types.Scene?, string?,
|
||||
end
|
||||
end
|
||||
if not attrs then
|
||||
return nil -- a mesh without vertices: the node stays a pivot
|
||||
return nil
|
||||
end
|
||||
if #mesh.primitives == 0 then
|
||||
table.insert(mesh.primitives, { attributes = attrs, mode = "points" :: "points" })
|
||||
@@ -472,7 +466,7 @@ function Read.scene(src: Bytes.Reader, opts: Options?): (Types.Scene?, string?,
|
||||
end
|
||||
end
|
||||
end
|
||||
local self: number? = nil -- the mesh node as a joint, for vertices no bone moves
|
||||
local self: number? = nil
|
||||
local jb, wb = buffer.create(nv * 8), buffer.create(nv * 16)
|
||||
for v = 0, nv - 1 do
|
||||
local e = inf[v + 1]
|
||||
@@ -484,7 +478,6 @@ function Read.scene(src: Bytes.Reader, opts: Options?): (Types.Scene?, string?,
|
||||
buffer.writeu16(jb, v * 8, self :: number)
|
||||
buffer.writef32(wb, v * 16, 1)
|
||||
else
|
||||
-- the 4 strongest, normalised
|
||||
local order = {}
|
||||
for k = 1, #e, 2 do
|
||||
table.insert(order, k)
|
||||
@@ -568,7 +561,7 @@ function Read.scene(src: Bytes.Reader, opts: Options?): (Types.Scene?, string?,
|
||||
brushes = #brushes,
|
||||
frames = if anim then anim.frames else nil,
|
||||
fps = if anim then anim.fps else nil,
|
||||
handedness = "left", -- converted: z mirrored, winding reversed
|
||||
handedness = "left",
|
||||
}
|
||||
return scene, nil, nil
|
||||
end
|
||||
|
||||
@@ -1,21 +1,7 @@
|
||||
--!strict
|
||||
-- model/b3d: Blitz3D .b3d (BB3D chunk format, version 1), read. Written from the Blitz3D file format description
|
||||
-- (blitzbasic.com "b3dfile_specs.txt") and checked against assimp's B3D importer.
|
||||
-- Reads: TEXS (texture files -> scene.textures, data through opts.files / opts.resolve, clamp flags -> sampler,
|
||||
-- position / scale / rotation -> the first texture's KHR_texture_transform, flag 65536 = second UV set), BRUS (brushes ->
|
||||
-- materials: colour + alpha, shininess -> roughness, FX 1 = unlit, 16 = double sided, 32 = blend; the first texture is
|
||||
-- baseColorTexture, the others are listed in extras.b3d.textures; texture flags 2 / 4 -> blend / mask), the NODE tree
|
||||
-- (name, position, scale, rotation; MESH -> one Mesh per node: VRTS -> POSITION / NORMAL / COLOR_0 (f32 RGBA) /
|
||||
-- TEXCOORD_0..7 shared by the mesh's primitives, one "triangles" primitive per TRIS chunk with its brush, else the
|
||||
-- mesh's master brush), BONE vertex weights -> a skin on the mesh read last before the bone (the 4 strongest weights
|
||||
-- per vertex, normalised, JOINTS_0 / WEIGHTS_0; vertices no bone moves follow the mesh node, which then joins the
|
||||
-- joints; inverse bind matrices from the bind pose), KEYS (position / scale / rotation keys) + ANIM (frames, fps,
|
||||
-- default 60) -> one animation of LINEAR channels at frame / fps seconds.
|
||||
-- Blitz3D is left-handed and Y up: z is mirrored (positions, normals, translations, keys), the triangle winding
|
||||
-- reversed and the rotations mirrored, so the scene is glTF's right-handed Y up (meta.up = "y"). UVs keep their
|
||||
-- top-left origin (= glTF). meta.source = { format, version, brushes, frames?, fps?, handedness = "left" }.
|
||||
-- Not supported: writing; multiple VRTS chunks in one MESH (the first is used); per-brush blend modes beyond the
|
||||
-- extras; texture coordinate sets of 3 / 4 components keep their first two.
|
||||
-- model/b3d: Blitz3D .b3d (BB3D chunks, version 1; blitzbasic.com "b3dfile_specs.txt", checked against assimp).
|
||||
-- Supports: TEXS, BRUS, NODE / MESH / VRTS / TRIS, BONE skins, KEYS + ANIM; left-handed, so z is mirrored.
|
||||
-- Not supported: writing; several VRTS chunks in one MESH (the first is used); 3 / 4 component texture coordinates.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -35,10 +21,10 @@ local B3d = {
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
resolve: ((uri: string) -> any)?, -- texture files (the api sets it for paths)
|
||||
files: { [string]: any }?, -- or: the files by name
|
||||
resolve: ((uri: string) -> any)?,
|
||||
files: { [string]: any }?,
|
||||
maxVertices: number?, -- default 2^24
|
||||
maxDepth: number?, -- node nesting (default 256)
|
||||
maxDepth: number?, -- default 256
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
@@ -54,7 +40,6 @@ function B3d.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
if r:peek(4) ~= "BB3D" then
|
||||
return nil, Err.new(Err.CORRUPT, "no BB3D chunk")
|
||||
end
|
||||
-- the scene builder is plain (not native) code: a bug there must not escape as an error
|
||||
local ok, scene, msg, kind = pcall(Read.scene, r, opts)
|
||||
if not ok then
|
||||
return nil, Err.new(Err.CORRUPT, "bad chunk tree: " .. tostring(scene))
|
||||
|
||||
Reference in new issue
Block a user