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,10 +1,6 @@
|
||||
--!strict
|
||||
-- model/gltf/Read: a parsed glTF 2.0 JSON document (+ its buffers) -> Scene. Accessors of every component type
|
||||
-- (normalized ones as unorm / snorm, matrices with their column padding removed, byte strides, sparse substitution,
|
||||
-- accessors without a bufferView as zeros), images kept encoded (bufferView / data: URI / external file through the
|
||||
-- fetch callback), samplers, PBR materials with every material extension (textureInfo objects -> TextureRefs), the
|
||||
-- KHR_texture_transform of every texture reference, cameras, KHR_lights_punctual, skins, animations, morph targets,
|
||||
-- scenes. glTF extras and unhandled extensions are kept in <object>.extras.gltf = { extras, extensions }.
|
||||
-- model/gltf/Read: a parsed glTF 2.0 JSON document (+ its buffers) -> Scene. glTF extras and unhandled extensions are
|
||||
-- kept in <object>.extras.gltf = { extras, extensions }.
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Base64 = require("@rosetta/core/Base64")
|
||||
@@ -27,7 +23,7 @@ local TYPE_COLUMNS = { MAT2 = 2, MAT3 = 3, MAT4 = 4 }
|
||||
local MODES = { [0] = "points", [1] = "lines", [2] = "line_loop", [3] = "line_strip", [4] = "triangles", [5] = "triangle_strip", [6] = "triangle_fan" }
|
||||
local WRAP: { [number]: "clamp" | "mirror" | "repeat" } = { [33071] = "clamp", [33648] = "mirror", [10497] = "repeat" }
|
||||
local MIN_FILTER = { [9728] = "nearest", [9729] = "linear", [9984] = "nearest_mipmap_nearest", [9985] = "linear_mipmap_nearest", [9986] = "nearest_mipmap_linear", [9987] = "linear_mipmap_linear" }
|
||||
-- the extensions this reader understands (everything else that is required -> UNSUPPORTED)
|
||||
-- the extensions this reader understands (any other required one -> UNSUPPORTED)
|
||||
local KNOWN = {
|
||||
KHR_lights_punctual = true,
|
||||
KHR_mesh_quantization = true,
|
||||
@@ -83,7 +79,6 @@ local function numbers(v: any, n: number): { number }?
|
||||
return table.move(v, 1, n, 1, {} :: { number })
|
||||
end
|
||||
|
||||
-- extras.gltf: the JSON extras and the extensions not turned into Scene fields
|
||||
local function keep(obj: any, handled: { [string]: boolean }?): { [string]: any }?
|
||||
local extras = obj.extras
|
||||
local ext: { [string]: any }? = nil
|
||||
@@ -102,8 +97,7 @@ local function keep(obj: any, handled: { [string]: boolean }?): { [string]: any
|
||||
return { gltf = { extras = extras, extensions = ext } }
|
||||
end
|
||||
|
||||
-- a Draco attribute as the type its glTF accessor declares (the bytes as they are when the layouts agree, else value
|
||||
-- by value: normalized integers <-> floats, ints widened or narrowed)
|
||||
-- a Draco attribute as its glTF accessor's type: bytes as they are when the layouts agree, else value by value
|
||||
local DRACO_HANDLED = { KHR_draco_mesh_compression = true }
|
||||
local DRACO_SIZE = { i8 = 1, u8 = 1, i16 = 2, u16 = 2, i32 = 4, u32 = 4, f32 = 4 }
|
||||
local DRACO_SAME: { [string]: string } = { i8 = "i8", u8 = "u8", i16 = "i16", u16 = "u16", u32 = "u32", f32 = "f32", snorm8 = "i8", unorm8 = "u8", snorm16 = "i16", unorm16 = "u16", i32 = "i32" }
|
||||
@@ -171,7 +165,6 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
|
||||
end
|
||||
end
|
||||
local scene = Types.Scene.new()
|
||||
-- buffers
|
||||
local buffers: { buffer } = {}
|
||||
local total = 0
|
||||
for i, b in arr(doc.buffers) do
|
||||
@@ -228,7 +221,6 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
|
||||
end
|
||||
views[i] = { buf = b, offset = off, length = len, stride = stride }
|
||||
end
|
||||
-- accessors
|
||||
local accessors: { Types.Accessor } = {}
|
||||
for i, a in arr(doc.accessors) do
|
||||
local label = "accessor " .. (i - 1)
|
||||
@@ -295,7 +287,7 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
|
||||
if io + n * isz > iv.length or vo + n * elem > vv.length then
|
||||
return nil, Err.new(Err.CORRUPT, label .. ": sparse data runs past its bufferView")
|
||||
end
|
||||
-- sparse values are tightly packed (matrices: padded columns, rare enough to repack first)
|
||||
-- sparse values are tightly packed (matrices: padded columns)
|
||||
local vals, valOff = vv.buf, vv.offset + vo
|
||||
if cols > 1 and colBytes ~= colStride then
|
||||
vals = buffer.create(n * elem)
|
||||
@@ -317,7 +309,6 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
|
||||
local k = int(v)
|
||||
return if k then accessors[k + 1] else nil
|
||||
end
|
||||
-- images (kept encoded), samplers, textures
|
||||
local images: { Img } = {}
|
||||
for i, im in arr(doc.images) do
|
||||
if type(im) ~= "table" then
|
||||
@@ -403,7 +394,6 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
|
||||
end
|
||||
table.insert(scene.textures, tex)
|
||||
end
|
||||
-- materials
|
||||
local function texRef(info: any): Types.TextureRef?
|
||||
if type(info) ~= "table" or not int(info.index) or info.index >= #scene.textures then
|
||||
return nil
|
||||
@@ -415,7 +405,7 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
|
||||
end
|
||||
return r
|
||||
end
|
||||
-- an extension object with every textureInfo (a table with an index, under a key ending in Texture) -> TextureRef
|
||||
-- a textureInfo: a table with an index, under a key ending in Texture
|
||||
local function convertExt(v: any): any
|
||||
if type(v) ~= "table" then
|
||||
return v
|
||||
@@ -465,7 +455,6 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
|
||||
mat.extras = kept
|
||||
table.insert(scene.materials, mat)
|
||||
end
|
||||
-- meshes
|
||||
for i, m in arr(doc.meshes) do
|
||||
if type(m) ~= "table" then
|
||||
return nil, Err.new(Err.CORRUPT, "bad mesh " .. (i - 1))
|
||||
@@ -534,7 +523,6 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
|
||||
if not ind or ind.components ~= 1 or (ind.type ~= "u8" and ind.type ~= "u16" and ind.type ~= "u32") then
|
||||
return nil, Err.new(Err.CORRUPT, "bad indices accessor")
|
||||
end
|
||||
-- indices must reference existing vertices
|
||||
local get = Types.Scene.reader(ind :: Types.Accessor) :: any
|
||||
for q = 0, ind.count - 1 do
|
||||
if get(q, 0) >= count then
|
||||
@@ -563,7 +551,6 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
|
||||
end
|
||||
table.insert(scene.meshes, mesh)
|
||||
end
|
||||
-- cameras, lights
|
||||
for _, c in arr(doc.cameras) do
|
||||
if type(c) ~= "table" then
|
||||
return nil, Err.new(Err.CORRUPT, "bad camera")
|
||||
@@ -591,7 +578,6 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
|
||||
})
|
||||
end
|
||||
end
|
||||
-- nodes
|
||||
local nodes = arr(doc.nodes)
|
||||
for i, n in nodes do
|
||||
if type(n) ~= "table" then
|
||||
@@ -647,7 +633,6 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
|
||||
p = parent[p]
|
||||
end
|
||||
end
|
||||
-- skins
|
||||
for _, s in arr(doc.skins) do
|
||||
if type(s) ~= "table" then
|
||||
return nil, Err.new(Err.CORRUPT, "bad skin")
|
||||
@@ -661,7 +646,6 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
|
||||
end
|
||||
table.insert(scene.skins, { name = str(s.name), joints = joints, inverseBindMatrices = accessor(s.inverseBindMatrices), skeleton = if int(s.skeleton) and s.skeleton < #scene.nodes then s.skeleton + 1 else nil })
|
||||
end
|
||||
-- animations
|
||||
for _, a in arr(doc.animations) do
|
||||
if type(a) ~= "table" then
|
||||
return nil, Err.new(Err.CORRUPT, "bad animation")
|
||||
@@ -698,7 +682,6 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
|
||||
end
|
||||
table.insert(scene.animations, anim)
|
||||
end
|
||||
-- scenes
|
||||
local scenes = arr(doc.scenes)
|
||||
local list = {}
|
||||
for _, s in scenes do
|
||||
@@ -720,7 +703,6 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
|
||||
end
|
||||
end
|
||||
end
|
||||
-- root extensions other than the lights, asset info
|
||||
local rootExt = nil
|
||||
if type(doc.extensions) == "table" then
|
||||
for k, v in doc.extensions do
|
||||
|
||||
Reference in new issue
Block a user