model/ase: 3ds Max ASCII Scene Export read + write (geometry, Multi/Sub-Object materials, texture channels, colours, normals, NODE_TM hierarchy, helpers, cameras, lights; checked against assimp)
This commit is contained in:
1 parent
01c044805f
commit
0b6773db8b
12 files changed
+1814
-1
No files matched your search
@@ -44,7 +44,7 @@ print(F.detect(webp), #F.formats()) -- "webp", the format list
|
||||
## Formats
|
||||
|
||||
<!-- formats:begin -->
|
||||
174 formats. Generated from the format contracts by `tools/gen_registry.py`; [docs/FORMATS.md](docs/FORMATS.md)
|
||||
175 formats. Generated from the format contracts by `tools/gen_registry.py`; [docs/FORMATS.md](docs/FORMATS.md)
|
||||
adds each format's capabilities.
|
||||
|
||||
| Category | Format | Extensions | Read | Write |
|
||||
@@ -108,6 +108,7 @@ adds each format's capabilities.
|
||||
| Video | y4m | .y4m | yes | yes |
|
||||
| 3D models | 3ds | .3ds | yes | yes |
|
||||
| 3D models | 3mf | .3mf | yes | yes |
|
||||
| 3D models | ase | .ase .ask | yes | yes |
|
||||
| 3D models | b3d | .b3d | yes | - |
|
||||
| 3D models | bsp | .bsp | yes | - |
|
||||
| 3D models | dae | .dae .zae | yes | yes |
|
||||
|
||||
@@ -64,6 +64,7 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
|
||||
| video | y4m | .y4m | yes | yes | streaming: true, video: "rawvideo", depths: 8, 10, 12, 16 | `tests/video/y4m.luau` |
|
||||
| model | 3ds | .3ds | yes | yes | materials: true, textures: true, animation: true, cameras: true, lights: true, instancing: true | `tests/model/3ds.luau` |
|
||||
| model | 3mf | .3mf | yes | yes | materials: true, vertexColors: true, textures: true, units: true | `tests/model/3mf.luau` |
|
||||
| model | ase | .ase .ask | yes | yes | text: true, materials: true, cameras: true, lights: true, vertexColors: true | `tests/model/ase.luau` |
|
||||
| model | b3d | .b3d | yes | - | materials: true, textures: true, skins: true, animation: true, colors: true | `tests/model/b3d.luau` |
|
||||
| model | bsp | .bsp | yes | - | variants: "quake", "halflife", "quake2", "quake3", lightmaps: "quake3" | `tests/model/bsp.luau` |
|
||||
| model | dae | .dae .zae | yes | yes | materials: true, polygons: true, skins: true, morphTargets: true, animation: true, cameras: true, lights: true, zae: true | `tests/model/dae.luau` |
|
||||
|
||||
@@ -716,6 +716,29 @@ assert(back.meta.source.format == "iqm" and back.meshes[1].name == "tri")
|
||||
assert(back.meshes[1].primitives[1].indices.count == 3)
|
||||
```
|
||||
|
||||
## 3ds Max ASCII export (ASE)
|
||||
|
||||
`.ase` files (`*3DSMAX_ASCIIEXPORT`) load as a Z-up scene: one node per geometry, helper, camera and light object,
|
||||
parented by `*NODE_PARENT`. ASE stores mesh vertices in world space; the reader moves them into the node's frame (the
|
||||
inverse `*NODE_TM`), so `Scene.flatten` puts them back where 3ds Max had them. Faces split into one primitive per
|
||||
sub-material of a Multi/Sub-Object material (`*MESH_MTLID`); each corner is its own vertex (texture channels,
|
||||
vertex colours and `*MESH_VERTEXNORMAL`s are per corner). Materials are Phong (the raw values in `extras`), bitmaps
|
||||
become textures with a `uri` (data through `opts.resolve`), UVW tiling / offset a texture transform. Camera FOVs
|
||||
stay the stored horizontal angle. `*TM_ANIMATION` tracks are not read (`meta.source.animated` says they exist).
|
||||
The writer puts every scene material in one Multi/Sub-Object material and writes world-space vertices.
|
||||
|
||||
```luau
|
||||
local scene = assert(F.load(aseText))
|
||||
assert(scene.meta.source.format == "ase" and scene.meta.up == "z")
|
||||
assert(#scene.materials == 2 and scene.materials[1].name == "Brick")
|
||||
local quad = scene.nodes[2]
|
||||
assert(quad.name == "Quad" and #scene.meshes[quad.mesh].primitives == 2)
|
||||
local tex = scene.textures[scene.materials[1].baseColorTexture.texture]
|
||||
assert(tex.uri == "textures\\brick.png")
|
||||
local again = assert(F.load(F.save(scene, nil, { format = "ase" })))
|
||||
assert(#again.meshes[again.nodes[2].mesh].primitives == 2)
|
||||
```
|
||||
|
||||
## Formats
|
||||
|
||||
| Format | Read | Write | Notes |
|
||||
@@ -746,6 +769,7 @@ assert(back.meshes[1].primitives[1].indices.count == 3)
|
||||
| STEP | via the CAD plug-in | | see [cad.md](cad.md) |
|
||||
| MD2, MD3, MDL, MD5, BSP | yes | - | game models and maps: see "Game models" above |
|
||||
| SMD | yes | yes | Valve studiomdl data |
|
||||
| ASE (3ds Max ASCII) | yes | yes | Multi/Sub-Object materials, texture channels, vertex colours, normals, helpers, cameras, lights; no animation; Z up |
|
||||
| Roblox `.mesh` (robloxmesh) | yes | yes | see [roblox.md](roblox.md) |
|
||||
| Roblox `.rbxm` / `.rbxl`, `.rbxmx` / `.rbxlx` | as an `RbxTree` | yes | instance trees with typed properties: see [roblox.md](roblox.md) |
|
||||
|
||||
|
||||
@@ -722,6 +722,18 @@ local list: { Entry } = {
|
||||
return require("@rosetta/model/3mf")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "ase",
|
||||
category = "model",
|
||||
path = "model/ase",
|
||||
extensions = { "ase", "ask" },
|
||||
mime = { "model/x-ase", "text/plain" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/text/Text", "codecs/model/Mat4" },
|
||||
caps = { decode = true, encode = true, text = true, materials = true, cameras = true, lights = true, vertexColors = true },
|
||||
load = function()
|
||||
return require("@rosetta/model/ase")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "b3d",
|
||||
category = "model",
|
||||
|
||||
@@ -0,0 +1,572 @@
|
||||
--!strict
|
||||
-- model/ase/Build: the parsed ASE tree into a Scene. Mesh vertices are stored in world space (3ds Max ASCII export):
|
||||
-- they are moved into the node's frame by the inverse NODE_TM, and node matrices are made relative to *NODE_PARENT.
|
||||
local Types = require("@rosetta/Types")
|
||||
local Mat4 = require("@rosetta/codecs/model/Mat4")
|
||||
local Parse = require("@rosetta/model/ase/Parse")
|
||||
|
||||
type Item = Parse.Item
|
||||
|
||||
local Build = {}
|
||||
|
||||
local S = Types.Scene
|
||||
|
||||
local function first(kids: { Item }?, key: string): Item?
|
||||
if kids then
|
||||
for _, it in kids do
|
||||
if it.key == key then
|
||||
return it
|
||||
end
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
local function num(it: Item?, i: number?): number?
|
||||
if not it then
|
||||
return nil
|
||||
end
|
||||
local v = tonumber(it.args[i or 1])
|
||||
if v and v == v and v ~= math.huge and v ~= -math.huge then
|
||||
return v
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
local function get(kids: { Item }?, key: string, i: number?): number?
|
||||
return num(first(kids, key), i)
|
||||
end
|
||||
|
||||
local function str(kids: { Item }?, key: string): string?
|
||||
local it = first(kids, key)
|
||||
return if it then it.args[1] else nil
|
||||
end
|
||||
|
||||
local function triple(kids: { Item }?, key: string): { number }?
|
||||
local it = first(kids, key)
|
||||
if not it then
|
||||
return nil
|
||||
end
|
||||
local a, b, c = num(it, 1), num(it, 2), num(it, 3)
|
||||
if a and b and c then
|
||||
return { a, b, c }
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- *TM_ROW0..2 are the axes, *TM_ROW3 the translation (row vectors): the columns of a column-major matrix
|
||||
local function nodeTm(kids: { Item }?): { number }
|
||||
local tm = first(kids, "NODE_TM")
|
||||
local m = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
|
||||
if not tm then
|
||||
return m
|
||||
end
|
||||
for r = 0, 3 do
|
||||
local row = triple(tm.kids, "TM_ROW" .. r)
|
||||
if row then
|
||||
m[r * 4 + 1], m[r * 4 + 2], m[r * 4 + 3] = row[1], row[2], row[3]
|
||||
end
|
||||
end
|
||||
return m
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ materials
|
||||
local MAPS = { "MAP_DIFFUSE", "MAP_BUMP", "MAP_OPACITY", "MAP_SELFILLUM", "MAP_SPECULAR", "MAP_SHINE", "MAP_AMBIENT", "MAP_REFLECT", "MAP_REFRACT", "MAP_SHINESTRENGTH", "MAP_FILTERCOLOR" }
|
||||
|
||||
local function textureRef(scene: Types.Scene, map: Item, opts: any): (Types.TextureRef?, { [string]: any })
|
||||
local k = map.kids
|
||||
local info: { [string]: any } = {
|
||||
name = str(k, "MAP_NAME"),
|
||||
class = str(k, "MAP_CLASS"),
|
||||
amount = get(k, "MAP_AMOUNT"),
|
||||
bitmap = str(k, "BITMAP"),
|
||||
uOffset = get(k, "UVW_U_OFFSET"),
|
||||
vOffset = get(k, "UVW_V_OFFSET"),
|
||||
uTiling = get(k, "UVW_U_TILING"),
|
||||
vTiling = get(k, "UVW_V_TILING"),
|
||||
angle = get(k, "UVW_ANGLE"),
|
||||
}
|
||||
local path = info.bitmap
|
||||
if type(path) ~= "string" or path == "" then
|
||||
return nil, info
|
||||
end
|
||||
local tex: Types.Texture = { name = path, uri = path }
|
||||
if opts and type(opts.resolve) == "function" then
|
||||
local ok, data = pcall(opts.resolve, path)
|
||||
if ok and type(data) == "buffer" then
|
||||
tex.data = data
|
||||
end
|
||||
end
|
||||
table.insert(scene.textures, tex)
|
||||
local ref: Types.TextureRef = { texture = #scene.textures }
|
||||
local su, sv = info.uTiling or 1, info.vTiling or 1
|
||||
local ou, ov = info.uOffset or 0, info.vOffset or 0
|
||||
local ang = info.angle or 0
|
||||
if su ~= 1 or sv ~= 1 or ou ~= 0 or ov ~= 0 or ang ~= 0 then
|
||||
ref.transform = { offset = { ou, ov }, scale = { su, sv }, rotation = ang }
|
||||
end
|
||||
return ref, info
|
||||
end
|
||||
|
||||
local function material(scene: Types.Scene, it: Item, opts: any): Types.Material
|
||||
local k = it.kids
|
||||
local m = S.material(str(k, "MATERIAL_NAME"))
|
||||
local diffuse = triple(k, "MATERIAL_DIFFUSE") or { 0.8, 0.8, 0.8 }
|
||||
local transparency = get(k, "MATERIAL_TRANSPARENCY") or 0
|
||||
local shine = get(k, "MATERIAL_SHINE") or 0
|
||||
local selfIllum = get(k, "MATERIAL_SELFILLUM") or 0
|
||||
m.baseColor = { diffuse[1], diffuse[2], diffuse[3], 1 - transparency }
|
||||
m.metallic = 0
|
||||
m.roughness = math.clamp(1 - shine, 0, 1)
|
||||
m.emissive = { diffuse[1] * selfIllum, diffuse[2] * selfIllum, diffuse[3] * selfIllum }
|
||||
if transparency > 0 then
|
||||
m.alphaMode = "blend"
|
||||
end
|
||||
m.doubleSided = first(k, "MATERIAL_TWOSIDED") ~= nil
|
||||
local maps: { [string]: any } = {}
|
||||
for _, key in MAPS do
|
||||
local map = first(k, key)
|
||||
if map then
|
||||
local ref, info = textureRef(scene, map, opts)
|
||||
maps[string.lower(string.sub(key, 5))] = info
|
||||
if ref then
|
||||
if key == "MAP_DIFFUSE" then
|
||||
m.baseColorTexture = ref
|
||||
elseif key == "MAP_BUMP" then
|
||||
m.normalTexture = ref
|
||||
elseif key == "MAP_SELFILLUM" then
|
||||
m.emissiveTexture = ref
|
||||
m.emissive = { 1, 1, 1 }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
m.extras = {
|
||||
ambient = triple(k, "MATERIAL_AMBIENT"),
|
||||
diffuse = diffuse,
|
||||
specular = triple(k, "MATERIAL_SPECULAR"),
|
||||
shininess = shine,
|
||||
shineStrength = get(k, "MATERIAL_SHINESTRENGTH"),
|
||||
transparency = transparency,
|
||||
selfIllum = selfIllum,
|
||||
ase = { class = str(k, "MATERIAL_CLASS"), shading = str(k, "MATERIAL_SHADING"), maps = maps },
|
||||
}
|
||||
return m
|
||||
end
|
||||
|
||||
-- ASE material index (0-based) -> { scene material per sub-material } (one entry when it has none)
|
||||
local function materials(scene: Types.Scene, list: Item?, opts: any): { { number } }
|
||||
local out: { { number } } = {}
|
||||
if not list or not list.kids then
|
||||
return out
|
||||
end
|
||||
for _, it in list.kids do
|
||||
if it.key == "MATERIAL" then
|
||||
local subs: { number } = {}
|
||||
for _, sub in it.kids or {} do
|
||||
if sub.key == "SUBMATERIAL" then
|
||||
table.insert(scene.materials, material(scene, sub, opts))
|
||||
table.insert(subs, #scene.materials)
|
||||
end
|
||||
end
|
||||
if #subs == 0 then
|
||||
table.insert(scene.materials, material(scene, it, opts))
|
||||
subs[1] = #scene.materials
|
||||
end
|
||||
table.insert(out, subs)
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ meshes
|
||||
type Lists = { v: { number }, n: number }
|
||||
|
||||
local function vectors(kids: { Item }?, listKey: string, itemKey: string, comps: number, max: number): { number }
|
||||
local out: { number } = {}
|
||||
local list = first(kids, listKey)
|
||||
if not list or not list.kids then
|
||||
return out
|
||||
end
|
||||
for _, it in list.kids do
|
||||
if it.key == itemKey then
|
||||
local i = num(it, 1)
|
||||
if i and i >= 0 and i < max and i % 1 == 0 then
|
||||
for c = 1, comps do
|
||||
out[i * comps + c] = num(it, c + 1) or 0
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local function corners(kids: { Item }?, listKey: string, itemKey: string, max: number): { number }
|
||||
local out: { number } = {}
|
||||
local list = first(kids, listKey)
|
||||
if not list or not list.kids then
|
||||
return out
|
||||
end
|
||||
for _, it in list.kids do
|
||||
if it.key == itemKey then
|
||||
local i = num(it, 1)
|
||||
if i and i >= 0 and i < max and i % 1 == 0 then
|
||||
out[i * 3 + 1], out[i * 3 + 2], out[i * 3 + 3] = num(it, 2) or 0, num(it, 3) or 0, num(it, 4) or 0
|
||||
end
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
type Face = { a: number, b: number, c: number, mtl: number, smoothing: string? }
|
||||
|
||||
-- *MESH_FACE i: A: a B: b C: c AB: e BC: e CA: e, then its *MESH_SMOOTHING / *MESH_MTLID siblings
|
||||
local function faces(list: Item?, nv: number, maxFaces: number): ({ Face }?, string?)
|
||||
local out: { Face } = {}
|
||||
if not list or not list.kids then
|
||||
return out, nil
|
||||
end
|
||||
local last: Face? = nil
|
||||
for _, it in list.kids do
|
||||
if it.key == "MESH_FACE" then
|
||||
local s = table.concat(it.args, " ")
|
||||
local i = tonumber(string.match(s, "^(%d+)"))
|
||||
local a = tonumber(string.match(s, "A:%s*(%d+)"))
|
||||
local b = tonumber(string.match(s, "B:%s*(%d+)"))
|
||||
local c = tonumber(string.match(s, "C:%s*(%d+)"))
|
||||
if not (i and a and b and c) then
|
||||
return nil, "bad *MESH_FACE at byte " .. it.at
|
||||
end
|
||||
if a >= nv or b >= nv or c >= nv then
|
||||
return nil, "face vertex index out of range at byte " .. it.at
|
||||
end
|
||||
if i >= maxFaces then
|
||||
return nil, "limit: face index " .. i
|
||||
end
|
||||
local f: Face = { a = a, b = b, c = c, mtl = 0, smoothing = nil }
|
||||
out[i + 1] = f
|
||||
last = f
|
||||
elseif it.key == "MESH_MTLID" and last then
|
||||
last.mtl = math.max(0, math.floor(num(it) or 0))
|
||||
elseif it.key == "MESH_SMOOTHING" and last then
|
||||
last.smoothing = it.args[1]
|
||||
end
|
||||
end
|
||||
for i = 1, #out do
|
||||
if out[i] == nil then
|
||||
out[i] = { a = 0, b = 0, c = 0, mtl = 0, smoothing = nil }
|
||||
end
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
type Channel = { verts: { number }, tf: { number }, comps: number }
|
||||
|
||||
local function channel(kids: { Item }?, numKey: string, listKey: string, vertKey: string, tfKey: string, tfListKey: string, nf: number, maxV: number): Channel?
|
||||
local n = get(kids, numKey) or 0
|
||||
if n <= 0 then
|
||||
return nil
|
||||
end
|
||||
n = math.min(n, maxV)
|
||||
local verts = vectors(kids, listKey, vertKey, 3, n)
|
||||
local tf = corners(kids, tfListKey, tfKey, nf)
|
||||
return { verts = verts, tf = tf, comps = 3 }
|
||||
end
|
||||
|
||||
local function writeVec(acc: Types.Accessor, i: number, x: number, y: number, z: number)
|
||||
local d = acc.data
|
||||
buffer.writef32(d, i * 12, x)
|
||||
buffer.writef32(d, i * 12 + 4, y)
|
||||
buffer.writef32(d, i * 12 + 8, z)
|
||||
end
|
||||
|
||||
local function mesh(scene: Types.Scene, obj: Item, matMap: { { number } }, world: { number }, opts: any): (number?, string?)
|
||||
local m = first(obj.kids, "MESH")
|
||||
if not m or not m.kids then
|
||||
return nil, nil
|
||||
end
|
||||
local k = m.kids
|
||||
local maxV = opts.maxVertices or 2 ^ 24
|
||||
local nv = get(k, "MESH_NUMVERTEX") or 0
|
||||
local nf = get(k, "MESH_NUMFACES") or 0
|
||||
if nv < 0 or nf < 0 or nv % 1 ~= 0 or nf % 1 ~= 0 then
|
||||
return nil, "bad vertex or face count"
|
||||
end
|
||||
if nv > maxV or nf * 3 > maxV then
|
||||
return nil, "limit: " .. nv .. " vertices, " .. nf .. " faces"
|
||||
end
|
||||
local pos = vectors(k, "MESH_VERTEX_LIST", "MESH_VERTEX", 3, nv)
|
||||
local fl, ferr = faces(first(k, "MESH_FACE_LIST"), nv, nf)
|
||||
if not fl then
|
||||
return nil, ferr
|
||||
end
|
||||
if #fl == 0 then
|
||||
return nil, nil
|
||||
end
|
||||
local uvs: { Channel } = {}
|
||||
local ch0 = channel(k, "MESH_NUMTVERTEX", "MESH_TVERTLIST", "MESH_TVERT", "MESH_TFACE", "MESH_TFACELIST", #fl, maxV)
|
||||
if ch0 then
|
||||
uvs[1] = ch0
|
||||
end
|
||||
for _, it in k do
|
||||
if it.key == "MESH_MAPPINGCHANNEL" and it.kids then
|
||||
local idx = num(it) or 0
|
||||
local ch = channel(it.kids, "MESH_NUMTVERTEX", "MESH_TVERTLIST", "MESH_TVERT", "MESH_TFACE", "MESH_TFACELIST", #fl, maxV)
|
||||
if ch and idx >= 2 and idx <= 8 then
|
||||
uvs[idx] = ch
|
||||
end
|
||||
end
|
||||
end
|
||||
local colors = channel(k, "MESH_NUMCVERTEX", "MESH_CVERTLIST", "MESH_VERTCOL", "MESH_CFACE", "MESH_CFACELIST", #fl, maxV)
|
||||
-- *MESH_NORMALS: *MESH_FACENORMAL f, then *MESH_VERTEXNORMAL v x y z for each of its corners
|
||||
local normals: { number }? = nil
|
||||
local nlist = first(k, "MESH_NORMALS")
|
||||
if nlist and nlist.kids then
|
||||
local nn: { number } = {}
|
||||
local face = -1
|
||||
for _, it in nlist.kids do
|
||||
if it.key == "MESH_FACENORMAL" then
|
||||
face = num(it) or -1
|
||||
elseif it.key == "MESH_VERTEXNORMAL" and face >= 0 and face < #fl then
|
||||
local f = fl[face + 1]
|
||||
local v = num(it)
|
||||
local corner = if v == f.a then 0 elseif v == f.b then 1 elseif v == f.c then 2 else nil
|
||||
if corner then
|
||||
local o = (face * 3 + corner) * 3
|
||||
nn[o + 1], nn[o + 2], nn[o + 3] = num(it, 2) or 0, num(it, 3) or 0, num(it, 4) or 0
|
||||
end
|
||||
end
|
||||
end
|
||||
normals = nn
|
||||
end
|
||||
-- faces by scene material (sub-material = *MESH_MTLID, the last one when out of range)
|
||||
local ref = get(obj.kids, "MATERIAL_REF")
|
||||
local subs: { number }? = if ref and ref >= 0 then matMap[math.min(ref, #matMap - 1) + 1] else nil
|
||||
local groups: { [number]: { number } } = {}
|
||||
local order: { number } = {}
|
||||
for i, f in fl do
|
||||
local key = 0
|
||||
if subs and #subs > 0 then
|
||||
key = subs[math.min(f.mtl, #subs - 1) + 1]
|
||||
end
|
||||
local g = groups[key]
|
||||
if not g then
|
||||
g = {}
|
||||
groups[key] = g
|
||||
table.insert(order, key)
|
||||
end
|
||||
table.insert(g, i)
|
||||
end
|
||||
table.sort(order)
|
||||
local inv = Mat4.inverse(world)
|
||||
local local_ = inv or S.IDENTITY
|
||||
local nmat = Mat4.normalMatrix(local_)
|
||||
local out: Types.Mesh = { name = str(obj.kids, "NODE_NAME"), primitives = {} }
|
||||
for _, key in order do
|
||||
local list = groups[key]
|
||||
local count = #list * 3
|
||||
local P = S.accessor(nil, "f32", 3, count)
|
||||
local N = if normals then S.accessor(nil, "f32", 3, count) else nil
|
||||
local UV: { [number]: Types.Accessor } = {}
|
||||
for idx in uvs do
|
||||
UV[idx] = S.accessor(nil, "f32", 2, count)
|
||||
end
|
||||
local C = if colors then S.accessor(nil, "f32", 4, count) else nil
|
||||
local I = S.accessor(nil, "u32", 1, count)
|
||||
local w = 0
|
||||
for _, fi in list do
|
||||
local f = fl[fi]
|
||||
local fc = { f.a, f.b, f.c }
|
||||
for corner = 0, 2 do
|
||||
local vi = fc[corner + 1]
|
||||
local x, y, z = Mat4.point(local_, pos[vi * 3 + 1] or 0, pos[vi * 3 + 2] or 0, pos[vi * 3 + 3] or 0)
|
||||
writeVec(P, w, x, y, z)
|
||||
if N and normals then
|
||||
local o = ((fi - 1) * 3 + corner) * 3
|
||||
local nx, ny, nz = normals[o + 1] or 0, normals[o + 2] or 0, normals[o + 3] or 0
|
||||
local tx = nmat[1] * nx + nmat[5] * ny + nmat[9] * nz
|
||||
local ty = nmat[2] * nx + nmat[6] * ny + nmat[10] * nz
|
||||
local tz = nmat[3] * nx + nmat[7] * ny + nmat[11] * nz
|
||||
local len = math.sqrt(tx * tx + ty * ty + tz * tz)
|
||||
if len > 0 then
|
||||
tx, ty, tz = tx / len, ty / len, tz / len
|
||||
end
|
||||
writeVec(N, w, tx, ty, tz)
|
||||
end
|
||||
for idx, ch in uvs do
|
||||
local ti = ch.tf[(fi - 1) * 3 + corner + 1] or 0
|
||||
local u, v = ch.verts[ti * 3 + 1] or 0, ch.verts[ti * 3 + 2] or 0
|
||||
buffer.writef32(UV[idx].data, w * 8, u)
|
||||
buffer.writef32(UV[idx].data, w * 8 + 4, 1 - v) -- 3ds Max v goes up
|
||||
end
|
||||
if C and colors then
|
||||
local ci = colors.tf[(fi - 1) * 3 + corner + 1] or 0
|
||||
local d = C.data
|
||||
buffer.writef32(d, w * 16, colors.verts[ci * 3 + 1] or 1)
|
||||
buffer.writef32(d, w * 16 + 4, colors.verts[ci * 3 + 2] or 1)
|
||||
buffer.writef32(d, w * 16 + 8, colors.verts[ci * 3 + 3] or 1)
|
||||
buffer.writef32(d, w * 16 + 12, 1)
|
||||
end
|
||||
buffer.writeu32(I.data, w * 4, w)
|
||||
w += 1
|
||||
end
|
||||
end
|
||||
local attrs: { [string]: Types.Accessor } = { POSITION = P }
|
||||
if N then
|
||||
attrs.NORMAL = N
|
||||
end
|
||||
for idx, acc in UV do
|
||||
attrs["TEXCOORD_" .. (idx - 1)] = acc
|
||||
end
|
||||
if C then
|
||||
attrs.COLOR_0 = C
|
||||
end
|
||||
local smoothing: { string } = {}
|
||||
for j, fi in list do
|
||||
smoothing[j] = fl[fi].smoothing or ""
|
||||
end
|
||||
local prim: Types.Primitive = {
|
||||
attributes = attrs,
|
||||
indices = I,
|
||||
mode = "triangles",
|
||||
material = if key > 0 then key else nil,
|
||||
extras = { smoothing = smoothing },
|
||||
}
|
||||
table.insert(out.primitives, prim)
|
||||
end
|
||||
table.insert(scene.meshes, out)
|
||||
return #scene.meshes, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ objects
|
||||
local LIGHTS: { [string]: "directional" | "point" | "spot" } = { Omni = "point", Target = "spot", Free = "spot", Directional = "directional", TargetDirectional = "directional" }
|
||||
|
||||
local function camera(scene: Types.Scene, obj: Item): number
|
||||
local st = first(obj.kids, "CAMERA_SETTINGS")
|
||||
local fov = get(st and st.kids, "CAMERA_FOV") or math.rad(45)
|
||||
local cam: Types.Camera = {
|
||||
name = str(obj.kids, "NODE_NAME"),
|
||||
type = "perspective",
|
||||
yfov = fov, -- 3ds Max stores the horizontal field of view (radians), kept as it is
|
||||
znear = get(st and st.kids, "CAMERA_NEAR") or 0,
|
||||
zfar = get(st and st.kids, "CAMERA_FAR"),
|
||||
}
|
||||
table.insert(scene.cameras, cam)
|
||||
return #scene.cameras
|
||||
end
|
||||
|
||||
local function light(scene: Types.Scene, obj: Item): number
|
||||
local st = first(obj.kids, "LIGHT_SETTINGS")
|
||||
local sk = st and st.kids
|
||||
local typ = LIGHTS[str(obj.kids, "LIGHT_TYPE") or "Omni"] or "point"
|
||||
local l: Types.Light = {
|
||||
name = str(obj.kids, "NODE_NAME"),
|
||||
type = typ,
|
||||
color = triple(sk, "LIGHT_COLOR") or { 1, 1, 1 },
|
||||
intensity = get(sk, "LIGHT_INTENS") or 1,
|
||||
}
|
||||
if typ == "spot" then
|
||||
l.innerCone = math.rad((get(sk, "LIGHT_HOTSPOT") or 43) / 2)
|
||||
l.outerCone = math.rad((get(sk, "LIGHT_FALLOFF") or 45) / 2)
|
||||
end
|
||||
table.insert(scene.lights, l)
|
||||
return #scene.lights
|
||||
end
|
||||
|
||||
local KINDS = { GEOMOBJECT = true, HELPEROBJECT = true, CAMERAOBJECT = true, LIGHTOBJECT = true, SHAPEOBJECT = true }
|
||||
|
||||
local function collect(kids: { Item }, into: { Item }, depth: number)
|
||||
for _, it in kids do
|
||||
if KINDS[it.key] then
|
||||
table.insert(into, it)
|
||||
elseif it.key == "GROUP" and it.kids and depth < 32 then
|
||||
collect(it.kids, into, depth + 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Build.scene(root: { Item }, opts: any): (Types.Scene?, string?)
|
||||
local o = opts or {}
|
||||
local scene = S.new()
|
||||
local version = get(root, "3DSMAX_ASCIIEXPORT")
|
||||
local sk = (first(root, "SCENE") or { kids = nil } :: any).kids
|
||||
scene.meta.up = "z"
|
||||
scene.meta.source = {
|
||||
format = "ase",
|
||||
version = version,
|
||||
comment = str(root, "COMMENT"),
|
||||
firstFrame = get(sk, "SCENE_FIRSTFRAME"),
|
||||
lastFrame = get(sk, "SCENE_LASTFRAME"),
|
||||
frameSpeed = get(sk, "SCENE_FRAMESPEED"),
|
||||
ticksPerFrame = get(sk, "SCENE_TICKSPERFRAME"),
|
||||
background = triple(sk, "SCENE_BACKGROUND_STATIC"),
|
||||
ambient = triple(sk, "SCENE_AMBIENT_STATIC"),
|
||||
}
|
||||
local matMap = materials(scene, first(root, "MATERIAL_LIST"), o)
|
||||
local objs: { Item } = {}
|
||||
collect(root, objs, 0)
|
||||
local byName: { [string]: number } = {}
|
||||
local worlds: { { number } } = {}
|
||||
local parents: { string? } = {}
|
||||
local animated = false
|
||||
for i, obj in objs do
|
||||
local name = str(obj.kids, "NODE_NAME")
|
||||
local world = nodeTm(obj.kids)
|
||||
local node: Types.Node = { name = name, children = {} }
|
||||
local extras: { [string]: any } = { class = string.lower((string.gsub(obj.key, "OBJECT$", ""))) }
|
||||
if obj.key == "GEOMOBJECT" then
|
||||
local mi, merr = mesh(scene, obj, matMap, world, o)
|
||||
if merr then
|
||||
return nil, merr
|
||||
end
|
||||
node.mesh = mi
|
||||
elseif obj.key == "CAMERAOBJECT" then
|
||||
node.camera = camera(scene, obj)
|
||||
elseif obj.key == "LIGHTOBJECT" then
|
||||
node.light = light(scene, obj)
|
||||
elseif obj.key == "HELPEROBJECT" then
|
||||
extras.helperClass = str(obj.kids, "HELPER_CLASS")
|
||||
end
|
||||
if first(obj.kids, "TM_ANIMATION") then
|
||||
animated = true
|
||||
end
|
||||
node.extras = { ase = extras }
|
||||
scene.nodes[i] = node
|
||||
worlds[i] = world
|
||||
parents[i] = str(obj.kids, "NODE_PARENT")
|
||||
if name and byName[name] == nil then
|
||||
byName[name] = i
|
||||
end
|
||||
end
|
||||
for i, node in scene.nodes do
|
||||
local pname = parents[i]
|
||||
local p = if pname then byName[pname] else nil
|
||||
if p and p ~= i then
|
||||
-- a parent cycle (or a parent listed later naming this node) falls back to a root
|
||||
local q, steps = p, 0
|
||||
while q and q ~= i and steps < #objs do
|
||||
local pn = parents[q]
|
||||
q = if pn then byName[pn] else nil
|
||||
steps += 1
|
||||
end
|
||||
if q == i then
|
||||
p = nil
|
||||
end
|
||||
else
|
||||
p = nil
|
||||
end
|
||||
local inv = if p then Mat4.inverse(worlds[p :: number]) else nil
|
||||
node.matrix = if inv then S.mul(inv, worlds[i]) else worlds[i]
|
||||
if p then
|
||||
table.insert(scene.nodes[p :: number].children, i)
|
||||
else
|
||||
table.insert(scene.roots, i)
|
||||
end
|
||||
end
|
||||
if animated then
|
||||
(scene.meta.source :: any).animated = true
|
||||
end
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
return Build
|
||||
@@ -0,0 +1,67 @@
|
||||
--!strict
|
||||
-- model/ase/Parse: ASE text into a tree of items (*KEY args { children }). Quoted strings carry no escapes (3ds Max
|
||||
-- ASCII export). Several items may share a line (*MESH_FACE ... *MESH_SMOOTHING 1 *MESH_MTLID 0).
|
||||
local Parse = {}
|
||||
|
||||
export type Item = { key: string, args: { string }, kids: { Item }?, at: number }
|
||||
|
||||
function Parse.tree(text: string, maxItems: number): ({ Item }?, string?)
|
||||
local root: { Item } = {}
|
||||
local stack: { { Item } } = { root }
|
||||
local cur: Item? = nil
|
||||
local p, n = 1, #text
|
||||
local count = 0
|
||||
local find, sub, byte = string.find, string.sub, string.byte
|
||||
while p <= n do
|
||||
local s = find(text, "[^ \t\r\n]", p)
|
||||
if not s then
|
||||
break
|
||||
end
|
||||
local c = byte(text, s)
|
||||
if c == 34 then -- '"'
|
||||
local e = find(text, "\"", s + 1, true)
|
||||
local stop = e or (n + 1)
|
||||
if cur then
|
||||
table.insert(cur.args, sub(text, s + 1, stop - 1))
|
||||
end
|
||||
p = stop + 1
|
||||
elseif c == 123 then -- '{'
|
||||
if not cur then
|
||||
return nil, "'{' without an item at byte " .. s - 1
|
||||
end
|
||||
if #stack > 64 then
|
||||
return nil, "limit: nesting too deep"
|
||||
end
|
||||
local kids: { Item } = {}
|
||||
cur.kids = kids
|
||||
table.insert(stack, kids)
|
||||
cur = nil
|
||||
p = s + 1
|
||||
elseif c == 125 then -- '}'
|
||||
if #stack == 1 then
|
||||
return nil, "unbalanced '}' at byte " .. s - 1
|
||||
end
|
||||
table.remove(stack)
|
||||
cur = nil
|
||||
p = s + 1
|
||||
else
|
||||
local e = find(text, "[ \t\r\n{}\"]", s) or (n + 1)
|
||||
local word = sub(text, s, e - 1)
|
||||
if c == 42 then -- '*'
|
||||
count += 1
|
||||
if count > maxItems then
|
||||
return nil, "limit: over " .. maxItems .. " items"
|
||||
end
|
||||
local it: Item = { key = sub(word, 2), args = {}, kids = nil, at = s - 1 }
|
||||
table.insert(stack[#stack], it)
|
||||
cur = it
|
||||
elseif cur then
|
||||
table.insert(cur.args, word)
|
||||
end
|
||||
p = e
|
||||
end
|
||||
end
|
||||
return root, nil
|
||||
end
|
||||
|
||||
return Parse
|
||||
@@ -0,0 +1,461 @@
|
||||
--!strict
|
||||
-- model/ase/Write: a Scene as ASE text (3DSMAX_ASCIIEXPORT 200). Vertices are written in world space with each node's
|
||||
-- world NODE_TM; every scene material is a sub-material of one Multi/Sub-Object material (faces pick it by
|
||||
-- *MESH_MTLID). Triangles, strips, fans and polygons (fanned) are written; points and lines are skipped.
|
||||
local Types = require("@rosetta/Types")
|
||||
local Mat4 = require("@rosetta/codecs/model/Mat4")
|
||||
|
||||
local Write = {}
|
||||
|
||||
local S = Types.Scene
|
||||
|
||||
local function f(v: number): string
|
||||
if v ~= v or v == math.huge or v == -math.huge then
|
||||
return "0.0000"
|
||||
end
|
||||
if v == math.floor(v) and math.abs(v) < 1e15 then
|
||||
return string.format("%.4f", v)
|
||||
end
|
||||
return string.format("%.9g", v)
|
||||
end
|
||||
|
||||
local function q(s: string): string
|
||||
return "\"" .. string.gsub(s, "\"", "'") .. "\""
|
||||
end
|
||||
|
||||
type Out = { lines: { string } }
|
||||
|
||||
local function put(out: Out, depth: number, s: string)
|
||||
table.insert(out.lines, string.rep("\t", depth) .. s)
|
||||
end
|
||||
|
||||
local function tri(prim: any): { number }?
|
||||
local pos = prim.attributes and prim.attributes.POSITION
|
||||
if type(pos) ~= "table" then
|
||||
return nil
|
||||
end
|
||||
local n = pos.count
|
||||
local idx: { number } = {}
|
||||
if prim.indices then
|
||||
local rd = S.reader(prim.indices)
|
||||
if not rd then
|
||||
return nil
|
||||
end
|
||||
for i = 0, prim.indices.count - 1 do
|
||||
idx[i + 1] = rd(i, 0)
|
||||
end
|
||||
else
|
||||
for i = 0, n - 1 do
|
||||
idx[i + 1] = i
|
||||
end
|
||||
end
|
||||
local mode = prim.mode or "triangles"
|
||||
local out: { number } = {}
|
||||
if mode == "triangles" then
|
||||
for i = 1, #idx - 2, 3 do
|
||||
table.insert(out, idx[i])
|
||||
table.insert(out, idx[i + 1])
|
||||
table.insert(out, idx[i + 2])
|
||||
end
|
||||
elseif mode == "triangle_strip" then
|
||||
for i = 1, #idx - 2 do
|
||||
if i % 2 == 1 then
|
||||
table.insert(out, idx[i])
|
||||
table.insert(out, idx[i + 1])
|
||||
else
|
||||
table.insert(out, idx[i + 1])
|
||||
table.insert(out, idx[i])
|
||||
end
|
||||
table.insert(out, idx[i + 2])
|
||||
end
|
||||
elseif mode == "triangle_fan" then
|
||||
for i = 2, #idx - 1 do
|
||||
table.insert(out, idx[1])
|
||||
table.insert(out, idx[i])
|
||||
table.insert(out, idx[i + 1])
|
||||
end
|
||||
elseif mode == "polygons" and prim.faces then
|
||||
local rd = S.reader(prim.faces)
|
||||
local at = 1
|
||||
if rd then
|
||||
for fi = 0, prim.faces.count - 1 do
|
||||
local size = rd(fi, 0)
|
||||
for k = 1, size - 2 do
|
||||
table.insert(out, idx[at])
|
||||
table.insert(out, idx[at + k])
|
||||
table.insert(out, idx[at + k + 1])
|
||||
end
|
||||
at += size
|
||||
end
|
||||
end
|
||||
else
|
||||
return nil
|
||||
end
|
||||
for _, v in out do
|
||||
if v < 0 or v >= n then
|
||||
return nil
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local function material(out: Out, d: number, key: string, index: number, m: any)
|
||||
put(out, d, "*" .. key .. " " .. index .. " {")
|
||||
local c = if type(m.baseColor) == "table" then m.baseColor else { 1, 1, 1, 1 }
|
||||
local x = if type(m.extras) == "table" then m.extras else {}
|
||||
local amb = if type(x.ambient) == "table" then x.ambient else { c[1] or 1, c[2] or 1, c[3] or 1 }
|
||||
local spec = if type(x.specular) == "table" then x.specular else { 0.9, 0.9, 0.9 }
|
||||
put(out, d + 1, "*MATERIAL_NAME " .. q(if type(m.name) == "string" then m.name else "Material #" .. index))
|
||||
put(out, d + 1, "*MATERIAL_CLASS \"Standard\"")
|
||||
put(out, d + 1, "*MATERIAL_AMBIENT " .. f(amb[1] or 0) .. "\t" .. f(amb[2] or 0) .. "\t" .. f(amb[3] or 0))
|
||||
put(out, d + 1, "*MATERIAL_DIFFUSE " .. f(c[1] or 1) .. "\t" .. f(c[2] or 1) .. "\t" .. f(c[3] or 1))
|
||||
put(out, d + 1, "*MATERIAL_SPECULAR " .. f(spec[1] or 0) .. "\t" .. f(spec[2] or 0) .. "\t" .. f(spec[3] or 0))
|
||||
put(out, d + 1, "*MATERIAL_SHINE " .. f(if type(x.shininess) == "number" then x.shininess else 1 - (m.roughness or 1)))
|
||||
put(out, d + 1, "*MATERIAL_SHINESTRENGTH " .. f(if type(x.shineStrength) == "number" then x.shineStrength else 0))
|
||||
put(out, d + 1, "*MATERIAL_TRANSPARENCY " .. f(1 - (c[4] or 1)))
|
||||
put(out, d + 1, "*MATERIAL_WIRESIZE 1.0000")
|
||||
if m.doubleSided then
|
||||
put(out, d + 1, "*MATERIAL_TWOSIDED")
|
||||
end
|
||||
put(out, d + 1, "*MATERIAL_SHADING Blinn")
|
||||
put(out, d + 1, "*MATERIAL_SELFILLUM " .. f(if type(x.selfIllum) == "number" then x.selfIllum else 0))
|
||||
local ref = m.baseColorTexture
|
||||
local tex = if type(ref) == "table" and type(m._textures) == "table" then m._textures[ref.texture] else nil
|
||||
if tex and type(tex.uri) == "string" then
|
||||
local tr = if type(ref.transform) == "table" then ref.transform else {}
|
||||
local off = if type(tr.offset) == "table" then tr.offset else { 0, 0 }
|
||||
local sc = if type(tr.scale) == "table" then tr.scale else { 1, 1 }
|
||||
put(out, d + 1, "*MAP_DIFFUSE {")
|
||||
put(out, d + 2, "*MAP_NAME " .. q(tex.name or tex.uri))
|
||||
put(out, d + 2, "*MAP_CLASS \"Bitmap\"")
|
||||
put(out, d + 2, "*MAP_SUBNO 1")
|
||||
put(out, d + 2, "*MAP_AMOUNT 1.0000")
|
||||
put(out, d + 2, "*BITMAP " .. q(tex.uri))
|
||||
put(out, d + 2, "*MAP_TYPE Screen")
|
||||
put(out, d + 2, "*UVW_U_OFFSET " .. f(off[1] or 0))
|
||||
put(out, d + 2, "*UVW_V_OFFSET " .. f(off[2] or 0))
|
||||
put(out, d + 2, "*UVW_U_TILING " .. f(sc[1] or 1))
|
||||
put(out, d + 2, "*UVW_V_TILING " .. f(sc[2] or 1))
|
||||
put(out, d + 2, "*UVW_ANGLE " .. f(tr.rotation or 0))
|
||||
put(out, d + 1, "}")
|
||||
end
|
||||
put(out, d, "}")
|
||||
end
|
||||
|
||||
local function nodeTm(out: Out, name: string, m: { number })
|
||||
put(out, 1, "*NODE_TM {")
|
||||
put(out, 2, "*NODE_NAME " .. q(name))
|
||||
put(out, 2, "*INHERIT_POS 0 0 0")
|
||||
put(out, 2, "*INHERIT_ROT 0 0 0")
|
||||
put(out, 2, "*INHERIT_SCL 0 0 0")
|
||||
for r = 0, 3 do
|
||||
put(out, 2, "*TM_ROW" .. r .. " " .. f(m[r * 4 + 1]) .. "\t" .. f(m[r * 4 + 2]) .. "\t" .. f(m[r * 4 + 3]))
|
||||
end
|
||||
local t, r, s = Mat4.decompose(m)
|
||||
local w = math.clamp(r[4], -1, 1)
|
||||
local angle = 2 * math.acos(w)
|
||||
local sn = math.sqrt(math.max(0, 1 - w * w))
|
||||
local ax = if sn > 1e-9 then { r[1] / sn, r[2] / sn, r[3] / sn } else { 0, 0, 0 }
|
||||
put(out, 2, "*TM_POS " .. f(t[1]) .. "\t" .. f(t[2]) .. "\t" .. f(t[3]))
|
||||
put(out, 2, "*TM_ROTAXIS " .. f(ax[1]) .. "\t" .. f(ax[2]) .. "\t" .. f(ax[3]))
|
||||
put(out, 2, "*TM_ROTANGLE " .. f(angle))
|
||||
put(out, 2, "*TM_SCALE " .. f(s[1]) .. "\t" .. f(s[2]) .. "\t" .. f(s[3]))
|
||||
put(out, 2, "*TM_SCALEAXIS 0.0000\t0.0000\t0.0000")
|
||||
put(out, 2, "*TM_SCALEAXISANG 0.0000")
|
||||
put(out, 1, "}")
|
||||
end
|
||||
|
||||
local function vec(rd: ((number, number) -> number)?, i: number, n: number, default: number): { number }
|
||||
local v = {}
|
||||
for c = 0, n - 1 do
|
||||
v[c + 1] = if rd then rd(i, c) else default
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
local function geom(out: Out, scene: any, mesh: any, world: { number }, matBase: { [number]: number }, hasMulti: boolean): boolean
|
||||
local P: { number } = {}
|
||||
local N: { number } = {}
|
||||
local T: { [number]: { number } } = {}
|
||||
local C: { number } = {}
|
||||
local F: { number } = {}
|
||||
local M: { number } = {}
|
||||
local hasN, hasC = false, false
|
||||
local uvSets: { [number]: boolean } = {}
|
||||
for _, prim in mesh.primitives or {} do
|
||||
for name in prim.attributes or {} do
|
||||
local k = string.match(name, "^TEXCOORD_(%d)$")
|
||||
if k then
|
||||
uvSets[tonumber(k) :: number] = true
|
||||
end
|
||||
end
|
||||
end
|
||||
local nm = Mat4.normalMatrix(world)
|
||||
for _, prim in mesh.primitives or {} do
|
||||
local idx = tri(prim)
|
||||
if idx then
|
||||
local a = prim.attributes
|
||||
local base = #P // 3
|
||||
local rp = S.reader(a.POSITION)
|
||||
local rn = if a.NORMAL then S.reader(a.NORMAL) else nil
|
||||
local rc = if a.COLOR_0 then S.reader(a.COLOR_0) else nil
|
||||
local cc = if a.COLOR_0 then a.COLOR_0.components else 0
|
||||
if rp then
|
||||
for i = 0, a.POSITION.count - 1 do
|
||||
local x, y, z = Mat4.point(world, rp(i, 0), rp(i, 1), rp(i, 2))
|
||||
table.insert(P, x)
|
||||
table.insert(P, y)
|
||||
table.insert(P, z)
|
||||
local nv = vec(rn, i, 3, 0)
|
||||
local tx = nm[1] * nv[1] + nm[5] * nv[2] + nm[9] * nv[3]
|
||||
local ty = nm[2] * nv[1] + nm[6] * nv[2] + nm[10] * nv[3]
|
||||
local tz = nm[3] * nv[1] + nm[7] * nv[2] + nm[11] * nv[3]
|
||||
local len = math.sqrt(tx * tx + ty * ty + tz * tz)
|
||||
if len > 0 then
|
||||
tx, ty, tz = tx / len, ty / len, tz / len
|
||||
end
|
||||
table.insert(N, tx)
|
||||
table.insert(N, ty)
|
||||
table.insert(N, tz)
|
||||
for k in uvSets do
|
||||
local acc = a["TEXCOORD_" .. k]
|
||||
local rt = if acc then S.reader(acc) else nil
|
||||
local list = T[k] or {}
|
||||
T[k] = list
|
||||
table.insert(list, if rt then rt(i, 0) else 0)
|
||||
table.insert(list, if rt then 1 - rt(i, 1) else 0)
|
||||
end
|
||||
local col = vec(rc, i, math.max(cc, 3), 1)
|
||||
table.insert(C, col[1])
|
||||
table.insert(C, col[2])
|
||||
table.insert(C, col[3])
|
||||
end
|
||||
hasN = hasN or rn ~= nil
|
||||
hasC = hasC or rc ~= nil
|
||||
local mid = if prim.material and matBase[prim.material] then matBase[prim.material] else (if hasMulti then #scene.materials else 0)
|
||||
for i = 1, #idx, 3 do
|
||||
table.insert(F, base + idx[i])
|
||||
table.insert(F, base + idx[i + 1])
|
||||
table.insert(F, base + idx[i + 2])
|
||||
table.insert(M, mid)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if #F == 0 then
|
||||
return false
|
||||
end
|
||||
local nv, nf = #P // 3, #F // 3
|
||||
put(out, 1, "*MESH {")
|
||||
put(out, 2, "*TIMEVALUE 0")
|
||||
put(out, 2, "*MESH_NUMVERTEX " .. nv)
|
||||
put(out, 2, "*MESH_NUMFACES " .. nf)
|
||||
put(out, 2, "*MESH_VERTEX_LIST {")
|
||||
for i = 0, nv - 1 do
|
||||
put(out, 3, "*MESH_VERTEX " .. i .. "\t" .. f(P[i * 3 + 1]) .. "\t" .. f(P[i * 3 + 2]) .. "\t" .. f(P[i * 3 + 3]))
|
||||
end
|
||||
put(out, 2, "}")
|
||||
put(out, 2, "*MESH_FACE_LIST {")
|
||||
for i = 0, nf - 1 do
|
||||
put(out, 3, string.format("*MESH_FACE %d: A: %d B: %d C: %d AB: 1 BC: 1 CA: 1\t*MESH_SMOOTHING 1\t*MESH_MTLID %d", i, F[i * 3 + 1], F[i * 3 + 2], F[i * 3 + 3], M[i + 1]))
|
||||
end
|
||||
put(out, 2, "}")
|
||||
local function uvBlock(d: number, list: { number })
|
||||
put(out, d, "*MESH_NUMTVERTEX " .. nv)
|
||||
put(out, d, "*MESH_TVERTLIST {")
|
||||
for i = 0, nv - 1 do
|
||||
put(out, d + 1, "*MESH_TVERT " .. i .. "\t" .. f(list[i * 2 + 1] or 0) .. "\t" .. f(list[i * 2 + 2] or 0) .. "\t0.0000")
|
||||
end
|
||||
put(out, d, "}")
|
||||
put(out, d, "*MESH_NUMTVFACES " .. nf)
|
||||
put(out, d, "*MESH_TFACELIST {")
|
||||
for i = 0, nf - 1 do
|
||||
put(out, d + 1, "*MESH_TFACE " .. i .. "\t" .. F[i * 3 + 1] .. "\t" .. F[i * 3 + 2] .. "\t" .. F[i * 3 + 3])
|
||||
end
|
||||
put(out, d, "}")
|
||||
end
|
||||
if T[0] then
|
||||
uvBlock(2, T[0])
|
||||
end
|
||||
for k = 1, 7 do
|
||||
if T[k] then
|
||||
put(out, 2, "*MESH_MAPPINGCHANNEL " .. (k + 1) .. " {")
|
||||
uvBlock(3, T[k])
|
||||
put(out, 2, "}")
|
||||
end
|
||||
end
|
||||
if hasC then
|
||||
put(out, 2, "*MESH_NUMCVERTEX " .. nv)
|
||||
put(out, 2, "*MESH_CVERTLIST {")
|
||||
for i = 0, nv - 1 do
|
||||
put(out, 3, "*MESH_VERTCOL " .. i .. "\t" .. f(C[i * 3 + 1]) .. "\t" .. f(C[i * 3 + 2]) .. "\t" .. f(C[i * 3 + 3]))
|
||||
end
|
||||
put(out, 2, "}")
|
||||
put(out, 2, "*MESH_NUMCVFACES " .. nf)
|
||||
put(out, 2, "*MESH_CFACELIST {")
|
||||
for i = 0, nf - 1 do
|
||||
put(out, 3, "*MESH_CFACE " .. i .. "\t" .. F[i * 3 + 1] .. "\t" .. F[i * 3 + 2] .. "\t" .. F[i * 3 + 3])
|
||||
end
|
||||
put(out, 2, "}")
|
||||
end
|
||||
if hasN then
|
||||
put(out, 2, "*MESH_NORMALS {")
|
||||
for i = 0, nf - 1 do
|
||||
local a, b, c = F[i * 3 + 1], F[i * 3 + 2], F[i * 3 + 3]
|
||||
local ux, uy, uz = P[b * 3 + 1] - P[a * 3 + 1], P[b * 3 + 2] - P[a * 3 + 2], P[b * 3 + 3] - P[a * 3 + 3]
|
||||
local vx, vy, vz = P[c * 3 + 1] - P[a * 3 + 1], P[c * 3 + 2] - P[a * 3 + 2], P[c * 3 + 3] - P[a * 3 + 3]
|
||||
local nx, ny, nz = uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx
|
||||
local len = math.sqrt(nx * nx + ny * ny + nz * nz)
|
||||
if len > 0 then
|
||||
nx, ny, nz = nx / len, ny / len, nz / len
|
||||
end
|
||||
put(out, 3, "*MESH_FACENORMAL " .. i .. "\t" .. f(nx) .. "\t" .. f(ny) .. "\t" .. f(nz))
|
||||
for _, v in { a, b, c } do
|
||||
put(out, 4, "*MESH_VERTEXNORMAL " .. v .. "\t" .. f(N[v * 3 + 1]) .. "\t" .. f(N[v * 3 + 2]) .. "\t" .. f(N[v * 3 + 3]))
|
||||
end
|
||||
end
|
||||
put(out, 2, "}")
|
||||
end
|
||||
put(out, 1, "}")
|
||||
return true
|
||||
end
|
||||
|
||||
function Write.scene(scene: any, opts: any): (string?, string?)
|
||||
local out: Out = { lines = {} }
|
||||
local src = if type(scene.meta) == "table" and type(scene.meta.source) == "table" then scene.meta.source else {}
|
||||
put(out, 0, "*3DSMAX_ASCIIEXPORT\t200")
|
||||
put(out, 0, "*COMMENT " .. q(if opts and type(opts.comment) == "string" then opts.comment else "AsciiExport-Version 2,00 - rosetta"))
|
||||
put(out, 0, "*SCENE {")
|
||||
put(out, 1, "*SCENE_FILENAME \"\"")
|
||||
put(out, 1, "*SCENE_FIRSTFRAME " .. math.floor(tonumber(src.firstFrame) or 0))
|
||||
put(out, 1, "*SCENE_LASTFRAME " .. math.floor(tonumber(src.lastFrame) or 100))
|
||||
put(out, 1, "*SCENE_FRAMESPEED " .. math.floor(tonumber(src.frameSpeed) or 30))
|
||||
put(out, 1, "*SCENE_TICKSPERFRAME " .. math.floor(tonumber(src.ticksPerFrame) or 160))
|
||||
put(out, 1, "*SCENE_BACKGROUND_STATIC 0.0000\t0.0000\t0.0000")
|
||||
put(out, 1, "*SCENE_AMBIENT_STATIC 0.0000\t0.0000\t0.0000")
|
||||
put(out, 0, "}")
|
||||
local mats = scene.materials or {}
|
||||
local nmat = #mats
|
||||
local matBase: { [number]: number } = {}
|
||||
-- a default sub-material for primitives without one, when other primitives have one
|
||||
local needDefault = false
|
||||
for _, mesh in scene.meshes or {} do
|
||||
for _, prim in mesh.primitives or {} do
|
||||
if not (prim.material and mats[prim.material]) then
|
||||
needDefault = true
|
||||
end
|
||||
end
|
||||
end
|
||||
put(out, 0, "*MATERIAL_LIST {")
|
||||
if nmat > 0 then
|
||||
put(out, 1, "*MATERIAL_COUNT 1")
|
||||
put(out, 1, "*MATERIAL 0 {")
|
||||
put(out, 2, "*MATERIAL_NAME \"Multimaterial\"")
|
||||
put(out, 2, "*MATERIAL_CLASS \"Multi/Sub-Object\"")
|
||||
put(out, 2, "*NUMSUBMTLS " .. (nmat + (if needDefault then 1 else 0)))
|
||||
for i, m in mats do
|
||||
matBase[i] = i - 1
|
||||
local mm = table.clone(m)
|
||||
mm._textures = scene.textures
|
||||
material(out, 2, "SUBMATERIAL", i - 1, mm)
|
||||
end
|
||||
if needDefault then
|
||||
material(out, 2, "SUBMATERIAL", nmat, S.material("Default"))
|
||||
end
|
||||
put(out, 1, "}")
|
||||
else
|
||||
put(out, 1, "*MATERIAL_COUNT 0")
|
||||
end
|
||||
put(out, 0, "}")
|
||||
-- world matrices and unique names
|
||||
local nodes = scene.nodes or {}
|
||||
local worlds: { [number]: { number } } = {}
|
||||
local parentOf: { [number]: number } = {}
|
||||
local order: { number } = {}
|
||||
local function walk(i: number, parent: { number }, depth: number)
|
||||
local node = nodes[i]
|
||||
if not node or worlds[i] or depth > 256 then
|
||||
return
|
||||
end
|
||||
worlds[i] = S.mul(parent, S.nodeMatrix(node))
|
||||
table.insert(order, i)
|
||||
for _, c in node.children or {} do
|
||||
if nodes[c] and not worlds[c] then
|
||||
parentOf[c] = i
|
||||
walk(c, worlds[i], depth + 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
local roots = scene.roots or {}
|
||||
if #roots == 0 then
|
||||
for i in nodes do
|
||||
table.insert(roots, i)
|
||||
end
|
||||
end
|
||||
for _, r in roots do
|
||||
walk(r, S.IDENTITY, 0)
|
||||
end
|
||||
local names: { [number]: string } = {}
|
||||
local used: { [string]: boolean } = {}
|
||||
for _, i in order do
|
||||
local base = if type(nodes[i].name) == "string" and nodes[i].name ~= "" then nodes[i].name else "Object" .. i
|
||||
local name, k = base, 1
|
||||
while used[name] do
|
||||
k += 1
|
||||
name = base .. "_" .. k
|
||||
end
|
||||
used[name] = true
|
||||
names[i] = name
|
||||
end
|
||||
for _, i in order do
|
||||
local node = nodes[i]
|
||||
local w = worlds[i]
|
||||
local mesh = if node.mesh then (scene.meshes or {})[node.mesh] else nil
|
||||
local cam = if node.camera then (scene.cameras or {})[node.camera] else nil
|
||||
local li = if node.light then (scene.lights or {})[node.light] else nil
|
||||
local key = if mesh then "GEOMOBJECT" elseif cam then "CAMERAOBJECT" elseif li then "LIGHTOBJECT" else "HELPEROBJECT"
|
||||
put(out, 0, "*" .. key .. " {")
|
||||
put(out, 1, "*NODE_NAME " .. q(names[i]))
|
||||
if parentOf[i] then
|
||||
put(out, 1, "*NODE_PARENT " .. q(names[parentOf[i]]))
|
||||
end
|
||||
if key == "HELPEROBJECT" then
|
||||
put(out, 1, "*HELPER_CLASS \"Dummy\"")
|
||||
elseif key == "CAMERAOBJECT" then
|
||||
put(out, 1, "*CAMERA_TYPE Free")
|
||||
elseif key == "LIGHTOBJECT" then
|
||||
put(out, 1, "*LIGHT_TYPE " .. (if li.type == "spot" then "Free" elseif li.type == "directional" then "Directional" else "Omni"))
|
||||
end
|
||||
nodeTm(out, names[i], w)
|
||||
if mesh then
|
||||
geom(out, scene, mesh, w, matBase, nmat > 0)
|
||||
put(out, 1, "*PROP_MOTIONBLUR 0")
|
||||
put(out, 1, "*PROP_CASTSHADOW 1")
|
||||
put(out, 1, "*PROP_RECVSHADOW 1")
|
||||
if nmat > 0 then
|
||||
put(out, 1, "*MATERIAL_REF 0")
|
||||
end
|
||||
elseif cam then
|
||||
put(out, 1, "*CAMERA_SETTINGS {")
|
||||
put(out, 2, "*TIMEVALUE 0")
|
||||
put(out, 2, "*CAMERA_NEAR " .. f(cam.znear or 0))
|
||||
put(out, 2, "*CAMERA_FAR " .. f(cam.zfar or 1000))
|
||||
put(out, 2, "*CAMERA_FOV " .. f(cam.yfov or math.rad(45)))
|
||||
put(out, 2, "*CAMERA_TDIST 100.0000")
|
||||
put(out, 1, "}")
|
||||
elseif li then
|
||||
local c = if type(li.color) == "table" then li.color else { 1, 1, 1 }
|
||||
put(out, 1, "*LIGHT_SETTINGS {")
|
||||
put(out, 2, "*TIMEVALUE 0")
|
||||
put(out, 2, "*LIGHT_COLOR " .. f(c[1] or 1) .. "\t" .. f(c[2] or 1) .. "\t" .. f(c[3] or 1))
|
||||
put(out, 2, "*LIGHT_INTENS " .. f(li.intensity or 1))
|
||||
put(out, 2, "*LIGHT_HOTSPOT " .. f(math.deg(li.innerCone or 0) * 2))
|
||||
put(out, 2, "*LIGHT_FALLOFF " .. f(math.deg(li.outerCone or math.pi / 4) * 2))
|
||||
put(out, 1, "}")
|
||||
end
|
||||
put(out, 0, "}")
|
||||
end
|
||||
return table.concat(out.lines, "\n") .. "\n", nil
|
||||
end
|
||||
|
||||
return Write
|
||||
@@ -0,0 +1,121 @@
|
||||
--!strict
|
||||
-- model/ase: 3ds Max ASCII Scene Export (*3DSMAX_ASCIIEXPORT 200), read + write; checked against assimp's ASE loader.
|
||||
-- Supports: geometry objects (texture channels, vertex colours, explicit normals, Multi/Sub-Object materials by
|
||||
-- *MESH_MTLID), NODE_TM hierarchies, helpers, cameras, lights, material maps (paths; data through opts.resolve),
|
||||
-- UTF-16 files. Not supported: *TM_ANIMATION tracks (meta.source.animated is set), shapes (nodes only), bones.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Text = require("@rosetta/codecs/text/Text")
|
||||
local Parse = require("@rosetta/model/ase/Parse")
|
||||
local Build = require("@rosetta/model/ase/Build")
|
||||
local Write = require("@rosetta/model/ase/Write")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Ase = {
|
||||
name = "ase",
|
||||
category = "model",
|
||||
extensions = { "ase", "ask" },
|
||||
mime = { "model/x-ase", "text/plain" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/text/Text", "codecs/model/Mat4" },
|
||||
caps = { decode = true, encode = true, text = true, materials = true, cameras = true, lights = true, vertexColors = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- default 2^28
|
||||
maxItems: number?, -- default 2^24 (*KEY items)
|
||||
maxVertices: number?, -- default 2^24 (per object)
|
||||
resolve: ((string) -> buffer?)?, -- texture bitmaps by path
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
comment: string?,
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local MAGIC = "*3DSMAX_ASCIIEXPORT"
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
local function utf16(r: Bytes.Reader): (boolean?, boolean)
|
||||
local h = r:peek(2)
|
||||
if h == "\255\254" then
|
||||
return true, false
|
||||
elseif h == "\254\255" then
|
||||
return true, true
|
||||
elseif #h == 2 and string.byte(h, 2) == 0 and string.byte(h, 1) == 42 then
|
||||
return true, false
|
||||
elseif #h == 2 and string.byte(h, 1) == 0 and string.byte(h, 2) == 42 then
|
||||
return true, true
|
||||
end
|
||||
return nil, false
|
||||
end
|
||||
|
||||
local function text(r: Bytes.Reader, n: number?): string
|
||||
local wide, be = utf16(r)
|
||||
local len = r.limit - r.pos
|
||||
if n then
|
||||
len = math.min(len, n)
|
||||
end
|
||||
if wide then
|
||||
local skip = if r:peek(2) == "\255\254" or r:peek(2) == "\254\255" then 2 else 0
|
||||
return Text.utf16(r.buf, r.pos + skip, len - skip, be)
|
||||
end
|
||||
return Text.toUtf8(buffer.readstring(r.buf, r.pos, len))
|
||||
end
|
||||
|
||||
function Ase.sniff(r: Bytes.Reader): number
|
||||
local h = string.match(text(r, 256), "^%s*(%*[%w_]+)")
|
||||
if h == MAGIC then
|
||||
return 1
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
function Ase.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
||||
local o: any = opts or {}
|
||||
if r.limit - r.pos > (o.maxBytes or 2 ^ 28) then
|
||||
return nil, Err.new(Err.LIMIT, "file is over the byte limit")
|
||||
end
|
||||
local s = text(r)
|
||||
if not string.match(s, "^%s*%*") then
|
||||
return nil, Err.new(Err.CORRUPT, "not an ASE file")
|
||||
end
|
||||
local ok, root, msg = pcall(Parse.tree, s, o.maxItems or 2 ^ 24)
|
||||
if not ok then
|
||||
return nil, Err.new(Err.CORRUPT, "bad data: " .. tostring(root))
|
||||
end
|
||||
if not root then
|
||||
return nil, Err.from(msg)
|
||||
end
|
||||
local ok2, scene, msg2 = pcall(Build.scene, root, o)
|
||||
if not ok2 then
|
||||
return nil, Err.from(tostring(scene))
|
||||
end
|
||||
if not scene then
|
||||
return nil, Err.from(msg2)
|
||||
end
|
||||
if #scene.nodes == 0 and (scene.meta.source :: any).version == nil then
|
||||
return nil, Err.new(Err.CORRUPT, "no *3DSMAX_ASCIIEXPORT header and no objects")
|
||||
end
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
function Ase.encode(scene: Types.Scene, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
if type(scene) ~= "table" or type(scene.nodes) ~= "table" then
|
||||
return nil, Err.new(Err.ARG, "needs a Scene")
|
||||
end
|
||||
local ok, s, msg = pcall(Write.scene, scene, opts)
|
||||
if not ok then
|
||||
return nil, Err.new(Err.ARG, "bad scene: " .. tostring(s))
|
||||
end
|
||||
if not s then
|
||||
return nil, Err.new(Err.ARG, msg or "bad scene")
|
||||
end
|
||||
return buffer.fromstring(s), nil
|
||||
end
|
||||
|
||||
return Ase
|
||||
@@ -0,0 +1,256 @@
|
||||
-- model/ase tests: assimp's ASE models and a hand-written file against assimp (meshes, faces, corners, cameras, lights,
|
||||
-- world bounds); the hand file's values; round trips; assimp reading our output. Fixtures: tools/fixtures/ase.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@rosetta")
|
||||
local Ase = require("@rosetta/model/ase")
|
||||
local MeshOps = require("@rosetta/ops/MeshOps")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Cases = require("@tests/gen/ase_cases")
|
||||
local Xcheck = require("@tests/gen/ase_xcheck")
|
||||
|
||||
local t = T.suite("ase")
|
||||
local S = Types.Scene
|
||||
|
||||
local function decode(data: any, opts: any?): (any, any)
|
||||
local b = if type(data) == "string" then buffer.fromstring(data) else data
|
||||
return Ase.decode(Bytes.reader(b), opts)
|
||||
end
|
||||
|
||||
t:eq(Ase.name, "ase", "name")
|
||||
t:eq(Ase.category, "model", "category")
|
||||
t:check(Ase.caps.decode and Ase.caps.encode, "contract")
|
||||
|
||||
local function near(a: number, b: number, eps: number): boolean
|
||||
return math.abs(a - b) <= eps * math.max(1, math.abs(a), math.abs(b))
|
||||
end
|
||||
|
||||
local function counts(scene: any): (number, number, number)
|
||||
local prims, faces, corners = 0, 0, 0
|
||||
for _, pl in S.flatten(scene) do
|
||||
prims += 1
|
||||
faces += pl.primitive.indices.count // 3
|
||||
corners += pl.primitive.indices.count
|
||||
end
|
||||
return prims, faces, corners
|
||||
end
|
||||
|
||||
-- assimp's ASE root rotates Z-up to Y-up: (x, z, -y)
|
||||
local function boundsMatch(lo: { number }, hi: { number }, want: any, eps: number): boolean
|
||||
local a, b = { lo[1], lo[3], -hi[2] }, { hi[1], hi[3], -lo[2] }
|
||||
for k = 1, 3 do
|
||||
if not near(a[k], want.min[k], eps) or not near(b[k], want.max[k], eps) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local function bstr(lo: any, hi: any): string
|
||||
if not lo then
|
||||
return "-"
|
||||
end
|
||||
return string.format("(%.4g %.4g %.4g) (%.4g %.4g %.4g)", lo[1], lo[2], lo[3], hi[1], hi[2], hi[3])
|
||||
end
|
||||
|
||||
-- assimp mis-reads the UTF-16 copies of ThreeCubesGreen (one mesh of 8 faces, a bone): they are compared with the
|
||||
-- UTF-8 file instead
|
||||
local UTF16 = { ["assimp/models/ThreeCubesGreen_UTF16BE.ASE"] = true, ["assimp/models/ThreeCubesGreen_UTF16LE.ASE"] = true }
|
||||
|
||||
local scenes: { [number]: any } = {}
|
||||
local byName: { [string]: any } = {}
|
||||
local samples = {}
|
||||
for i, c in Cases do
|
||||
local scene, err = decode(c.data)
|
||||
t:check(scene, c.name .. ": decode: " .. tostring(err))
|
||||
t:eq(Ase.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, c.name .. ": sniff")
|
||||
scenes[i] = scene
|
||||
byName[c.name] = scene
|
||||
if #c.data < 60000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
local a = c.assimp
|
||||
if scene and not a.failed and not UTF16[c.name] then
|
||||
local prims, faces, corners = counts(scene)
|
||||
t:eq(prims, a.meshes, c.name .. ": meshes (assimp)")
|
||||
t:eq(faces, a.faces, c.name .. ": faces (assimp)")
|
||||
t:eq(corners, a.vertices, c.name .. ": corners (assimp)")
|
||||
t:eq(#scene.cameras, a.cameras, c.name .. ": cameras (assimp)")
|
||||
t:eq(#scene.lights, a.lights, c.name .. ": lights (assimp)")
|
||||
if a.min then
|
||||
local lo, hi = MeshOps.bounds(MeshOps.merge(scene))
|
||||
t:check(lo and boundsMatch(lo, hi, a, 1e-3), c.name .. ": world bounds (assimp) " .. bstr(lo, hi) .. " want " .. bstr(a.min, a.max))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
do
|
||||
local u8 = byName["assimp/models/ThreeCubesGreen.ASE"]
|
||||
for name in UTF16 do
|
||||
local s = byName[name]
|
||||
if s and u8 then
|
||||
local p1, f1 = counts(s)
|
||||
local p2, f2 = counts(u8)
|
||||
local lo1, hi1 = MeshOps.bounds(MeshOps.merge(s))
|
||||
local lo2, hi2 = MeshOps.bounds(MeshOps.merge(u8))
|
||||
t:check(p1 == p2 and f1 == f2 and bstr(lo1, hi1) == bstr(lo2, hi2), name .. ": same scene as the UTF-8 file")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- the hand-written file
|
||||
do
|
||||
local s = byName["hand.ase"]
|
||||
t:eq(s.meta.up, "z", "hand: up")
|
||||
t:eq(s.meta.source.frameSpeed, 25, "hand: frame speed")
|
||||
t:eq(#s.materials, 2, "hand: sub-materials")
|
||||
local brick, glass = s.materials[1], s.materials[2]
|
||||
t:eq(brick.name, "Brick", "hand: material name")
|
||||
t:check(near(brick.baseColor[1], 0.8, 1e-6) and near(brick.baseColor[3], 0.2, 1e-6), "hand: diffuse")
|
||||
t:eq(s.textures[brick.baseColorTexture.texture].uri, "textures\\brick.png", "hand: bitmap path")
|
||||
t:check(brick.baseColorTexture.transform and brick.baseColorTexture.transform.scale[2] == 3, "hand: UVW tiling")
|
||||
t:check(glass.alphaMode == "blend" and near(glass.baseColor[4], 0.5, 1e-6) and glass.doubleSided, "hand: transparency, two-sided")
|
||||
local quad
|
||||
for _, n in s.nodes do
|
||||
if n.name == "Quad" then
|
||||
quad = n
|
||||
end
|
||||
end
|
||||
local pivot = s.nodes[s.roots[1]]
|
||||
t:eq(pivot.name, "Pivot", "hand: helper root")
|
||||
t:check(table.find(pivot.children, table.find(s.nodes, quad)) ~= nil, "hand: NODE_PARENT")
|
||||
-- local frame: Quad's world TM rotates 90 degrees about z at (10, 5, 0); relative to Pivot at (10, 0, 0)
|
||||
t:check(near(quad.matrix[13], 0, 1e-6) and near(quad.matrix[14], 5, 1e-6) and near(quad.matrix[2], 1, 1e-6), "hand: matrix relative to the parent")
|
||||
local mesh = s.meshes[quad.mesh]
|
||||
t:eq(#mesh.primitives, 2, "hand: one primitive per sub-material")
|
||||
local p1 = mesh.primitives[1]
|
||||
t:eq(p1.material, 1, "hand: MESH_MTLID 0")
|
||||
local P = S.reader(p1.attributes.POSITION) :: any
|
||||
-- vertex 1 (10, 7, 0) world -> local: inverse of (rot z 90, t (10, 5, 0)) = (2, 0, 0)
|
||||
t:check(near(P(1, 0), 2, 1e-6) and near(P(1, 1), 0, 1e-6), "hand: vertices in the node frame")
|
||||
local UV = S.reader(p1.attributes.TEXCOORD_0) :: any
|
||||
t:check(near(UV(1, 0), 1, 1e-6) and near(UV(1, 1), 1, 1e-6) and near(UV(2, 1), 0, 1e-6), "hand: UVs (v flipped)")
|
||||
local UV2 = S.reader(p1.attributes.TEXCOORD_1) :: any
|
||||
t:check(near(UV2(0, 0), 0.25, 1e-6) and near(UV2(0, 1), 0.25, 1e-6), "hand: mapping channel 2")
|
||||
local C = S.reader(p1.attributes.COLOR_0) :: any
|
||||
t:check(C(0, 0) == 1 and C(1, 1) == 1 and C(2, 0) == 1, "hand: vertex colours")
|
||||
local N = S.reader(p1.attributes.NORMAL) :: any
|
||||
-- (0, 0.6, 0.8) world under the inverse rotation: (0.6, 0, 0.8)
|
||||
t:check(near(N(2, 0), 0.6, 1e-6) and near(N(2, 2), 0.8, 1e-6), "hand: explicit vertex normals")
|
||||
t:eq(s.cameras[1].zfar, 500, "hand: camera far")
|
||||
t:check(s.lights[1].type == "spot" and s.lights[1].intensity == 2 and near(s.lights[1].outerCone, math.rad(30), 1e-9), "hand: spot light")
|
||||
end
|
||||
|
||||
-- round trips: ours -> ours keeps counts, bounds, materials, UVs
|
||||
local function sameScene(a: any, b: any, label: string)
|
||||
local p1, f1, c1 = counts(a)
|
||||
local p2, f2, c2 = counts(b)
|
||||
t:check(p1 == p2 and f1 == f2 and c1 == c2, label .. ": counts " .. p1 .. "/" .. f1 .. " vs " .. p2 .. "/" .. f2)
|
||||
local lo1, hi1 = MeshOps.bounds(MeshOps.merge(a))
|
||||
local lo2, hi2 = MeshOps.bounds(MeshOps.merge(b))
|
||||
if lo1 and lo2 then
|
||||
local ok = true
|
||||
for k = 1, 3 do
|
||||
ok = ok and near(lo1[k], lo2[k], 1e-5) and near(hi1[k], hi2[k], 1e-5)
|
||||
end
|
||||
t:check(ok, label .. ": bounds " .. bstr(lo1, hi1) .. " vs " .. bstr(lo2, hi2))
|
||||
end
|
||||
t:eq(#b.cameras, #a.cameras, label .. ": cameras")
|
||||
t:eq(#b.lights, #a.lights, label .. ": lights")
|
||||
end
|
||||
|
||||
for i, c in Cases do
|
||||
local s = scenes[i]
|
||||
if s then
|
||||
local out, err = Ase.encode(s)
|
||||
t:check(out, c.name .. ": encode: " .. tostring(err))
|
||||
if out then
|
||||
local back, berr = decode(out)
|
||||
t:check(back, c.name .. ": re-decode: " .. tostring(berr))
|
||||
if back then
|
||||
sameScene(s, back, c.name .. ": round trip")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
do
|
||||
local s = byName["hand.ase"]
|
||||
local back = decode(assert(Ase.encode(s)))
|
||||
local m = back.materials[1]
|
||||
t:eq(m.name, "Brick", "hand round trip: material")
|
||||
t:eq(back.textures[m.baseColorTexture.texture].uri, "textures\\brick.png", "hand round trip: bitmap")
|
||||
local q
|
||||
for _, n in back.nodes do
|
||||
if n.name == "Quad" then
|
||||
q = n
|
||||
end
|
||||
end
|
||||
local p = back.meshes[q.mesh].primitives[1]
|
||||
t:check(p.attributes.TEXCOORD_1 and p.attributes.COLOR_0 and p.attributes.NORMAL, "hand round trip: attributes")
|
||||
end
|
||||
|
||||
-- glTF -> ASE (any Scene): a mesh with no materials, an n-gon, a strip
|
||||
do
|
||||
local s = S.new()
|
||||
local P = S.accessor(nil, "f32", 3, 5)
|
||||
for i, v in { { 0, 0, 0 }, { 1, 0, 0 }, { 1, 1, 0 }, { 0.5, 1.5, 0 }, { 0, 1, 0 } } do
|
||||
for k = 1, 3 do
|
||||
buffer.writef32(P.data, ((i - 1) * 3 + k - 1) * 4, v[k])
|
||||
end
|
||||
end
|
||||
local faces = S.accessor(nil, "u32", 1, 1)
|
||||
buffer.writeu32(faces.data, 0, 5)
|
||||
s.meshes[1] = { primitives = { { attributes = { POSITION = P }, mode = "polygons", faces = faces } } }
|
||||
s.meshes[2] = { primitives = { { attributes = { POSITION = P }, mode = "triangle_strip" } } }
|
||||
s.nodes = { { name = "ngon", children = { 2 }, mesh = 1, translation = { 0, 0, 3 } }, { name = "ngon", children = {}, mesh = 2, scale = { 2, 2, 2 } } }
|
||||
s.roots = { 1 }
|
||||
local out = assert(Ase.encode(s))
|
||||
local back = assert(decode(out))
|
||||
local _, faces2 = counts(back)
|
||||
t:eq(faces2, 6, "n-gon (3) + strip (3) as triangles")
|
||||
t:eq(back.nodes[2].name, "ngon_2", "duplicate names made unique")
|
||||
local lo, hi = MeshOps.bounds(MeshOps.merge(back))
|
||||
t:check(near(lo[3], 3, 1e-6) and near(hi[1], 2, 1e-6), "child world transform " .. bstr(lo, hi))
|
||||
local via = F.save(s, nil, { format = "ase" })
|
||||
t:check(type(via) == "buffer" and F.detect(via) == "ase", "F.save / F.detect")
|
||||
end
|
||||
|
||||
-- assimp reads what we write
|
||||
for _, x in Xcheck do
|
||||
local c = Cases[x.case]
|
||||
local s = scenes[x.case]
|
||||
local a = x.assimp
|
||||
t:check(not a.failed, x.name .. " (" .. c.name .. "): assimp reads it")
|
||||
if s and not a.failed then
|
||||
local prims, faces = counts(s)
|
||||
t:eq(a.faces, faces, c.name .. ": assimp faces of ours")
|
||||
t:eq(a.meshes, prims, c.name .. ": assimp meshes of ours")
|
||||
t:eq(a.cameras, #s.cameras, c.name .. ": assimp cameras of ours")
|
||||
t:eq(a.lights, #s.lights, c.name .. ": assimp lights of ours")
|
||||
if a.min then
|
||||
local lo, hi = MeshOps.bounds(MeshOps.merge(s))
|
||||
t:check(lo and boundsMatch(lo, hi, a, 1e-3), c.name .. ": assimp bounds of ours " .. bstr(lo, hi) .. " vs " .. bstr(a.min, a.max))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e = decode("*3DSMAX_ASCIIEXPORT 200\n*GEOMOBJECT { *MESH { *MESH_NUMVERTEX 1 *MESH_NUMFACES 1 *MESH_FACE_LIST { *MESH_FACE 0: A: 0 B: 5 C: 0 } } }")
|
||||
t:check(e and e.code == Err.CORRUPT, "face index out of range: " .. tostring(e))
|
||||
local _, e2 = decode("*3DSMAX_ASCIIEXPORT 200\n}")
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "unbalanced brace")
|
||||
local _, e3 = decode("hello")
|
||||
t:check(e3 and e3.code == Err.CORRUPT, "not ASE")
|
||||
local _, e4 = decode(Cases[1].data, { maxItems = 10 })
|
||||
t:check(e4 and e4.code == Err.LIMIT, "item limit")
|
||||
local _, e5 = Ase.encode({} :: any)
|
||||
t:check(e5 and e5.code == Err.ARG, "encode arg")
|
||||
end
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Ase.decode(Bytes.reader(b))
|
||||
end, samples)
|
||||
t:done()
|
||||
@@ -0,0 +1,29 @@
|
||||
-- tools/fixtures/_ase_encode: run by tools/fixtures/ase.py (luau CLI, repo root): decodes every ASE case and writes it
|
||||
-- back with our ASE writer, printing "<file>\t<hex>" lines for assimp to read. Files: case_<i>.ase.
|
||||
local Ase = require("@rosetta/model/ase")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Cases = require("@tests/gen/ase_cases")
|
||||
|
||||
local HEX = {}
|
||||
for i = 0, 255 do
|
||||
HEX[i] = string.format("%02x", i)
|
||||
end
|
||||
local function hex(b: buffer): string
|
||||
local out = table.create(buffer.len(b))
|
||||
for i = 0, buffer.len(b) - 1 do
|
||||
out[i + 1] = HEX[buffer.readu8(b, i)]
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
for i, c in Cases do
|
||||
local scene = Ase.decode(Bytes.reader(buffer.fromstring(c.data)))
|
||||
if scene then
|
||||
local out, err = Ase.encode(scene)
|
||||
if out then
|
||||
print(string.format("case_%d.ase", i) .. "\t" .. hex(out))
|
||||
else
|
||||
print("encode failed " .. c.name .. ": " .. tostring(err))
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,249 @@
|
||||
# ASE fixtures (tools/fixtures.py): assimp's ASE test models (test/models/ASE and test/models-nonbsd/ASE, read from the
|
||||
# nixpkgs assimp source, never committed) plus a hand-written file (Multi/Sub-Object material, a bitmap map with UVW
|
||||
# tiling, a second mapping channel, vertex colours, explicit normals, a helper parent, a camera, a light), with
|
||||
# reference values from assimp (`assimp info -r`: meshes, faces, vertices, cameras, lights; world bounds from its
|
||||
# PreTransformVertices export). Then our encoder's output (tools/fixtures/_ase_encode.luau) read back by assimp.
|
||||
# tests/gen/ase_cases.luau { name, data, assimp }
|
||||
# tests/gen/ase_xcheck.luau { name, case, assimp }
|
||||
import importlib.util, os
|
||||
|
||||
INPUTS = ["src/Rosetta/model/ase", "tools/fixtures/_model_ref.py", "tools/fixtures/fbx.py", "tools/fixtures/_ase_encode.luau"]
|
||||
|
||||
HAND = """*3DSMAX_ASCIIEXPORT\t200
|
||||
*COMMENT "hand-written for rosetta"
|
||||
*SCENE {
|
||||
\t*SCENE_FILENAME "hand.max"
|
||||
\t*SCENE_FIRSTFRAME 0
|
||||
\t*SCENE_LASTFRAME 50
|
||||
\t*SCENE_FRAMESPEED 25
|
||||
\t*SCENE_TICKSPERFRAME 192
|
||||
\t*SCENE_BACKGROUND_STATIC 0.1000\t0.2000\t0.3000
|
||||
\t*SCENE_AMBIENT_STATIC 0.0000\t0.0000\t0.0000
|
||||
}
|
||||
*MATERIAL_LIST {
|
||||
\t*MATERIAL_COUNT 1
|
||||
\t*MATERIAL 0 {
|
||||
\t\t*MATERIAL_NAME "Multi"
|
||||
\t\t*MATERIAL_CLASS "Multi/Sub-Object"
|
||||
\t\t*NUMSUBMTLS 2
|
||||
\t\t*SUBMATERIAL 0 {
|
||||
\t\t\t*MATERIAL_NAME "Brick"
|
||||
\t\t\t*MATERIAL_CLASS "Standard"
|
||||
\t\t\t*MATERIAL_AMBIENT 0.1000\t0.1000\t0.1000
|
||||
\t\t\t*MATERIAL_DIFFUSE 0.8000\t0.4000\t0.2000
|
||||
\t\t\t*MATERIAL_SPECULAR 0.9000\t0.9000\t0.9000
|
||||
\t\t\t*MATERIAL_SHINE 0.2500
|
||||
\t\t\t*MATERIAL_SHINESTRENGTH 0.5000
|
||||
\t\t\t*MATERIAL_TRANSPARENCY 0.0000
|
||||
\t\t\t*MATERIAL_SELFILLUM 0.0000
|
||||
\t\t\t*MAP_DIFFUSE {
|
||||
\t\t\t\t*MAP_NAME "BrickMap"
|
||||
\t\t\t\t*MAP_CLASS "Bitmap"
|
||||
\t\t\t\t*MAP_SUBNO 1
|
||||
\t\t\t\t*MAP_AMOUNT 1.0000
|
||||
\t\t\t\t*BITMAP "textures\\brick.png"
|
||||
\t\t\t\t*MAP_TYPE Screen
|
||||
\t\t\t\t*UVW_U_OFFSET 0.0000
|
||||
\t\t\t\t*UVW_V_OFFSET 0.0000
|
||||
\t\t\t\t*UVW_U_TILING 2.0000
|
||||
\t\t\t\t*UVW_V_TILING 3.0000
|
||||
\t\t\t\t*UVW_ANGLE 0.0000
|
||||
\t\t\t}
|
||||
\t\t}
|
||||
\t\t*SUBMATERIAL 1 {
|
||||
\t\t\t*MATERIAL_NAME "Glass"
|
||||
\t\t\t*MATERIAL_CLASS "Standard"
|
||||
\t\t\t*MATERIAL_AMBIENT 0.0000\t0.0000\t0.0000
|
||||
\t\t\t*MATERIAL_DIFFUSE 0.2000\t0.6000\t0.9000
|
||||
\t\t\t*MATERIAL_SPECULAR 1.0000\t1.0000\t1.0000
|
||||
\t\t\t*MATERIAL_SHINE 0.9000
|
||||
\t\t\t*MATERIAL_TRANSPARENCY 0.5000
|
||||
\t\t\t*MATERIAL_TWOSIDED
|
||||
\t\t}
|
||||
\t}
|
||||
}
|
||||
*HELPEROBJECT {
|
||||
\t*NODE_NAME "Pivot"
|
||||
\t*HELPER_CLASS "Dummy"
|
||||
\t*NODE_TM {
|
||||
\t\t*NODE_NAME "Pivot"
|
||||
\t\t*TM_ROW0 1.0000\t0.0000\t0.0000
|
||||
\t\t*TM_ROW1 0.0000\t1.0000\t0.0000
|
||||
\t\t*TM_ROW2 0.0000\t0.0000\t1.0000
|
||||
\t\t*TM_ROW3 10.0000\t0.0000\t0.0000
|
||||
\t}
|
||||
}
|
||||
*GEOMOBJECT {
|
||||
\t*NODE_NAME "Quad"
|
||||
\t*NODE_PARENT "Pivot"
|
||||
\t*NODE_TM {
|
||||
\t\t*NODE_NAME "Quad"
|
||||
\t\t*TM_ROW0 0.0000\t1.0000\t0.0000
|
||||
\t\t*TM_ROW1 -1.0000\t0.0000\t0.0000
|
||||
\t\t*TM_ROW2 0.0000\t0.0000\t1.0000
|
||||
\t\t*TM_ROW3 10.0000\t5.0000\t0.0000
|
||||
\t}
|
||||
\t*MESH {
|
||||
\t\t*TIMEVALUE 0
|
||||
\t\t*MESH_NUMVERTEX 4
|
||||
\t\t*MESH_NUMFACES 2
|
||||
\t\t*MESH_VERTEX_LIST {
|
||||
\t\t\t*MESH_VERTEX 0\t10.0000\t5.0000\t0.0000
|
||||
\t\t\t*MESH_VERTEX 1\t10.0000\t7.0000\t0.0000
|
||||
\t\t\t*MESH_VERTEX 2\t8.0000\t7.0000\t0.0000
|
||||
\t\t\t*MESH_VERTEX 3\t8.0000\t5.0000\t0.0000
|
||||
\t\t}
|
||||
\t\t*MESH_FACE_LIST {
|
||||
\t\t\t*MESH_FACE 0: A: 0 B: 1 C: 2 AB: 1 BC: 1 CA: 0\t*MESH_SMOOTHING 1\t*MESH_MTLID 0
|
||||
\t\t\t*MESH_FACE 1: A: 0 B: 2 C: 3 AB: 1 BC: 1 CA: 0\t*MESH_SMOOTHING \t*MESH_MTLID 1
|
||||
\t\t}
|
||||
\t\t*MESH_NUMTVERTEX 4
|
||||
\t\t*MESH_TVERTLIST {
|
||||
\t\t\t*MESH_TVERT 0\t0.0000\t0.0000\t0.0000
|
||||
\t\t\t*MESH_TVERT 1\t1.0000\t0.0000\t0.0000
|
||||
\t\t\t*MESH_TVERT 2\t1.0000\t1.0000\t0.0000
|
||||
\t\t\t*MESH_TVERT 3\t0.0000\t1.0000\t0.0000
|
||||
\t\t}
|
||||
\t\t*MESH_NUMTVFACES 2
|
||||
\t\t*MESH_TFACELIST {
|
||||
\t\t\t*MESH_TFACE 0\t0\t1\t2
|
||||
\t\t\t*MESH_TFACE 1\t0\t2\t3
|
||||
\t\t}
|
||||
\t\t*MESH_MAPPINGCHANNEL 2 {
|
||||
\t\t\t*MESH_NUMTVERTEX 1
|
||||
\t\t\t*MESH_TVERTLIST {
|
||||
\t\t\t\t*MESH_TVERT 0\t0.2500\t0.7500\t0.0000
|
||||
\t\t\t}
|
||||
\t\t\t*MESH_NUMTVFACES 2
|
||||
\t\t\t*MESH_TFACELIST {
|
||||
\t\t\t\t*MESH_TFACE 0\t0\t0\t0
|
||||
\t\t\t\t*MESH_TFACE 1\t0\t0\t0
|
||||
\t\t\t}
|
||||
\t\t}
|
||||
\t\t*MESH_NUMCVERTEX 2
|
||||
\t\t*MESH_CVERTLIST {
|
||||
\t\t\t*MESH_VERTCOL 0\t1.0000\t0.0000\t0.0000
|
||||
\t\t\t*MESH_VERTCOL 1\t0.0000\t1.0000\t0.0000
|
||||
\t\t}
|
||||
\t\t*MESH_NUMCVFACES 2
|
||||
\t\t*MESH_CFACELIST {
|
||||
\t\t\t*MESH_CFACE 0\t0\t1\t0
|
||||
\t\t\t*MESH_CFACE 1\t1\t1\t1
|
||||
\t\t}
|
||||
\t\t*MESH_NORMALS {
|
||||
\t\t\t*MESH_FACENORMAL 0\t0.0000\t0.0000\t1.0000
|
||||
\t\t\t\t*MESH_VERTEXNORMAL 0\t0.0000\t0.0000\t1.0000
|
||||
\t\t\t\t*MESH_VERTEXNORMAL 1\t0.0000\t0.0000\t1.0000
|
||||
\t\t\t\t*MESH_VERTEXNORMAL 2\t0.0000\t0.6000\t0.8000
|
||||
\t\t\t*MESH_FACENORMAL 1\t0.0000\t0.0000\t1.0000
|
||||
\t\t\t\t*MESH_VERTEXNORMAL 0\t0.0000\t0.0000\t1.0000
|
||||
\t\t\t\t*MESH_VERTEXNORMAL 2\t0.0000\t0.6000\t0.8000
|
||||
\t\t\t\t*MESH_VERTEXNORMAL 3\t0.0000\t0.0000\t1.0000
|
||||
\t\t}
|
||||
\t}
|
||||
\t*PROP_MOTIONBLUR 0
|
||||
\t*PROP_CASTSHADOW 1
|
||||
\t*PROP_RECVSHADOW 1
|
||||
\t*MATERIAL_REF 0
|
||||
}
|
||||
*CAMERAOBJECT {
|
||||
\t*NODE_NAME "Cam"
|
||||
\t*CAMERA_TYPE Free
|
||||
\t*NODE_TM {
|
||||
\t\t*NODE_NAME "Cam"
|
||||
\t\t*TM_ROW0 1.0000\t0.0000\t0.0000
|
||||
\t\t*TM_ROW1 0.0000\t0.0000\t1.0000
|
||||
\t\t*TM_ROW2 0.0000\t-1.0000\t0.0000
|
||||
\t\t*TM_ROW3 0.0000\t-50.0000\t10.0000
|
||||
\t}
|
||||
\t*CAMERA_SETTINGS {
|
||||
\t\t*TIMEVALUE 0
|
||||
\t\t*CAMERA_NEAR 1.0000
|
||||
\t\t*CAMERA_FAR 500.0000
|
||||
\t\t*CAMERA_FOV 0.7854
|
||||
\t\t*CAMERA_TDIST 50.0000
|
||||
\t}
|
||||
}
|
||||
*LIGHTOBJECT {
|
||||
\t*NODE_NAME "Spot"
|
||||
\t*LIGHT_TYPE Target
|
||||
\t*NODE_TM {
|
||||
\t\t*NODE_NAME "Spot"
|
||||
\t\t*TM_ROW0 1.0000\t0.0000\t0.0000
|
||||
\t\t*TM_ROW1 0.0000\t1.0000\t0.0000
|
||||
\t\t*TM_ROW2 0.0000\t0.0000\t1.0000
|
||||
\t\t*TM_ROW3 0.0000\t0.0000\t100.0000
|
||||
\t}
|
||||
\t*NODE_TM {
|
||||
\t\t*NODE_NAME "Spot.Target"
|
||||
\t\t*TM_ROW0 1.0000\t0.0000\t0.0000
|
||||
\t\t*TM_ROW1 0.0000\t1.0000\t0.0000
|
||||
\t\t*TM_ROW2 0.0000\t0.0000\t1.0000
|
||||
\t\t*TM_ROW3 0.0000\t0.0000\t0.0000
|
||||
\t}
|
||||
\t*LIGHT_SHADOWS Off
|
||||
\t*LIGHT_SETTINGS {
|
||||
\t\t*TIMEVALUE 0
|
||||
\t\t*LIGHT_COLOR 1.0000\t0.9000\t0.8000
|
||||
\t\t*LIGHT_INTENS 2.0000
|
||||
\t\t*LIGHT_HOTSPOT 30.0000
|
||||
\t\t*LIGHT_FALLOFF 60.0000
|
||||
\t}
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
def load(ctx, name):
|
||||
spec = importlib.util.spec_from_file_location("_" + name.replace(".", "_"), os.path.join(ctx.root, "tools", "fixtures", name + ".py"))
|
||||
m = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(m)
|
||||
return m
|
||||
|
||||
|
||||
def reference(ctx, M, X, paths, tmp):
|
||||
out = []
|
||||
for p, a in zip(paths, M.assimp_info(ctx, paths, raw=True)):
|
||||
a.update(X.assimp_extra(ctx, p))
|
||||
for k in ("min", "max", "perMesh"):
|
||||
a.pop(k, None)
|
||||
if not a.get("failed"):
|
||||
a.update(X.ptv_bounds(ctx, p, tmp))
|
||||
out.append(a)
|
||||
return out
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
M = load(ctx, "_model_ref")
|
||||
X = load(ctx, "fbx")
|
||||
tmp = M.tempdir()
|
||||
src = X.assimp_src(ctx)
|
||||
cases = []
|
||||
for sub in ("models", "models-nonbsd"):
|
||||
d = os.path.join(src, "test", sub, "ASE")
|
||||
for root, _, files in sorted(os.walk(d)):
|
||||
for f in sorted(files):
|
||||
if f.lower().endswith((".ase", ".ask")) or f == "TestFormatDetection":
|
||||
p = os.path.join(root, f)
|
||||
cases.append({"name": "assimp/%s/%s" % (sub, os.path.relpath(p, d)), "path": p})
|
||||
hand = os.path.join(tmp, "hand.ase")
|
||||
with open(hand, "w") as fh:
|
||||
fh.write(HAND)
|
||||
cases.append({"name": "hand.ase", "path": hand})
|
||||
for c, a in zip(cases, reference(ctx, M, X, [c["path"] for c in cases], tmp)):
|
||||
c["assimp"] = a
|
||||
c["data"] = open(c["path"], "rb").read()
|
||||
del c["path"]
|
||||
ctx.emit("ase_cases", cases)
|
||||
|
||||
ours = M.luau_encode(ctx, "tools/fixtures/_ase_encode.luau")
|
||||
names = sorted((n for n in ours if n.endswith(".ase")), key=lambda n: int(n[5:-4]))
|
||||
paths = []
|
||||
for n in names:
|
||||
p = os.path.join(tmp, n)
|
||||
with open(p, "wb") as f:
|
||||
f.write(ours[n])
|
||||
paths.append(p)
|
||||
xs = []
|
||||
for n, a in zip(names, reference(ctx, M, X, paths, tmp)):
|
||||
xs.append({"name": n, "case": int(n[5:-4]), "assimp": a})
|
||||
ctx.emit("ase_xcheck", xs)
|
||||
@@ -0,0 +1,20 @@
|
||||
# Docs inputs for the engine / level-editor sections of the scene, tile map, archive and data pages (tools/fixtures.py,
|
||||
# tools/docs_check.py): the hand-written ASE of tools/fixtures/ase.py.
|
||||
# tests/gen/docs_engines.luau { files = { [name] = bytes }, vars = { aseText = "hand.ase", ... } }
|
||||
import importlib.util, os
|
||||
|
||||
INPUTS = ["tools/docs_check.py", "tools/fixtures/ase.py"]
|
||||
|
||||
|
||||
def mod(ctx, name):
|
||||
spec = importlib.util.spec_from_file_location("fx_" + name, os.path.join(ctx.root, "tools", "fixtures", name + ".py"))
|
||||
m = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(m)
|
||||
return m
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
check = mod(ctx, "../docs_check")
|
||||
files = {"hand.ase": mod(ctx, "ase").HAND.encode()}
|
||||
vars = {"aseText": "hand.ase"}
|
||||
check.emit_inputs(ctx, files, vars)
|
||||
Reference in new issue
Block a user