model formats (mca..x3d): comments are specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
b952c10222
49 files changed
+310
-936
No files matched your search
@@ -1,8 +1,6 @@
|
||||
--!strict
|
||||
-- model/obj/Mtl: Wavefront .mtl material libraries (Paul Bourke's MTL spec + the common PBR extension: Pr, Pm, Ps, Pc,
|
||||
-- Pcr, aniso, anisor, Ke, map_Pr, map_Pm, map_Ke, norm). parse(text) -> { name -> { statement -> values } } keeping
|
||||
-- every statement; toMaterial maps one onto the Scene Material (glTF PBR, Phong values in extras.obj); write is the
|
||||
-- reverse (from extras.obj when present, else from the PBR fields).
|
||||
-- model/obj/Mtl: Wavefront .mtl (Paul Bourke's MTL spec + the PBR extension: Pr, Pm, Ps, Pc, Pcr, aniso, anisor, Ke,
|
||||
-- map_Pr, map_Pm, map_Ke, norm); Phong values are kept in extras.obj.
|
||||
local Types = require("@rosetta/Types")
|
||||
|
||||
local Mtl = {}
|
||||
@@ -35,7 +33,6 @@ local function parseMap(args: { string }): Map
|
||||
end
|
||||
end
|
||||
else
|
||||
-- -o / -s / -t: up to three numbers
|
||||
while i <= #args and #vals < 3 and tonumber(args[i]) ~= nil and i < #args do
|
||||
table.insert(vals, args[i])
|
||||
i += 1
|
||||
@@ -48,7 +45,6 @@ local function parseMap(args: { string }): Map
|
||||
return { file = file, options = opts }
|
||||
end
|
||||
|
||||
-- parse(text) -> { Entry } in file order
|
||||
function Mtl.parse(text: string): { Entry }
|
||||
local out: { Entry } = {}
|
||||
local cur: Entry? = nil
|
||||
@@ -96,7 +92,6 @@ function Mtl.mime(file: string): string?
|
||||
return if ext then MIME[string.lower(ext)] else nil
|
||||
end
|
||||
|
||||
-- toMaterial(entry, texture) -> Material: texture(map) returns the scene texture index for a map
|
||||
function Mtl.toMaterial(e: Entry, texture: (Map) -> number): Types.Material
|
||||
local m = Types.Scene.material(e.name)
|
||||
local v = e.values
|
||||
@@ -144,7 +139,6 @@ function Mtl.toMaterial(e: Entry, texture: (Map) -> number): Types.Material
|
||||
if m.emissiveTexture and not ke then
|
||||
m.emissive = { 1, 1, 1 }
|
||||
end
|
||||
-- everything else as it was, for the writer and for Phong renderers
|
||||
local extra: { [string]: any } = {}
|
||||
for k, vals in v do
|
||||
extra[k] = vals
|
||||
@@ -171,7 +165,6 @@ local function num(v: number): string
|
||||
end
|
||||
Mtl.num = num
|
||||
|
||||
-- write(materials, name, textureFile) -> MTL text. textureFile(index) -> the file name a map refers to.
|
||||
function Mtl.write(materials: { Types.Material }, names: { string }, textureFile: (number) -> string?): string
|
||||
local out = { "# written by rosetta" }
|
||||
for i, m in materials do
|
||||
@@ -190,7 +183,6 @@ function Mtl.write(materials: { Types.Material }, names: { string }, textureFile
|
||||
end
|
||||
order = table.clone(saved.order or {})
|
||||
end
|
||||
-- the PBR fields win over the saved Phong values they map to
|
||||
local bc = m.baseColor or { 1, 1, 1, 1 }
|
||||
values.Kd = { bc[1], bc[2], bc[3] }
|
||||
if (bc[4] or 1) < 1 or values.d then
|
||||
@@ -235,7 +227,6 @@ function Mtl.write(materials: { Types.Material }, names: { string }, textureFile
|
||||
setMap("norm", m.normalTexture)
|
||||
end
|
||||
setMap("map_ORM", m.metallicRoughnessTexture)
|
||||
-- the saved order first, then the rest sorted
|
||||
local keys = {}
|
||||
local seen = {}
|
||||
for _, k in order do
|
||||
|
||||
@@ -1,17 +1,7 @@
|
||||
--!strict
|
||||
-- model/obj: Wavefront OBJ (+ MTL), from Paul Bourke's OBJ / MTL specifications and what Blender / Maya / assimp
|
||||
-- write. Supports: v (with the common "v x y z r g b" vertex colour extension), vt, vn, f with every index form
|
||||
-- (v, v/vt, v//vn, v/vt/vn, negative = relative), l, p, o / g (nodes), usemtl / mtllib (materials: Phong + the PBR
|
||||
-- extension, texture maps with -o / -s / -bm options; texture images stay encoded bytes or file names, see Types), line
|
||||
-- continuations, comments. n-gons are fan-triangulated by default (opts.triangulate = false keeps them as "polygons",
|
||||
-- MeshOps.triangulate ear-clips concave ones). TEXCOORD_0 is flipped to glTF's top-left origin (opts.flipV = false
|
||||
-- keeps OBJ's).
|
||||
-- Nodes: one per object (o); groups (g) inside an object become child nodes; files without o: one root node per group.
|
||||
-- Each node has a mesh with one primitive per material. Vertices are unwelded per (v, vt, vn) combination.
|
||||
-- Not supported: free-form geometry (curv, surf, ...: skipped and counted in meta.source.skipped), smoothing groups
|
||||
-- (s: kept per node in extras.obj.smoothing, not used to compute normals).
|
||||
-- encode: every mesh primitive of the scene baked into world space, one o per node; materials into a .mtl (written to
|
||||
-- opts.files[<name>.mtl] when opts.files is a table, see Types), embedded texture bytes into opts.files too.
|
||||
-- model/obj: Wavefront OBJ + MTL (Paul Bourke's specs, as Blender / Maya / assimp write them). Supports: v (with
|
||||
-- "v x y z r g b"), vt, vn, f in every index form, l, p, o / g, usemtl / mtllib, continuations; TEXCOORD_0 flipped to
|
||||
-- top-left. Not supported: free-form geometry (curv, surf: skipped), smoothing groups (kept in extras.obj.smoothing).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -31,21 +21,21 @@ local Obj = {
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
triangulate: boolean?, -- fan-triangulate n-gons (default true); false keeps "polygons" primitives
|
||||
flipV: boolean?, -- TEXCOORD_0.v = 1 - v (default true: glTF's convention)
|
||||
mtl: (string | buffer)?, -- the material library text, when not found through resolve / files
|
||||
resolve: ((uri: string) -> any)?, -- reads mtllib / texture files (the api sets it for paths)
|
||||
files: { [string]: any }?, -- or: the other files by name
|
||||
triangulate: boolean?, -- default true
|
||||
flipV: boolean?, -- default true
|
||||
mtl: (string | buffer)?,
|
||||
resolve: ((uri: string) -> any)?,
|
||||
files: { [string]: any }?,
|
||||
maxVertices: number?, -- refuse more v / vt / vn / corners (default 2^25)
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
name: string?, -- the destination file name: the .mtl is named after it (default "model.mtl")
|
||||
mtlName: string?, -- the .mtl file name (overrides name)
|
||||
files: { [string]: any }?, -- receives the .mtl and embedded textures
|
||||
name: string?, -- default "model.mtl"
|
||||
mtlName: string?,
|
||||
files: { [string]: any }?,
|
||||
flipV: boolean?, -- default true (the inverse of decode)
|
||||
normals: boolean?, -- write vn (default true when the primitives have NORMAL)
|
||||
texcoords: boolean?, -- write vt (default true when the primitives have TEXCOORD_0)
|
||||
colors: boolean?, -- write vertex colours after v (default true when COLOR_0 exists)
|
||||
normals: boolean?, -- default true when the primitives have NORMAL
|
||||
texcoords: boolean?, -- default true when the primitives have TEXCOORD_0
|
||||
colors: boolean?, -- default true when COLOR_0 exists
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
@@ -146,7 +136,6 @@ function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
end
|
||||
return p
|
||||
end
|
||||
-- one corner "v/vt/vn" -> indices (0-based, -1 for none) | nil
|
||||
local function corner(tok: string): (number?, number, number)
|
||||
local a, b, c = string.match(tok, "^([^/]*)/?([^/]*)/?([^/]*)$")
|
||||
local vi = tonumber(a)
|
||||
@@ -180,7 +169,6 @@ function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
local line = string.sub(text, pos, e - 1)
|
||||
pos = e + 1
|
||||
lineNo += 1
|
||||
-- continuation lines
|
||||
while string.sub(line, -1) == "\\" or string.sub(line, -2) == "\\\r" do
|
||||
line = string.gsub(line, "\\\r?$", " ")
|
||||
local e2 = string.find(text, "\n", pos, true) or (len + 1)
|
||||
@@ -192,7 +180,7 @@ function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
if key == nil or string.sub(key, 1, 1) == "#" then
|
||||
continue
|
||||
end
|
||||
local rest: string = rest :: string -- both captures match together
|
||||
local rest: string = rest :: string
|
||||
local hash = string.find(rest, "#", 1, true)
|
||||
if hash then
|
||||
rest = string.gsub(string.sub(rest, 1, hash - 1), "%s+$", "")
|
||||
@@ -266,7 +254,6 @@ function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
end
|
||||
end
|
||||
if mode == "triangles" and k > 3 then
|
||||
-- fan: rewrite the k corners as (k - 2) triangles
|
||||
local cv, ct, cn = table.move(p.cv, base + 1, base + k, 1, {}), table.move(p.ct, base + 1, base + k, 1, {}), table.move(p.cn, base + 1, base + k, 1, {})
|
||||
local o = base
|
||||
for a = 2, k - 1 do
|
||||
@@ -276,7 +263,6 @@ function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
end
|
||||
end
|
||||
elseif mode == "lines" then
|
||||
-- a polyline -> segments
|
||||
local cv, ct, cn = table.move(p.cv, base + 1, base + k, 1, {}), table.move(p.ct, base + 1, base + k, 1, {}), table.move(p.cn, base + 1, base + k, 1, {})
|
||||
local o = base
|
||||
for a = 1, k - 1 do
|
||||
@@ -309,7 +295,6 @@ function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
skipped += 1
|
||||
end
|
||||
end
|
||||
-- materials
|
||||
local scene = Types.Scene.new()
|
||||
local matIndex: { [string]: number } = {}
|
||||
local texIndex: { [string]: number } = {}
|
||||
@@ -354,7 +339,7 @@ function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
end
|
||||
end
|
||||
end
|
||||
-- materials named by usemtl but not defined anywhere: a default one with the name
|
||||
-- usemtl of an undefined material: a default one with that name
|
||||
for _, s in sections do
|
||||
for _, p in s.prims do
|
||||
if p.material and not matIndex[p.material] then
|
||||
@@ -364,7 +349,6 @@ function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
end
|
||||
end
|
||||
end
|
||||
-- vertices: one per distinct (v, vt, vn) in each primitive
|
||||
local f32 = buffer.writef32
|
||||
local KT, KN = nt + 1, nn + 1
|
||||
local numericKeys = np * KT * KN < 2 ^ 52
|
||||
@@ -434,7 +418,6 @@ function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
end
|
||||
return out
|
||||
end
|
||||
-- nodes: objects at the root, their groups as children; without objects every group is a root
|
||||
local objectNode: { [string]: number } = {}
|
||||
for _, s in sections do
|
||||
if #s.prims > 0 then
|
||||
@@ -463,7 +446,6 @@ function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
objectNode[s.object] = #scene.nodes
|
||||
table.insert(scene.roots, #scene.nodes)
|
||||
elseif hasObjects and s.object and objectNode[s.object] then
|
||||
-- the object's own faces after its groups: a child too
|
||||
table.insert(scene.nodes, node)
|
||||
table.insert(scene.nodes[objectNode[s.object]].children, #scene.nodes)
|
||||
else
|
||||
@@ -488,7 +470,6 @@ function Obj.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
-- the shortest decimal that reads back as the same f32
|
||||
local probe = buffer.create(4)
|
||||
local function f32str(v: number): string
|
||||
if v ~= v or v == math.huge or v == -math.huge then
|
||||
@@ -527,7 +508,6 @@ function Obj.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
|
||||
local placed = Types.Scene.flatten(scene)
|
||||
local out = { "# written by rosetta" }
|
||||
local mats = scene.materials or {}
|
||||
-- material names (unique), texture file names
|
||||
local names: { string } = {}
|
||||
local used: { [string]: boolean } = {}
|
||||
for i, m in mats do
|
||||
@@ -595,7 +575,6 @@ function Obj.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
|
||||
return nil, Err.new(Err.ARG, "an attribute is shorter than POSITION")
|
||||
end
|
||||
end
|
||||
-- the normal matrix (inverse transpose) for baked normals
|
||||
local nm: { number }? = nil
|
||||
if Nr and not ident then
|
||||
local a, b, c, d, e, f, g, h, i = m[1], m[5], m[9], m[2], m[6], m[10], m[3], m[7], m[11]
|
||||
@@ -647,7 +626,6 @@ function Obj.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
|
||||
end
|
||||
return s
|
||||
end
|
||||
-- the corner list
|
||||
local ind = prim.indices
|
||||
local I: ((number, number) -> number)? = if ind then Types.Scene.reader(ind) else nil
|
||||
local count = if ind then ind.count else n
|
||||
@@ -743,7 +721,6 @@ function Obj.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- encodeMtl(scene, opts?) -> the .mtl text for the scene's materials (what encode puts in opts.files)
|
||||
function Obj.encodeMtl(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
if type(scene) ~= "table" or type(scene.materials) ~= "table" then
|
||||
return nil, Err.new(Err.ARG, "needs a Scene")
|
||||
|
||||
Reference in new issue
Block a user