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,11 +1,6 @@
|
||||
--!strict
|
||||
-- model/3mf/Read: a 3MF package (an OPC zip) -> Scene. The root model part from _rels/.rels (fallback: the first
|
||||
-- *.model under 3D/), other model parts referenced by the production extension's p:path (components and build items),
|
||||
-- core resources (objects with mesh vertices / triangles, components with transforms), the materials extension
|
||||
-- (basematerials, colorgroup, texture2d + texture2dgroup, compositematerials blended into one colour per entry,
|
||||
-- multiproperties resolved through their first layer), build items with transforms -> root nodes, metadata, the unit.
|
||||
-- Triangles are grouped into one primitive per property (a base material, a colour group, a texture group); colour and
|
||||
-- texture groups give per-corner COLOR_0 / TEXCOORD_0, so vertices are split where corners of one vertex differ.
|
||||
-- model/3mf/Read: a 3MF package -> Scene. The root model part comes from _rels/.rels (fallback: the first *.model under
|
||||
-- 3D/); vertices are split where corners of one vertex carry different colours / texture coordinates.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Xml = require("@rosetta/codecs/text/Xml")
|
||||
@@ -21,7 +16,6 @@ local MAX_DEPTH = 64
|
||||
|
||||
local localName = Xml.localName
|
||||
|
||||
-- an attribute by local name (namespaced attributes such as p:path)
|
||||
local function attr(node: any, name: string): string?
|
||||
local a = node.attrs
|
||||
local v = a[name]
|
||||
@@ -44,7 +38,7 @@ local function int(s: string?): number?
|
||||
return nil
|
||||
end
|
||||
|
||||
-- "#RRGGBB" / "#RRGGBBAA" -> { r, g, b, a } sRGB 0..1
|
||||
-- "#RRGGBB" / "#RRGGBBAA", sRGB
|
||||
local function hexColor(s: string?): { number }?
|
||||
if type(s) ~= "string" then
|
||||
return nil
|
||||
@@ -68,7 +62,7 @@ function Read.toLinear(c: number): number
|
||||
end
|
||||
local toLinear = Read.toLinear
|
||||
|
||||
-- "m00 m01 m02 m10 ... m32" (3MF row-vector convention) -> a column-major glTF matrix, nil for identity / invalid
|
||||
-- "m00 m01 m02 m10 ... m32": 3MF's row-vector convention
|
||||
local function transform(s: string?): ({ number }?, boolean)
|
||||
if not s then
|
||||
return nil, true
|
||||
@@ -99,7 +93,6 @@ local function normPath(p: string): string
|
||||
return p
|
||||
end
|
||||
|
||||
-- one model part parsed into its resources and build
|
||||
type Model = { path: string, unit: string?, metadata: { [string]: string }, res: { [number]: any }, items: { any }, required: string? }
|
||||
|
||||
local function parseModel(text: string, path: string, opts: any): (Model?, Err.Err?)
|
||||
@@ -253,7 +246,6 @@ local function parseModel(text: string, path: string, opts: any): (Model?, Err.E
|
||||
return model, nil
|
||||
end
|
||||
|
||||
-- the root model part's path
|
||||
local function rootPath(zip: any): string?
|
||||
local rels = zip:readString("_rels/.rels")
|
||||
if rels then
|
||||
@@ -313,7 +305,6 @@ function Read.scene(zip: any, opts: any): (Types.Scene?, Err.Err?)
|
||||
scene.meta.metadata = root.metadata
|
||||
scene.meta.source = { format = "3mf", unit = unitName, part = rootP, requiredExtensions = root.required }
|
||||
|
||||
-- textures and materials (shared across model parts by part path + resource id)
|
||||
local texCache: { [string]: number } = {}
|
||||
local function texture(model: Model, id: number): number?
|
||||
local key = model.path .. "#" .. id
|
||||
@@ -359,7 +350,6 @@ function Read.scene(zip: any, opts: any): (Types.Scene?, Err.Err?)
|
||||
return m
|
||||
end
|
||||
|
||||
-- property of one corner -> (group key, material index?, kind, resource, index)
|
||||
local function resolve(model: Model, pid: number, p: number, depth: number?): (string, number?, string, any, number)
|
||||
local r = model.res[pid]
|
||||
if not r or p < 0 then
|
||||
@@ -429,7 +419,6 @@ function Read.scene(zip: any, opts: any): (Types.Scene?, Err.Err?)
|
||||
return "none", nil, "none", nil, 0
|
||||
end
|
||||
|
||||
-- one Scene mesh per (part, object) with a mesh
|
||||
local meshCache: { [string]: number } = {}
|
||||
local function buildMesh(model: Model, o: any): number?
|
||||
local key = model.path .. "#" .. o.id
|
||||
@@ -532,7 +521,6 @@ function Read.scene(zip: any, opts: any): (Types.Scene?, Err.Err?)
|
||||
return #scene.meshes
|
||||
end
|
||||
|
||||
-- nodes: one per object instance (build items and components)
|
||||
local nodeCount = 0
|
||||
local maxNodes = opts.maxNodes or 2 ^ 20
|
||||
local instantiate: (model: Model, oid: number, matrix: { number }?, depth: number, extra: any) -> (number?, Err.Err?)
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
--!strict
|
||||
-- model/3mf/Write: Scene -> a 3MF package. One mesh object per Scene mesh (every triangle primitive: triangles,
|
||||
-- strips, fans, polygons fan-split; points and lines are skipped), one build item per node with a mesh, carrying the
|
||||
-- node's world matrix (the hierarchy is flattened). Materials go into one basematerials group (sRGB display colours),
|
||||
-- primitives with COLOR_0 into a colorgroup, primitives with TEXCOORD_0 and a material whose baseColorTexture has
|
||||
-- PNG / JPEG bytes into a texture2d + texture2dgroup (the image stored in the package under 3D/Textures/).
|
||||
-- Units: meta.unit picks the 3MF unit when it is one of them; otherwise millimetres and the build transforms scale.
|
||||
-- model/3mf/Write: Scene -> a 3MF package; the node hierarchy is flattened into build items at world matrices.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Xml = require("@rosetta/codecs/text/Xml")
|
||||
@@ -48,7 +43,6 @@ local function hex(r: number, g: number, b: number, a: number): string
|
||||
return fmt("#%02X%02X%02X%02X", q(toSrgb(r)), q(toSrgb(g)), q(toSrgb(b)), q(math.clamp(a, 0, 1)))
|
||||
end
|
||||
|
||||
-- the triangle corner list of a primitive (0-based vertex indices) or nil
|
||||
local function triangles(prim: any): { number }?
|
||||
local pos = prim.attributes and prim.attributes.POSITION
|
||||
if type(pos) ~= "table" then
|
||||
@@ -128,10 +122,9 @@ function Write.build(scene: Types.Scene, opts: any): (buffer?, Err.Err?)
|
||||
nextId += 1
|
||||
return nextId
|
||||
end
|
||||
local res: { string } = {} -- resource XML chunks, in definition order
|
||||
local res: { string } = {}
|
||||
local files: { { name: string, data: buffer, mime: string } } = {}
|
||||
|
||||
-- unit
|
||||
local unitName: string, scale: number = "millimeter", 1
|
||||
local mu: any = scene.meta and scene.meta.unit
|
||||
if type(o.unit) == "string" and UNITS[o.unit] then
|
||||
@@ -152,16 +145,14 @@ function Write.build(scene: Types.Scene, opts: any): (buffer?, Err.Err?)
|
||||
end
|
||||
end
|
||||
|
||||
-- base materials: one group with every Scene material
|
||||
local baseId: number? = nil
|
||||
local nmat = #scene.materials
|
||||
local defaultBase = nmat -- 0-based index of an extra white "default" base, added when needed
|
||||
local defaultBase = nmat
|
||||
local needDefault = false
|
||||
if nmat > 0 then
|
||||
baseId = id()
|
||||
end
|
||||
|
||||
-- textures used as base colour textures
|
||||
local texRes: { [number]: number } = {}
|
||||
local function textureId(ti: number): number?
|
||||
if texRes[ti] ~= nil then
|
||||
@@ -186,7 +177,6 @@ function Write.build(scene: Types.Scene, opts: any): (buffer?, Err.Err?)
|
||||
return rid
|
||||
end
|
||||
|
||||
-- objects
|
||||
local objectOf: { [number]: number } = {}
|
||||
local objChunks: { string } = {}
|
||||
for mi, mesh in scene.meshes do
|
||||
@@ -195,7 +185,7 @@ function Write.build(scene: Types.Scene, opts: any): (buffer?, Err.Err?)
|
||||
local base = 0
|
||||
local objPid: number?, objP: number? = nil, nil
|
||||
local anyNone = false
|
||||
local triList: { { number } } = {} -- { v1, v2, v3, pid | -1, p1, p2, p3 }
|
||||
local triList: { { number } } = {}
|
||||
for _, prim in mesh.primitives do
|
||||
local corners = triangles(prim)
|
||||
if not corners or #corners == 0 then
|
||||
@@ -210,7 +200,6 @@ function Write.build(scene: Types.Scene, opts: any): (buffer?, Err.Err?)
|
||||
for v = 0, n - 1 do
|
||||
table.insert(verts, fmt('\t\t\t\t\t<vertex x="%s" y="%s" z="%s" />\n', num(getP(v, 0)), num(if pos.components >= 2 then getP(v, 1) else 0), num(if pos.components >= 3 then getP(v, 2) else 0)))
|
||||
end
|
||||
-- properties: texture group, colour group, base material, none
|
||||
local pid, kind = -1, "none"
|
||||
local mat = prim.material and scene.materials[prim.material]
|
||||
local uv = prim.attributes.TEXCOORD_0
|
||||
@@ -296,7 +285,6 @@ function Write.build(scene: Types.Scene, opts: any): (buffer?, Err.Err?)
|
||||
table.insert(res, objChunks[#objChunks])
|
||||
end
|
||||
|
||||
-- the base material group goes first (its id was taken first)
|
||||
if baseId then
|
||||
local parts = { fmt('\t\t<basematerials id="%d">\n', baseId) }
|
||||
for _, m in scene.materials do
|
||||
@@ -310,7 +298,6 @@ function Write.build(scene: Types.Scene, opts: any): (buffer?, Err.Err?)
|
||||
table.insert(res, 1, table.concat(parts))
|
||||
end
|
||||
|
||||
-- build items: every node with a mesh, at its world matrix
|
||||
local items: { string } = {}
|
||||
local seen: { [number]: boolean } = {}
|
||||
for _, pl in Types.Scene.flatten(scene) do
|
||||
@@ -342,7 +329,6 @@ function Write.build(scene: Types.Scene, opts: any): (buffer?, Err.Err?)
|
||||
end
|
||||
end
|
||||
|
||||
-- metadata
|
||||
local meta: { string } = {}
|
||||
local md = scene.meta and scene.meta.metadata
|
||||
if type(md) == "table" then
|
||||
|
||||
@@ -1,21 +1,7 @@
|
||||
--!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/text/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).
|
||||
-- model/3mf: 3D Manufacturing Format (core 1.3, materials extension 1.2, production extension 1.1 p:path).
|
||||
-- Supports: meshes, components, build items, base materials, colour / texture / composite / multi groups.
|
||||
-- Not supported: beam lattice, slices, displacement, volumetric and secure content, thumbnails, normals.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -37,13 +23,13 @@ local ThreeMf = {
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ 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)
|
||||
maxVertices: number?, -- default 2^25
|
||||
maxNodes: number?, -- 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)
|
||||
-- millimeter)
|
||||
level: number?, -- default 6; 0 stores
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
@@ -51,7 +37,6 @@ 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
|
||||
|
||||
Reference in new issue
Block a user