69 lines
3.7 KiB
Luau
69 lines
3.7 KiB
Luau
--!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.
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@rosetta/core/Err")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Types = require("@rosetta/Types")
|
|
local Read = require("@rosetta/model/b3d/Read")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local B3d = {
|
|
name = "b3d",
|
|
category = "model",
|
|
extensions = { "b3d" },
|
|
mime = { "model/x-b3d" },
|
|
deps = { "core/Err", "core/Bytes", "Types", "containers/Chunks", "codecs/model/Mat4" },
|
|
caps = { decode = true, encode = false, materials = true, textures = true, skins = true, animation = true, colors = true },
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ types
|
|
export type DecodeOptions = {
|
|
resolve: ((uri: string) -> any)?, -- texture files (the api sets it for paths)
|
|
files: { [string]: any }?, -- or: the files by name
|
|
maxVertices: number?, -- default 2^24
|
|
maxDepth: number?, -- node nesting (default 256)
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
function B3d.sniff(r: Bytes.Reader): number
|
|
if r:peek(4) == "BB3D" then
|
|
return if r:peek(4, 8) ~= "" then 1 else 0.8
|
|
end
|
|
return 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
function B3d.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
|
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))
|
|
end
|
|
if not scene then
|
|
return nil, Err.from(msg, nil, kind)
|
|
end
|
|
return scene, nil
|
|
end
|
|
|
|
return B3d
|