rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings
This commit is contained in:
1 parent
2956576d7f
commit
22ddcd78fa
941 files changed
+3364
-3364
No files matched your search
@@ -0,0 +1,479 @@
|
||||
--!strict
|
||||
-- codecs/model/Draco/Attributes: Draco attribute decoding. The attributes decoders (one per connectivity: linear
|
||||
-- sequencer for sequential meshes and point clouds, traversal sequencers for Edgebreaker), their attribute
|
||||
-- descriptors, and the sequential attribute decoders: generic (raw values), integer, quantization (dequantized to
|
||||
-- f32 exactly like the C++) and octahedral normals; prediction through codecs/model/Draco/Predict. Bitstream
|
||||
-- versions 1.0 - 2.2. Ported from Google's Draco 1.5.7 (compression/attributes/*, compression/point_cloud/
|
||||
-- point_cloud_decoder.cc, attributes/attribute_quantization_transform.cc, attribute_octahedron_transform.cc;
|
||||
-- Apache-2.0, licenses/draco-LICENSE.txt).
|
||||
local Reader = require("@rosetta/codecs/model/Draco/Reader")
|
||||
local Connectivity = require("@rosetta/codecs/model/Draco/Connectivity")
|
||||
local Predict = require("@rosetta/codecs/model/Draco/Predict")
|
||||
local KdTree = require("@rosetta/codecs/model/Draco/KdTree")
|
||||
|
||||
local Attributes = {}
|
||||
|
||||
type D = Reader.D
|
||||
|
||||
-- data type ids (draco::DataType) -> byte length
|
||||
local DT_LENGTH = { [1] = 1, [2] = 1, [3] = 2, [4] = 2, [5] = 4, [6] = 4, [7] = 8, [8] = 8, [9] = 4, [10] = 8, [11] = 1 }
|
||||
Attributes.DT_LENGTH = DT_LENGTH
|
||||
|
||||
export type Attr = {
|
||||
id: number, -- 0-based attribute id
|
||||
type: number, -- GeometryAttribute::Type (0 POSITION, 1 NORMAL, 2 COLOR, 3 TEX_COORD, 4 GENERIC, 5.. transcoder types)
|
||||
dataType: number,
|
||||
comps: number,
|
||||
normalized: boolean,
|
||||
uniqueId: number,
|
||||
seqType: number, -- 0 generic, 1 integer, 2 quantization, 3 normals
|
||||
map: { number }?, -- point -> value (0-based values at [point + 1]); nil = identity
|
||||
numValues: number,
|
||||
portable: { number }?, -- the integer (portable) values
|
||||
values: { number }?, -- the final values (numbers), flat
|
||||
raw: buffer?, -- generic attributes: the raw values
|
||||
quant: { min: { number }, range: number, bits: number }?,
|
||||
octBits: number?,
|
||||
scheme: Predict.Scheme?,
|
||||
}
|
||||
|
||||
type Decoder = {
|
||||
attrs: { Attr },
|
||||
traversal: { ct: Connectivity.CT, enc: Connectivity.Enc, method: number }?,
|
||||
md: Predict.MeshData?,
|
||||
points: { number }?,
|
||||
}
|
||||
|
||||
-- the geometry the attributes decode against
|
||||
export type Geom = { numPoints: number, mesh: Connectivity.Mesh?, edgebreaker: boolean, kdTree: boolean }
|
||||
|
||||
local function zigzag(v: number): number
|
||||
if v % 2 == 1 then
|
||||
return -(v // 2) - 1
|
||||
end
|
||||
return v // 2
|
||||
end
|
||||
|
||||
-- the parent (position) getter for a scheme: portable integers from 2.0 on, the final values before (truncated to
|
||||
-- integers unless the scheme works in floats)
|
||||
local function parentGetter(ver: number, pos: Attr, floats: boolean): Predict.Parent?
|
||||
local old = ver < 0x0200
|
||||
local vals = if old then pos.values else pos.portable
|
||||
if not vals or pos.comps ~= 3 or (not old and pos.seqType == 3) then
|
||||
return nil
|
||||
end
|
||||
local map = pos.map
|
||||
local trunc = old and not floats
|
||||
return function(point: number): (number, number, number)
|
||||
local e = if map then (map[point + 1] or 0) else point
|
||||
local o = e * 3
|
||||
local x, y, z = vals[o + 1] or 0, vals[o + 2] or 0, vals[o + 3] or 0
|
||||
if trunc then
|
||||
x = if x < 0 then math.ceil(x) else math.floor(x)
|
||||
y = if y < 0 then math.ceil(y) else math.floor(y)
|
||||
z = if z < 0 then math.ceil(z) else math.floor(z)
|
||||
end
|
||||
return x, y, z
|
||||
end
|
||||
end
|
||||
|
||||
local function decodeIntegerValues(d: D, a: Attr, points: { number }, scheme: Predict.Scheme?): (boolean, string?)
|
||||
local comps = if a.seqType == 3 then 2 else a.comps
|
||||
local n = #points
|
||||
local numValues = n * comps
|
||||
if n == 0 then
|
||||
return false, "no values"
|
||||
end
|
||||
local compressed = Reader.u8(d)
|
||||
if d.bad then
|
||||
return false, d.bad
|
||||
end
|
||||
local vals: { number }
|
||||
if compressed > 0 then
|
||||
local s = Reader.symbols(d, numValues, comps)
|
||||
if not s then
|
||||
return false, d.bad or "bad values"
|
||||
end
|
||||
vals = s
|
||||
else
|
||||
local nb = Reader.u8(d)
|
||||
if d.bad then
|
||||
return false, d.bad
|
||||
end
|
||||
if nb < 1 or nb > 4 then
|
||||
return false, "bad value byte count"
|
||||
end
|
||||
if Reader.remaining(d) < nb * numValues then
|
||||
return false, "truncated values"
|
||||
end
|
||||
vals = table.create(numValues, 0)
|
||||
local b, p = d.b, d.pos
|
||||
if nb == 4 then
|
||||
for i = 1, numValues do
|
||||
vals[i] = buffer.readu32(b, p)
|
||||
p += 4
|
||||
end
|
||||
elseif nb == 1 then
|
||||
for i = 1, numValues do
|
||||
vals[i] = buffer.readu8(b, p)
|
||||
p += 1
|
||||
end
|
||||
elseif nb == 2 then
|
||||
for i = 1, numValues do
|
||||
vals[i] = buffer.readu16(b, p)
|
||||
p += 2
|
||||
end
|
||||
else
|
||||
for i = 1, numValues do
|
||||
vals[i] = buffer.readu16(b, p) + buffer.readu8(b, p + 2) * 65536
|
||||
p += 3
|
||||
end
|
||||
end
|
||||
d.pos = p
|
||||
end
|
||||
if not scheme or not scheme.tr.positive then
|
||||
for i = 1, numValues do
|
||||
vals[i] = zigzag(vals[i])
|
||||
end
|
||||
else
|
||||
local i32 = Predict.i32
|
||||
for i = 1, numValues do
|
||||
vals[i] = i32(vals[i])
|
||||
end
|
||||
end
|
||||
if scheme then
|
||||
if not Predict.decodeData(scheme, d) then
|
||||
return false, d.bad or "bad prediction data"
|
||||
end
|
||||
local ok, err = Predict.compute(scheme, vals, n, comps, points)
|
||||
if not ok then
|
||||
return false, err
|
||||
end
|
||||
end
|
||||
a.portable = vals
|
||||
a.numValues = n
|
||||
return true, nil
|
||||
end
|
||||
|
||||
local function decodeQuantInfo(d: D, a: Attr): boolean
|
||||
local mins = table.create(a.comps, 0)
|
||||
for c = 1, a.comps do
|
||||
mins[c] = Reader.f32(d)
|
||||
end
|
||||
local range = Reader.f32(d)
|
||||
local bits = Reader.u8(d)
|
||||
if d.bad or bits < 1 or bits > 30 then
|
||||
return false
|
||||
end
|
||||
a.quant = { min = mins, range = range, bits = bits }
|
||||
return true
|
||||
end
|
||||
|
||||
-- StoreValues: integer -> the data type; quantized -> f32; octahedral -> unit vectors
|
||||
local function storeValues(a: Attr): (boolean, string?)
|
||||
local src = a.portable :: { number }
|
||||
local n = a.numValues
|
||||
local fround = Predict.fround
|
||||
if a.seqType == 1 then
|
||||
local dt = a.dataType
|
||||
local out = table.create(n * a.comps, 0)
|
||||
for i = 1, n * a.comps do
|
||||
local v = src[i]
|
||||
if dt == 1 then
|
||||
v = (v + 128) % 256 - 128
|
||||
elseif dt == 2 or dt == 11 then
|
||||
v %= 256
|
||||
elseif dt == 3 then
|
||||
v = (v + 32768) % 65536 - 32768
|
||||
elseif dt == 4 then
|
||||
v %= 65536
|
||||
elseif dt == 6 then
|
||||
v %= 4294967296
|
||||
elseif dt ~= 5 then
|
||||
return false, "unsupported: integer attribute of data type " .. dt
|
||||
end
|
||||
out[i] = v
|
||||
end
|
||||
a.values = out
|
||||
elseif a.seqType == 2 then
|
||||
local q = a.quant
|
||||
if not q then
|
||||
return false, "no quantization data"
|
||||
end
|
||||
local maxQ = 2 ^ q.bits - 1
|
||||
local delta = fround(q.range / fround(maxQ))
|
||||
local comps = a.comps
|
||||
local mins = q.min
|
||||
local out = table.create(n * comps, 0)
|
||||
local k = 1
|
||||
for _ = 1, n do
|
||||
for c = 1, comps do
|
||||
out[k] = fround(fround(fround(src[k]) * delta) + mins[c])
|
||||
k += 1
|
||||
end
|
||||
end
|
||||
a.values = out
|
||||
elseif a.seqType == 3 then
|
||||
local o = Predict.octahedron(a.octBits or 0)
|
||||
if not o then
|
||||
return false, "bad normal quantization"
|
||||
end
|
||||
local out = table.create(n * 3, 0)
|
||||
for i = 0, n - 1 do
|
||||
local x, y, z = Predict.octToVector(o, src[i * 2 + 1], src[i * 2 + 2])
|
||||
out[i * 3 + 1], out[i * 3 + 2], out[i * 3 + 3] = x, y, z
|
||||
end
|
||||
a.values = out
|
||||
end
|
||||
return true, nil
|
||||
end
|
||||
|
||||
-- decodes every attribute; returns them in attribute id order
|
||||
function Attributes.decode(d: D, g: Geom): ({ Attr }?, string?)
|
||||
local ver = d.ver
|
||||
local numDecoders = Reader.u8(d)
|
||||
if d.bad then
|
||||
return nil, d.bad
|
||||
end
|
||||
local mesh = g.mesh
|
||||
local decoders: { Decoder } = {}
|
||||
for i = 0, numDecoders - 1 do
|
||||
local dec: Decoder = { attrs = {}, traversal = nil, md = nil, points = nil }
|
||||
if g.edgebreaker and mesh then
|
||||
local attDataId = Reader.i8(d)
|
||||
local decoderType = Reader.u8(d)
|
||||
if d.bad then
|
||||
return nil, d.bad
|
||||
end
|
||||
local ad = nil
|
||||
if attDataId >= 0 then
|
||||
ad = mesh.attrData[attDataId + 1]
|
||||
if not ad or ad.decoderId >= 0 then
|
||||
return nil, "bad attribute data id"
|
||||
end
|
||||
ad.decoderId = i
|
||||
else
|
||||
if mesh.posDecoderId >= 0 then
|
||||
return nil, "two position decoders"
|
||||
end
|
||||
mesh.posDecoderId = i
|
||||
end
|
||||
local method = 0
|
||||
if ver >= 0x0102 then
|
||||
method = Reader.u8(d)
|
||||
if d.bad or method >= 2 then
|
||||
return nil, "bad traversal method"
|
||||
end
|
||||
end
|
||||
local ct = mesh.ct :: Connectivity.CT
|
||||
local enc
|
||||
if decoderType == 0 then
|
||||
if ad then
|
||||
enc = ad.enc
|
||||
ad.used = false
|
||||
else
|
||||
enc = mesh.posEnc :: Connectivity.Enc
|
||||
end
|
||||
dec.traversal = { ct = ct, enc = enc, method = method }
|
||||
else
|
||||
if method ~= 0 or not ad then
|
||||
return nil, "bad corner attribute decoder"
|
||||
end
|
||||
local cad = ad :: Connectivity.AttrData
|
||||
enc = cad.enc
|
||||
dec.traversal = { ct = cad.ct :: Connectivity.CT, enc = enc, method = 0 }
|
||||
end
|
||||
-- prediction data: the attribute's own corner table when its seams are in use
|
||||
local pct = if ad and ad.used then ad.ct :: Connectivity.CT else ct
|
||||
dec.md = { ct = pct, e2c = enc.e2c, v2e = enc.v2e }
|
||||
end
|
||||
decoders[i + 1] = dec
|
||||
end
|
||||
-- the attribute descriptors
|
||||
local all: { Attr } = {}
|
||||
for _, dec in decoders do
|
||||
local n = Reader.count(d, 0x0200)
|
||||
if d.bad then
|
||||
return nil, d.bad
|
||||
end
|
||||
if n == 0 or n > 5 * Reader.remaining(d) then
|
||||
return nil, "bad attribute count"
|
||||
end
|
||||
for _ = 1, n do
|
||||
local attType, dataType, comps, normalized = Reader.u8(d), Reader.u8(d), Reader.u8(d), Reader.u8(d)
|
||||
if d.bad then
|
||||
return nil, d.bad
|
||||
end
|
||||
if attType >= 9 or dataType == 0 or dataType >= 12 or comps == 0 then
|
||||
return nil, "bad attribute descriptor"
|
||||
end
|
||||
local uniqueId = if ver < 0x0103 then Reader.u16(d) else Reader.varint(d)
|
||||
local a: Attr = {
|
||||
id = #all,
|
||||
type = attType,
|
||||
dataType = dataType,
|
||||
comps = comps,
|
||||
normalized = normalized > 0,
|
||||
uniqueId = uniqueId,
|
||||
seqType = 0,
|
||||
map = nil,
|
||||
numValues = 0,
|
||||
}
|
||||
table.insert(all, a)
|
||||
table.insert(dec.attrs, a)
|
||||
end
|
||||
for _, a in (if g.kdTree then {} else dec.attrs) do
|
||||
local t = Reader.u8(d)
|
||||
if d.bad then
|
||||
return nil, d.bad
|
||||
end
|
||||
if t > 3 then
|
||||
return nil, "unknown attribute decoder " .. t
|
||||
end
|
||||
if (t == 2 or t == 3) and a.dataType ~= 9 then
|
||||
return nil, "quantized attribute is not f32"
|
||||
end
|
||||
if t == 3 and a.comps ~= 3 then
|
||||
return nil, "normal attribute without 3 components"
|
||||
end
|
||||
a.seqType = t
|
||||
end
|
||||
end
|
||||
if d.bad then
|
||||
return nil, d.bad
|
||||
end
|
||||
local function named(t: number): Attr?
|
||||
for _, a in all do
|
||||
if a.type == t then
|
||||
return a
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
-- decode
|
||||
for _, dec in decoders do
|
||||
if g.kdTree then
|
||||
local ok, err = KdTree.decode(d, dec.attrs, g.numPoints)
|
||||
if not ok then
|
||||
return nil, err
|
||||
end
|
||||
continue
|
||||
end
|
||||
local points: { number }
|
||||
local trav = dec.traversal
|
||||
if trav then
|
||||
local faces = (mesh :: Connectivity.Mesh).faces
|
||||
local p = Connectivity.traverse(trav.ct, faces, trav.enc, trav.method)
|
||||
if not p then
|
||||
return nil, "bad attribute traversal"
|
||||
end
|
||||
points = p
|
||||
local numPoints = (mesh :: Connectivity.Mesh).numPoints
|
||||
for _, a in dec.attrs do
|
||||
local m = Connectivity.mapping(trav.ct, faces, trav.enc, numPoints)
|
||||
if not m then
|
||||
return nil, "bad attribute mapping"
|
||||
end
|
||||
a.map = m
|
||||
end
|
||||
else
|
||||
points = table.create(g.numPoints, 0)
|
||||
for i = 1, g.numPoints do
|
||||
points[i] = i - 1
|
||||
end
|
||||
end
|
||||
dec.points = points
|
||||
-- portable values
|
||||
for _, a in dec.attrs do
|
||||
if a.seqType == 0 then
|
||||
local stride = DT_LENGTH[a.dataType] * a.comps
|
||||
local size = #points * stride
|
||||
if Reader.remaining(d) < size then
|
||||
return nil, "truncated attribute values"
|
||||
end
|
||||
local raw = buffer.create(size)
|
||||
buffer.copy(raw, 0, d.b, d.pos, size)
|
||||
d.pos += size
|
||||
a.raw = raw
|
||||
a.numValues = #points
|
||||
continue
|
||||
end
|
||||
local psm = Reader.i8(d)
|
||||
if d.bad then
|
||||
return nil, d.bad
|
||||
end
|
||||
if psm < -2 or psm >= 7 then
|
||||
return nil, "bad prediction scheme"
|
||||
end
|
||||
local scheme: Predict.Scheme? = nil
|
||||
if psm ~= -2 then
|
||||
local ptt = Reader.i8(d)
|
||||
if d.bad then
|
||||
return nil, d.bad
|
||||
end
|
||||
if ptt < -1 or ptt >= 4 then
|
||||
return nil, "bad prediction transform"
|
||||
end
|
||||
if a.seqType == 3 then
|
||||
if ptt == 2 or ptt == 3 then
|
||||
scheme = Predict.new(psm, Predict.transform(ptt), dec.md)
|
||||
end
|
||||
elseif ptt == 1 then
|
||||
scheme = Predict.new(psm, Predict.transform(1), dec.md)
|
||||
end
|
||||
end
|
||||
if scheme and Predict.needsParent(scheme) then
|
||||
local pos = named(0)
|
||||
local getter = pos and parentGetter(ver, pos, scheme.method == 3)
|
||||
if not getter then
|
||||
return nil, "prediction needs the decoded positions"
|
||||
end
|
||||
scheme.parent = getter
|
||||
end
|
||||
if ver < 0x0200 then
|
||||
if a.seqType == 2 and not decodeQuantInfo(d, a) then
|
||||
return nil, d.bad or "bad quantization data"
|
||||
elseif a.seqType == 3 then
|
||||
a.octBits = Reader.u8(d)
|
||||
end
|
||||
end
|
||||
local ok, err = decodeIntegerValues(d, a, points, scheme)
|
||||
if not ok then
|
||||
return nil, err
|
||||
end
|
||||
a.scheme = scheme
|
||||
if ver < 0x0200 then
|
||||
local ok2, err2 = storeValues(a)
|
||||
if not ok2 then
|
||||
return nil, err2
|
||||
end
|
||||
end
|
||||
end
|
||||
-- data needed by the transforms
|
||||
if ver >= 0x0200 then
|
||||
for _, a in dec.attrs do
|
||||
if a.seqType == 2 then
|
||||
if not decodeQuantInfo(d, a) then
|
||||
return nil, d.bad or "bad quantization data"
|
||||
end
|
||||
elseif a.seqType == 3 then
|
||||
a.octBits = Reader.u8(d)
|
||||
end
|
||||
end
|
||||
if d.bad then
|
||||
return nil, d.bad
|
||||
end
|
||||
for _, a in dec.attrs do
|
||||
if a.seqType ~= 0 then
|
||||
local ok, err = storeValues(a)
|
||||
if not ok then
|
||||
return nil, err
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return all, nil
|
||||
end
|
||||
|
||||
return Attributes
|
||||
Reference in new issue
Block a user