205 lines
11 KiB
Python
205 lines
11 KiB
Python
# Unreal T3D fixtures (tools/fixtures.py): maps written here in UnrealEd's export layout (UE1-3: Begin Map / Actor /
|
|
# Brush / PolyList / Polygon with Origin / Normal / TextureU / TextureV / Pan / Vertex records, Location / Rotation in
|
|
# 65536ths of a turn, PrePivot / MainScale / PostScale; UE4: Begin Level, components with Relative* in degrees), with
|
|
# reference values computed here independently: each brush's world vertices (ABrush coords: v - PrePivot, MainScale,
|
|
# FRotationMatrix, PostScale, + Location), polygon areas per texture, bounds, and the texel coordinates of probe
|
|
# vertices; actors, lights and static mesh references.
|
|
# tests/gen/t3d.luau { cases = { { name, data, sizes = { [texture] = { w, h } }, actors = { { name, class, mesh?,
|
|
# area = { [texture] = a }, triangles, min, max, probe = { x, y, z, u, v }, origin } } } } }
|
|
import math
|
|
|
|
INPUTS = ["src/Rosetta/model/t3d"]
|
|
|
|
|
|
def box(sx, sy, sz, tex):
|
|
"""an axis box centred on 0: six quads as UnrealEd's cube builder lists them (CCW about the outward normal)"""
|
|
hx, hy, hz = sx / 2, sy / 2, sz / 2
|
|
faces = []
|
|
for axis in range(3):
|
|
for sign in (-1, 1):
|
|
n = [0, 0, 0]
|
|
n[axis] = sign
|
|
a, b = [(1, 2), (2, 0), (0, 1)][axis]
|
|
h = [hx, hy, hz]
|
|
pts = []
|
|
for ca, cb in ((-1, -1), (-1, 1), (1, 1), (1, -1)) if sign < 0 else ((-1, -1), (1, -1), (1, 1), (-1, 1)):
|
|
p = [0, 0, 0]
|
|
p[axis] = sign * h[axis]
|
|
p[a] = ca * h[a]
|
|
p[b] = cb * h[b]
|
|
pts.append(p)
|
|
# make it CCW about n (right-handed cross)
|
|
e1 = [pts[1][i] - pts[0][i] for i in range(3)]
|
|
e2 = [pts[2][i] - pts[0][i] for i in range(3)]
|
|
c = [e1[1] * e2[2] - e1[2] * e2[1], e1[2] * e2[0] - e1[0] * e2[2], e1[0] * e2[1] - e1[1] * e2[0]]
|
|
if sum(c[i] * n[i] for i in range(3)) < 0:
|
|
pts.reverse()
|
|
u = [0, 0, 0]
|
|
v = [0, 0, 0]
|
|
u[a] = 1
|
|
v[b] = -1
|
|
faces.append({"tex": tex(axis, sign), "normal": n, "origin": pts[0], "u": u, "v": v, "pan": (axis * 8, sign * 4), "verts": pts, "flags": 0})
|
|
return faces
|
|
|
|
|
|
def fmt(v):
|
|
return ",".join("%+013.6f" % x for x in v)
|
|
|
|
|
|
def polylist(faces, ind=" "):
|
|
out = [ind + "Begin PolyList"]
|
|
for f in faces:
|
|
out.append(ind + " Begin Polygon Texture=%s Flags=%d Link=0" % (f["tex"], f["flags"]))
|
|
out.append(ind + " Origin " + fmt(f["origin"]))
|
|
out.append(ind + " Normal " + fmt(f["normal"]))
|
|
out.append(ind + " TextureU " + fmt(f["u"]))
|
|
out.append(ind + " TextureV " + fmt(f["v"]))
|
|
out.append(ind + " Pan U=%d V=%d" % f["pan"])
|
|
for p in f["verts"]:
|
|
out.append(ind + " Vertex " + fmt(p))
|
|
out.append(ind + " End Polygon")
|
|
out.append(ind + "End PolyList")
|
|
return out
|
|
|
|
|
|
def rotmat(pitch, yaw, roll, k):
|
|
p, y, r = pitch * k, yaw * k, roll * k
|
|
SP, CP, SY, CY, SR, CR = math.sin(p), math.cos(p), math.sin(y), math.cos(y), math.sin(r), math.cos(r)
|
|
# FRotationMatrix rows = axes
|
|
return [[CP * CY, CP * SY, SP], [SR * SP * CY - CR * SY, SR * SP * SY + CR * CY, -SR * CP], [-(CR * SP * CY + SR * SY), CY * SR - CR * SP * SY, CR * CP]]
|
|
|
|
|
|
def world(v, a):
|
|
x = [v[i] - a["prepivot"][i] for i in range(3)]
|
|
x = [x[i] * a["main"][i] for i in range(3)]
|
|
R = a["rot"]
|
|
x = [x[0] * R[0][i] + x[1] * R[1][i] + x[2] * R[2][i] for i in range(3)]
|
|
x = [x[i] * a["post"][i] for i in range(3)]
|
|
return [x[i] + a["loc"][i] for i in range(3)]
|
|
|
|
|
|
def area(pts):
|
|
ax = ay = az = 0
|
|
for k in range(1, len(pts) - 1):
|
|
u = [pts[k][i] - pts[0][i] for i in range(3)]
|
|
v = [pts[k + 1][i] - pts[0][i] for i in range(3)]
|
|
ax += u[1] * v[2] - u[2] * v[1]
|
|
ay += u[2] * v[0] - u[0] * v[2]
|
|
az += u[0] * v[1] - u[1] * v[0]
|
|
return math.sqrt(ax * ax + ay * ay + az * az) / 2
|
|
|
|
|
|
def reference(a, faces, sizes, builder=False):
|
|
ref = {"name": a["name"], "class": a["class"]}
|
|
if builder:
|
|
return ref
|
|
ar, tris = {}, 0
|
|
lo, hi = [1e30] * 3, [-1e30] * 3
|
|
for f in faces:
|
|
w = [world(p, a) for p in f["verts"]]
|
|
ar[f["tex"]] = ar.get(f["tex"], 0) + area(w)
|
|
tris += len(w) - 2
|
|
for p in w:
|
|
lo = [min(lo[i], p[i]) for i in range(3)]
|
|
hi = [max(hi[i], p[i]) for i in range(3)]
|
|
f = faces[0]
|
|
p = f["verts"][2]
|
|
W, H = sizes.get(f["tex"], (256, 256))
|
|
du = sum((p[i] - f["origin"][i]) * f["u"][i] for i in range(3)) + f["pan"][0]
|
|
dv = sum((p[i] - f["origin"][i]) * f["v"][i] for i in range(3)) + f["pan"][1]
|
|
ref.update({"mesh": True, "area": ar, "triangles": tris, "min": lo, "max": hi, "probe": list(p) + [du / W, dv / H]})
|
|
return ref
|
|
|
|
|
|
K = math.pi * 2 / 65536
|
|
|
|
|
|
def ue1():
|
|
sizes = {"Bricks": (128, 64), "Floor": (256, 256), "Glass": (64, 64)}
|
|
lines = ["Begin Map", "Begin Actor Class=LevelInfo Name=LevelInfo0", " Title=\"Test Level\"", " Author=\"rosetta\"", "End Actor"]
|
|
actors = []
|
|
# the builder brush (skipped)
|
|
builder = {"name": "Brush", "class": "Brush", "loc": [0, 0, 0], "rot": rotmat(0, 0, 0, K), "prepivot": [0, 0, 0], "main": [1, 1, 1], "post": [1, 1, 1]}
|
|
fb = box(256, 256, 256, lambda a, s: "Bricks")
|
|
lines += ["Begin Actor Class=Brush Name=Brush", " MainScale=(SheerAxis=SHEER_ZX)", " PostScale=(SheerAxis=SHEER_ZX)", " Begin Brush Name=Brush"] + polylist(fb) + [" End Brush", " Brush=Model'MyLevel.Brush'", "End Actor"]
|
|
actors.append(reference(builder, fb, sizes, True))
|
|
# a subtracted room: yawed a quarter turn, pre-pivot, main scale
|
|
room = {"name": "Brush0", "class": "Brush", "loc": [64, -32, 16], "rot": rotmat(0, 16384, 0, K), "prepivot": [16, 0, 8], "main": [2, 1, 0.5], "post": [1, 1, 1]}
|
|
fr = box(512, 384, 256, lambda a, s: "Floor" if a == 2 else "Bricks")
|
|
lines += ["Begin Actor Class=Brush Name=Brush0", " CsgOper=CSG_Subtract", " MainScale=(Scale=(X=2.000000,Z=0.500000),SheerAxis=SHEER_ZX)",
|
|
" PostScale=(SheerAxis=SHEER_ZX)", " Location=(X=64.000000,Y=-32.000000,Z=16.000000)", " Rotation=(Yaw=16384)",
|
|
" PrePivot=(X=16.000000,Z=8.000000)", " Begin Brush Name=Model1"] + polylist(fr) + [" End Brush", " Brush=Model'MyLevel.Model1'", "End Actor"]
|
|
actors.append(reference(room, fr, sizes))
|
|
# an added pillar: pitch + roll, post scale
|
|
pil = {"name": "Brush1", "class": "Brush", "loc": [-100, 50, 0], "rot": rotmat(8192, 0, 4096, K), "prepivot": [0, 0, 0], "main": [1, 1, 1], "post": [1, 1.5, 2]}
|
|
fp = box(32, 32, 200, lambda a, s: "Glass")
|
|
for f in fp[:2]:
|
|
f["flags"] = 2
|
|
lines += ["Begin Actor Class=Brush Name=Brush1", " CsgOper=CSG_Add", " PostScale=(Scale=(Y=1.500000,Z=2.000000),SheerAxis=SHEER_ZX)",
|
|
" Location=(X=-100.000000,Y=50.000000)", " Rotation=(Pitch=8192,Roll=4096)", " Begin Brush Name=Model2"] + polylist(fp) + [" End Brush", " Brush=Model'MyLevel.Model2'", "End Actor"]
|
|
actors.append(reference(pil, fp, sizes))
|
|
lines += ["Begin Actor Class=Light Name=Light0", " LightBrightness=128", " LightHue=40", " LightSaturation=64", " LightRadius=32",
|
|
" Location=(X=0.000000,Y=0.000000,Z=96.000000)", "End Actor"]
|
|
actors.append({"name": "Light0", "class": "Light", "origin": [0, 0, 96]})
|
|
lines += ["Begin Actor Class=PlayerStart Name=PlayerStart0", " Location=(X=10.000000,Y=20.000000,Z=30.000000)", " Rotation=(Yaw=-16384)", " DrawScale=2.000000", "End Actor"]
|
|
actors.append({"name": "PlayerStart0", "class": "PlayerStart", "origin": [10, 20, 30]})
|
|
lines += ["End Map", ""]
|
|
return "\r\n".join(lines), sizes, actors
|
|
|
|
|
|
def ue4():
|
|
sizes = {}
|
|
lines = ["Begin Map", " Begin Level"]
|
|
actors = []
|
|
a = {"name": "Brush_1", "class": "/Script/Engine.Brush", "loc": [100, 200, 300], "rot": rotmat(0, 45, 0, math.pi / 180), "prepivot": [0, 0, 0], "main": [1, 1, 1], "post": [1, 1, 1]}
|
|
fb = box(200, 200, 100, lambda ax, s: "/Game/Materials/M_Wall.M_Wall")
|
|
lines += [" Begin Actor Class=/Script/Engine.Brush Name=Brush_1 Archetype=/Script/Engine.Brush'/Script/Engine.Default__Brush'",
|
|
" Begin Object Class=/Script/Engine.BrushComponent Name=\"BrushComponent0\" Archetype=BrushComponent'/Script/Engine.Default__Brush:BrushComponent0'",
|
|
" End Object",
|
|
" Begin Object Name=\"BrushComponent0\"",
|
|
" Brush=Model'\"Model_1\"'",
|
|
" RelativeLocation=(X=100.000000,Y=200.000000,Z=300.000000)",
|
|
" RelativeRotation=(Pitch=0.000000,Yaw=45.000000,Roll=0.000000)",
|
|
" End Object",
|
|
" BrushType=Brush_Add",
|
|
" Begin Brush Name=Model_1"] + polylist(fb, " ") + [" End Brush",
|
|
" Brush=Model'\"Model_1\"'", " BrushComponent=BrushComponent'\"BrushComponent0\"'", " RootComponent=BrushComponent'\"BrushComponent0\"'",
|
|
" ActorLabel=\"Box Brush\"", " End Actor"]
|
|
actors.append(reference(a, fb, sizes))
|
|
lines += [" Begin Actor Class=/Script/Engine.StaticMeshActor Name=StaticMeshActor_3",
|
|
" Begin Object Class=/Script/Engine.StaticMeshComponent Name=\"StaticMeshComponent0\"",
|
|
" End Object",
|
|
" Begin Object Name=\"StaticMeshComponent0\"",
|
|
" StaticMesh=StaticMesh'\"/Game/Meshes/SM_Chair.SM_Chair\"'",
|
|
" RelativeLocation=(X=-50.000000,Y=0.000000,Z=10.000000)",
|
|
" RelativeScale3D=(X=2.000000,Y=2.000000,Z=2.000000)",
|
|
" End Object",
|
|
" StaticMeshComponent=StaticMeshComponent0",
|
|
" RootComponent=StaticMeshComponent0",
|
|
" End Actor"]
|
|
actors.append({"name": "StaticMeshActor_3", "class": "/Script/Engine.StaticMeshActor", "origin": [-50, 0, 10], "staticMesh": "/Game/Meshes/SM_Chair.SM_Chair"})
|
|
lines += [" Begin Actor Class=/Script/Engine.PointLight Name=PointLight_0",
|
|
" Begin Object Class=/Script/Engine.PointLightComponent Name=\"LightComponent0\"",
|
|
" End Object",
|
|
" Begin Object Name=\"LightComponent0\"",
|
|
" AttenuationRadius=500.000000",
|
|
" Intensity=3000.000000",
|
|
" LightColor=(B=64,G=128,R=255,A=255)",
|
|
" RelativeLocation=(X=0.000000,Y=0.000000,Z=250.000000)",
|
|
" End Object",
|
|
" PointLightComponent=LightComponent0",
|
|
" LightComponent=LightComponent0",
|
|
" RootComponent=LightComponent0",
|
|
" End Actor"]
|
|
actors.append({"name": "PointLight_0", "class": "/Script/Engine.PointLight", "origin": [0, 0, 250]})
|
|
lines += [" End Level", "Begin Surface", "End Surface", "End Map", ""]
|
|
return "\n".join(lines), sizes, actors
|
|
|
|
|
|
def generate(ctx):
|
|
cases = []
|
|
for name, f in (("ue1.t3d", ue1), ("ue4.t3d", ue4)):
|
|
text, sizes, actors = f()
|
|
cases.append({"name": name, "data": text.encode(), "sizes": {k: list(v) for k, v in sizes.items()}, "actors": actors})
|
|
ctx.emit("t3d", {"cases": cases})
|