model/t3d: Unreal T3D (UE1-4) brushes through codecs/model/Brush and actors as nodes
This commit is contained in:
1 parent
37aa29d03e
commit
8d305bb942
10 files changed
+1084
-4
No files matched your search
@@ -1082,6 +1082,18 @@ local list: { Entry } = {
|
||||
return require("@rosetta/model/stl")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "t3d",
|
||||
category = "model",
|
||||
path = "model/t3d",
|
||||
extensions = { "t3d" },
|
||||
mime = { "text/x-unreal-t3d" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/text/Text", "codecs/model/Brush" },
|
||||
caps = { decode = true, encode = false, text = true, brushes = true, actors = true },
|
||||
load = function()
|
||||
return require("@rosetta/model/t3d")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "usda",
|
||||
category = "model",
|
||||
|
||||
@@ -0,0 +1,403 @@
|
||||
--!strict
|
||||
-- model/t3d/Build: parsed T3D blocks into a Scene: one node per actor (Unreal's own Z-up, left-handed coordinates,
|
||||
-- kept as they are), brush polygons as primitives per texture (codecs/model/Brush). Brush transform as UnrealEd's
|
||||
-- ABrush coords: v - PrePivot, MainScale, Rotation, PostScale, + Location. Rotation as FRotationMatrix (Pitch / Yaw /
|
||||
-- Roll: 65536 units per turn, or degrees in UE4 text). Polygon winding follows the stored Normal; subtractive
|
||||
-- brushes face inwards.
|
||||
local Types = require("@rosetta/Types")
|
||||
local Brush = require("@rosetta/codecs/model/Brush")
|
||||
local Parse = require("@rosetta/model/t3d/Parse")
|
||||
|
||||
type Block = Parse.Block
|
||||
|
||||
local Build = {}
|
||||
|
||||
local S = Types.Scene
|
||||
|
||||
local function num(v: any, d: number): number
|
||||
return if type(v) == "number" and v == v then v else d
|
||||
end
|
||||
|
||||
local function vec3(s: string?): { number }?
|
||||
if type(s) ~= "string" then
|
||||
return nil
|
||||
end
|
||||
local a, b, c = string.match(s, "^%s*([^,%s]+)%s*,%s*([^,%s]+)%s*,%s*([^,%s]+)")
|
||||
if not a then
|
||||
return nil
|
||||
end
|
||||
local x, y, z = tonumber((string.gsub(a, "^%+", ""))), tonumber((string.gsub(b :: string, "^%+", ""))), tonumber((string.gsub(c :: string, "^%+", "")))
|
||||
if x and y and z then
|
||||
return { x, y, z }
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
local function xyz(v: any, d: { number }): { number }
|
||||
if type(v) == "table" then
|
||||
return { num(v.X, d[1]), num(v.Y, d[2]), num(v.Z, d[3]) }
|
||||
end
|
||||
return d
|
||||
end
|
||||
|
||||
-- FRotationMatrix rows are the X / Y / Z axes: the columns of a column-major matrix
|
||||
local function rotation(r: any, degrees: boolean): { number }
|
||||
if type(r) ~= "table" then
|
||||
return S.IDENTITY
|
||||
end
|
||||
local k = if degrees then math.pi / 180 else math.pi * 2 / 65536
|
||||
local p, y, ro = num(r.Pitch, 0) * k, num(r.Yaw, 0) * k, num(r.Roll, 0) * k
|
||||
local SP, CP, SY, CY, SR, CR = math.sin(p), math.cos(p), math.sin(y), math.cos(y), math.sin(ro), math.cos(ro)
|
||||
return {
|
||||
CP * CY, CP * SY, SP, 0,
|
||||
SR * SP * CY - CR * SY, SR * SP * SY + CR * CY, -SR * CP, 0,
|
||||
-(CR * SP * CY + SR * SY), CY * SR - CR * SP * SY, CR * CP, 0,
|
||||
0, 0, 0, 1,
|
||||
}
|
||||
end
|
||||
|
||||
local function scaling(s: { number }): { number }
|
||||
return { s[1], 0, 0, 0, 0, s[2], 0, 0, 0, 0, s[3], 0, 0, 0, 0, 1 }
|
||||
end
|
||||
|
||||
local function translation(t: { number }): { number }
|
||||
return { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, t[1], t[2], t[3], 1 }
|
||||
end
|
||||
|
||||
local function plain(b: Block): { [string]: any }
|
||||
local out: { [string]: any } = {}
|
||||
for _, k in b.order do
|
||||
out[k] = b.props[k]
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- the component holding the actor's transform (UE4: RootComponent's Relative* properties)
|
||||
local function component(actor: Block, objects: { [string]: { [string]: any } }): { [string]: any }?
|
||||
local root = actor.props.RootComponent
|
||||
if type(root) == "string" then
|
||||
local name = string.match(root, "'\"?([^'\"]+)\"?'") or root
|
||||
name = string.match(name, "([^%.:]+)$") or name
|
||||
if objects[name] then
|
||||
return objects[name]
|
||||
end
|
||||
end
|
||||
for _, o in objects do
|
||||
if o.RelativeLocation ~= nil or o.RelativeRotation ~= nil or o.RelativeScale3D ~= nil then
|
||||
return o
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
local function polygons(b: Block, out: { Block })
|
||||
for _, c in b.children do
|
||||
local k = string.lower(c.kind)
|
||||
if k == "polygon" then
|
||||
table.insert(out, c)
|
||||
elseif k == "polylist" or k == "brush" then
|
||||
polygons(c, out)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
type Ctx = { scene: Types.Scene, materials: { [string]: number }, opts: any, polys: number }
|
||||
|
||||
local function material(ctx: Ctx, name: string): number
|
||||
local m = ctx.materials[name]
|
||||
if m then
|
||||
return m
|
||||
end
|
||||
local mat = S.material(if name ~= "" then name else nil)
|
||||
mat.metallic = 0
|
||||
if name ~= "" then
|
||||
table.insert(ctx.scene.textures, { name = name })
|
||||
mat.baseColorTexture = { texture = #ctx.scene.textures }
|
||||
end
|
||||
mat.extras = { t3d = { texture = name } }
|
||||
table.insert(ctx.scene.materials, mat)
|
||||
ctx.materials[name] = #ctx.scene.materials
|
||||
return #ctx.scene.materials
|
||||
end
|
||||
|
||||
-- the brush's polygons as Scene primitives (local brush space)
|
||||
local function brushMesh(ctx: Ctx, brush: Block, subtract: boolean): (Types.Mesh?, string?)
|
||||
local list: { Block } = {}
|
||||
polygons(brush, list)
|
||||
if #list == 0 then
|
||||
return nil, nil
|
||||
end
|
||||
local faces: { Brush.Face } = {}
|
||||
local polys: { Brush.Polygon } = {}
|
||||
local flags: { number } = {}
|
||||
for _, pb in list do
|
||||
local rec: { [string]: string } = {}
|
||||
local verts: { number } = {}
|
||||
for _, r in pb.records do
|
||||
local name = string.lower(r[1])
|
||||
if name == "vertex" then
|
||||
local v = vec3(r[2])
|
||||
if v then
|
||||
table.insert(verts, v[1])
|
||||
table.insert(verts, v[2])
|
||||
table.insert(verts, v[3])
|
||||
end
|
||||
elseif rec[name] == nil then
|
||||
rec[name] = r[2]
|
||||
end
|
||||
end
|
||||
local nv = #verts // 3
|
||||
if nv >= 3 then
|
||||
ctx.polys += 1
|
||||
if ctx.polys > (ctx.opts.maxPolygons or 2 ^ 20) then
|
||||
return nil, "limit: over " .. (ctx.opts.maxPolygons or 2 ^ 20) .. " polygons"
|
||||
end
|
||||
-- Newell normal of the vertex order
|
||||
local nx, ny, nz = 0, 0, 0
|
||||
for k = 0, nv - 1 do
|
||||
local j = (k + 1) % nv
|
||||
local ax, ay, az = verts[k * 3 + 1], verts[k * 3 + 2], verts[k * 3 + 3]
|
||||
local bx, by, bz = verts[j * 3 + 1], verts[j * 3 + 2], verts[j * 3 + 3]
|
||||
nx += (ay - by) * (az + bz)
|
||||
ny += (az - bz) * (ax + bx)
|
||||
nz += (ax - bx) * (ay + by)
|
||||
end
|
||||
local stated = vec3(rec.normal)
|
||||
local n = stated or { nx, ny, nz }
|
||||
local l = math.sqrt(n[1] * n[1] + n[2] * n[2] + n[3] * n[3])
|
||||
if l > 0 then
|
||||
n = { n[1] / l, n[2] / l, n[3] / l }
|
||||
end
|
||||
local reverse = nx * n[1] + ny * n[2] + nz * n[3] < 0
|
||||
if subtract then
|
||||
reverse = not reverse
|
||||
n = { -n[1], -n[2], -n[3] }
|
||||
end
|
||||
if reverse then
|
||||
local r: { number } = {}
|
||||
for k = nv - 1, 0, -1 do
|
||||
table.insert(r, verts[k * 3 + 1])
|
||||
table.insert(r, verts[k * 3 + 2])
|
||||
table.insert(r, verts[k * 3 + 3])
|
||||
end
|
||||
verts = r
|
||||
end
|
||||
local tex = if type(pb.attrs.Texture) == "string" then pb.attrs.Texture else (if pb.attrs.Texture ~= nil then tostring(pb.attrs.Texture) else "")
|
||||
local o = vec3(rec.origin) or { 0, 0, 0 }
|
||||
local u = vec3(rec.textureu) or { 1, 0, 0 }
|
||||
local v = vec3(rec.texturev) or { 0, 1, 0 }
|
||||
local panU, panV = 0, 0
|
||||
if rec.pan then
|
||||
panU = tonumber(string.match(rec.pan, "U=%s*([-+]?[%d%.]+)")) or 0
|
||||
panV = tonumber(string.match(rec.pan, "V=%s*([-+]?[%d%.]+)")) or 0
|
||||
end
|
||||
-- texel = (p - Origin) . TextureU + PanU
|
||||
local face: Brush.Face = {
|
||||
plane = { n[1], n[2], n[3], n[1] * o[1] + n[2] * o[2] + n[3] * o[3] },
|
||||
texture = tex,
|
||||
vecs = {
|
||||
{ u[1], u[2], u[3], panU - (o[1] * u[1] + o[2] * u[2] + o[3] * u[3]) },
|
||||
{ v[1], v[2], v[3], panV - (o[1] * v[1] + o[2] * v[2] + o[3] * v[3]) },
|
||||
},
|
||||
}
|
||||
table.insert(faces, face)
|
||||
table.insert(polys, { brush = 1, face = #faces, points = verts, normal = n })
|
||||
table.insert(flags, num(pb.attrs.Flags, 0))
|
||||
end
|
||||
end
|
||||
local size = ctx.opts.textureSize
|
||||
local built = Brush.primitives(polys, { faces }, function(name: string): (number, number)
|
||||
if type(size) == "function" then
|
||||
local ok, w, h = pcall(size, name)
|
||||
if ok and type(w) == "number" then
|
||||
return w, if type(h) == "number" then h else w
|
||||
end
|
||||
elseif type(size) == "table" and type(size[name]) == "table" then
|
||||
return num(size[name][1], 256), num(size[name][2], 256)
|
||||
end
|
||||
return 256, 256
|
||||
end)
|
||||
local mesh: Types.Mesh = { name = if type(brush.attrs.Name) == "string" then brush.attrs.Name else nil, primitives = {} }
|
||||
for _, b in built do
|
||||
b.primitive.material = material(ctx, b.texture)
|
||||
table.insert(mesh.primitives, b.primitive)
|
||||
end
|
||||
mesh.extras = { t3d = { polygons = #polys } }
|
||||
return mesh, nil
|
||||
end
|
||||
|
||||
local LIGHTS: { [string]: "point" | "spot" | "directional" } = {
|
||||
Light = "point", PointLight = "point", SpotLight = "spot", DirectionalLight = "directional", TriggerLight = "point",
|
||||
SkyLight = "directional", Spotlight = "spot", SunLight = "directional", DominantDirectionalLight = "directional",
|
||||
PointLightMovable = "point", SpotLightMovable = "spot", DirectionalLightMovable = "directional",
|
||||
}
|
||||
|
||||
-- HSV of UE1 lights: LightHue / LightSaturation (255 = white) / LightBrightness, all 0..255
|
||||
local function hsv(h: number, s: number): { number }
|
||||
local hh = (h % 256) / 256 * 6
|
||||
local i = math.floor(hh)
|
||||
local f = hh - i
|
||||
local p, q, t = 1 - s, 1 - s * f, 1 - s * (1 - f)
|
||||
local rgb = ({ { 1, t, p }, { q, 1, p }, { p, 1, t }, { p, q, 1 }, { t, p, 1 }, { 1, p, q } })[i % 6 + 1]
|
||||
return rgb
|
||||
end
|
||||
|
||||
local function light(ctx: Ctx, class: string, p: { [string]: any }, comp: { [string]: any }?): number
|
||||
local typ = LIGHTS[class]
|
||||
local l: Types.Light = { type = typ, color = { 1, 1, 1 }, intensity = 1 }
|
||||
if p.LightBrightness ~= nil or p.LightHue ~= nil or p.LightSaturation ~= nil then
|
||||
l.color = hsv(num(p.LightHue, 0), 1 - num(p.LightSaturation, 255) / 255)
|
||||
l.intensity = num(p.LightBrightness, 64) / 255
|
||||
end
|
||||
local c: any = comp or {}
|
||||
local col = c.LightColor or p.LightColor
|
||||
if type(col) == "table" then
|
||||
l.color = { num(col.R, 255) / 255, num(col.G, 255) / 255, num(col.B, 255) / 255 }
|
||||
end
|
||||
if c.Intensity ~= nil or c.Brightness ~= nil then
|
||||
l.intensity = num(c.Intensity, num(c.Brightness, 1))
|
||||
end
|
||||
local radius = c.AttenuationRadius or c.Radius
|
||||
if type(radius) == "number" then
|
||||
l.range = radius
|
||||
end
|
||||
if typ == "spot" then
|
||||
l.outerCone = math.rad(num(c.OuterConeAngle, num(p.LightCone, 44)))
|
||||
l.innerCone = math.rad(num(c.InnerConeAngle, 0))
|
||||
end
|
||||
table.insert(ctx.scene.lights, l)
|
||||
return #ctx.scene.lights
|
||||
end
|
||||
|
||||
local function actors(b: Block, out: { Block })
|
||||
for _, c in b.children do
|
||||
local k = string.lower(c.kind)
|
||||
if k == "actor" then
|
||||
table.insert(out, c)
|
||||
elseif k == "map" or k == "level" or k == "surface" then
|
||||
actors(c, out)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Build.scene(root: Block, opts: any): (Types.Scene?, string?)
|
||||
local sc = S.new()
|
||||
sc.meta.up = "z"
|
||||
sc.meta.handedness = "left"
|
||||
local list: { Block } = {}
|
||||
actors(root, list)
|
||||
local ctx: Ctx = { scene = sc, materials = {}, opts = opts or {}, polys = 0 }
|
||||
local ue4 = false
|
||||
for _, a in list do
|
||||
local rawClass = if a.attrs.Class ~= nil then tostring(a.attrs.Class) else "Actor"
|
||||
local class = string.match(rawClass, "([%w_]+)$") or rawClass
|
||||
if string.find(rawClass, "/Script/", 1, true) then
|
||||
ue4 = true
|
||||
end
|
||||
local p = a.props
|
||||
local objects: { [string]: { [string]: any } } = {}
|
||||
local objectClass: { [string]: string } = {}
|
||||
for _, c in a.children do
|
||||
if string.lower(c.kind) == "object" then
|
||||
local name = if c.attrs.Name ~= nil then tostring(c.attrs.Name) else ("Object" .. #a.children)
|
||||
local o = objects[name] or {}
|
||||
for _, k in c.order do
|
||||
o[k] = c.props[k]
|
||||
end
|
||||
objects[name] = o
|
||||
if c.attrs.Class ~= nil then
|
||||
objectClass[name] = tostring(c.attrs.Class)
|
||||
end
|
||||
end
|
||||
end
|
||||
local comp = component(a, objects)
|
||||
local node: Types.Node = { name = if a.attrs.Name ~= nil then tostring(a.attrs.Name) else class, children = {} }
|
||||
local m: { number }
|
||||
if comp and p.Location == nil then
|
||||
local deg = true
|
||||
local loc = xyz(comp.RelativeLocation, { 0, 0, 0 })
|
||||
local sc3 = xyz(comp.RelativeScale3D, { 1, 1, 1 })
|
||||
m = S.mul(translation(loc), S.mul(rotation(comp.RelativeRotation, deg), scaling(sc3)))
|
||||
else
|
||||
local loc = xyz(p.Location, { 0, 0, 0 })
|
||||
local deg = a.degrees.Rotation == true
|
||||
local rot = rotation(p.Rotation, deg)
|
||||
local isBrush = false
|
||||
for _, c in a.children do
|
||||
if string.lower(c.kind) == "brush" then
|
||||
isBrush = true
|
||||
end
|
||||
end
|
||||
if isBrush then
|
||||
local pre = xyz(p.PrePivot, { 0, 0, 0 })
|
||||
local main = xyz(type(p.MainScale) == "table" and p.MainScale.Scale, { 1, 1, 1 })
|
||||
local post = xyz(type(p.PostScale) == "table" and p.PostScale.Scale, { 1, 1, 1 })
|
||||
m = S.mul(translation(loc), S.mul(scaling(post), S.mul(rot, S.mul(scaling(main), translation({ -pre[1], -pre[2], -pre[3] })))))
|
||||
else
|
||||
local ds = num(p.DrawScale, 1)
|
||||
local s3 = xyz(p.DrawScale3D, { 1, 1, 1 })
|
||||
m = S.mul(translation(loc), S.mul(rot, scaling({ s3[1] * ds, s3[2] * ds, s3[3] * ds })))
|
||||
end
|
||||
end
|
||||
node.matrix = m
|
||||
local csg = if type(p.CsgOper) == "string" then p.CsgOper
|
||||
elseif p.BrushType == "Brush_Subtract" then "CSG_Subtract"
|
||||
elseif p.BrushType == "Brush_Add" then "CSG_Add"
|
||||
else nil
|
||||
local extras: { [string]: any } = { class = rawClass, properties = plain(a), csg = csg }
|
||||
if next(objects) then
|
||||
extras.objects = objects
|
||||
extras.objectClasses = objectClass
|
||||
end
|
||||
for _, c in a.children do
|
||||
if string.lower(c.kind) == "brush" then
|
||||
local builder = csg == "CSG_Active" or (class == "Brush" and csg == nil and node.name == "Brush")
|
||||
extras.builder = if builder then true else nil
|
||||
if not builder or ctx.opts.builder then
|
||||
local mesh, err = brushMesh(ctx, c, csg == "CSG_Subtract")
|
||||
if err then
|
||||
return nil, err
|
||||
end
|
||||
if mesh then
|
||||
table.insert(sc.meshes, mesh)
|
||||
node.mesh = #sc.meshes
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if LIGHTS[class] then
|
||||
local lc: { [string]: any }? = nil
|
||||
for name, o in objects do
|
||||
if string.find(objectClass[name] or name, "Light", 1, true) then
|
||||
lc = o
|
||||
end
|
||||
end
|
||||
node.light = light(ctx, class, p, lc)
|
||||
elseif class == "CameraActor" or class == "Camera" then
|
||||
local fov = num(p.FOVAngle, 90)
|
||||
for _, o in objects do
|
||||
if o.FieldOfView ~= nil then
|
||||
fov = num(o.FieldOfView, fov)
|
||||
end
|
||||
end
|
||||
table.insert(sc.cameras, { type = "perspective", yfov = math.rad(fov), znear = 1 } :: any)
|
||||
node.camera = #sc.cameras
|
||||
end
|
||||
local sm = p.StaticMesh
|
||||
for _, o in objects do
|
||||
if o.StaticMesh ~= nil then
|
||||
sm = o.StaticMesh
|
||||
end
|
||||
end
|
||||
if type(sm) == "string" then
|
||||
extras.staticMesh = string.match(sm, "'\"?([^'\"]+)\"?'") or sm
|
||||
end
|
||||
node.extras = { t3d = extras }
|
||||
table.insert(sc.nodes, node)
|
||||
table.insert(sc.roots, #sc.nodes)
|
||||
end
|
||||
sc.meta.source = { format = "t3d", engine = if ue4 then "ue4" else "ue1-3", actors = #list, polygons = ctx.polys }
|
||||
return sc, nil
|
||||
end
|
||||
|
||||
return Build
|
||||
@@ -0,0 +1,166 @@
|
||||
--!strict
|
||||
-- model/t3d/Parse: Unreal T3D text into nested blocks. A line `Begin <Kind> Key=Value ...` opens a block, `End <Kind>`
|
||||
-- closes it; other lines are `Key=Value` properties or, inside polygons, `Origin x,y,z` / `Vertex x,y,z` style
|
||||
-- records. Values: numbers, quoted strings, structs `(X=1,Y=2)` as tables, object references
|
||||
-- (`Model'MyLevel.Model1'`) as strings.
|
||||
local Parse = {}
|
||||
|
||||
export type Value = any
|
||||
export type Block = {
|
||||
kind: string,
|
||||
attrs: { [string]: Value },
|
||||
props: { [string]: Value },
|
||||
order: { string },
|
||||
records: { { string } },
|
||||
children: { Block },
|
||||
degrees: { [string]: boolean },
|
||||
}
|
||||
|
||||
-- "(X=1.0,Y=2,Z=-3)" / "(Scale=(X=1,Y=1,Z=1),SheerAxis=SHEER_ZX)"; i at '('
|
||||
local function struct(s: string, i: number, depth: number): (any, number)
|
||||
local out: { [any]: any } = {}
|
||||
local n = #s
|
||||
i += 1
|
||||
while i <= n do
|
||||
local c = string.sub(s, i, i)
|
||||
if c == ")" then
|
||||
return out, i + 1
|
||||
elseif c == "," or c == " " or c == "\t" then
|
||||
i += 1
|
||||
else
|
||||
local k, e = string.match(s, "^([^=,()]+)=()", i)
|
||||
if not k then
|
||||
-- an array element without a key: (1,2,3)
|
||||
local v, ni = Parse.value(s, i, depth + 1)
|
||||
table.insert(out, v)
|
||||
i = ni
|
||||
else
|
||||
local key = string.gsub(k :: string, "^%s+", "")
|
||||
local v, ni = Parse.value(s, e :: number, depth + 1)
|
||||
out[key] = v
|
||||
i = ni
|
||||
end
|
||||
end
|
||||
end
|
||||
return out, i
|
||||
end
|
||||
|
||||
-- a value starting at i, up to the next unbracketed ',' / ')' (inside structs) or the end
|
||||
function Parse.value(s: string, i: number, depth: number): (any, number)
|
||||
local n = #s
|
||||
local c = string.sub(s, i, i)
|
||||
if c == "(" and depth < 32 then
|
||||
return struct(s, i, depth)
|
||||
elseif c == "\"" then
|
||||
local e = string.find(s, "\"", i + 1, true) or (n + 1)
|
||||
return string.sub(s, i + 1, e - 1), e + 1
|
||||
end
|
||||
-- a bare token; object references may hold quotes ('...' and "...")
|
||||
local j = i
|
||||
local q: string? = nil
|
||||
while j <= n do
|
||||
local ch = string.sub(s, j, j)
|
||||
if q then
|
||||
if ch == q then
|
||||
q = nil
|
||||
end
|
||||
elseif ch == "'" or ch == "\"" then
|
||||
q = ch
|
||||
elseif depth > 0 and (ch == "," or ch == ")") then
|
||||
break
|
||||
end
|
||||
j += 1
|
||||
end
|
||||
local tok = string.match(string.sub(s, i, j - 1), "^%s*(.-)%s*$") :: string
|
||||
local num = tonumber(tok)
|
||||
if num ~= nil and string.match(tok, "^[-+]?[%d%.]+[eE]?[-+]?%d*$") then
|
||||
return num, j
|
||||
end
|
||||
return tok, j
|
||||
end
|
||||
|
||||
-- Key=Value pairs of a Begin line ("Begin Actor Class=Light Name=Light0"); quoted values may hold spaces
|
||||
local function attrs(rest: string): { [string]: any }
|
||||
local out: { [string]: any } = {}
|
||||
local i, n = 1, #rest
|
||||
while i <= n do
|
||||
local k, e = string.match(rest, "^%s*([%w_%.]+)=()", i)
|
||||
if not k then
|
||||
break
|
||||
end
|
||||
local v: string
|
||||
if string.sub(rest, e :: number, e :: number) == "\"" then
|
||||
local q = string.find(rest, "\"", (e :: number) + 1, true) or (n + 1)
|
||||
v = string.sub(rest, (e :: number) + 1, q - 1)
|
||||
i = q + 1
|
||||
else
|
||||
local stop = string.find(rest, "%s", e :: number) or (n + 1)
|
||||
v = string.sub(rest, e :: number, stop - 1)
|
||||
i = stop
|
||||
end
|
||||
out[k :: string] = tonumber(v) or v
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local function block(kind: string, a: { [string]: any }): Block
|
||||
return { kind = kind, attrs = a, props = {}, order = {}, records = {}, children = {}, degrees = {} }
|
||||
end
|
||||
|
||||
function Parse.tree(text: string, maxBlocks: number): (Block?, string?)
|
||||
local root = block("", {})
|
||||
local stack: { Block } = { root }
|
||||
local count = 0
|
||||
local lineNo = 0
|
||||
for raw in string.gmatch(text, "[^\n]*") do
|
||||
lineNo += 1
|
||||
local line = string.match(raw, "^%s*(.-)%s*$") :: string
|
||||
if line ~= "" and string.sub(line, 1, 2) ~= "//" then
|
||||
local cur = stack[#stack]
|
||||
local word, rest = string.match(line, "^(%a+)%s*(.*)$")
|
||||
local lw = if word then string.lower(word) else ""
|
||||
if lw == "begin" then
|
||||
local kind, after = string.match(rest :: string, "^(%S+)%s*(.*)$")
|
||||
if not kind then
|
||||
return nil, "Begin without a kind at line " .. lineNo
|
||||
end
|
||||
count += 1
|
||||
if count > maxBlocks then
|
||||
return nil, "limit: over " .. maxBlocks .. " blocks"
|
||||
end
|
||||
if #stack > 64 then
|
||||
return nil, "limit: blocks nested too deep"
|
||||
end
|
||||
local b = block(kind :: string, attrs(after :: string))
|
||||
table.insert(cur.children, b)
|
||||
table.insert(stack, b)
|
||||
elseif lw == "end" and #stack > 1 and (rest == "" or string.lower(string.match(rest :: string, "^(%S+)") or "") == string.lower(cur.kind)) then
|
||||
table.remove(stack)
|
||||
else
|
||||
local k, e = string.match(line, "^([%w_%(%)%[%]%.]+)%s*=()")
|
||||
if k then
|
||||
local v = Parse.value(line, e :: number, 0)
|
||||
if cur.props[k :: string] == nil then
|
||||
table.insert(cur.order, k :: string)
|
||||
end
|
||||
cur.props[k :: string] = v
|
||||
if string.find(string.sub(line, e :: number), "Pitch=[-+]?%d*%.") or string.find(string.sub(line, e :: number), "Yaw=[-+]?%d*%.") then
|
||||
cur.degrees[k :: string] = true
|
||||
end
|
||||
else
|
||||
-- a record: Origin / Normal / TextureU / TextureV / Vertex / Pan U=0 V=0
|
||||
local name, data = string.match(line, "^(%a+)%s+(.*)$")
|
||||
if name then
|
||||
table.insert(cur.records, { name :: string, data :: string })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if #stack > 1 then
|
||||
return nil, "truncated: " .. stack[#stack].kind .. " block not closed"
|
||||
end
|
||||
return root, nil
|
||||
end
|
||||
|
||||
return Parse
|
||||
@@ -0,0 +1,92 @@
|
||||
--!strict
|
||||
-- model/t3d: Unreal T3D text (UnrealEd map / brush export: Begin Map / Level / Actor / Brush / PolyList / Polygon,
|
||||
-- Begin Object components), read only. Supports: brush polygons with texture projection (TextureU / TextureV /
|
||||
-- Origin / Pan; sizes through opts.textureSize, default 256) as primitives per texture, CSG operation in extras
|
||||
-- (subtractive brushes face inwards; the builder brush is skipped), actors as nodes with their properties and
|
||||
-- transforms (UE1-3 Location / Rotation / DrawScale, UE4 RootComponent Relative*), lights, cameras, static mesh
|
||||
-- references. Not supported: BSP CSG between brushes, sheared brush scales, terrain, static mesh geometry.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Sniff = require("@rosetta/core/Sniff")
|
||||
local Types = require("@rosetta/Types")
|
||||
local Text = require("@rosetta/codecs/text/Text")
|
||||
local Parse = require("@rosetta/model/t3d/Parse")
|
||||
local Build = require("@rosetta/model/t3d/Build")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local T3d = {
|
||||
name = "t3d",
|
||||
category = "model",
|
||||
extensions = { "t3d" },
|
||||
mime = { "text/x-unreal-t3d" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "Types", "codecs/text/Text", "codecs/model/Brush" },
|
||||
caps = { decode = true, encode = false, text = true, brushes = true, actors = true },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- default 2^28
|
||||
maxBlocks: number?, -- default 2^22
|
||||
maxPolygons: number?, -- default 2^20
|
||||
textureSize: ((name: string) -> (number, number))? | { [string]: { number } }?, -- texture width, height (default 256)
|
||||
builder: boolean?, -- include the builder brush's geometry (default false)
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
function T3d.sniff(r: Bytes.Reader): number
|
||||
local h = Sniff.text(r, 512)
|
||||
local first = string.match(h, "^%s*(Begin%s+%a+)")
|
||||
if first == nil then
|
||||
return 0
|
||||
end
|
||||
local kind = string.lower(string.match(first, "%a+$") or "")
|
||||
if kind == "map" or kind == "actor" or kind == "level" or kind == "brush" or kind == "polylist" then
|
||||
return 0.9
|
||||
end
|
||||
return 0.3
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
function T3d.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Scene?, Err.Err?)
|
||||
local o: any = opts or {}
|
||||
if r.limit - r.base > (o.maxBytes or 2 ^ 28) then
|
||||
return nil, Err.new(Err.LIMIT, "file is over the byte limit")
|
||||
end
|
||||
local text = buffer.readstring(r.buf, r.base, r.limit - r.base)
|
||||
if string.sub(text, 1, 2) == "\255\254" then
|
||||
text = Text.utf16(r.buf, r.base + 2, r.limit - r.base - 2, false)
|
||||
else
|
||||
text = Text.toUtf8(text)
|
||||
end
|
||||
local ok, root, msg = pcall(Parse.tree, text, o.maxBlocks or 2 ^ 22)
|
||||
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
|
||||
if #root.children == 0 then
|
||||
return nil, Err.new(Err.CORRUPT, "no Begin blocks")
|
||||
end
|
||||
-- a bare brush export (Begin PolyList / Begin Brush): wrap it in an actor
|
||||
local k = string.lower(root.children[1].kind)
|
||||
if k == "polylist" or k == "brush" then
|
||||
local function block(kind: string, attrs: any, props: any, children: any): Parse.Block
|
||||
return { kind = kind, attrs = attrs, props = props, order = { "CsgOper" }, records = {}, degrees = {}, children = children }
|
||||
end
|
||||
local brush = if k == "brush" then root.children else { block("Brush", { Name = "Brush0" }, {}, root.children) }
|
||||
root = block("", {}, {}, { block("Actor", { Class = "Brush", Name = "Brush0" }, { CsgOper = "CSG_Add" }, brush) })
|
||||
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
|
||||
return scene, nil
|
||||
end
|
||||
|
||||
return T3d
|
||||
Reference in new issue
Block a user