ws-cleanup-types: image/pnm, model/*: type lookup tables, table.create results and literals; casts for known values

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-31 10:59:11 -05:00
1 parent 98b3957d51
commit 5ee6736c6e
23 files changed
+139 -137

No files matched your search

+5 -5
View File
@@ -123,7 +123,7 @@ function Codec.pack(s: buffer, n: number, ch: number, maxval: number, invert: bo
r, g, b, a = get(q), get(q + 1), get(q + 2), get(q + 3)
end
if fmt == 0 then
writeu32(dst, i * 4, r + g * 256 + b * 65536 + a * 16777216)
writeu32(dst, i * 4, r + g * 256 + b * 65536 + (a :: number) * 16777216)
elseif fmt == 1 then
writeu8(dst, i * 3, r)
writeu8(dst, i * 3 + 1, g)
@@ -132,12 +132,12 @@ function Codec.pack(s: buffer, n: number, ch: number, maxval: number, invert: bo
writeu8(dst, i, r)
elseif fmt == 3 then
writeu8(dst, i * 2, r)
writeu8(dst, i * 2 + 1, a)
writeu8(dst, i * 2 + 1, a :: number)
elseif fmt == 4 then
writeu16(dst, i * 8, r)
writeu16(dst, i * 8 + 2, g)
writeu16(dst, i * 8 + 4, b)
writeu16(dst, i * 8 + 6, a)
writeu16(dst, i * 8 + 6, a :: number)
elseif fmt == 5 then
writeu16(dst, i * 2, r)
end
@@ -217,9 +217,9 @@ function Codec.extract(px: buffer, fmt: number, n: number, ch: number, wide: boo
for c = 0, vals - 1 do
local v
if ch <= 2 then
v = if c == 0 then gray else a
v = if c == 0 then gray else a :: number
else
v = if c == 0 then r elseif c == 1 then g elseif c == 2 then b else a
v = if c == 0 then r elseif c == 1 then g elseif c == 2 then b else a :: number
end
if wide and not w16 then
v *= 257
+14 -13
View File
@@ -38,8 +38,8 @@ export type EncodeOptions = {
}
-- ------------------------------------------------------------------------------------------------ constants
local FMT_CODE = { rgba8 = 0, rgb8 = 1, gray8 = 2, graya8 = 3, rgba16 = 4, gray16 = 5, rgbaf32 = 6 }
local BPP = { rgba8 = 4, rgb8 = 3, gray8 = 1, graya8 = 2, rgba16 = 8, gray16 = 2, rgbaf32 = 16 }
local FMT_CODE: { [string]: number } = { rgba8 = 0, rgb8 = 1, gray8 = 2, graya8 = 3, rgba16 = 4, gray16 = 5, rgbaf32 = 6 }
local BPP: { [string]: number } = { rgba8 = 4, rgb8 = 3, gray8 = 1, graya8 = 2, rgba16 = 8, gray16 = 2, rgbaf32 = 16 }
local readu8 = buffer.readu8
-- ------------------------------------------------------------------------------------------------ sniff
@@ -106,7 +106,7 @@ function Pnm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
end
local kind = string.char(readu8(b, r.base + 1))
local p = r.base + 2
local w, h, maxval, ch, mode
local w: number?, h: number?, maxval: number?, ch: number, mode: number
local bits = false -- PBM: 1 = black
local bw = false -- PAM BLACKANDWHITE: 1 = white
local tupl: string? = nil
@@ -137,9 +137,9 @@ function Pnm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
end
ch = if kind == "3" or kind == "6" or kind == "F" then 3 else 1
bits = kind == "1" or kind == "4"
mode = if kind == "1" then 4 elseif kind == "2" or kind == "3" then 3 elseif kind == "4" then 0 elseif maxval > 255 then 2 else 1
mode = if kind == "1" then 4 elseif kind == "2" or kind == "3" then 3 elseif kind == "4" then 0 elseif (maxval :: number) > 255 then 2 else 1
elseif kind == "7" then
local depth
local depth: number?
while true do
local key
key, p = token(b, p, limit)
@@ -175,15 +175,16 @@ function Pnm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
if w == nil or h == nil or depth == nil or maxval == nil then
return nil, Err.new(Err.CORRUPT, "PAM header misses WIDTH / HEIGHT / DEPTH / MAXVAL", p)
end
if depth < 1 or depth > 4 then
return nil, Err.new(Err.UNSUPPORTED, "PAM depth " .. depth)
if (depth :: number) < 1 or (depth :: number) > 4 then
return nil, Err.new(Err.UNSUPPORTED, "PAM depth " .. (depth :: number))
end
ch = depth
bw = tupl ~= nil and string.sub(tupl, 1, 13) == "BLACKANDWHITE"
mode = if maxval > 255 then 2 else 1
ch = depth :: number
bw = tupl ~= nil and string.sub(tupl :: string, 1, 13) == "BLACKANDWHITE"
mode = if (maxval :: number) > 255 then 2 else 1
else
return nil, Err.new(Err.CORRUPT, "not a Netpbm file", r.base)
end
local w, h, maxval = w :: number, h :: number, maxval :: number -- each branch above returns when one is nil
if w < 1 or h < 1 or maxval < 1 or maxval > 65535 then
return nil, Err.new(Err.CORRUPT, string.format("bad size %d x %d or maxval %d", w, h, maxval))
end
@@ -221,7 +222,7 @@ function Pnm.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Image?, Err.E
if not ok then
return nil, Err.new(if e == "truncated" then Err.TRUNCATED else Err.CORRUPT, tostring(e))
end
local fmt = "rgba8"
local fmt: Types.PixelFormat = "rgba8"
if native then
local wide = maxval > 255
if bits or bw or ch == 1 then
@@ -260,7 +261,7 @@ function Pnm.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
if fc == nil then
return nil, Err.new(Err.ARG, "unsupported pixel format " .. tostring(img.format))
end
local w, h = img.width, img.height
local w: number, h: number = img.width, img.height
if type(w) ~= "number" or type(h) ~= "number" or w < 1 or h < 1 or w % 1 ~= 0 or h % 1 ~= 0 or buffer.len(img.pixels) < w * h * BPP[img.format] then
return nil, Err.new(Err.ARG, "bad image size or pixel buffer")
end
@@ -301,7 +302,7 @@ function Pnm.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?)
end
local ascii = o.ascii == true
local wideSrc = img.format == "gray16" or img.format == "rgba16" or img.format == "rgbaf32"
local maxval = o.maxval or (if wideSrc then 65535 else 255)
local maxval: number = o.maxval or (if wideSrc then 65535 else 255)
if type(maxval) ~= "number" or maxval < 1 or maxval > 65535 or maxval % 1 ~= 0 then
return nil, Err.new(Err.ARG, "maxval must be 1..65535")
end
+13 -13
View File
@@ -24,7 +24,7 @@ local children, child, text = Xml.children, Xml.child, Xml.text
local MIME = { png = "image/png", jpg = "image/jpeg", jpeg = "image/jpeg", tga = "image/tga", bmp = "image/bmp", gif = "image/gif", webp = "image/webp", dds = "image/vnd-ms.dds", tif = "image/tiff", tiff = "image/tiff", ktx2 = "image/ktx2", hdr = "image/vnd.radiance", exr = "image/x-exr", psd = "image/vnd.adobe.photoshop" }
local WRAP = { WRAP = "repeat", MIRROR = "mirror", CLAMP = "clamp", BORDER = "clamp", MIRROR_ONCE = "mirror", NONE = "clamp" }
local PARAM_SIZE = { float2 = 2, float3 = 3, float4 = 4, float2x2 = 4, float3x3 = 9, float4x4 = 16, int2 = 2, int3 = 3, int4 = 4 }
local PARAM_SIZE: { [string]: number } = { float2 = 2, float3 = 3, float4 = 4, float2x2 = 4, float3x3 = 9, float4x4 = 16, int2 = 2, int3 = 3, int4 = 4 }
local MEMBER = { X = 1, Y = 2, Z = 3, ANGLE = 4, W = 4, R = 1, G = 2, B = 3, A = 4, S = 1, T = 2, U = 1, V = 2, P = 3, Q = 4 }
-- ------------------------------------------------------------------------------------------------ helpers
@@ -353,7 +353,7 @@ local function texture(C: Ctx, params: { [string]: any }, samplerSid: string): n
local f = upper(match(text(mag), "[%w_]+") or "")
smp.filter = if f == "NEAREST" then "nearest" elseif f == "LINEAR" then "linear" else nil
end
if next(smp) then
if next(smp :: any) then
tex.sampler = smp
end
end
@@ -728,7 +728,7 @@ local function buildPrim(C: Ctx, prim: any, verticesEl: any, kind: string): Buil
local np = #p
local keyOf: { [any]: number } = {}
local tuples: { number } = {} -- the first p index of each output vertex's tuple
local corner = table.create(#starts)
local corner = table.create(#starts) :: { number }
local count = 0
for ci, s in starts do
local base = s * stride
@@ -803,7 +803,7 @@ local function buildPrim(C: Ctx, prim: any, verticesEl: any, kind: string): Buil
mode = "triangles"
elseif C.triangulate then
-- fan triangulation
local tri = {}
local tri: { number } = {}
local at = 0
for _, f in faces do
for k = 1, f - 2 do
@@ -1067,13 +1067,13 @@ local function camera(C: Ctx, el: any): number?
local tx, ty = math.tan(math.rad(numText(xfov, 45)) / 2), math.tan(yf / 2)
ar = if ty > 0 then tx / ty else nil
end
cam = { name = attr(el, "name") or attr(el, "id"), type = "perspective", yfov = yf, aspect = ar, znear = numText(child(persp, "znear"), 0.1), zfar = numText(child(persp, "zfar"), 1000) }
cam = { name = attr(el, "name") or attr(el, "id"), type = "perspective" :: "perspective", yfov = yf, aspect = ar, znear = numText(child(persp, "znear"), 0.1), zfar = numText(child(persp, "zfar"), 1000) } :: Types.Camera
elseif ortho then
local xm, ym = child(ortho, "xmag"), child(ortho, "ymag")
local ar = numText(child(ortho, "aspect_ratio"), 1)
local x = if xm then numText(xm, 1) else numText(ym, 1) * ar
local y = if ym then numText(ym, 1) else numText(xm, 1) / ar
cam = { name = attr(el, "name") or attr(el, "id"), type = "orthographic", xmag = x, ymag = y, znear = numText(child(ortho, "znear"), 0.1), zfar = numText(child(ortho, "zfar"), 1000) }
cam = { name = attr(el, "name") or attr(el, "id"), type = "orthographic" :: "orthographic", xmag = x, ymag = y, znear = numText(child(ortho, "znear"), 0.1), zfar = numText(child(ortho, "zfar"), 1000) } :: Types.Camera
else
return nil
end
@@ -1091,7 +1091,7 @@ local function light(C: Ctx, el: any): (number?, { number }?)
if amb then
return nil, color(amb) or { 1, 1, 1, 1 }
end
local kinds = { directional = "directional", point = "point", spot = "spot" }
local kinds: { [string]: string } = { directional = "directional", point = "point", spot = "spot" }
for k, t in kinds do
local l = child(tc, k)
if l then
@@ -1518,7 +1518,7 @@ local function sample(c: Curve, t: number): { number }
local inp, out, st = c.input, c.output, c.stride
local n = #inp
local function at(i: number): { number }
local r = table.create(st)
local r = table.create(st) :: { number }
for k = 1, st do
r[k] = out[(i - 1) * st + k] or 0
end
@@ -1561,7 +1561,7 @@ local function target(C: Ctx, s: string): ({ number }?, any, number?)
if not id then
return nil, nil, nil
end
local spath, rest = match(sids, "^([^%.%(]*)(.*)$")
local spath, rest = match(sids :: string, "^([^%.%(]*)(.*)$")
if not spath then
return nil, nil, nil
end
@@ -1734,9 +1734,9 @@ local function bake(C: Ctx, name: string?, byNode: { [number]: { Curve } }, orde
buffer.writef32(ib, (i - 1) * 4, t)
end
local input = Scene.accessor(ib, "f32", 1, n)
table.insert(channels, { node = ni, path = "translation", input = input, output = Scene.accessor(tb, "f32", 3, n), interpolation = "LINEAR" })
table.insert(channels, { node = ni, path = "rotation", input = input, output = Scene.accessor(rb, "f32", 4, n), interpolation = "LINEAR" })
table.insert(channels, { node = ni, path = "scale", input = input, output = Scene.accessor(sb, "f32", 3, n), interpolation = "LINEAR" })
table.insert(channels, { node = ni, path = "translation", input = input, output = Scene.accessor(tb, "f32", 3, n), interpolation = "LINEAR" } :: Types.Channel)
table.insert(channels, { node = ni, path = "rotation", input = input, output = Scene.accessor(rb, "f32", 4, n), interpolation = "LINEAR" } :: Types.Channel)
table.insert(channels, { node = ni, path = "scale", input = input, output = Scene.accessor(sb, "f32", 3, n), interpolation = "LINEAR" } :: Types.Channel)
-- the node is now TRS-driven
local node = C.scene.nodes[ni]
if node.matrix then
@@ -1829,7 +1829,7 @@ function Read.scene(doc: any, fetch: Fetch, opts: any?): (Types.Scene?, Err.Err?
local asset = child(root, "asset")
local up = asset and child(asset, "up_axis")
local upAxis = if up then upper(match(text(up), "[%w_]+") or "Y_UP") else "Y_UP"
scene.meta.up = if upAxis == "Z_UP" then "z" elseif upAxis == "X_UP" then "x" :: any else "y"
scene.meta.up = if upAxis == "Z_UP" then "z" :: "z" elseif upAxis == "X_UP" then "x" :: any else "y" :: "y"
local unit = asset and child(asset, "unit")
local meter = unit and attr(unit, "meter")
scene.meta.unit = if meter then tonumber(meter) or tonumber((string.gsub(meter, ",", "."))) or 1 else 1
+2 -2
View File
@@ -157,7 +157,7 @@ function Write.build(scene: Types.Scene, opts: any?): string?
table.insert(out, s)
end
w('<?xml version="1.0" encoding="utf-8"?>\n<COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1">\n')
local meta = scene.meta or {}
local meta: { [string]: any } = scene.meta or {}
local up = if meta.up == "z" then "Z_UP" elseif meta.up == "x" then "X_UP" else "Y_UP"
w(format('<asset><contributor><authoring_tool>formats-luau</authoring_tool></contributor><created>1970-01-01T00:00:00Z</created><modified>1970-01-01T00:00:00Z</modified><unit meter="%s" name="unit"/><up_axis>%s</up_axis></asset>\n', f(meta.unit or 1), up))
-- images
@@ -189,7 +189,7 @@ function Write.build(scene: Types.Scene, opts: any?): string?
w("<library_effects>\n")
for i, mat in scene.materials do
local ph: any = mat.extras and mat.extras.phong or {}
local params = {}
local params: { [any]: any } = {} -- the newparam strings, and their sids as seen-keys
local function texParam(ref: Types.TextureRef?): (string?, number?)
if not ref or not imageId[ref.texture] then
return nil, nil
+1 -1
View File
@@ -117,7 +117,7 @@ local function zae(r: Bytes.Reader): (any, string?, Err.Err?)
local manifest = zip:readString("manifest.xml")
if manifest then
local d = Xml.decode(manifest)
local rootEl = d and d.root and Xml.child(d.root, "dae_root")
local rootEl = d and d.root and Xml.child(d.root :: Xml.Node, "dae_root")
if rootEl then
main = string.match(Xml.text(rootEl), "^%s*(.-)%s*$")
main = main and string.gsub(main, "^%./", "")
+6 -6
View File
@@ -63,17 +63,17 @@ function Ascii.parse(text: string, opts: { maxBytes: number? }?): (Node?, any, n
-- the values of one node (after the ':'), up to a new name, '{' or '}'
local function values(): ({ any }?, string?)
local props = {}
local props: { any } = {}
while true do
skip()
if i > n then
return props
return props, nil
end
local c = sbyte(text, i)
if c == 44 then -- ,
i += 1
elseif c == 123 or c == 125 then
return props
return props, nil
elseif c == 34 then -- "
local e = sfind(text, "\"", i + 1, true)
if not e then
@@ -117,7 +117,7 @@ function Ascii.parse(text: string, opts: { maxBytes: number? }?): (Node?, any, n
else
local nm = nameAt()
if nm then
return props
return props, nil
end
local s, e = sfind(text, "^[%w_]+", i)
if not s then
@@ -230,7 +230,7 @@ function Ascii.write(root: any, version: number?): string
table.insert(parts, string.sub(v, 2))
else
local nm, cls = string.match(v, "^(.-)%z\1(.*)$")
local sv = if nm then cls .. "::" .. nm else v
local sv = if nm then (cls :: string) .. "::" .. nm else v
table.insert(parts, "\"" .. string.gsub(sv, "\"", "&quot;") .. "\"")
end
elseif tv == "table" and v.array then
@@ -246,7 +246,7 @@ function Ascii.write(root: any, version: number?): string
end
end
if arr then
local vals = table.create(arr.count)
local vals = table.create(arr.count) :: { string }
for k = 0, arr.count - 1 do
vals[k + 1] = fmt(buffer.readf64(arr.data, k * 8))
end
+2 -2
View File
@@ -38,7 +38,7 @@ local function i64(b: buffer, p: number, asString: boolean): any
end
Binary.i64 = i64
local ELEM = { f = 4, d = 8, l = 8, i = 4, b = 1, c = 1 }
local ELEM: { [string]: number } = { f = 4, d = 8, l = 8, i = 4, b = 1, c = 1 }
-- the elements of a raw array (n elements of type t at p) as f64s
local function toF64(t: string, src: buffer, p: number, n: number): buffer
@@ -170,7 +170,7 @@ function Binary.parse(b: buffer, base: number, limit: number, opts: { maxBytes:
plen = readu32(b, p + 16) + readu32(b, p + 20) * 4294967296
nlen = readu8(b, p + 24)
else
endOff, nprops, plen, nlen = readu32(b, p), readu32(b, p + 4), readu32(b, p + 8), readu8(b, p + 12)
endOff, nprops, plen, nlen = readu32(b, p), readu32(b, p + 4), readu32(b, p + 8), (readu8(b, p + 12))
end
if endOff == 0 then
return false, p + head
+6 -6
View File
@@ -1058,7 +1058,7 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
local near, far = pnum(p, "NearPlane", 10), pnum(p, "FarPlane", 4000)
if ortho then
local zoom = pnum(p, "OrthoZoom", 1)
cam = { name = a.name ~= "" and a.name or o.name, type = "orthographic", xmag = zoom * aspect, ymag = zoom, znear = near, zfar = far }
cam = { name = a.name ~= "" and a.name or o.name, type = "orthographic", xmag = zoom * aspect, ymag = zoom, znear = near, zfar = far } :: Types.Camera
else
local focal = pnum(p, "FocalLength", 0)
local hfov
@@ -1068,7 +1068,7 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
hfov = pnum(p, "FieldOfView", 40) * math.pi / 180
end
local yfov = 2 * math.atan(math.tan(hfov / 2) / aspect)
cam = { name = a.name ~= "" and a.name or o.name, type = "perspective", yfov = yfov, aspect = aspect, znear = near, zfar = far }
cam = { name = a.name ~= "" and a.name or o.name, type = "perspective", yfov = yfov, aspect = aspect, znear = near, zfar = far } :: Types.Camera
end
table.insert(scene.cameras, cam)
table.insert(scene.nodes, { name = o.name .. "_camera", children = {}, camera = #scene.cameras, rotation = { 0, -orient, 0, orient }, extras = { fbx = { orientation = "camera" } } })
@@ -1317,13 +1317,13 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
return false
end
if tr.curves.T or varies(T, 3, nd.translation or { 0, 0, 0 }) then
table.insert(anim.channels, { node = ni, path = "translation", input = input, output = f32acc(T, 3), interpolation = interp })
table.insert(anim.channels, { node = ni, path = "translation", input = input, output = f32acc(T, 3), interpolation = interp } :: Types.Channel)
end
if tr.curves.R or varies(R, 4, nd.rotation or { 0, 0, 0, 1 }) then
table.insert(anim.channels, { node = ni, path = "rotation", input = input, output = f32acc(R, 4), interpolation = interp })
table.insert(anim.channels, { node = ni, path = "rotation", input = input, output = f32acc(R, 4), interpolation = interp } :: Types.Channel)
end
if tr.curves.S or varies(S, 3, nd.scale or { 1, 1, 1 }) then
table.insert(anim.channels, { node = ni, path = "scale", input = input, output = f32acc(S, 3), interpolation = interp })
table.insert(anim.channels, { node = ni, path = "scale", input = input, output = f32acc(S, 3), interpolation = interp } :: Types.Channel)
end
end
end
@@ -1348,7 +1348,7 @@ function Read.scene(root: Node, version: number, fetch: Fetch, opts: any?): (Typ
end
end
local acc = f32acc(out, 1)
table.insert(anim.channels, { node = ni, path = "weights", input = f32acc(times, 1), output = acc, interpolation = if allConstant(cs) then "STEP" else "LINEAR" })
table.insert(anim.channels, { node = ni, path = "weights", input = f32acc(times, 1), output = acc, interpolation = if allConstant(cs) then "STEP" else "LINEAR" } :: Types.Channel)
end
end
end
+12 -12
View File
@@ -91,7 +91,7 @@ local function faces(prim: Types.Primitive): { { number } }?
if p + sz > count then
break
end
local poly = table.create(sz)
local poly = table.create(sz) :: { number }
for q = 0, sz - 1 do
poly[q + 1] = I(p + q)
end
@@ -148,9 +148,9 @@ function Write.tree(scene: Types.Scene, opts: any?): (WNode?, Err.Err?)
local nodes = scene.nodes or {}
-- ------------------------------------------------------------------------------------------ textures
local EXT = { ["image/png"] = "png", ["image/jpeg"] = "jpg", ["image/bmp"] = "bmp", ["image/webp"] = "webp", ["image/x-tga"] = "tga", ["image/vnd-ms.dds"] = "dds", ["image/tiff"] = "tif" }
local EXT: { [string]: string } = { ["image/png"] = "png", ["image/jpeg"] = "jpg", ["image/bmp"] = "bmp", ["image/webp"] = "webp", ["image/x-tga"] = "tga", ["image/vnd-ms.dds"] = "dds", ["image/tiff"] = "tif" }
local texIds: { [number]: number } = {}
local base = opts and opts.name and string.gsub(string.match(opts.name, "([^/\\]+)$") or opts.name, "%.[^.]*$", "") or "model"
local base: string = opts and opts.name and string.gsub(string.match(opts.name, "([^/\\]+)$") or opts.name, "%.[^.]*$", "") or "model"
for ti, tex in scene.textures or {} do
local file = tex.uri or (base .. "_" .. (tex.name or tostring(ti)) .. "." .. (EXT[tex.mime or ""] or "png"))
local tname = tex.name or ("Texture" .. ti)
@@ -295,14 +295,14 @@ function Write.tree(scene: Types.Scene, opts: any?): (WNode?, Err.Err?)
end
visiting[i] = true
local nd = nodes[i]
local l = if nd.matrix and #nd.matrix == 16 then nd.matrix else Xform.compose(nd.translation, nd.rotation, nd.scale)
local l = if nd.matrix and #(nd.matrix :: Xform.M4) == 16 then nd.matrix else Xform.compose(nd.translation, nd.rotation, nd.scale)
local p = parentOf[i]
local w = if p then Xform.mul(world(p), l) else l
worldM[i] = w
return w
end
local function trs(nd: Types.Node): ({ number }, { number }, { number })
if nd.matrix and #nd.matrix == 16 then
if nd.matrix and #(nd.matrix :: Xform.M4) == 16 then
return Xform.decompose(nd.matrix)
end
return nd.translation or { 0, 0, 0 }, nd.rotation or { 0, 0, 0, 1 }, nd.scale or { 1, 1, 1 }
@@ -505,7 +505,7 @@ function Write.tree(scene: Types.Scene, opts: any?): (WNode?, Err.Err?)
for v = 0, n - 1 do
local u, w = 0, 0
if T then
u, w = T(v, 0), T(v, 1)
u, w = T(v, 0), (T(v, 1))
end
table.insert(UV[k], u)
table.insert(UV[k], if flipV then 1 - w else w)
@@ -647,8 +647,8 @@ function Write.tree(scene: Types.Scene, opts: any?): (WNode?, Err.Err?)
end
for _, l in layout do
local tg = l.prim.targets and (l.prim.targets :: any)[t]
local TP = tg and accessorReader(tg.POSITION)
local TN = tg and accessorReader(tg.NORMAL)
local TP: ((i: number, c: number) -> number)? = tg and accessorReader(tg.POSITION)
local TN: ((i: number, c: number) -> number)? = tg and accessorReader(tg.NORMAL)
if TP then
for v = 0, l.prim.attributes.POSITION.count - 1 do
local x, y, z = TP(v, 0), TP(v, 1), TP(v, 2)
@@ -822,7 +822,7 @@ function Write.tree(scene: Types.Scene, opts: any?): (WNode?, Err.Err?)
local PATH = { translation = { "T", "Lcl Translation", 3 }, rotation = { "R", "Lcl Rotation", 4 }, scale = { "S", "Lcl Scaling", 3 } }
for ai, anim in scene.animations or {} do
local stid, lid = id(), id()
local astart, astop = math.huge, -math.huge
local astart: number, astop: number = math.huge, -math.huge
local layerNodes = {}
for _, ch in anim.channels do
local mid = modelIds[ch.node]
@@ -834,7 +834,7 @@ function Write.tree(scene: Types.Scene, opts: any?): (WNode?, Err.Err?)
local times = table.create(n, 0)
for k = 0, n - 1 do
times[k + 1] = IN(k, 0)
astart, astop = math.min(astart, times[k + 1]), math.max(astop, times[k + 1])
astart, astop = math.min(astart, times[k + 1]), (math.max(astop, times[k + 1]))
end
local cubic = ch.interpolation == "CUBICSPLINE"
local step = ch.interpolation == "STEP"
@@ -919,8 +919,8 @@ function Write.tree(scene: Types.Scene, opts: any?): (WNode?, Err.Err?)
end
-- ------------------------------------------------------------------------------------------ document
local meta = scene.meta or {}
local src = type(meta.source) == "table" and meta.source or {}
local meta: { [string]: any } = scene.meta or {}
local src: { [string]: any } = type(meta.source) == "table" and meta.source or {}
local up = if meta.up == "z" then 2 else 1
local upSign, front, frontSign, coord, coordSign = 1, 2, 1, 0, 1
if up == 2 then
+1 -1
View File
@@ -148,7 +148,7 @@ function Fbx.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
if not scene then
return nil, err
end
scene.meta.source.binary = binary
(scene.meta.source :: { [string]: any }).binary = binary
return scene, nil
end
+6 -6
View File
@@ -24,7 +24,7 @@ local SIZE = { [5120] = 1, [5121] = 1, [5122] = 2, [5123] = 2, [5125] = 4, [5126
local TYPE_COMPONENTS = { SCALAR = 1, VEC2 = 2, VEC3 = 3, VEC4 = 4, MAT2 = 4, MAT3 = 9, MAT4 = 16 }
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 = { [33071] = "clamp", [33648] = "mirror", [10497] = "repeat" }
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)
local KNOWN = {
@@ -78,7 +78,7 @@ local function numbers(v: any, n: number): { number }?
return nil
end
end
return table.move(v, 1, n, 1, {})
return table.move(v, 1, n, 1, {} :: { number })
end
-- extras.gltf: the JSON extras and the extensions not turned into Scene fields
@@ -449,13 +449,13 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
return nil, Err.new(Err.CORRUPT, "bad indices accessor")
end
-- indices must reference existing vertices
local get = Types.Scene.reader(ind) :: any
local get = Types.Scene.reader(ind :: Types.Accessor) :: any
for q = 0, ind.count - 1 do
if get(q, 0) >= count then
return nil, Err.new(Err.CORRUPT, "index out of range")
end
end
prim.indices = ind
prim.indices = ind :: Types.Accessor
end
if type(p.targets) == "table" then
local targets = {}
@@ -484,10 +484,10 @@ function Read.scene(doc: any, bin: buffer?, fetch: Fetch, opts: any?): (Types.Sc
end
if c.type == "orthographic" and type(c.orthographic) == "table" then
local o = c.orthographic
table.insert(scene.cameras, { name = str(c.name), type = "orthographic", xmag = num(o.xmag, 1), ymag = num(o.ymag, 1), znear = num(o.znear, 0), zfar = num(o.zfar, 100) })
table.insert(scene.cameras, { name = str(c.name), type = "orthographic" :: "orthographic", xmag = num(o.xmag, 1), ymag = num(o.ymag, 1), znear = num(o.znear, 0), zfar = num(o.zfar, 100) } :: Types.Camera)
else
local p = if type(c.perspective) == "table" then c.perspective else {}
table.insert(scene.cameras, { name = str(c.name), type = "perspective", yfov = num(p.yfov, 0.8), aspect = if type(p.aspectRatio) == "number" then p.aspectRatio else nil, znear = num(p.znear, 0.01), zfar = if type(p.zfar) == "number" then p.zfar else nil })
table.insert(scene.cameras, { name = str(c.name), type = "perspective" :: "perspective", yfov = num(p.yfov, 0.8), aspect = if type(p.aspectRatio) == "number" then p.aspectRatio else nil, znear = num(p.znear, 0.01), zfar = if type(p.zfar) == "number" then p.zfar else nil } :: Types.Camera)
end
end
local lp = type(doc.extensions) == "table" and doc.extensions.KHR_lights_punctual
+2 -2
View File
@@ -385,7 +385,7 @@ function Write.build(scene: Types.Scene, opts: Options?): (Out?, Err.Err?)
end
local gp = object()
gp.attributes = object()
local names = {}
local names: { string } = {}
for name in p.attributes do
table.insert(names, name)
end
@@ -603,7 +603,7 @@ function Write.build(scene: Types.Scene, opts: Options?): (Out?, Err.Err?)
return nil, failure
end
-- scenes: the original list (meta.gltf.scenes) when it is there, else the roots
local meta = if type(scene.meta) == "table" then scene.meta else {}
local meta: { [string]: any } = if type(scene.meta) == "table" then scene.meta else {}
local mg = type(meta.gltf) == "table" and meta.gltf or {}
local scenes = {}
if type(mg.scenes) == "table" and #mg.scenes > 0 then
+1 -1
View File
@@ -158,7 +158,7 @@ function Gltf.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.
end
-- ------------------------------------------------------------------------------------------------ encode
local EXT = { ["image/png"] = "png", ["image/jpeg"] = "jpg", ["image/webp"] = "webp", ["image/ktx2"] = "ktx2", ["image/avif"] = "avif" }
local EXT: { [string]: string } = { ["image/png"] = "png", ["image/jpeg"] = "jpg", ["image/webp"] = "webp", ["image/ktx2"] = "ktx2", ["image/avif"] = "avif" }
function Gltf.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err?)
if type(scene) ~= "table" or scene.kind ~= "scene" then
+2 -2
View File
@@ -104,7 +104,7 @@ function Mtl.toMaterial(e: Entry, texture: (Map) -> number): Types.Material
if kd and #kd >= 3 then
m.baseColor = { kd[1], kd[2], kd[3], 1 }
end
local d = if v.d then v.d[1] elseif v.Tr then 1 - v.Tr[1] else 1
local d = if v.d then (v.d :: { number })[1] elseif v.Tr then 1 - (v.Tr :: { number })[1] else 1
m.baseColor[4] = d
if d < 1 then
m.alphaMode = "blend"
@@ -113,7 +113,7 @@ function Mtl.toMaterial(e: Entry, texture: (Map) -> number): Types.Material
if ke and #ke >= 3 then
m.emissive = { ke[1], ke[2], ke[3] }
end
m.metallic = if v.Pm then v.Pm[1] else 0
m.metallic = if v.Pm then (v.Pm :: { number })[1] else 0
if v.Pr then
m.roughness = v.Pr[1]
elseif v.Ns then
+8 -7
View File
@@ -192,6 +192,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 hash = string.find(rest, "#", 1, true)
if hash then
rest = string.gsub(string.sub(rest, 1, hash - 1), "%s+$", "")
@@ -513,14 +514,14 @@ local function nameOk(s: string): string
return t
end
local EXT = { ["image/png"] = "png", ["image/jpeg"] = "jpg", ["image/webp"] = "webp", ["image/gif"] = "gif", ["image/bmp"] = "bmp", ["image/ktx2"] = "ktx2" }
local EXT: { [string]: string } = { ["image/png"] = "png", ["image/jpeg"] = "jpg", ["image/webp"] = "webp", ["image/gif"] = "gif", ["image/bmp"] = "bmp", ["image/ktx2"] = "ktx2" }
function Obj.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err?)
if type(scene) ~= "table" or scene.kind ~= "scene" or type(scene.nodes) ~= "table" or type(scene.meshes) ~= "table" then
return nil, Err.new(Err.ARG, "needs a Scene")
end
local flipV = not (opts and opts.flipV == false)
local base = opts and opts.name and string.gsub(string.match(opts.name, "([^/\\]+)$") or opts.name, "%.[^.]*$", "") or "model"
local base: string = opts and opts.name and string.gsub(string.match(opts.name, "([^/\\]+)$") or opts.name, "%.[^.]*$", "") or "model"
local mtlName = opts and opts.mtlName or (base .. ".mtl")
local files = opts and opts.files
local placed = Types.Scene.flatten(scene)
@@ -586,9 +587,9 @@ function Obj.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
table.insert(out, "o " .. nameOk(node.name or ("node_" .. pl.node)))
end
local n = pos.count
local Cc = if attrs.COLOR_0 and not (opts and opts.colors == false) then Types.Scene.reader(attrs.COLOR_0) else nil
local T = if attrs.TEXCOORD_0 and not (opts and opts.texcoords == false) then Types.Scene.reader(attrs.TEXCOORD_0) else nil
local Nr = if attrs.NORMAL and not (opts and opts.normals == false) then Types.Scene.reader(attrs.NORMAL) else nil
local Cc = if attrs.COLOR_0 and not (opts and opts.colors == false) then Types.Scene.reader(attrs.COLOR_0 :: Types.Accessor) else nil
local T = if attrs.TEXCOORD_0 and not (opts and opts.texcoords == false) then Types.Scene.reader(attrs.TEXCOORD_0 :: Types.Accessor) else nil
local Nr = if attrs.NORMAL and not (opts and opts.normals == false) then Types.Scene.reader(attrs.NORMAL :: Types.Accessor) else nil
for _, a in { attrs.COLOR_0, attrs.TEXCOORD_0, attrs.NORMAL } do
if a and (a.count < n or buffer.len(a.data) < a.count * a.components * (Types.COMPONENT_SIZE[a.type] or 4)) then
return nil, Err.new(Err.ARG, "an attribute is shorter than POSITION")
@@ -692,7 +693,7 @@ function Obj.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
if o + k > count then
return nil, Err.new(Err.ARG, "faces run past the indices")
end
local list = table.create(k)
local list = table.create(k) :: { number }
for j = 0, k - 1 do
list[j + 1] = at(o + j)
end
@@ -747,7 +748,7 @@ function Obj.encodeMtl(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.
if type(scene) ~= "table" or type(scene.materials) ~= "table" then
return nil, Err.new(Err.ARG, "needs a Scene")
end
local files = {}
local files: { [string]: string } = {}
local o: any = table.clone(opts or {})
o.files = files
o.mtlName = "x.mtl"
+5 -5
View File
@@ -153,13 +153,13 @@ function Off.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
local rest = kw
local st, hasC, hasN = false, false, false
if string.sub(rest, 1, 2) == "ST" then
st, rest = true, string.sub(rest, 3)
st, rest = true, (string.sub(rest, 3))
end
if string.sub(rest, 1, 1) == "C" then
hasC, rest = true, string.sub(rest, 2)
hasC, rest = true, (string.sub(rest, 2))
end
if string.sub(rest, 1, 1) == "N" then
hasN, rest = true, string.sub(rest, 2)
hasN, rest = true, (string.sub(rest, 2))
end
if rest == "4OFF" or rest == "nOFF" or rest == "4nOFF" then
return nil, Err.new(Err.UNSUPPORTED, kw .. " (homogeneous / n-dimensional vertices)")
@@ -442,7 +442,7 @@ function Off.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
local a: Flatten.Attr = { type = acc.type, components = acc.components, raw = raw, values = vals }
if acc.count == nf then
fc = a
elseif ex.faceOfTriangle then
elseif (ex :: { [string]: any }).faceOfTriangle then
local fot = Flatten.numbers(ex.faceOfTriangle, true)
if fot and #fot == nf then
fc, fcMap = a, fot
@@ -468,7 +468,7 @@ function Off.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
end
p += s
if fc then
local src = if fcMap then fcMap[f] else f - 1
local src = if fcMap then (fcMap :: { number })[f] else f - 1
table.insert(line, colorText(fc, fc.values, fc.components, src))
end
table.insert(out, table.concat(line, " "))
+19 -19
View File
@@ -127,8 +127,8 @@ local function parseHeader(r: Bytes.Reader): (Header?, Err.Err?)
if not enc or not ENCODINGS[enc] then
return nil, Err.new(Err.UNSUPPORTED, "unknown format " .. tostring(enc), lineAt)
end
if string.sub(ver, 1, 1) ~= "1" then
return nil, Err.new(Err.UNSUPPORTED, "version " .. ver, lineAt)
if string.sub(ver :: string, 1, 1) ~= "1" then
return nil, Err.new(Err.UNSUPPORTED, "version " .. (ver :: string), lineAt)
end
h.encoding = enc
elseif kw == "element" then
@@ -143,7 +143,7 @@ local function parseHeader(r: Bytes.Reader): (Header?, Err.Err?)
if not el then
return nil, Err.new(Err.CORRUPT, "property before any element", lineAt)
end
local words = {}
local words: { string } = {}
for w in string.gmatch(line, "%S+") do
table.insert(words, w)
end
@@ -153,9 +153,9 @@ local function parseHeader(r: Bytes.Reader): (Header?, Err.Err?)
if not cc or not ic then
return nil, Err.new(Err.CORRUPT, "unknown property type in " .. line, lineAt)
end
p = { name = words[5], ply = words[4], code = ic, list = true, countPly = words[3], countCode = cc }
p = { name = words[5], ply = words[4], code = ic, list = true, countPly = words[3], countCode = cc } :: Prop
elseif #words == 3 and CODES[words[2]] then
p = { name = words[3], ply = words[2], code = CODES[words[2]], list = false }
p = { name = words[3], ply = words[2], code = CODES[words[2]], list = false } :: Prop
else
return nil, Err.new(Err.CORRUPT, "bad property line: " .. string.sub(line, 1, 60), lineAt)
end
@@ -195,11 +195,11 @@ local function newPlan(e: Elem): Plan
code = table.create(np, 0),
isList = table.create(np, false),
countCode = table.create(np, 2),
dst = table.create(np),
dst = table.create(np) :: { buffer },
dOff = table.create(np, 0),
dStride = table.create(np, 0),
dCode = table.create(np, 0),
sizes = table.create(np),
sizes = table.create(np) :: { buffer },
bound = table.create(np, -1),
totals = table.create(np, 0),
}
@@ -291,7 +291,7 @@ local function readAscii(nextTok: () -> string?, e: Elem, pl: Plan): Err.Err?
local listItems: { [number]: { number } } = {}
for k, p in e.props do
if p.list then
listSizes[k] = table.create(n)
listSizes[k] = table.create(n) :: { number }
listItems[k] = {}
end
end
@@ -485,7 +485,7 @@ function Ply.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.E
if not h then
return nil, herr
end
local vi, fi, idxK
local vi: number?, fi: number?, idxK: number
for i, e in h.elems do
if e.name == "vertex" and vi == nil then
vi = i
@@ -767,7 +767,7 @@ local function bufNumbers(b: any, code: number, count: number): { number }?
if type(b) ~= "buffer" or buffer.len(b) < count * SIZE[code] then
return nil
end
local out = table.create(count)
local out = table.create(count) :: { number }
for i = 0, count - 1 do
out[i + 1] = Binary.rd(b, i * SIZE[code], code, false)
end
@@ -940,7 +940,7 @@ function Ply.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
if count == nf then
return vals
elseif fot and #fot == nf then
local out = table.create(nf)
local out = table.create(nf) :: { number }
for i = 1, nf do
local v = vals[fot[i] + 1]
if v == nil then
@@ -954,19 +954,19 @@ function Ply.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
end
local extraRows: { [string]: Row } = {}
if type(ex) == "table" and type(ex.faceAttributes) == "table" then
for name, acc in ex.faceAttributes do
for name: string, acc: any in ex.faceAttributes do
local vals = Flatten.numbers(acc, true)
local code = CODE_OF_ACC[acc.type]
if vals and code and acc.components == 1 then
local pv = perFace(vals, acc.count)
if pv then
extraRows[name] = { kind = "s", col = { name = name, ply = PLY_NAME[code], code = code, values = pv, stride = 1, offset = 0 } }
extraRows[name] = { kind = "s" :: "s", col = { name = name, ply = PLY_NAME[code], code = code, values = pv, stride = 1, offset = 0 } }
end
end
end
end
if type(ex) == "table" and type(ex.faceLists) == "table" then
for name, fl in ex.faceLists do
for name: string, fl: any in ex.faceLists do
local sz = type(fl) == "table" and Flatten.numbers(fl.sizes, true)
local items = type(fl) == "table" and Flatten.numbers(fl.items, true)
local code = type(fl) == "table" and (CODES[fl.itemType or ""] or CODE_OF_ACC[fl.items and fl.items.type or ""])
@@ -980,7 +980,7 @@ function Ply.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
if scc and scc <= 6 and SIZE[scc] >= SIZE[lcc] then
lcc = scc
end
extraRows[name] = { kind = "l", col = { name = name, ply = spell(fl.itemType, code), code = code, countPly = spell(fl.countType, lcc), countCode = lcc, sizes = sz, items = items } }
extraRows[name] = { kind = "l" :: "l", col = { name = name, ply = spell(fl.itemType, code), code = code, countPly = spell(fl.countType, lcc), countCode = lcc, sizes = sz, items = items } }
end
end
end
@@ -1025,7 +1025,7 @@ function Ply.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
continue
end
local rows: { Row } = {}
for _, pr in el.properties do
for _, pr: any in el.properties do
local code = CODES[pr.type or ""]
if not code then
return nil, Err.new(Err.ARG, "element " .. el.name .. ": unknown property type")
@@ -1043,13 +1043,13 @@ function Ply.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
if not (cc and sz and items) then
return nil, Err.new(Err.ARG, "element " .. el.name .. ": bad list property " .. tostring(pr.name))
end
table.insert(rows, { kind = "l", col = { name = pr.name, ply = pr.type, code = code, countPly = pr.countType, countCode = cc, sizes = sz, items = items } })
table.insert(rows, { kind = "l" :: "l", col = { name = pr.name, ply = pr.type, code = code, countPly = pr.countType, countCode = cc, sizes = sz, items = items } })
else
local vals = bufNumbers(pr.values, code, el.count)
if not vals then
return nil, Err.new(Err.ARG, "element " .. el.name .. ": bad property " .. tostring(pr.name))
end
table.insert(rows, { kind = "s", col = { name = pr.name, ply = pr.type, code = code, values = vals, stride = 1, offset = 0 } })
table.insert(rows, { kind = "s" :: "s", col = { name = pr.name, ply = pr.type, code = code, values = vals, stride = 1, offset = 0 } })
end
end
raws[el.name] = { name = el.name, count = el.count, rows = rows }
@@ -1084,7 +1084,7 @@ function Ply.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
-- header
local vrows: { Row } = {}
for _, c in cols do
table.insert(vrows, { kind = "s", col = c })
table.insert(vrows, { kind = "s" :: "s", col = c })
end
local lines = { "ply", "format " .. enc .. " 1.0" }
local function noNewline(s: any): string
+1 -1
View File
@@ -337,7 +337,7 @@ function Stl.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
return nil, Err.new(Err.ARG, tostring(merr))
end
local P = m.attrs.POSITION.values
local N = if o.normals == "attribute" and m.attrs.NORMAL then m.attrs.NORMAL.values else nil
local N = if o.normals == "attribute" and m.attrs.NORMAL then (m.attrs.NORMAL :: Flatten.Attr).values else nil
local C = m.attrs.COLOR_0
local corners = m.corners
local nf = #corners // 3
+6 -6
View File
@@ -579,7 +579,7 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
end
local textureIndex: { [string]: number } = {}
local WRAP = { ["repeat"] = "repeat", clamp = "clamp", mirror = "mirror", black = "clamp", useMetadata = "repeat" }
local WRAP: { [string]: string } = { ["repeat"] = "repeat", clamp = "clamp", mirror = "mirror", black = "clamp", useMetadata = "repeat" }
-- UsdUVTexture shader -> TextureRef (texture index + texCoord + transform)
local function textureRef(shader: Prim): Types.TextureRef?
if shaderId(shader) ~= "UsdUVTexture" then
@@ -1012,7 +1012,7 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
local cnt0 = counts[f + 1]
if cnt0 >= 3 and not hole[f] then
local base = faceStart[f + 1]
local verts = table.create(cnt0)
local verts = table.create(cnt0) :: { number }
for k = 0, cnt0 - 1 do
local corner = base + k
local point = indices[corner + 1]
@@ -1027,7 +1027,7 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
verts[k + 1] = v
end
if leftHanded then
local r = table.create(cnt0)
local r = table.create(cnt0) :: { number }
for k = 1, cnt0 do
r[k] = verts[cnt0 - k + 1]
end
@@ -1267,9 +1267,9 @@ function Build.scene(layer: Parse.Layer, opts: Options): (Types.Scene?, string?)
buffer.writef32(ro.data, ((k - 1) * 4 + c) * 4, R[c + 1])
end
end
table.insert(animation.channels, { node = ni, path = "translation", input = tin, output = tr, interpolation = "LINEAR" })
table.insert(animation.channels, { node = ni, path = "rotation", input = tin, output = ro, interpolation = "LINEAR" })
table.insert(animation.channels, { node = ni, path = "scale", input = tin, output = sc, interpolation = "LINEAR" })
table.insert(animation.channels, { node = ni, path = "translation", input = tin, output = tr, interpolation = "LINEAR" } :: Types.Channel)
table.insert(animation.channels, { node = ni, path = "rotation", input = tin, output = ro, interpolation = "LINEAR" } :: Types.Channel)
table.insert(animation.channels, { node = ni, path = "scale", input = tin, output = sc, interpolation = "LINEAR" } :: Types.Channel)
local T, R, S = decompose(localMatrix(ops, times[1]))
n.translation, n.rotation, n.scale = T, R, S
elseif #ops > 0 then
+5 -5
View File
@@ -501,7 +501,7 @@ local function parser(s: string, maxBytes: number)
-- time samples: { time: value, ... }
local function samples(i: number): ({ times: { number }, values: { Value } }, number)
local times, values = {}, {}
local times: { number }, values: { Value } = {}, {}
i = expect(i, "{")
while true do
i = ws(i)
@@ -523,7 +523,7 @@ local function parser(s: string, maxBytes: number)
i = expect(i, ":")
local v
v, i = value(i)
table.insert(times, t)
table.insert(times, t :: number) -- fail() threw on nil
table.insert(values, v)
end
return { times = times, values = values }, i
@@ -664,7 +664,7 @@ local function parser(s: string, maxBytes: number)
end
local prop = p.props[name :: string]
if not prop then
prop = { name = name :: string, rel = isRel }
prop = { name = name :: string, rel = isRel } :: Prop
p.props[name :: string] = prop
table.insert(p.order, name :: string)
end
@@ -806,8 +806,8 @@ function Parse.layer(text: string, maxBytes: number?): (Layer?, string?, number?
if ok then
return res, nil, nil
end
if type(res) == "table" and res.usda then
return nil, res.usda, math.max((res.at or 1) - 1, 0)
if type(res) == "table" and (res :: any).usda then
return nil, (res :: any).usda, math.max(((res :: any).at or 1) - 1, 0)
end
return nil, "parse error: " .. tostring(res), nil
end
+14 -14
View File
@@ -14,7 +14,7 @@ local Types = require("@formats/Types")
local Write = {}
local EXT = { ["image/png"] = "png", ["image/jpeg"] = "jpg", ["image/webp"] = "webp", ["image/ktx2"] = "ktx2", ["image/avif"] = "avif", ["image/bmp"] = "bmp", ["image/x-tga"] = "tga", ["image/gif"] = "gif", ["image/tiff"] = "tiff" }
local EXT: { [string]: string } = { ["image/png"] = "png", ["image/jpeg"] = "jpg", ["image/webp"] = "webp", ["image/ktx2"] = "ktx2", ["image/avif"] = "avif", ["image/bmp"] = "bmp", ["image/x-tga"] = "tga", ["image/gif"] = "gif", ["image/tiff"] = "tiff" }
local RAD = 180 / math.pi
export type Options = { files: { [string]: any }?, name: string?, upAxis: string?, metersPerUnit: number? }
@@ -79,7 +79,7 @@ local function values(acc: Types.Accessor?): ({ number }?, number)
return nil, 0
end
local n, c = acc.count, acc.components
local out = table.create(n * c)
local out = table.create(n * c) :: { number }
for i = 0, n - 1 do
for k = 0, c - 1 do
out[i * c + k + 1] = get(i, k)
@@ -123,14 +123,14 @@ type Merged = {
local function faces(p: Types.Primitive, nverts: number): ({ number }?, { number }?)
local idx: { number }
if p.indices then
idx = (values(p.indices)) or {}
idx = (values(p.indices)) or {} :: { number }
else
idx = table.create(nverts)
idx = table.create(nverts) :: { number }
for i = 1, nverts do
idx[i] = i - 1
end
end
local counts, out = {}, {}
local counts: { number }, out: { number } = {}, {}
local mode = p.mode
if mode == "triangles" or mode == nil then
for k = 1, #idx - 2, 3 do
@@ -180,7 +180,7 @@ end
local function merge(mesh: Types.Mesh, notes: { [string]: number }): Merged?
local prims: { Types.Primitive } = {}
for _, p in mesh.primitives do
if p.attributes and p.attributes.POSITION and p.attributes.POSITION.components == 3 and p.mode ~= "lines" and p.mode ~= "line_strip" and p.mode ~= "line_loop" and p.mode ~= "points" then
if p.attributes and p.attributes.POSITION and (p.attributes.POSITION :: Types.Accessor).components == 3 and p.mode ~= "lines" and p.mode ~= "line_strip" and p.mode ~= "line_loop" and p.mode ~= "points" then
table.insert(prims, p)
else
notes["lines / points primitives"] = (notes["lines / points primitives"] or 0) + 1
@@ -318,7 +318,7 @@ function Write.text(scene: Types.Scene, opts: Options?): (string, { [string]: nu
local rootName = if wrap then "root" else rootNames(nodes[roots[1]].name, "root")
-- texture files
local base = o.name and string.gsub(string.match(o.name, "([^/\\]+)$") or o.name, "%.[^.]*$", "") or "model"
local base: string = o.name and string.gsub(string.match(o.name, "([^/\\]+)$") or o.name, "%.[^.]*$", "") or "model"
local texFile: { [number]: string } = {}
for i, tex in scene.textures or {} do
local uri: string? = if tex.uri then (string.gsub(tex.uri, "\\", "/")) else nil
@@ -340,7 +340,7 @@ function Write.text(scene: Types.Scene, opts: Options?): (string, { [string]: nu
-- animation: per node, per path -> { times, values (components) }
type Track = { times: { number }, vals: { number }, comps: number }
local tracks: { [number]: { [string]: Track } } = {}
local tracks: { [number]: { [string]: Track? } } = {}
local tcps = 24
local tMin, tMax = math.huge, -math.huge
for _, anim in scene.animations or {} do
@@ -399,7 +399,7 @@ function Write.text(scene: Types.Scene, opts: Options?): (string, { [string]: nu
-- xform ops of a node (+ its animation)
local function xform(depth: number, ni: number, n: Types.Node)
local tr = tracks[ni]
local tr: { [string]: Track? }? = tracks[ni]
local order: { string } = {}
local function samples(track: Track, f: (vals: { number }, at: number) -> string): string
local parts = {}
@@ -438,7 +438,7 @@ function Write.text(scene: Types.Scene, opts: Options?): (string, { [string]: nu
line(depth, "double3 xformOp:translate = " .. tuple({ t[1] or 0, t[2] or 0, t[3] or 0 }))
end
if tr and tr.translation then
line(depth, "double3 xformOp:translate.timeSamples = " .. samples(tr.translation, vec3))
line(depth, "double3 xformOp:translate.timeSamples = " .. samples(tr.translation :: Track, vec3))
end
table.insert(order, '"xformOp:translate"')
end
@@ -447,7 +447,7 @@ function Write.text(scene: Types.Scene, opts: Options?): (string, { [string]: nu
line(depth, "quatf xformOp:orient = " .. quat(r, 0))
end
if tr and tr.rotation then
line(depth, "quatf xformOp:orient.timeSamples = " .. samples(tr.rotation, quat))
line(depth, "quatf xformOp:orient.timeSamples = " .. samples(tr.rotation :: Track, quat))
end
table.insert(order, '"xformOp:orient"')
end
@@ -456,7 +456,7 @@ function Write.text(scene: Types.Scene, opts: Options?): (string, { [string]: nu
line(depth, "float3 xformOp:scale = " .. tuple({ s[1] or 1, s[2] or 1, s[3] or 1 }))
end
if tr and tr.scale then
line(depth, "float3 xformOp:scale.timeSamples = " .. samples(tr.scale, vec3))
line(depth, "float3 xformOp:scale.timeSamples = " .. samples(tr.scale :: Track, vec3))
end
table.insert(order, '"xformOp:scale"')
end
@@ -570,7 +570,7 @@ function Write.text(scene: Types.Scene, opts: Options?): (string, { [string]: nu
line(depth, "float verticalAperture = " .. fmt(vap))
end
local LIGHT_TYPE = { directional = "DistantLight", point = "SphereLight", spot = "SphereLight" }
local LIGHT_TYPE: { [string]: string } = { directional = "DistantLight", point = "SphereLight", spot = "SphereLight" }
local function lightBody(depth: number, l: Types.Light)
local c = l.color or { 1, 1, 1 }
line(depth, "color3f inputs:color = " .. tuple({ c[1] or 1, c[2] or 1, c[3] or 1 }))
@@ -808,7 +808,7 @@ function Write._looks(out: { string }, depth: number, scene: Types.Scene, matPat
stSource = path .. "/" .. tn.name .. "_Transform.outputs:result"
end
local tex = scene.textures[ref.texture]
local sampler = tex and tex.sampler or {}
local sampler: Types.Sampler = tex and tex.sampler or {}
table.insert(out, "")
line(d, 'def Shader "' .. tn.name .. '"')
line(d, "{")
+4 -4
View File
@@ -107,11 +107,11 @@ local function fetcher(opts: DecodeOptions?, package: any): (uri: string) -> buf
end
end
local v: any = nil
if opts and type(opts.files) == "table" then
v = opts.files[u]
if opts and type((opts :: DecodeOptions).files) == "table" then
v = ((opts :: DecodeOptions).files :: { [string]: any })[u]
end
if v == nil and opts and type(opts.resolve) == "function" then
local ok, res = pcall(opts.resolve, u)
if v == nil and opts and type((opts :: DecodeOptions).resolve) == "function" then
local ok, res = pcall((opts :: DecodeOptions).resolve :: (uri: string) -> any, u)
v = if ok then res else nil
end
if type(v) == "string" then
+4 -4
View File
@@ -275,7 +275,7 @@ local function parse(r: Bytes.Reader, opts: DecodeOptions?): (Parsed?, Err.Err?)
if n < 1024 then
return nil, Err.new(Err.CORRUPT, "short RGBA chunk", at)
end
local pal = table.create(255)
local pal = table.create(255) :: { { number } }
for i = 1, 255 do
pal[i] = { c:u8(), c:u8(), c:u8(), c:u8() }
end
@@ -310,7 +310,7 @@ local function parse(r: Bytes.Reader, opts: DecodeOptions?): (Parsed?, Err.Err?)
if c.err or nc < 0 or nc * 4 > c:remaining() then
return nil, Err.new(Err.CORRUPT, "bad nGRP chunk", at)
end
local children = table.create(nc)
local children = table.create(nc) :: { number }
for k = 1, nc do
children[k] = c:i32le()
end
@@ -625,7 +625,7 @@ function Vox.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
return nil, Err.new(Err.ARG, "voxels " .. i .. ": data is smaller than the grid")
end
end
local meta = if type(scene.meta) == "table" and type(scene.meta.vox) == "table" then scene.meta.vox else {}
local meta: any = if type(scene.meta) == "table" and type(scene.meta.vox) == "table" then scene.meta.vox else {}
local w = Bytes.writer(65536)
w:string("VOX ")
w:u32le(version)
@@ -675,7 +675,7 @@ function Vox.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
local failure: string? = nil
local function shape(model: number, vx: any?): number
local id = alloc()
local ms = {}
local ms: { { model: number, attributes: Dict? } } = {}
local saved = vx and vx.shape
if saved and type(saved.models) == "table" and saved.models[1] and saved.models[1].model == model then
for _, sm in saved.models do