54 lines
2.1 KiB
Luau
54 lines
2.1 KiB
Luau
--!strict
|
|
-- 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")
|
|
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)?,
|
|
files: { [string]: any }?,
|
|
maxVertices: number?, -- default 2^24
|
|
maxDepth: number?, -- 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
|
|
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
|