96 lines
4.9 KiB
Luau
96 lines
4.9 KiB
Luau
--!strict
|
|
-- model/3mf: 3D Manufacturing Format (3MF Consortium core specification 1.3, materials and properties extension 1.2,
|
|
-- production extension 1.1 part references). New code. An OPC package (containers/Zip) holding XML model parts
|
|
-- (codecs/Xml).
|
|
-- Reads: the root model part (_rels/.rels), objects (mesh vertices / triangles; components with transforms, also from
|
|
-- other model parts through p:path), build items -> root nodes with their transforms (3MF's row-vector 4x3 matrices
|
|
-- become column-major node.matrix), object names / types / part numbers (node / mesh extras["3mf"]), metadata
|
|
-- (meta.metadata), the unit (meta.unit in metres; meta.up = "z"), basematerials (-> materials, sRGB display colours
|
|
-- made linear), colorgroup (-> COLOR_0, linear RGBA), texture2d + texture2dgroup (-> TEXCOORD_0 with v flipped to
|
|
-- glTF's top-left origin, a material with baseColorTexture, the texture kept encoded from its package part: decode it
|
|
-- with F.model.loadTextures), compositematerials (one blended colour per composite), multiproperties (their first
|
|
-- layer), per-triangle pid / p1 / p2 / p3 and per-object pid / pindex defaults. One primitive per property; vertices
|
|
-- are split where a vertex's corners carry different colours / texture coordinates.
|
|
-- Writes: one object per mesh (all triangle-like primitives; points / lines skipped), one build item per node with a
|
|
-- mesh at its world matrix (the hierarchy is flattened), materials as basematerials, COLOR_0 as colour groups,
|
|
-- PNG / JPEG base colour textures as texture2d + texture2dgroup, metadata, the unit (opts.unit or from meta.unit).
|
|
-- Not supported: beam lattice, slices, displacement, volumetric and secure content extensions (their elements are
|
|
-- ignored; meta.source.requiredExtensions lists what the file declared), thumbnails, normals (3MF has none).
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@formats/core/Err")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Types = require("@formats/Types")
|
|
local Zip = require("@formats/containers/Zip")
|
|
local Read = require("@formats/model/3mf/Read")
|
|
local Write = require("@formats/model/3mf/Write")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local ThreeMf = {
|
|
name = "3mf",
|
|
category = "model",
|
|
extensions = { "3mf" },
|
|
mime = { "model/3mf" },
|
|
deps = { "core/Err", "core/Bytes", "Types", "codecs/Xml", "containers/Zip" },
|
|
caps = { decode = true, encode = true, materials = true, vertexColors = true, textures = true, units = true },
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ types
|
|
export type DecodeOptions = {
|
|
maxVertices: number?, -- refuse more vertices + triangle corners in total (default 2^25)
|
|
maxNodes: number?, -- refuse more object instances (default 2^20)
|
|
}
|
|
export type EncodeOptions = {
|
|
unit: string?, -- "micron" | "millimeter" | "centimeter" | "inch" | "foot" | "meter" (default: from meta.unit, else
|
|
-- millimeter); positions are scaled through the build transforms when meta.unit differs
|
|
level: number?, -- deflate level for the XML parts (default 6; 0 stores them)
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
function ThreeMf.sniff(r: Bytes.Reader): number
|
|
if r:peek(4) ~= "PK\3\4" then
|
|
return 0
|
|
end
|
|
-- the central directory sits at the end: look for the model part's name there, then at the start
|
|
local n = r:size()
|
|
local tail = buffer.readstring(r.buf, r.base + math.max(0, n - 65536), math.min(n, 65536))
|
|
if string.find(tail, "3D/[^/]*%.model") or string.find(tail, "3dmodel.model", 1, true) then
|
|
return 0.95
|
|
end
|
|
local head = r:peek(256)
|
|
if string.find(head, "3D/", 1, true) then
|
|
return 0.7
|
|
end
|
|
return 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
function ThreeMf.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
|
local zip, zerr = Zip.open(r)
|
|
if not zip then
|
|
return nil, zerr
|
|
end
|
|
local ok, scene, err = pcall(Read.scene, zip, opts or {})
|
|
if not ok then
|
|
return nil, Err.new(Err.CORRUPT, "bad package (" .. tostring(scene) .. ")")
|
|
end
|
|
return scene, err
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ encode
|
|
function ThreeMf.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err?)
|
|
if type(scene) ~= "table" or scene.kind ~= "scene" or type(scene.meshes) ~= "table" or type(scene.materials) ~= "table" or type(scene.textures) ~= "table" then
|
|
return nil, Err.new(Err.ARG, "needs a Scene")
|
|
end
|
|
if opts and opts.unit ~= nil and not Read.UNITS[opts.unit] then
|
|
return nil, Err.new(Err.ARG, "unknown unit " .. tostring(opts.unit))
|
|
end
|
|
local ok, out, err = pcall(Write.build, scene, opts)
|
|
if not ok then
|
|
return nil, Err.new(Err.ARG, "bad scene (" .. tostring(out) .. ")")
|
|
end
|
|
return out, err
|
|
end
|
|
|
|
return ThreeMf
|