model/blend: Blender 1.6x-2.0x files (big-endian, 32-bit, TFace / MCol, IPO curves, additive dsize), easing curves, owner-scoped address lookup; containers/Blend tests; docs section; bench line
This commit is contained in:
1 parent
2e82b7d0af
commit
3c8d5e99d7
14 files changed
+766
-110
No files matched your search
@@ -184,7 +184,7 @@ local function parseDna(f: File, at: number, size: number): string?
|
||||
if n < 0 or n > e - p then
|
||||
return nil, p
|
||||
end
|
||||
local out = table.create(n)
|
||||
local out = table.create(n) :: { string }
|
||||
for k = 1, n do
|
||||
local q = p
|
||||
while q < e and readu8(b, q) ~= 0 do
|
||||
@@ -215,7 +215,7 @@ local function parseDna(f: File, at: number, size: number): string?
|
||||
if p + 4 + nt * 2 > e or readstring(b, p, 4) ~= "TLEN" then
|
||||
return "bad SDNA TLEN"
|
||||
end
|
||||
local lengths = table.create(nt)
|
||||
local lengths = table.create(nt) :: { number }
|
||||
for k = 1, nt do
|
||||
lengths[k] = f.u16(b, p + 4 + (k - 1) * 2)
|
||||
end
|
||||
@@ -230,7 +230,7 @@ local function parseDna(f: File, at: number, size: number): string?
|
||||
end
|
||||
local i16 = f.i16
|
||||
local ps = f.pointerSize
|
||||
local structs, byName = table.create(ns), {}
|
||||
local structs, byName = table.create(ns) :: { Struct }, {} :: { [string]: Struct }
|
||||
for k = 1, ns do
|
||||
if p + 4 > e then
|
||||
return "truncated SDNA structs"
|
||||
@@ -240,7 +240,8 @@ local function parseDna(f: File, at: number, size: number): string?
|
||||
if t < 0 or t >= nt or nf < 0 or p + nf * 4 > e then
|
||||
return "bad SDNA struct " .. k
|
||||
end
|
||||
local s: Struct = { name = types[t + 1], index = k - 1, size = lengths[t + 1], fields = table.create(nf), byName = {} }
|
||||
local tnames = types :: { string }
|
||||
local s: Struct = { name = tnames[t + 1], index = k - 1, size = lengths[t + 1], fields = table.create(nf) :: { Field }, byName = {} }
|
||||
local off = 0
|
||||
for j = 1, nf do
|
||||
local ft, fn = i16(b, p), i16(b, p + 2)
|
||||
@@ -248,9 +249,9 @@ local function parseDna(f: File, at: number, size: number): string?
|
||||
if ft < 0 or ft >= nt or fn < 0 or fn >= #names then
|
||||
return "bad SDNA field in " .. s.name
|
||||
end
|
||||
local decl = names[fn + 1]
|
||||
local decl: string = (names :: { string })[fn + 1]
|
||||
local pointer = string.sub(decl, 1, 1) == "*" or string.sub(decl, 1, 2) == "(*"
|
||||
local dims, count = {}, 1
|
||||
local dims: { number }, count = {}, 1
|
||||
for d in string.gmatch(decl, "%[(%d+)%]") do
|
||||
local v = tonumber(d) or 1
|
||||
table.insert(dims, v)
|
||||
@@ -258,9 +259,9 @@ local function parseDna(f: File, at: number, size: number): string?
|
||||
end
|
||||
local base = string.gsub(decl, "%[.*$", "")
|
||||
base = string.gsub(base, "[%*%(%)]", "")
|
||||
local elem = if pointer then ps else lengths[ft + 1]
|
||||
local elem: number = if pointer then ps else lengths[ft + 1]
|
||||
local fld: Field = {
|
||||
name = base, decl = decl, type = types[ft + 1], offset = off, size = elem * count,
|
||||
name = base, decl = decl, type = tnames[ft + 1], offset = off, size = elem * count,
|
||||
pointer = pointer, dims = dims, count = count, elem = elem,
|
||||
}
|
||||
s.fields[j] = fld
|
||||
@@ -272,7 +273,7 @@ local function parseDna(f: File, at: number, size: number): string?
|
||||
structs[k] = s
|
||||
byName[s.name] = s
|
||||
end
|
||||
f.dna = { names = names, types = types, lengths = lengths, structs = structs, byName = byName }
|
||||
f.dna = { names = names :: { string }, types = types :: { string }, lengths = lengths, structs = structs, byName = byName }
|
||||
return nil
|
||||
end
|
||||
|
||||
@@ -298,7 +299,8 @@ function Blend.open(data: buffer, offset: number?, length: number?, opts: Option
|
||||
end
|
||||
local hs, ps, little, version, fmt, herr = header(b, n)
|
||||
if not hs then
|
||||
return nil, Err.new(if n < 12 then Err.TRUNCATED else Err.CORRUPT, herr or "bad header", 0)
|
||||
local code = if n < 12 then Err.TRUNCATED elseif herr and string.sub(herr, 1, 12) == "unsupported:" then Err.UNSUPPORTED else Err.CORRUPT
|
||||
return nil, Err.new(code, herr or "bad header", 0)
|
||||
end
|
||||
local f: File = {
|
||||
buf = b, version = version, subversion = nil, pointerSize = ps, little = little, format = fmt,
|
||||
@@ -341,7 +343,8 @@ function Blend.open(data: buffer, offset: number?, length: number?, opts: Option
|
||||
count = i32(b, p + 12 + ps)
|
||||
end
|
||||
p += bh
|
||||
code = string.gsub(code, "%z+$", "")
|
||||
-- two-letter ID codes are a 16-bit value in a 32-bit field: "\0\0SC" in big-endian files
|
||||
code = string.gsub(string.gsub(code, "%z+$", ""), "^%z+", "")
|
||||
if code == "ENDB" then
|
||||
break
|
||||
end
|
||||
@@ -389,29 +392,33 @@ function Blend.open(data: buffer, offset: number?, length: number?, opts: Option
|
||||
f.byAddr[if ps == 8 then string.rep("\0", 8) else 0] = nil
|
||||
local glob = f.byCode.GLOB
|
||||
local fg = f.dna.byName.FileGlobal
|
||||
if glob and fg and fg.byName.subversion then
|
||||
local fld = fg.byName.subversion
|
||||
if fld.offset + 2 <= glob[1].size then
|
||||
f.subversion = f.i16(b, glob[1].offset + fld.offset)
|
||||
local g1: Block? = if glob then glob[1] else nil
|
||||
local fld = fg and fg.byName.subversion
|
||||
if g1 and fld then
|
||||
if fld.offset + 2 <= g1.size then
|
||||
f.subversion = f.i16(b, g1.offset + fld.offset)
|
||||
end
|
||||
end
|
||||
return f, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ structs
|
||||
export type View = {
|
||||
file: File,
|
||||
struct: Struct,
|
||||
offset: number, -- the struct's first byte in file.buf
|
||||
block: Block?,
|
||||
}
|
||||
|
||||
local View = {}
|
||||
View.__index = View
|
||||
Blend.View = View
|
||||
|
||||
export type View = typeof(setmetatable(
|
||||
{} :: {
|
||||
file: File,
|
||||
struct: Struct,
|
||||
offset: number, -- the struct's first byte in file.buf
|
||||
block: Block?,
|
||||
},
|
||||
View
|
||||
))
|
||||
|
||||
local function view(f: File, s: Struct, offset: number, blk: Block?): View
|
||||
return setmetatable({ file = f, struct = s, offset = offset, block = blk }, View) :: any
|
||||
return setmetatable({ file = f, struct = s, offset = offset, block = blk }, View)
|
||||
end
|
||||
|
||||
function Blend.structOf(f: File, blk: Block): Struct?
|
||||
@@ -641,7 +648,7 @@ function View.array(self: View, name: string | { string }, as: string?): { View
|
||||
local blk = first.block :: Block
|
||||
local s = first.struct
|
||||
local n = if s.size > 0 then blk.size // s.size else 0
|
||||
local out = table.create(n)
|
||||
local out = table.create(n) :: { View }
|
||||
for k = 0, n - 1 do
|
||||
out[k + 1] = view(self.file, s, blk.offset + k * s.size, blk)
|
||||
end
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
--!strict
|
||||
-- model/blend/Anim: Blender actions (legacy bAction.curves, and the layered actions of 4.4+: layers -> strips ->
|
||||
-- keyframe data -> channel bags matched by the AnimData slot handle) and f-curve evaluation: constant, linear,
|
||||
-- bezier (handles shortened so the segment stays a function of time, as Blender does) and the Penner easing modes;
|
||||
-- bezier (handles shortened so the segment stays a function of time, as Blender does) and the Penner easing modes
|
||||
-- (elastic uses Penner's amplitude / period, which Blender scales differently);
|
||||
-- constant or linear extrapolation. F-curve modifiers and NLA strips are not evaluated.
|
||||
local Blend = require("@rosetta/containers/Blend")
|
||||
|
||||
@@ -112,6 +113,62 @@ function Anim.curves(f: Blend.File, adt: Blend.View?): ({ Curve }?, string?)
|
||||
return out, act:idName()
|
||||
end
|
||||
|
||||
-- pre-2.5 IPO curves (Object.ipo / Key.ipo): the curve's adrcode picks the channel; object rotations are in units of
|
||||
-- 10 degrees; the interpolation and extrapolation (0 constant, 1 linear; cyclic ones as constant) are per curve
|
||||
local OB_ADR: { [number]: { any } } = {
|
||||
[1] = { "location", 0 }, [2] = { "location", 1 }, [3] = { "location", 2 },
|
||||
[4] = { "delta_location", 0 }, [5] = { "delta_location", 1 }, [6] = { "delta_location", 2 },
|
||||
[7] = { "rotation_euler", 0, math.pi / 18 }, [8] = { "rotation_euler", 1, math.pi / 18 }, [9] = { "rotation_euler", 2, math.pi / 18 },
|
||||
[10] = { "delta_rotation_euler", 0, math.pi / 18 }, [11] = { "delta_rotation_euler", 1, math.pi / 18 }, [12] = { "delta_rotation_euler", 2, math.pi / 18 },
|
||||
[13] = { "scale", 0 }, [14] = { "scale", 1 }, [15] = { "scale", 2 },
|
||||
}
|
||||
|
||||
function Anim.ipo(f: Blend.File, ipo: Blend.View?, keyNames: { string }?): ({ Curve }?, string?)
|
||||
if not ipo then
|
||||
return nil, nil
|
||||
end
|
||||
local out: { Curve } = {}
|
||||
for _, icu in ipo:list("curve", "IpoCurve") do
|
||||
local adr = icu:num("adrcode") or -1
|
||||
local path, index, scale
|
||||
if keyNames then
|
||||
if adr >= 1 and keyNames[adr + 1] then
|
||||
path, index, scale = 'key_blocks["' .. keyNames[adr + 1] .. '"].value', 0, 1
|
||||
end
|
||||
else
|
||||
local m = OB_ADR[adr]
|
||||
if m then
|
||||
path, index, scale = m[1], m[2], m[3] or 1
|
||||
end
|
||||
end
|
||||
if path then
|
||||
local ipoMode = icu:num("ipo") or BEZ
|
||||
local keys: { Key } = {}
|
||||
for _, b in icu:array("bezt", "BezTriple") do
|
||||
local v = b:vec("vec")
|
||||
if v and #v >= 9 then
|
||||
table.insert(keys, {
|
||||
lx = v[1], ly = v[2] * scale, x = v[4], y = v[5] * scale, rx = v[7], ry = v[8] * scale,
|
||||
ipo = if ipoMode == CONST or ipoMode == LIN then ipoMode else BEZ, easing = 0, back = 1.70158, amplitude = 0.8, period = 4.1,
|
||||
})
|
||||
end
|
||||
end
|
||||
local n = math.min(#keys, icu:num("totvert") or #keys)
|
||||
while #keys > n do
|
||||
table.remove(keys)
|
||||
end
|
||||
if #keys > 0 then
|
||||
table.sort(keys, function(a: Key, b: Key): boolean
|
||||
return a.x < b.x
|
||||
end)
|
||||
table.insert(out, { path = path, index = index, keys = keys, extend = if (icu:num("extrap") or 0) == 1 then 1 else 0, modifiers = 0 })
|
||||
end
|
||||
end
|
||||
end
|
||||
local _ = f
|
||||
return out, ipo:idName()
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ evaluation
|
||||
local function bezierAt(p0: number, p1: number, p2: number, p3: number, t: number): number
|
||||
local u = 1 - t
|
||||
@@ -191,7 +248,7 @@ local function easeIn(ipo: number, t: number, a: Key): number -- 0..1 -> 0..1
|
||||
t -= 1
|
||||
return -(amp * 2 ^ (10 * t) * math.sin((t - s) * (2 * math.pi) / p))
|
||||
elseif ipo == EXPO then
|
||||
return if t <= 0 then 0 else 2 ^ (10 * (t - 1))
|
||||
return (2 ^ (10 * (t - 1)) - 2 ^ -10) / (1 - 2 ^ -10) -- normalized to run from 0 to 1, as Blender's does
|
||||
elseif ipo == QUAD then
|
||||
return t * t
|
||||
elseif ipo == QUART then
|
||||
|
||||
@@ -190,9 +190,11 @@ local function imageRef(ctx: Ctx, g: Graph, uvNames: any, s: Blend.View?, throug
|
||||
return nil, n
|
||||
end
|
||||
local sampler: Types.Sampler = {}
|
||||
local custom = false
|
||||
local st = n.view:deref("storage", 0, "NodeTexImage")
|
||||
if st then
|
||||
local interp, ext = st:num("interpolation") or 0, st:num("extension") or 0
|
||||
custom = interp == 1 or (ext >= 1 and ext <= 3)
|
||||
if interp == 1 then
|
||||
sampler.filter = "nearest"
|
||||
sampler.minFilter = "nearest"
|
||||
@@ -203,7 +205,7 @@ local function imageRef(ctx: Ctx, g: Graph, uvNames: any, s: Blend.View?, throug
|
||||
sampler.wrapS, sampler.wrapT = "mirror", "mirror"
|
||||
end
|
||||
end
|
||||
local t = texture(ctx, img, if next(sampler) then sampler else nil)
|
||||
local t = texture(ctx, img, if custom then sampler else nil)
|
||||
if not t then
|
||||
return nil, n
|
||||
end
|
||||
|
||||
@@ -198,6 +198,29 @@ local function fieldReader(f: Blend.File, L: Layer, field: string): ((number, nu
|
||||
end
|
||||
end
|
||||
|
||||
-- tessellated face colours: corner k of face i at offset + i * faceStride + first + k * cornerStride, bytes in MCol's
|
||||
-- memory order (alpha, blue, green, red; reversed when an int held them in a big-endian file), sRGB -> linear;
|
||||
-- TFace colours load opaque, as Blender's versioning makes them
|
||||
local function tessColors(f: Blend.File, tess: buffer, nc: number, offset: number, bytes: number, faceStride: number, first: number, cornerStride: number, swap: boolean, opaque: boolean?): buffer
|
||||
local d = buffer.create(nc * 16)
|
||||
for i = 0, nc - 1 do
|
||||
local tc = buffer.readu32(tess, i * 4)
|
||||
local at = (tc // 4) * faceStride + first + (tc % 4) * cornerStride
|
||||
if at + 4 <= bytes then
|
||||
local o = offset + at
|
||||
local aa, bb, gg, rr = readu8(f.buf, o), readu8(f.buf, o + 1), readu8(f.buf, o + 2), readu8(f.buf, o + 3)
|
||||
if swap then
|
||||
aa, bb, gg, rr = rr, gg, bb, aa
|
||||
end
|
||||
buffer.writef32(d, i * 16, SRGB[rr + 1])
|
||||
buffer.writef32(d, i * 16 + 4, SRGB[gg + 1])
|
||||
buffer.writef32(d, i * 16 + 8, SRGB[bb + 1])
|
||||
buffer.writef32(d, i * 16 + 12, if opaque then 1 else aa / 255)
|
||||
end
|
||||
end
|
||||
return d
|
||||
end
|
||||
|
||||
function Mesh.layers(f: Blend.File, me: Blend.View): { Layer }
|
||||
local layers: { Layer } = {}
|
||||
customData(f, me, { "vdata", "vert_data" }, "point", layers)
|
||||
@@ -396,10 +419,11 @@ function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, stri
|
||||
end
|
||||
elseif L.domain == "tess" and L.kind == "MTFace" and corner.tess then
|
||||
local r = fieldReader(f, L, "uv")
|
||||
local tess = corner.tess :: buffer
|
||||
if r then
|
||||
local d = buffer.create(nc * 8)
|
||||
for i = 0, nc - 1 do
|
||||
local tc = buffer.readu32(corner.tess, i * 4)
|
||||
local tc = buffer.readu32(tess, i * 4)
|
||||
local face, k = tc // 4, tc % 4
|
||||
buffer.writef32(d, i * 8, r(face, k * 2))
|
||||
buffer.writef32(d, i * 8 + 4, r(face, k * 2 + 1))
|
||||
@@ -408,6 +432,21 @@ function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, stri
|
||||
end
|
||||
end
|
||||
end
|
||||
-- before 2.42 (no CustomData): Mesh.tface (TFace, uv[4][2] per face) and Mesh.mcol (MCol x 4 per face)
|
||||
if #poly.uvs == 0 and corner.tess then
|
||||
local tf = legacy(f, me, "tface", "TFace", "tess")
|
||||
local r = tf and fieldReader(f, tf, "uv")
|
||||
if r then
|
||||
local tess = corner.tess :: buffer
|
||||
local d = buffer.create(nc * 8)
|
||||
for i = 0, nc - 1 do
|
||||
local tc = buffer.readu32(tess, i * 4)
|
||||
buffer.writef32(d, i * 8, r(tc // 4, (tc % 4) * 2))
|
||||
buffer.writef32(d, i * 8 + 4, r(tc // 4, (tc % 4) * 2 + 1))
|
||||
end
|
||||
table.insert(poly.uvs, { name = "UVMap", data = d })
|
||||
end
|
||||
end
|
||||
local active = me:cstring("default_uv_map_attribute") or me:cstring("active_uv_map_attribute")
|
||||
if active then
|
||||
for k, u in poly.uvs do
|
||||
@@ -434,23 +473,20 @@ function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, stri
|
||||
end
|
||||
table.insert(poly.colors, { name = L.name, domain = L.domain, data = d })
|
||||
elseif L.kind == "MCol" and L.domain == "tess" and corner.tess then
|
||||
-- MCol keeps alpha, blue, green, red in its a, r, g, b fields
|
||||
local st = f.dna.byName.MCol
|
||||
local d = buffer.create(nc * 16)
|
||||
local stride = if st then st.size else 4
|
||||
for i = 0, nc - 1 do
|
||||
local tc = buffer.readu32(corner.tess, i * 4)
|
||||
local at = (tc // 4) * 4 * stride + (tc % 4) * stride
|
||||
if at + 4 <= L.bytes then
|
||||
local o = L.offset + at
|
||||
local aa, bb, gg, rr = readu8(f.buf, o), readu8(f.buf, o + 1), readu8(f.buf, o + 2), readu8(f.buf, o + 3)
|
||||
buffer.writef32(d, i * 16, SRGB[rr + 1])
|
||||
buffer.writef32(d, i * 16 + 4, SRGB[gg + 1])
|
||||
buffer.writef32(d, i * 16 + 8, SRGB[bb + 1])
|
||||
buffer.writef32(d, i * 16 + 12, aa / 255)
|
||||
end
|
||||
end
|
||||
table.insert(poly.colors, { name = L.name, domain = "corner", data = d })
|
||||
table.insert(poly.colors, { name = L.name, domain = "corner", data = tessColors(f, corner.tess :: buffer, nc, L.offset, L.bytes, stride * 4, 0, stride, false) })
|
||||
end
|
||||
end
|
||||
-- before 2.42 (no CustomData): Mesh.mcol, or TFace.col (unsigned int col[4], MCol's byte layout in memory)
|
||||
if #poly.colors == 0 and corner.tess then
|
||||
local mc = legacy(f, me, "mcol", "MCol", "tess")
|
||||
local tf = legacy(f, me, "tface", "TFace", "tess")
|
||||
local col = f.dna.byName.TFace and f.dna.byName.TFace.byName.col
|
||||
if mc then
|
||||
table.insert(poly.colors, { name = "Col", domain = "corner", data = tessColors(f, corner.tess :: buffer, nc, mc.offset, mc.bytes, mc.elem * 4, 0, mc.elem, false) })
|
||||
elseif tf and col then
|
||||
table.insert(poly.colors, { name = "Col", domain = "corner", data = tessColors(f, corner.tess :: buffer, nc, tf.offset, tf.bytes, tf.elem, col.offset, 4, not f.little, true) })
|
||||
end
|
||||
end
|
||||
-- MLoopCol layers carry the CD_PROP_BYTE_COLOR type (17) and are read above as bytecolor (r, g, b, a bytes)
|
||||
@@ -539,7 +575,7 @@ function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, stri
|
||||
local mw = f.dna.byName.MDeformWeight
|
||||
local nrf, wf = mw and mw.byName.def_nr, mw and mw.byName.weight
|
||||
if dwf and twf and mw and nrf and wf then
|
||||
local weights = table.create(nv)
|
||||
local weights = table.create(nv) :: { { number } }
|
||||
for i = 0, nv - 1 do
|
||||
local list = {}
|
||||
local at = i * dv.elem
|
||||
@@ -565,7 +601,7 @@ function Mesh.read(f: Blend.File, me: Blend.View, limits: Limits?): (Poly?, stri
|
||||
end
|
||||
|
||||
-- shape keys: { name, value, relative (0-based), data: f32 x 3 per vertex } in file order, and the reference index
|
||||
export type Shape = { name: string, value: number, relative: number, data: buffer, mute: boolean }
|
||||
export type Shape = { name: string, value: number, relative: number, data: buffer, mute: boolean, min: number, max: number }
|
||||
function Mesh.shapes(f: Blend.File, me: Blend.View, nv: number): ({ Shape }, number?, Blend.View?)
|
||||
local key = me:deref("key", 0, "Key")
|
||||
if not key then
|
||||
@@ -591,8 +627,11 @@ function Mesh.shapes(f: Blend.File, me: Blend.View, nv: number): ({ Shape }, num
|
||||
ref = i
|
||||
end
|
||||
table.insert(out, {
|
||||
name = kb:str("name") or ("Key " .. i), value = kb:num("curval") or 0, relative = kb:num("relative") or 0, data = d,
|
||||
-- unnamed in old files: Key 0 (the basis), Key 1, ...
|
||||
name = kb:str("name") or ("Key " .. (i - 1)), value = kb:num("curval") or 0, relative = kb:num("relative") or 0, data = d,
|
||||
mute = bit32.band(kb:num("flag") or 0, 2) ~= 0,
|
||||
min = kb:num("slidermin") or 0,
|
||||
max = kb:num("slidermax") or 1,
|
||||
})
|
||||
end
|
||||
if ref == nil and #out > 0 then
|
||||
|
||||
@@ -185,15 +185,10 @@ function Prim.cornerNormals(poly: MeshT.Poly, fn: buffer, ang: buffer, fans: Fan
|
||||
local r = readu32(fanId, c * 4)
|
||||
local x, y, z = readf32(acc, r * 12), readf32(acc, r * 12 + 4), readf32(acc, r * 12 + 8)
|
||||
local l = sqrt(x * x + y * y + z * z)
|
||||
if l > 1e-20 then
|
||||
x, y, z = x / l, y / l, z / l
|
||||
else
|
||||
local fi = readu32(fans.face, c * 4)
|
||||
x, y, z = readf32(fn, fi * 12), readf32(fn, fi * 12 + 4), readf32(fn, fi * 12 + 8)
|
||||
end
|
||||
writef32(out, c * 12, x)
|
||||
writef32(out, c * 12 + 4, y)
|
||||
writef32(out, c * 12 + 8, z)
|
||||
local fi = readu32(fans.face, c * 4)
|
||||
writef32(out, c * 12, if l > 1e-20 then x / l else readf32(fn, fi * 12))
|
||||
writef32(out, c * 12 + 4, if l > 1e-20 then y / l else readf32(fn, fi * 12 + 4))
|
||||
writef32(out, c * 12 + 8, if l > 1e-20 then z / l else readf32(fn, fi * 12 + 8))
|
||||
end
|
||||
return out, fanId
|
||||
end
|
||||
@@ -231,7 +226,7 @@ local function triangulate(poly: MeshT.Poly, fi: number, fnx: number, fny: numbe
|
||||
elseif ay >= az then
|
||||
cu, cv, sign = 2, 0, fny
|
||||
end
|
||||
local ring = table.create(n * 2)
|
||||
local ring = table.create(n * 2) :: { number }
|
||||
for c = s0, s1 - 1 do
|
||||
local v = readu32(poly.cornerVert, c * 4)
|
||||
local u, w = readf32(poly.pos, (v * 3 + cu) * 4), readf32(poly.pos, (v * 3 + cv) * 4)
|
||||
@@ -284,7 +279,7 @@ function Prim.build(poly: MeshT.Poly, slots: { number }, opts: Options?, skin: S
|
||||
table.insert(g, fi)
|
||||
end
|
||||
table.sort(order)
|
||||
local cornerColors, pointColors = {}, {}
|
||||
local cornerColors: { MeshT.Color }, pointColors: { MeshT.Color } = {}, {}
|
||||
for _, c in poly.colors do
|
||||
table.insert(if c.domain == "corner" then cornerColors else pointColors, c)
|
||||
end
|
||||
@@ -367,7 +362,7 @@ function Prim.build(poly: MeshT.Poly, slots: { number }, opts: Options?, skin: S
|
||||
ci += 1
|
||||
end
|
||||
if skin and poly.weights then
|
||||
local lists = table.create(n)
|
||||
local lists = table.create(n) :: { { number } }
|
||||
for i = 1, n do
|
||||
local src = poly.weights[vsrc[i] + 1] or {}
|
||||
local l = {}
|
||||
@@ -415,17 +410,17 @@ function Prim.build(poly: MeshT.Poly, slots: { number }, opts: Options?, skin: S
|
||||
end
|
||||
if shapes and ref and #shapes > 1 then
|
||||
local base = shapes[ref].data
|
||||
local targets = {}
|
||||
local targets: { { [string]: Types.Accessor } } = {}
|
||||
for k, s in shapes do
|
||||
if k ~= ref then
|
||||
local t = table.create(n * 3)
|
||||
local t = table.create(n * 3) :: { number }
|
||||
for i = 1, n do
|
||||
local v = vsrc[i]
|
||||
for c = 0, 2 do
|
||||
t[(i - 1) * 3 + c + 1] = readf32(s.data, (v * 3 + c) * 4) - readf32(base, (v * 3 + c) * 4)
|
||||
end
|
||||
end
|
||||
table.insert(targets, { POSITION = accessor(t, 3) })
|
||||
table.insert(targets, { POSITION = accessor(t, 3) } :: { [string]: Types.Accessor })
|
||||
end
|
||||
end
|
||||
prim.targets = targets
|
||||
|
||||
@@ -86,26 +86,33 @@ export type Props = { [string]: { number } } -- location / rotation_euler / ...
|
||||
|
||||
-- an object's or pose bone's transform properties, keyed by their RNA names
|
||||
local function objectProps(ob: Blend.View): Props
|
||||
local function v(n: number, ...: string): { number }
|
||||
-- fields missing from old files take their neutral value
|
||||
local function v(fallback: { number }, ...: string): { number }
|
||||
local x = ob:vec(...)
|
||||
if x and #x >= n then
|
||||
if x and #x >= #fallback then
|
||||
return table.clone(x)
|
||||
end
|
||||
return table.create(n, 0)
|
||||
return fallback
|
||||
end
|
||||
return {
|
||||
location = v(3, "loc"),
|
||||
rotation_euler = v(3, "rot"),
|
||||
rotation_quaternion = v(4, "quat"),
|
||||
rotation_axis_angle = { ob:num("rotAngle") or 0, table.unpack(v(3, "rotAxis")) },
|
||||
scale = v(3, "scale", "size"),
|
||||
delta_location = v(3, "dloc"),
|
||||
delta_rotation_euler = v(3, "drot"),
|
||||
delta_rotation_quaternion = v(4, "dquat"),
|
||||
delta_rotation_axis_angle = { ob:num("drotAngle") or 0, table.unpack(v(3, "drotAxis")) },
|
||||
delta_scale = v(3, "dscale", "dsize"),
|
||||
local p: Props = {
|
||||
location = v({ 0, 0, 0 }, "loc"),
|
||||
rotation_euler = v({ 0, 0, 0 }, "rot"),
|
||||
rotation_quaternion = v({ 1, 0, 0, 0 }, "quat"),
|
||||
rotation_axis_angle = { ob:num("rotAngle") or 0, table.unpack(v({ 0, 1, 0 }, "rotAxis")) },
|
||||
scale = v({ 1, 1, 1 }, "scale", "size"),
|
||||
delta_location = v({ 0, 0, 0 }, "dloc"),
|
||||
delta_rotation_euler = v({ 0, 0, 0 }, "drot"),
|
||||
delta_rotation_quaternion = v({ 1, 0, 0, 0 }, "dquat"),
|
||||
delta_rotation_axis_angle = { ob:num("drotAngle") or 0, table.unpack(v({ 0, 1, 0 }, "drotAxis")) },
|
||||
delta_scale = v({ 1, 1, 1 }, "dscale"),
|
||||
rotmode = { ob:num("rotmode") or 1 },
|
||||
}
|
||||
if not ob:has("dscale") then
|
||||
-- before 2.5x the delta scale ("dsize") was added to the scale
|
||||
local d = v({ 0, 0, 0 }, "dsize")
|
||||
p.scale = { p.scale[1] + d[1], p.scale[2] + d[2], p.scale[3] + d[3] }
|
||||
end
|
||||
return p
|
||||
end
|
||||
|
||||
local function poseProps(pc: Blend.View?): Props
|
||||
@@ -267,12 +274,13 @@ local function camera(ctx: Ctx, ob: Blend.View, node: number)
|
||||
local znear, zfar = cam:num("clip_start", "clipsta") or 0.1, cam:num("clip_end", "clipend") or 100
|
||||
local c: Types.Camera
|
||||
if (cam:num("type") or 0) == 1 then
|
||||
local half = (cam:num("ortho_scale") or 6) / 2
|
||||
-- files without ortho_scale (before 2.3x) scaled orthographic views by 256 / lens, as Blender's versioning sets it
|
||||
local half = (cam:num("ortho_scale") or 256 / math.max(cam:num("lens") or 35, 1e-3)) / 2
|
||||
local xmag, ymag = half, half / aspect
|
||||
if aspect < 1 then
|
||||
xmag, ymag = half * aspect, half
|
||||
end
|
||||
c = { name = ob:idName(), type = "orthographic", xmag = xmag, ymag = ymag, znear = znear, zfar = zfar }
|
||||
c = { name = ob:idName(), type = "orthographic", xmag = xmag, ymag = ymag, znear = znear, zfar = zfar } :: Types.Camera
|
||||
else
|
||||
local lens = cam:num("lens") or 50
|
||||
local sx, sy = cam:num("sensor_x") or 36, cam:num("sensor_y") or 24
|
||||
@@ -285,7 +293,7 @@ local function camera(ctx: Ctx, ob: Blend.View, node: number)
|
||||
else
|
||||
yfov = 2 * math.atan(sx / 2 / lens)
|
||||
end
|
||||
c = { name = ob:idName(), type = "perspective", yfov = yfov, aspect = aspect, znear = znear, zfar = zfar }
|
||||
c = { name = ob:idName(), type = "perspective", yfov = yfov, aspect = aspect, znear = znear, zfar = zfar } :: Types.Camera
|
||||
end
|
||||
table.insert(ctx.scene.cameras, c)
|
||||
ctx.scene.nodes[node].camera = #ctx.scene.cameras
|
||||
@@ -299,9 +307,13 @@ local function light(ctx: Ctx, ob: Blend.View, node: number)
|
||||
local t = la:num("type") or 0
|
||||
local color = { la:num("r") or 1, la:num("g") or 1, la:num("b") or 1 }
|
||||
local energy = la:num("energy") or 1
|
||||
if ctx.f.version < 280 then
|
||||
-- before 2.8 Cycles took the strength from the lamp's Emission node, else 100 (sun: 1); Blender's 2.8
|
||||
-- versioning moves that to energy
|
||||
if ctx.f.version < 280 and ctx.meta.engine ~= "CYCLES" then
|
||||
-- Blender's 2.8 versioning: Blender Internal energies times 100 (suns unchanged)
|
||||
if t ~= LA_SUN and t ~= LA_HEMI then
|
||||
energy *= 100
|
||||
end
|
||||
elseif ctx.f.version < 280 then
|
||||
-- Cycles took the strength from the lamp's Emission node, else 100 (sun: 1)
|
||||
energy = if t == LA_SUN or t == LA_HEMI then 1 else 100
|
||||
local tree = (la:num("use_nodes") or 0) ~= 0 and la:deref("nodetree", 0, "bNodeTree") or nil
|
||||
for _, n in if tree then tree:list("nodes", "bNode") else {} do
|
||||
@@ -318,13 +330,17 @@ local function light(ctx: Ctx, ob: Blend.View, node: number)
|
||||
local name = ob:idName()
|
||||
local l: Types.Light
|
||||
if t == LA_SUN or t == LA_HEMI then
|
||||
l = { name = name, type = "directional", color = color, intensity = energy * W2LM }
|
||||
l = { name = name, type = "directional", color = color, intensity = energy * W2LM } :: Types.Light
|
||||
elseif t == LA_SPOT then
|
||||
local outer = (la:num("spotsize") or math.rad(45)) / 2
|
||||
local size = la:num("spotsize") or math.rad(45)
|
||||
if ctx.f.version < 263 then
|
||||
size = math.rad(size) -- degrees before 2.63
|
||||
end
|
||||
local outer = size / 2
|
||||
local blend = la:num("spotblend") or 0
|
||||
l = { name = name, type = "spot", color = color, intensity = energy / (4 * math.pi) * W2LM, outerCone = outer, innerCone = outer - outer * blend }
|
||||
l = { name = name, type = "spot", color = color, intensity = energy / (4 * math.pi) * W2LM, outerCone = outer, innerCone = outer - outer * blend } :: Types.Light
|
||||
elseif t == LA_LOCAL then
|
||||
l = { name = name, type = "point", color = color, intensity = energy / (4 * math.pi) * W2LM }
|
||||
l = { name = name, type = "point", color = color, intensity = energy / (4 * math.pi) * W2LM } :: Types.Light
|
||||
else
|
||||
local skipped = ctx.meta.skippedLights or {}
|
||||
ctx.meta.skippedLights = skipped
|
||||
@@ -568,11 +584,23 @@ type Job = { kind: string, node: number?, nodes: { number }?, info: any?, curves
|
||||
local function animations(ctx: Ctx)
|
||||
local f = ctx.f
|
||||
local jobs: { Job } = {}
|
||||
-- an IPO shared by several objects animates the first by name only, as Blender's 2.5 conversion leaves it
|
||||
local ipoOwner: { [any]: string } = {}
|
||||
for _, ob in ctx.objView do
|
||||
local a, nm = ob:ptr("ipo"), ob:idName() or ""
|
||||
if a ~= nil and (ipoOwner[a] == nil or nm < ipoOwner[a]) then
|
||||
ipoOwner[a] = nm
|
||||
end
|
||||
end
|
||||
for node = 1, #ctx.scene.nodes do
|
||||
local ob = ctx.objView[node]
|
||||
local curves, name
|
||||
if ob then
|
||||
curves, name = Anim.curves(f, ob:deref("adt", 0, "AnimData"))
|
||||
local ipoAddr = ob:ptr("ipo")
|
||||
if not curves and not ob:has("adt") and ipoAddr ~= nil and ipoOwner[ipoAddr] == ob:idName() then
|
||||
curves, name = Anim.ipo(f, ob:deref("ipo", 0, "Ipo"))
|
||||
end
|
||||
end
|
||||
if curves and #curves > 0 and name then
|
||||
table.insert(jobs, { kind = "object", node = node, curves = curves, action = name })
|
||||
@@ -584,6 +612,13 @@ local function animations(ctx: Ctx)
|
||||
end
|
||||
for meshIdx, sk in ctx.meta.shapeKeys do
|
||||
local curves, name = Anim.curves(f, sk.key and sk.key:deref("adt", 0, "AnimData"))
|
||||
if not curves and sk.key and not sk.key:has("adt") then
|
||||
local names = {}
|
||||
for i, s in sk.shapes do
|
||||
names[i] = s.name
|
||||
end
|
||||
curves, name = Anim.ipo(f, sk.key:deref("ipo", 0, "Ipo"), names)
|
||||
end
|
||||
-- 2.7x could key shape values from the mesh ("shape_keys.key_blocks[...].value")
|
||||
local mc, mname = Anim.curves(f, sk.mesh:deref("adt", 0, "AnimData"))
|
||||
for _, c in mc or {} do
|
||||
@@ -601,7 +636,7 @@ local function animations(ctx: Ctx)
|
||||
table.insert(nodes, i)
|
||||
end
|
||||
end
|
||||
table.insert(jobs, { kind = "shapes", nodes = nodes, curves = curves, action = name, meshIdx = meshIdx })
|
||||
table.insert(jobs, { kind = "shapes", nodes = nodes, curves = curves :: { Curve }, action = name, meshIdx = meshIdx })
|
||||
end
|
||||
end
|
||||
if #jobs == 0 then
|
||||
@@ -622,12 +657,12 @@ local function animations(ctx: Ctx)
|
||||
for _, j in jobs do
|
||||
local anim = byAction[j.action]
|
||||
if not anim then
|
||||
anim = { name = j.action, channels = {} }
|
||||
anim = { name = j.action, channels = {} } :: Types.Animation
|
||||
byAction[j.action] = anim
|
||||
table.insert(ctx.scene.animations, anim)
|
||||
end
|
||||
local lo, hi = Anim.range(j.curves)
|
||||
local fs = frames(lo, hi, step)
|
||||
local fs = frames(math.floor(lo), math.ceil(hi), step) -- whole frames, as Blender plays them
|
||||
local times = {}
|
||||
for i, fr in fs do
|
||||
times[i] = (fr - zero) / fps
|
||||
@@ -695,7 +730,7 @@ local function animations(ctx: Ctx)
|
||||
for _, c in j.curves do
|
||||
local kind, who, rest = Anim.split(c.path)
|
||||
if kind == "key_blocks" and who == s.name and rest == "value" then
|
||||
v = Anim.evaluate(c, fr)
|
||||
v = math.clamp(Anim.evaluate(c, fr), math.min(s.min, s.max), math.max(s.min, s.max)) -- the slider range
|
||||
end
|
||||
end
|
||||
buffer.writef32(out, ((i - 1) * count + k) * 4, v)
|
||||
@@ -764,6 +799,7 @@ function Read.scene(f: Blend.File, opts: Options?): (Types.Scene?, string?)
|
||||
local fps = (r:num("frs_sec") or 24) / math.max(r:num("frs_sec_base") or 1, 1e-6)
|
||||
meta.fps = if fps > 0 then fps else 24
|
||||
meta.frameStart = r:num("sfra") or 1
|
||||
meta.engine = r:str("engine")
|
||||
local xs, ys = r:num("xsch") or 1920, r:num("ysch") or 1080
|
||||
local xa, ya = r:num("xasp") or 1, r:num("yasp") or 1
|
||||
if xs > 0 and ys > 0 and xa > 0 and ya > 0 then
|
||||
@@ -846,6 +882,7 @@ function Read.scene(f: Blend.File, opts: Options?): (Types.Scene?, string?)
|
||||
animations(ctx)
|
||||
end
|
||||
meta.shapeKeys = nil
|
||||
meta.engine = nil
|
||||
local render = meta.render
|
||||
meta.render = nil
|
||||
scene.meta.up = "z"
|
||||
|
||||
@@ -69,7 +69,9 @@ function BlendFormat.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
BlendFormat.container = Blend
|
||||
BlendFormat.evaluate = Anim.evaluate
|
||||
BlendFormat.curves = Anim.curves
|
||||
BlendFormat.ipo = Anim.ipo
|
||||
|
||||
return BlendFormat
|
||||
Reference in new issue
Block a user