Files
rosetta/src/Formats/model/3ds/init.luau
T

101 lines
5.5 KiB
Luau

--!strict
-- model/3ds: Autodesk 3D Studio (.3ds, the chunked binary of 3D Studio R4 / 3ds Max's exporter). New code, from the
-- chunk layout as documented by the 3DS File Toolkit notes and read by assimp's 3DS importer (BSD-3: the reference
-- for transforms and conventions; no code ported) and lib3ds (layouts only).
-- Reads: triangle meshes (vertices, faces, uvs (TEXCOORD_0, flipped to glTF's top-left origin), the mesh matrix,
-- per-face materials -> one primitive per material, smoothing groups -> NORMAL: corners of faces sharing a group and
-- a position are averaged, group 0 is flat; extras["3ds"].smoothing keeps the groups per triangle), materials
-- (ambient / diffuse / specular colours, shininess, shininess strength, transparency, self-illumination, two-sided,
-- shading, wire; texture maps -> Textures with `uri` (never loaded: F.model.loadTextures resolves them), diffuse as
-- baseColorTexture, self-illumination as emissiveTexture, the others in extras["3ds"].maps; uv scale / offset /
-- angle as a texture transform), cameras (lens -> yfov at a 4:3 aspect, near / far ranges; -Z looking at the
-- target, rolled), lights (omni -> point, spot -> spot with hotspot / falloff cones, colour, multiplier, outer range),
-- the keyframer (node hierarchy by node id, $$$DUMMY nodes with their instance names, instances of one object,
-- pivots, the first key of every track as the node's TRS, tracks with several keys -> one animation: position /
-- scale keys linear, rotation keys accumulated from their axis / angle deltas, cameras / spotlights sampled from
-- their position, target and roll tracks; TCB spline parameters are ignored, times are frame / opts.fps). Objects
-- without a keyframer node stand at their mesh matrix. meta.up = "z"; meta.source has version, masterScale (not
-- applied), the keyframer frame range.
-- Writes: see model/3ds/Write (meshes, materials, texture names, cameras, lights, the keyframer and the first
-- animation). Not supported: 3DS project / material library files (.prj, .mli), morph / hide tracks, procedural
-- and shadow parameters, background / atmosphere settings, mesh limits beyond 65535 vertices per object (split on
-- write), vertex colours / normals (3DS has neither; normals come from smoothing groups).
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Types = require("@formats/Types")
local Read = require("@formats/model/3ds/Read")
local Build = require("@formats/model/3ds/Build")
local Write = require("@formats/model/3ds/Write")
-- ------------------------------------------------------------------------------------------------ contract
local ThreeDs = {
name = "3ds",
category = "model",
extensions = { "3ds" },
mime = { "application/x-3ds", "image/x-3ds" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/model/Mat4", "codecs/model/MeshFlatten", "codecs/model/Sampler" },
caps = { decode = true, encode = true, materials = true, textures = true, animation = true, cameras = true, lights = true, instancing = true },
}
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
normals: boolean?, -- compute NORMAL from the smoothing groups (default true; false keeps the file's vertices)
flipV: boolean?, -- TEXCOORD_0.v = 1 - v (default true: glTF's convention)
fps: number?, -- keyframer frames per second (default 30, 3ds Max's default)
maxVertices: number?, -- refuse more vertices in total (default 2^24)
}
export type EncodeOptions = {
flipV: boolean?, -- default true (the inverse of decode)
fps: number?, -- seconds -> keyframer frames (default 30)
name: string?, -- the destination's file name (names textures that have no uri)
files: { [string]: any }?, -- receives the bytes of textures that have data but no uri
}
-- ------------------------------------------------------------------------------------------------ sniff
function ThreeDs.sniff(r: Bytes.Reader): number
local h = r:peek(8)
if #h < 8 or string.byte(h, 1) ~= 0x4D or string.byte(h, 2) ~= 0x4D then
return 0
end
local b1, b2, b3, b4, c1, c2 = string.byte(h, 3, 8)
local size = b1 + b2 * 256 + b3 * 65536 + b4 * 16777216
local child = c1 + c2 * 256
if size < 16 or not (child == 0x0002 or child == 0x3D3D or child == 0xB000) then
return 0
end
return if size == r.limit - r.pos then 0.95 else 0.7
end
-- ------------------------------------------------------------------------------------------------ decode
function ThreeDs.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
local raw, msg, at = Read.parse(r.buf, r.base, r.limit, opts)
if not raw then
return nil, Err.from(msg, at)
end
-- the scene builder is plain (not native) code: a bug there must not escape as an error
local ok, scene = pcall(Build.scene, raw, opts :: any)
if not ok then
return nil, Err.new(Err.CORRUPT, "bad object data: " .. tostring(scene))
end
return scene, nil
end
-- ------------------------------------------------------------------------------------------------ encode
function ThreeDs.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err?)
if type(scene) ~= "table" or scene.kind ~= "scene" then
return nil, Err.new(Err.ARG, "needs a Scene")
end
local ok, out, err = pcall(Write.encode, scene, opts :: any)
if not ok then
return nil, Err.new(Err.ARG, "bad scene: " .. tostring(out))
end
if not out then
return nil, Err.new(Err.ARG, tostring(err))
end
return out, nil
end
return ThreeDs