perf: OBJ encode: triangle lines built directly, number format lists hoisted
This commit is contained in:
1 parent
b7e96cbf34
commit
f6c7857525
2 files changed
+6
-3
No files matched your search
@@ -151,11 +151,12 @@ function Mtl.toMaterial(e: Entry, texture: (Map) -> number): Types.Material
|
||||
return m
|
||||
end
|
||||
|
||||
local NUM_FORMATS = { "%.6g", "%.9g", "%.17g" }
|
||||
local function num(v: number): string
|
||||
if v == math.floor(v) and math.abs(v) < 1e15 then
|
||||
return string.format("%d", v)
|
||||
end
|
||||
for _, f in { "%.6g", "%.9g", "%.17g" } do
|
||||
for _, f in NUM_FORMATS do
|
||||
local s = string.format(f, v)
|
||||
if tonumber(s) == v then
|
||||
return s
|
||||
|
||||
@@ -545,6 +545,7 @@ end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
local probe = buffer.create(4)
|
||||
local F32_FORMATS = { "%.6g", "%.7g", "%.8g" }
|
||||
local function f32str(v: number): string
|
||||
if v ~= v or v == math.huge or v == -math.huge then
|
||||
return "0"
|
||||
@@ -554,7 +555,7 @@ local function f32str(v: number): string
|
||||
end
|
||||
buffer.writef32(probe, 0, v)
|
||||
local want = buffer.readf32(probe, 0)
|
||||
for _, f in { "%.6g", "%.7g", "%.8g" } do
|
||||
for _, f in F32_FORMATS do
|
||||
local s = string.format(f, v)
|
||||
buffer.writef32(probe, 0, tonumber(s) :: number)
|
||||
if buffer.readf32(probe, 0) == want then
|
||||
@@ -732,7 +733,8 @@ function Obj.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err
|
||||
local mode = prim.mode or "triangles"
|
||||
if mode == "triangles" then
|
||||
for t = 0, count // 3 - 1 do
|
||||
face({ at(t * 3), at(t * 3 + 1), at(t * 3 + 2) })
|
||||
local a, b, c = ref(at(t * 3)), ref(at(t * 3 + 1)), ref(at(t * 3 + 2))
|
||||
table.insert(out, if mirror then "f " .. c .. " " .. b .. " " .. a else "f " .. a .. " " .. b .. " " .. c)
|
||||
end
|
||||
elseif mode == "polygons" then
|
||||
local F = if prim.faces then Types.Scene.reader(prim.faces) else nil
|
||||
|
||||
Reference in new issue
Block a user