MESH-Benchmark / code /generation /native /build_static_v1_scene.py
Heiheihaha17's picture
Add files using upload-large-folder tool
85407a7 verified
Raw History Blame Contribute Delete
11.3 kB
#!/usr/bin/env python3
"""Build the dependency-free static V1 exhibition calibration scene.
The output is a render/collision GLB plus a vertex-coloured semantic GLB. The
semantic object ids are deterministic and match the Gibson-style descriptor,
which lets the SoundSpaces audio sensor attach acoustic materials per surface.
"""
from __future__ import annotations
import argparse
import json
import struct
from pathlib import Path
ROOM_SIZES = {
"compact": (10.0, 4.0, 7.0),
"base": (14.0, 5.0, 10.0),
"large": (20.0, 6.0, 14.0),
}
def build_surfaces(room_size, layout_template, center_occluder_type):
width, height, depth = room_size
wall = 0.16
surfaces = [
("floor", "floor_surface", (0.0, -wall / 2, 0.0), (width, wall, depth)),
("ceiling", "ceiling_surface", (0.0, height + wall / 2, 0.0), (width, wall, depth)),
("wall_left", "wall_surface", (-width / 2 - wall / 2, height / 2, 0.0), (wall, height, depth)),
("wall_right", "wall_surface", (width / 2 + wall / 2, height / 2, 0.0), (wall, height, depth)),
("wall_front", "wall_surface", (0.0, height / 2, -depth / 2 - wall / 2), (width, height, wall)),
("wall_back", "wall_surface", (0.0, height / 2, depth / 2 + wall / 2), (width, height, wall)),
]
x_edge, z_edge = width / 2 - 1.5, depth / 2 - 1.2
if layout_template == "open_center":
surfaces.extend([
("case_back_left", "glass_display", (-0.72 * x_edge, 0.75, z_edge), (1.8, 1.5, 0.8)),
("case_back_right", "glass_display", (0.72 * x_edge, 0.75, z_edge), (1.8, 1.5, 0.8)),
("plinth_left", "wood_display", (-0.85 * x_edge, 0.55, -0.72 * z_edge), (1.2, 1.1, 1.2)),
("plinth_right", "wood_display", (0.85 * x_edge, 0.55, -0.72 * z_edge), (1.2, 1.1, 1.2)),
])
elif layout_template == "perimeter_displays":
surfaces.extend([
("case_back_left", "glass_display", (-0.65 * x_edge, 0.75, z_edge), (1.8, 1.5, 0.8)),
("case_back_right", "glass_display", (0.65 * x_edge, 0.75, z_edge), (1.8, 1.5, 0.8)),
("case_front_left", "glass_display", (-0.65 * x_edge, 0.75, -z_edge), (1.8, 1.5, 0.8)),
("case_front_right", "glass_display", (0.65 * x_edge, 0.75, -z_edge), (1.8, 1.5, 0.8)),
("plinth_left", "wood_display", (-x_edge, 0.55, 0.0), (0.8, 1.1, 1.8)),
("plinth_right", "wood_display", (x_edge, 0.55, 0.0), (0.8, 1.1, 1.8)),
])
elif layout_template == "central_islands":
island_x = min(width / 2 - 1.4, 3.6)
surfaces.extend([
("island_left", "glass_display", (-island_x, 0.8, -0.7), (1.4, 1.6, 2.2)),
("island_right", "glass_display", (island_x, 0.8, 0.7), (1.4, 1.6, 2.2)),
("plinth_back_left", "wood_display", (-2.8, 0.55, 2.7), (1.2, 1.1, 1.2)),
("plinth_back_right", "wood_display", (2.8, 0.55, 2.7), (1.2, 1.1, 1.2)),
])
elif layout_template == "corridor_like":
surfaces.extend([
("partition_left", "fabric_partition", (-2.2, 1.1, 0.7), (0.20, 2.2, 5.0)),
("partition_right", "fabric_partition", (2.2, 1.1, -0.7), (0.20, 2.2, 5.0)),
("case_back", "glass_display", (0.0, 0.75, z_edge), (2.0, 1.5, 0.8)),
])
else:
raise ValueError(f"Unknown layout template: {layout_template}")
if center_occluder_type != "none":
# The centre of this object intersects the listener-to-[0,1.55,-4]
# segment used by EASE-V1-07 while leaving the rear visitor pair clear.
surfaces.append(("target_path_occluder", center_occluder_type,
(0.0, 1.10, -2.0), (2.2, 2.2, 0.60)))
return tuple(surfaces)
def align4(data: bytearray) -> None:
while len(data) % 4:
data.append(0)
def add_box(vertices, normals, colors, indices, center, size, color):
cx, cy, cz = center
hx, hy, hz = (value / 2.0 for value in size)
faces = (
((1, 0, 0), ((hx, -hy, -hz), (hx, -hy, hz), (hx, hy, hz), (hx, hy, -hz))),
((-1, 0, 0), ((-hx, -hy, hz), (-hx, -hy, -hz), (-hx, hy, -hz), (-hx, hy, hz))),
((0, 1, 0), ((-hx, hy, -hz), (hx, hy, -hz), (hx, hy, hz), (-hx, hy, hz))),
((0, -1, 0), ((-hx, -hy, hz), (hx, -hy, hz), (hx, -hy, -hz), (-hx, -hy, -hz))),
((0, 0, 1), ((hx, -hy, hz), (-hx, -hy, hz), (-hx, hy, hz), (hx, hy, hz))),
((0, 0, -1), ((-hx, -hy, -hz), (hx, -hy, -hz), (hx, hy, -hz), (-hx, hy, -hz))),
)
for normal, corners in faces:
first = len(vertices)
for x, y, z in corners:
vertices.append((x + cx, y + cy, z + cz))
normals.append(normal)
colors.append(color)
indices.extend((first, first + 1, first + 2, first, first + 2, first + 3))
def build_glb(path: Path, surfaces, geometry_id: str) -> dict:
vertices, normals, colors, indices = [], [], [], []
for object_id, (_, _, center, size) in enumerate(surfaces):
# RGB values are deliberately unique and non-zero; alpha is opaque.
color = ((37 * object_id + 31) % 251 + 1, (73 * object_id + 47) % 251 + 1,
(109 * object_id + 61) % 251 + 1, 255)
add_box(vertices, normals, colors, indices, center, size, color)
binary = bytearray()
views = []
def append_view(blob: bytes, target: int) -> int:
align4(binary)
offset = len(binary)
binary.extend(blob)
views.append({"buffer": 0, "byteOffset": offset, "byteLength": len(blob), "target": target})
return len(views) - 1
pos_blob = b"".join(struct.pack("<3f", *value) for value in vertices)
normal_blob = b"".join(struct.pack("<3f", *value) for value in normals)
color_blob = b"".join(struct.pack("<4B", *value) for value in colors)
index_blob = b"".join(struct.pack("<I", value) for value in indices)
pos_view = append_view(pos_blob, 34962)
normal_view = append_view(normal_blob, 34962)
color_view = append_view(color_blob, 34962)
index_view = append_view(index_blob, 34963)
mins = [min(value[axis] for value in vertices) for axis in range(3)]
maxs = [max(value[axis] for value in vertices) for axis in range(3)]
gltf = {
"asset": {"version": "2.0", "generator": "EASE static V1 scene builder"},
"scene": 0,
"scenes": [{"nodes": [0]}],
"nodes": [{"mesh": 0, "name": geometry_id}],
"meshes": [{"name": "semantic_surfaces", "primitives": [{
"attributes": {"POSITION": 0, "NORMAL": 1, "COLOR_0": 2},
"indices": 3,
"mode": 4,
}]}],
"accessors": [
{"bufferView": pos_view, "componentType": 5126, "count": len(vertices),
"type": "VEC3", "min": mins, "max": maxs},
{"bufferView": normal_view, "componentType": 5126, "count": len(normals), "type": "VEC3"},
{"bufferView": color_view, "componentType": 5121, "normalized": True,
"count": len(colors), "type": "VEC4"},
{"bufferView": index_view, "componentType": 5125, "count": len(indices), "type": "SCALAR"},
],
"bufferViews": views,
"buffers": [{"byteLength": len(binary)}],
}
json_chunk = json.dumps(gltf, separators=(",", ":")).encode("utf-8")
while len(json_chunk) % 4:
json_chunk += b" "
align4(binary)
total = 12 + 8 + len(json_chunk) + 8 + len(binary)
output = bytearray(struct.pack("<4sII", b"glTF", 2, total))
output.extend(struct.pack("<I4s", len(json_chunk), b"JSON"))
output.extend(json_chunk)
output.extend(struct.pack("<I4s", len(binary), b"BIN\x00"))
output.extend(binary)
path.write_bytes(output)
return {"vertices": len(vertices), "triangles": len(indices) // 3, "bounds": [mins, maxs]}
def write_json(path: Path, value) -> None:
path.write_text(json.dumps(value, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
def main() -> None:
root = Path(__file__).resolve().parents[1]
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--room-size-variant", choices=sorted(ROOM_SIZES), default="base")
parser.add_argument("--layout-template", choices=("open_center", "perimeter_displays", "central_islands", "corridor_like"), default="open_center")
parser.add_argument("--center-occluder-type", choices=("none", "glass_display", "wood_display", "fabric_partition"), default="none")
parser.add_argument("--geometry-id")
parser.add_argument("--output-dir", type=Path)
args = parser.parse_args()
room_size = ROOM_SIZES[args.room_size_variant]
suffix = args.layout_template
if args.center_occluder_type != "none":
suffix += f"_blocked_{args.center_occluder_type}"
geometry_id = args.geometry_id or f"exhibition_hall_{args.room_size_variant}_{suffix}"
surfaces = build_surfaces(room_size, args.layout_template, args.center_occluder_type)
if args.output_dir is None:
args.output_dir = root / "scene" / "static_v1" / geometry_id
output_dir = args.output_dir.resolve()
output_dir.mkdir(parents=True, exist_ok=True)
glb = output_dir / f"{geometry_id}.glb"
semantic_glb = output_dir / f"{geometry_id}.semantic.glb"
mesh_stats = build_glb(glb, surfaces, geometry_id)
semantic_glb.write_bytes(glb.read_bytes())
objects = []
for object_id, (name, category, center, size) in enumerate(surfaces):
objects.append({"id": object_id, "class_": category, "name": name,
"location": list(center), "size": list(size)})
descriptor = output_dir / f"{geometry_id}.semantic.json"
write_json(descriptor, {"objects": objects})
stage = output_dir / f"{geometry_id}.stage_config.json"
write_json(stage, {
"render_asset": glb.name,
"collision_asset": glb.name,
"semantic_asset": semantic_glb.name,
"semantic_descriptor_filename": descriptor.name,
"up": [0, 1, 0], "front": [0, 0, -1],
"semantic_up": [0, 1, 0], "semantic_front": [0, 0, -1],
"gravity": [0, -9.8, 0], "units_to_meters": 1.0,
})
dataset = output_dir / "static_v1.scene_dataset_config.json"
write_json(dataset, {"stages": {"configs": [{
"template_handle": geometry_id,
"attributes": {
"render_asset": glb.name,
"collision_asset": glb.name,
"semantic_asset": semantic_glb.name,
"semantic_descriptor_filename": descriptor.name,
"up": [0, 1, 0], "front": [0, 0, -1],
"semantic_up": [0, 1, 0], "semantic_front": [0, 0, -1],
"units_to_meters": 1.0,
},
}]}})
layout = {
"geometry_id": geometry_id,
"room_size_variant": args.room_size_variant,
"room_size_m": list(room_size),
"layout_template": args.layout_template,
"center_occluder_type": args.center_occluder_type,
"render_asset": str(glb), "semantic_asset": str(semantic_glb),
"semantic_descriptor": str(descriptor), "stage_config": str(stage),
"scene_dataset_config": str(dataset), "surfaces": objects,
**mesh_stats,
}
write_json(output_dir / "layout.json", layout)
print(json.dumps(layout, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()