assemble-cad-bim.py 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. """Export a CAD/BIM seated plan as a spatial-LOD 3D Tiles tree.
  2. Run with Blender: blender --background --factory-startup --python-exit-code 1 \
  3. --python tools/city-massing/assemble-cad-bim.py -- \
  4. --plan tools/city-massing/cad-bim/seated-plan.json \
  5. --output tools/city-massing/cad-bim/lod-preview --grid 32 --coarse-grid 8
  6. Same output conventions as assemble-lod-district.py (GLB cells, `box()` bounding volumes,
  7. tileset.json/manifest.json, root-anchor.glb), so verify-lod-district.mjs still gates it.
  8. Difference: input footprints are surveyed polygons, so `kind == "massing"` buildings are
  9. built here as extruded prisms instead of being copied from the house library.
  10. """
  11. import json
  12. import math
  13. import struct
  14. import argparse
  15. import sys
  16. from pathlib import Path
  17. import bpy
  18. import bmesh
  19. from mathutils import Vector
  20. from mathutils.geometry import tessellate_polygon
  21. ROOT = Path(__file__).resolve().parents[2]
  22. parser = argparse.ArgumentParser()
  23. parser.add_argument('--plan', required=True)
  24. parser.add_argument('--output', required=True)
  25. parser.add_argument('--grid', type=int, default=32)
  26. parser.add_argument('--coarse-grid', type=int, default=0)
  27. parser.add_argument('--limit', type=int, default=0, help='export only the first N buildings')
  28. parser.add_argument('--decimate-lod1', type=float, default=1.0,
  29. help='Decimate ratio applied to library asset meshes at LOD1 (1.0 = off)')
  30. parser.add_argument('--decimate-lod0', type=float, default=1.0,
  31. help='Decimate ratio applied to library asset meshes at LOD0 (1.0 = off)')
  32. parser.add_argument('--merge-buildings', action='store_true',
  33. help='Bake each cell into ONE merged mesh (per-material primitives) instead of one node per building')
  34. parser.add_argument('--lod1-footprint-only', action='store_true',
  35. help='Use each surveyed GIS footprint for LOD1/coarse geometry, including asset buildings')
  36. parser.add_argument('--draco', action='store_true', help='Enable Draco mesh compression in GLB export')
  37. args = parser.parse_args(sys.argv[sys.argv.index('--') + 1:] if '--' in sys.argv else [])
  38. for ratio in (args.decimate_lod1, args.decimate_lod0):
  39. if not (0.0 < ratio <= 1.0):
  40. raise RuntimeError('--decimate-lod* ratios must be in (0, 1]')
  41. if args.grid < 2:
  42. raise RuntimeError('--grid must be at least 2')
  43. if args.coarse_grid and (args.coarse_grid < 2 or args.coarse_grid >= args.grid):
  44. raise RuntimeError('--coarse-grid must be at least 2 and smaller than --grid')
  45. PLAN = Path(args.plan).resolve()
  46. OUT = Path(args.output).resolve()
  47. LIBRARY_DIR = ROOT / 'tools/city-massing/house-library'
  48. plan = json.loads(PLAN.read_text(encoding='utf-8'))
  49. lib = json.loads((LIBRARY_DIR / 'library.json').read_text(encoding='utf-8'))
  50. entries = lib['archetypes']
  51. # Same asset-fit contract the placement generator used, so the two stages cannot disagree about
  52. # what counts as an acceptable match.
  53. ASSET_FIT = json.loads((ROOT / 'tools/city-massing/asset-fit.json').read_text(encoding='utf-8'))['variants']
  54. # Scene variety without random re-rolls: a stable hash of the building id picks one wall tone and
  55. # one roof tone, so neighbours differ while the whole extrusion stays coherent.
  56. WALL_TONES = ('Plaster_Cream', 'Plaster_Ivory', 'Plaster_Sand', 'Concrete_Warm')
  57. ROOF_TONES = ('Roof_Slate', 'Roof_Slate_Light', 'Roof_Terracotta', 'Roof_Terra_Light')
  58. def select_asset(building):
  59. family = building.get('archetype') or 'house'
  60. prefix = {'house': 'house', 'apartment': 'slab', 'midrise': 'midrise'}.get(family, 'house')
  61. # A building may name the exact approved variants it was planned for; otherwise any variant of
  62. # its family that fits the shared scale window is eligible.
  63. preferred = building.get('preferredAssets') or []
  64. family_key = family if family in ASSET_FIT else prefix
  65. limits = ASSET_FIT.get(family_key) or ASSET_FIT.get(prefix) or ASSET_FIT['house']
  66. min_x, min_y = limits['minScale']
  67. max_x, max_y = limits['maxScale']
  68. candidates = []
  69. for entry in entries:
  70. if preferred:
  71. if entry['id'] not in preferred:
  72. continue
  73. elif not entry['id'].startswith(prefix):
  74. continue
  75. width, depth = entry['nominal_footprint_m']
  76. sx, sy = building['width'] / width, building['depth'] / depth
  77. if not (min_x <= sx <= max_x and min_y <= sy <= max_y):
  78. continue
  79. preference = preferred.index(entry['id']) if entry['id'] in preferred else len(preferred) + 1
  80. score = preference * 0.25 + abs(entry['floors'] - building['floors']) * 0.8 + abs(math.log(sx)) + abs(math.log(sy))
  81. candidates.append((score, entry, sx, sy))
  82. return min(candidates, key=lambda item: item[0]) if candidates else None
  83. # ---------------------------------------------------------------- polygon helpers
  84. def ring_area(points):
  85. """Signed XY area; positive means counter-clockwise."""
  86. return 0.5 * sum(points[i][0] * points[(i + 1) % len(points)][1]
  87. - points[(i + 1) % len(points)][0] * points[i][1] for i in range(len(points)))
  88. def as_ccw(points):
  89. """Winding fix: surveyed rings arrive CW or CCW, prisms need one known orientation."""
  90. return list(points) if ring_area(points) > 0 else list(reversed(points))
  91. def convex_hull(points):
  92. """Monotone chain; used as the cheap LOD1 outline for a massing prism."""
  93. ordered = sorted({(round(x, 6), round(y, 6)) for x, y in points})
  94. if len(ordered) < 3:
  95. return ordered
  96. def half(seq):
  97. out = []
  98. for point in seq:
  99. while len(out) >= 2 and ((out[-1][0] - out[-2][0]) * (point[1] - out[-2][1])
  100. - (out[-1][1] - out[-2][1]) * (point[0] - out[-2][0])) <= 0:
  101. out.pop()
  102. out.append(point)
  103. return out
  104. return half(ordered)[:-1] + half(reversed(ordered))[:-1]
  105. def prism_triangles(n):
  106. """n-gon prism: two n-gon caps plus n wall quads."""
  107. return 4 * n - 4
  108. def facade_window_count(edge_length, level):
  109. """Small, bounded facade detail count; LOD1 keeps one readable window per edge."""
  110. if level == 'lod1':
  111. return 1
  112. return max(1, min(3, int(edge_length / 3.5)))
  113. def massing_triangles(footprint, floors, level):
  114. """Reference triangles for a massing prism plus lightweight facade details."""
  115. base = prism_triangles(len(footprint))
  116. windows = sum(2 * facade_window_count(math.hypot(
  117. footprint[(i + 1) % len(footprint)][0] - footprint[i][0],
  118. footprint[(i + 1) % len(footprint)][1] - footprint[i][1]), level) * floors
  119. for i in range(len(footprint)))
  120. return base + windows + 12 # one closed six-face door box on the longest facade
  121. def poly_prism(footprint, bottom, top, wall_material, roof_material, anchor, floors, level,
  122. window_material, door_material):
  123. """Build a surveyed-outline building with bounded facade details.
  124. The solid uses the exact GIS ring. Windows are shallow, outward-facing quads repeated per
  125. floor and door is one first-floor quad on the longest wall. They add a readable facade without
  126. adding separate Blender objects or changing the building footprint.
  127. """
  128. # Create the mesh directly: repeated scene-wide select/cube operators make city exports quadratic.
  129. mesh = bpy.data.meshes.new('Surveyed_building')
  130. obj = bpy.data.objects.new('Surveyed_building', mesh)
  131. n = len(footprint)
  132. ax, ay, az = anchor
  133. vertices = [(x - ax, y - ay, bottom - az) for x, y in footprint] + \
  134. [(x - ax, y - ay, top - az) for x, y in footprint]
  135. faces = [(i, (i + 1) % n, n + (i + 1) % n, n + i) for i in range(n)]
  136. materials = [0] * n
  137. # Explicit caps survive merging/export and also handle concave surveyed outlines.
  138. # Use the same footprint vertices; no hull, overhang or XY displacement is introduced.
  139. cap_points = [Vector((x - ax, y - ay, 0)) for x, y in footprint]
  140. cap_indices = {tuple(point): i for i, point in enumerate(cap_points)}
  141. roof_area = 0.0
  142. for triangle in tessellate_polygon([cap_points]):
  143. # Blender 5.2 returns point indices; earlier versions returned Vector triples.
  144. indices = [point if isinstance(point, int) else cap_indices[tuple(point)] for point in triangle]
  145. area = ring_area([footprint[index] for index in indices])
  146. if abs(area) < 1e-10:
  147. continue
  148. if area < 0:
  149. indices.reverse()
  150. faces.append(tuple(n + index for index in indices)) # roof points up
  151. materials.append(1)
  152. faces.append(tuple(reversed(indices))) # buried bottom points down
  153. materials.append(0)
  154. roof_area += abs(area)
  155. # GIS vertices are rounded to centimetres. One real ring contains a 1cm backtrack and its
  156. # signed area differs by 0.08m² from the filled roof. Bound that tolerance by source precision,
  157. # instead of accepting missing caps or moving the whole surveyed footprint.
  158. perimeter = sum(math.hypot(footprint[(i + 1) % n][0] - p[0],
  159. footprint[(i + 1) % n][1] - p[1]) for i, p in enumerate(footprint))
  160. tolerance = max(0.001, roof_area * 1e-6, perimeter * 0.01)
  161. if abs(roof_area - ring_area(footprint)) > tolerance:
  162. raise RuntimeError('Triangulated roof does not cover its GIS footprint')
  163. floor_height = max(2.4, (top - bottom) / max(1, floors))
  164. def edge_panel_points(a, b, z0, z1, inset, width_ratio=0.42):
  165. dx, dy = b[0] - a[0], b[1] - a[1]
  166. length = math.hypot(dx, dy)
  167. if length < 0.35 or z1 <= z0:
  168. return None
  169. nx, ny = dy / length, -dx / length
  170. half = min(length * 0.22, max(0.45, length / 3.5 * width_ratio))
  171. cx, cy = (a[0] + b[0]) * 0.5, (a[1] + b[1]) * 0.5
  172. tx, ty = dx / length, dy / length
  173. return [(cx - tx * half + nx * inset, cy - ty * half + ny * inset),
  174. (cx + tx * half + nx * inset, cy + ty * half + ny * inset)]
  175. def add_panel(a, b, z0, z1, material_index, inset=0.035):
  176. points = edge_panel_points(a, b, z0, z1, inset)
  177. if not points:
  178. return
  179. base = len(vertices)
  180. vertices.extend([(x - ax, y - ay, z - az) for x, y in
  181. [(points[0][0], points[0][1]), (points[1][0], points[1][1])]
  182. for z in (z0, z1)])
  183. faces.append((base, base + 2, base + 3, base + 1))
  184. materials.append(material_index)
  185. def add_box_panel(a, b, z0, z1, material_index):
  186. """Closed shallow door box; keeps a door material through merged GLB export."""
  187. points = edge_panel_points(a, b, z0, z1, 0.055, 0.72)
  188. if not points:
  189. return
  190. dx, dy = b[0] - a[0], b[1] - a[1]
  191. length = math.hypot(dx, dy) or 1
  192. nx, ny = dy / length, -dx / length
  193. outer = 0.14
  194. inner = 0.045
  195. p = points
  196. base = len(vertices)
  197. for depth in (inner, outer):
  198. vertices.extend([(p[0][0] - ax + nx * depth, p[0][1] - ay + ny * depth, z - az)
  199. for z in (z0, z1)])
  200. vertices.extend([(p[1][0] - ax + nx * depth, p[1][1] - ay + ny * depth, z - az)
  201. for z in (z0, z1)])
  202. faces.extend([(base, base + 1, base + 3, base + 2),
  203. (base + 4, base + 6, base + 7, base + 5),
  204. (base, base + 4, base + 5, base + 1),
  205. (base + 2, base + 3, base + 7, base + 6),
  206. (base, base + 2, base + 6, base + 4),
  207. (base + 1, base + 5, base + 7, base + 3)])
  208. materials.extend([material_index] * 6)
  209. # One or a few windows per wall and floor, with a dark blue material that cannot read as a flat cube.
  210. for i in range(n):
  211. a, b = footprint[i], footprint[(i + 1) % n]
  212. count = facade_window_count(math.hypot(b[0] - a[0], b[1] - a[1]), level)
  213. dx, dy = b[0] - a[0], b[1] - a[1]
  214. for floor in range(max(1, floors)):
  215. z0 = bottom + floor * floor_height + floor_height * 0.34
  216. z1 = min(top - 0.08, z0 + floor_height * 0.36)
  217. for window in range(count):
  218. t = (window + 1) / (count + 1)
  219. center = (a[0] + dx * t, a[1] + dy * t)
  220. span = math.hypot(dx, dy) / (count + 1) * 0.34
  221. wa = (center[0] - dx / (math.hypot(dx, dy) or 1) * span,
  222. center[1] - dy / (math.hypot(dx, dy) or 1) * span)
  223. wb = (center[0] + dx / (math.hypot(dx, dy) or 1) * span,
  224. center[1] + dy / (math.hypot(dx, dy) or 1) * span)
  225. add_panel(wa, wb, z0, z1, 2)
  226. # One door on the longest wall, visible from at least one approach direction.
  227. longest = max(range(n), key=lambda i: math.hypot(footprint[(i + 1) % n][0] - footprint[i][0],
  228. footprint[(i + 1) % n][1] - footprint[i][1]))
  229. edge_a = footprint[longest]
  230. edge_b = footprint[(longest + 1) % n]
  231. door_h = min(2.2, max(1.8, floor_height * 0.76))
  232. add_box_panel(edge_a, edge_b, bottom + 0.08, min(top - 0.05, bottom + door_h), 3)
  233. mesh.from_pydata(vertices, [], faces)
  234. obj.location = anchor
  235. edit = bmesh.new()
  236. edit.from_mesh(mesh)
  237. bmesh.ops.recalc_face_normals(edit, faces=edit.faces)
  238. edit.to_mesh(mesh)
  239. edit.free()
  240. mesh.update()
  241. for material in (wall_material, roof_material, window_material, door_material):
  242. mesh.materials.append(material)
  243. mesh.polygons.foreach_set('material_index', materials)
  244. mesh.update()
  245. return obj
  246. def prism_volume_sign(obj):
  247. """Volume of obj's closed surface read from Blender's own face normals.
  248. V = sum(centre . normal * area) / 3 is positive exactly when the normals point outward, which is
  249. the property the GLB export and every downstream renderer depend on. Local space is enough: the
  250. prisms are built unrotated and unscaled, and reading Blender's normals avoids re-deriving the
  251. winding sign by hand.
  252. """
  253. return sum(polygon.center.dot(polygon.normal) * polygon.area
  254. for polygon in obj.data.polygons) / 3
  255. def box(bounds):
  256. low, high = bounds
  257. centre = [(a + b) / 2 for a, b in zip(low, high)]
  258. half = [(b - a) / 2 for a, b in zip(low, high)]
  259. return [centre[0], centre[1], centre[2], half[0], 0, 0, 0, half[1], 0, 0, 0, half[2]]
  260. # ---------------------------------------------------------------- plan intake
  261. def build_geometry(building):
  262. """Return (level -> (outline, triangles)) for one building, validating its outline."""
  263. outline = [(float(x), float(y)) for x, y in building['footprintENU']]
  264. if len(outline) < 3:
  265. raise RuntimeError(building['id'] + ' has fewer than 3 outline points')
  266. outline = as_ccw(outline)
  267. if building['kind'] == 'asset' and not args.lod1_footprint_only:
  268. # The library mesh is already authored in this ring; only the transform differs per level.
  269. return {level: (None, None) for level in ('lod0', 'lod1')}
  270. if ring_area(outline) <= 0:
  271. raise RuntimeError(building['id'] + ' outline is degenerate')
  272. if building['positionENU'][2] + building['heightM'] <= building['foundationBottom']:
  273. raise RuntimeError(building['id'] + ' massing prism has zero volume')
  274. # Both runtime levels use the surveyed ring. LOD1 reduces facade detail, not the GIS footprint;
  275. # replacing an irregular ring with its hull was the source of visibly misplaced road-covering mass.
  276. return {'lod0': (outline, massing_triangles(outline, building['floors'], 'lod0')),
  277. 'lod1': (outline, massing_triangles(outline, building['floors'], 'lod1'))}
  278. placements, rejected, expected_triangles = [], [], {}
  279. library_ids = {entry['id'] for entry in entries}
  280. for building in plan['buildings'][:args.limit or None]:
  281. if building['kind'] == 'asset':
  282. # A named variant that is not in the library is a broken plan, not a building to skip.
  283. unknown = set(building.get('preferredAssets') or []).difference(library_ids)
  284. if unknown:
  285. raise RuntimeError(f"{building['id']} names missing library objects: " + ', '.join(sorted(unknown)))
  286. choice = select_asset(building)
  287. if not choice:
  288. rejected.append(building['id'])
  289. continue
  290. _, entry, sx, sy = choice
  291. expected_triangles[building['id']] = None # filled from the library once templates load
  292. placements.append((building, entry, sx, sy))
  293. elif building['kind'] == 'massing':
  294. geometry = build_geometry(building)
  295. expected_triangles[building['id']] = {level: geometry[level][1] for level in geometry}
  296. placements.append((building, None, None, None))
  297. else:
  298. raise RuntimeError(building['id'] + ' has unknown kind ' + str(building['kind']))
  299. if not placements or len({b['id'] for b, _, _, _ in placements}) != len(placements):
  300. raise RuntimeError('The seated plan does not yield unique, nonempty placements')
  301. # ---------------------------------------------------------------- scene setup
  302. bpy.ops.wm.read_factory_settings(use_empty=True)
  303. bpy.context.scene.unit_settings.system = 'METRIC'
  304. bpy.context.scene.unit_settings.scale_length = 1
  305. OUT.mkdir(parents=True, exist_ok=True)
  306. for pattern in ('*-lod0.glb', '*-lod1.glb'):
  307. for stale in OUT.glob(pattern):
  308. stale.unlink()
  309. for stale in (OUT / 'root-anchor.glb', OUT / 'tileset.json', OUT / 'manifest.json'):
  310. if stale.exists():
  311. stale.unlink()
  312. stone = bpy.data.materials.new('Buried_foundation_stone')
  313. stone.diffuse_color = (0.32, 0.33, 0.32, 1)
  314. stone.use_nodes = True
  315. stone.node_tree.nodes['Principled BSDF'].inputs['Base Color'].default_value = stone.diffuse_color
  316. stone.node_tree.nodes['Principled BSDF'].inputs['Roughness'].default_value = 0.92
  317. # The dimensions/height vary per foundation; scale a shared unit cube mesh per instance.
  318. foundation_mesh = bpy.data.meshes.new('Seated_foundation_unit_cube')
  319. foundation_mesh.from_pydata(
  320. [(-.5, -.5, -.5), (.5, -.5, -.5), (.5, .5, -.5), (-.5, .5, -.5),
  321. (-.5, -.5, .5), (.5, -.5, .5), (.5, .5, .5), (-.5, .5, .5)],
  322. [],
  323. [(3, 2, 1, 0), (4, 5, 6, 7), (0, 1, 5, 4),
  324. (1, 2, 6, 5), (2, 3, 7, 6), (3, 0, 4, 7)],
  325. )
  326. foundation_mesh.materials.append(stone)
  327. # Load the placed library templates together so common materials retain their names. Massing
  328. # buildings reuse those same material datablocks, so no extra palette has to be authored here.
  329. required = {entry['joined_object'] for _, entry, _, _ in placements if entry}
  330. required.update(entry['lod1']['joined_object'] for _, entry, _, _ in placements if entry)
  331. with bpy.data.libraries.load(str(LIBRARY_DIR / 'house-library.blend'), link=False) as (src, dst):
  332. missing = required.difference(src.objects)
  333. if missing:
  334. raise RuntimeError('Missing Blender library templates: ' + ', '.join(sorted(missing)))
  335. dst.objects = sorted(required)
  336. templates = {obj.name: obj for obj in dst.objects if obj is not None}
  337. if len(templates) != len(required) or any(obj.type != 'MESH' or not obj.data.polygons
  338. for obj in templates.values()):
  339. raise RuntimeError('A required Blender template is empty')
  340. # Asset-level reduction: a Decimate modifier on the TEMPLATE mesh collapses every placed instance
  341. # at once (object copies share mesh data). Ratios come from the CLI; 1.0 (default) leaves meshes
  342. # untouched. Collapsing an inset window frame to one sheet is acceptable at these viewing
  343. # distances; what must survive is the building silhouette, its openings as dark insets, and the
  344. # storey rhythm — the maintainer's acceptance bar for "looks like a building".
  345. DECIMATE_RATIOS = {'lod0': args.decimate_lod0, 'lod1': args.decimate_lod1}
  346. decimated_templates = {}
  347. if any(ratio < 1.0 for ratio in DECIMATE_RATIOS.values()):
  348. for name, obj in templates.items():
  349. level = 'lod0' if name.endswith('_LOD0') else 'lod1' if name.endswith('_LOD1') else None
  350. ratio = DECIMATE_RATIOS.get(level, 1.0)
  351. if ratio < 1.0:
  352. modifier = obj.modifiers.new('AssetDecimate', 'DECIMATE')
  353. modifier.ratio = ratio
  354. modifier.use_collapse_triangulate = True
  355. decimated_templates[name] = modifier
  356. has_asset_placements = any(entry for _, entry, _, _ in placements)
  357. if not decimated_templates and has_asset_placements:
  358. raise RuntimeError('Decimate requested but no template name ends with _LOD0/_LOD1')
  359. # A massing-only batch (no asset placements, e.g. the unfitted-rectangle tail) has no templates
  360. # to decimate — its prisms are already minimal, so Draco-only export is the correct behavior.
  361. if decimated_templates:
  362. # Library.json triangle counts no longer describe the decimated meshes: link the decimated
  363. # templates into the view layer, evaluate them, and measure the real triangulated counts.
  364. scratch = bpy.data.collections.new('Template_eval_scratch')
  365. bpy.context.scene.collection.children.link(scratch)
  366. for name in decimated_templates:
  367. scratch.objects.link(templates[name])
  368. bpy.context.view_layer.update()
  369. depsgraph = bpy.context.evaluated_depsgraph_get()
  370. evaluated_triangles = {}
  371. for name in decimated_templates:
  372. evaluated = templates[name].evaluated_get(depsgraph)
  373. mesh = evaluated.to_mesh()
  374. evaluated_triangles[name] = sum(len(polygon.vertices) - 2 for polygon in mesh.polygons)
  375. evaluated.to_mesh_clear()
  376. for building, entry, _, _ in placements:
  377. if entry:
  378. expected_triangles[building['id']] = {}
  379. for level in ('lod0', 'lod1'):
  380. if args.lod1_footprint_only and level == 'lod1':
  381. expected_triangles[building['id']][level] = build_geometry(building)[level][1]
  382. continue
  383. template_name = entry['joined_object'] if level == 'lod0' else entry['lod1']['joined_object']
  384. fallback = (entry['validation'] if level == 'lod0' else entry['lod1']['validation'])['triangles']
  385. expected_triangles[building['id']][level] = evaluated_triangles.get(template_name, fallback)
  386. else:
  387. for building, entry, _, _ in placements:
  388. if entry:
  389. expected_triangles[building['id']] = {
  390. level: (build_geometry(building)[level][1] if args.lod1_footprint_only and level == 'lod1'
  391. else (entry['validation'] if level == 'lod0' else entry['lod1']['validation'])['triangles'])
  392. for level in ('lod0', 'lod1')}
  393. # Append the whole library palette on top of the templates: templates that happen to share no
  394. # material would otherwise leave a massing tone name unavailable.
  395. with bpy.data.libraries.load(str(LIBRARY_DIR / 'house-library.blend'), link=False) as (src, dst):
  396. dst.materials = list(src.materials)
  397. missing_tones = set(WALL_TONES + ROOF_TONES + ('Glass_Dark', 'Door_Walnut')).difference(bpy.data.materials.keys())
  398. if missing_tones:
  399. raise RuntimeError('Library is missing material tones: ' + ', '.join(sorted(missing_tones)))
  400. def id_seed(building_id):
  401. value = 2166136261
  402. for character in str(building_id):
  403. value = ((value ^ ord(character)) * 16777619) % 4294967296
  404. return value
  405. def massing_tone(building_id):
  406. seed = id_seed(building_id)
  407. return (bpy.data.materials[WALL_TONES[seed % len(WALL_TONES)]],
  408. bpy.data.materials[ROOF_TONES[(seed // len(WALL_TONES)) % len(ROOF_TONES)]],
  409. bpy.data.materials['Glass_Dark'], bpy.data.materials['Door_Walnut'])
  410. def material_remap(building_id):
  411. """Asset palette rotation, keyed by the same stable hash as the massing tones."""
  412. seed = id_seed(building_id)
  413. roof_shift = (seed // 3) % len(ROOF_TONES)
  414. wall_shift = seed % len(WALL_TONES)
  415. remap = {name: ROOF_TONES[(index + roof_shift) % len(ROOF_TONES)]
  416. for index, name in enumerate(ROOF_TONES)}
  417. remap.update({name: WALL_TONES[(index + wall_shift) % len(WALL_TONES)]
  418. for index, name in enumerate(WALL_TONES)})
  419. remap['Roof_Membrane'] = ROOF_TONES[roof_shift]
  420. return remap
  421. def template(entry, level):
  422. return templates[entry['joined_object'] if level == 'lod0' else entry['lod1']['joined_object']]
  423. def make_cell(group, level):
  424. collection = bpy.data.collections.new('Cad_' + level)
  425. bpy.context.scene.collection.children.link(collection)
  426. for building, entry, sx, sy in group:
  427. x, y, top = building['positionENU']
  428. bottom = building['foundationBottom']
  429. if entry and not (args.lod1_footprint_only and level == 'lod1'):
  430. obj = template(entry, level).copy() # Object copy shares mesh data with siblings.
  431. obj.name = building['id']
  432. collection.objects.link(obj)
  433. obj.location = building['positionENU']
  434. obj.rotation_euler = (0, 0, building['heading'])
  435. obj.scale = (sx, sy, 1)
  436. obj['source_footprint'] = building.get('sourceId', building['id'])
  437. remap = material_remap(building['id'])
  438. for slot in obj.material_slots:
  439. if slot.material and slot.material.name in remap:
  440. material = bpy.data.materials.get(remap[slot.material.name])
  441. if material:
  442. slot.link = 'OBJECT'
  443. slot.material = material
  444. else:
  445. geometry = build_geometry(building)[level]
  446. outline = geometry[0]
  447. wall, roof, window, door = massing_tone(building['id'])
  448. top_z = top + building['heightM']
  449. obj = poly_prism(outline, bottom, top_z, wall, roof, building['positionENU'],
  450. max(1, int(building.get('floors') or round(building['heightM'] / 3.2))), level,
  451. window, door)
  452. # Self-check the prism is a solid with outward normals. If the ring somehow came out
  453. # clockwise, rebuild once reversed instead of silently emitting an inside-out building.
  454. if prism_volume_sign(obj) <= 0:
  455. bpy.data.meshes.remove(obj.data, do_unlink=True)
  456. obj = poly_prism(list(reversed(outline)), bottom, top_z, wall, roof, building['positionENU'],
  457. max(1, int(building.get('floors') or round(building['heightM'] / 3.2))), level,
  458. window, door)
  459. if prism_volume_sign(obj) <= 0:
  460. raise RuntimeError(building['id'] + ' massing prism is inside-out or flat')
  461. obj.name = building['id']
  462. for collection_link in list(obj.users_collection):
  463. collection_link.objects.unlink(obj)
  464. collection.objects.link(obj)
  465. # Buried foundation. An asset instance is a library mesh seated at floor level, so it needs
  466. # its own box down to foundationBottom. A massing prism already spans foundationBottom to the
  467. # roof, i.e. its buried foundation IS the prism; a rectangular box under an L-shaped or
  468. # rotated outline would poke outside the building and z-fight its walls. So one cell holds
  469. # two mesh nodes per asset placement and one per massing building.
  470. if entry and not (args.lod1_footprint_only and level == 'lod1'):
  471. foundation = bpy.data.objects.new(building['id'] + '_buried_foundation', foundation_mesh)
  472. collection.objects.link(foundation)
  473. foundation.location = (x, y, (bottom + top) / 2)
  474. foundation.rotation_euler.z = building['heading']
  475. foundation.scale = (building['width'], building['depth'], top - bottom)
  476. bpy.context.view_layer.update()
  477. objects = list(collection.objects)
  478. if args.merge_buildings and objects:
  479. objects = merge_cell_objects(objects, collection)
  480. corners = [obj.matrix_world @ Vector(corner) for obj in objects for corner in obj.bound_box]
  481. bounds = [[min(p[axis] for p in corners) for axis in range(3)],
  482. [max(p[axis] for p in corners) for axis in range(3)]]
  483. return collection, objects, bounds
  484. def merge_cell_objects(objects, collection):
  485. """Bake every building/foundation in the cell into ONE mesh.
  486. The per-building export produces a separate node and material primitives per placement.
  487. Merging reduces that structural cost to one primitive per used material; actual frame-time
  488. improvement still requires a runtime measurement. The decimate modifier is baked via the
  489. depsgraph. Transforms are baked into ENU vertices; normals are recalculated with flat shading.
  490. Material slots are preserved. Roof/window appearance must still be visually reviewed.
  491. """
  492. bpy.context.view_layer.update()
  493. depsgraph = bpy.context.evaluated_depsgraph_get()
  494. vertices, faces, face_material_names = [], [], []
  495. material_names = {}
  496. for obj in objects:
  497. evaluated = obj.evaluated_get(depsgraph)
  498. mesh = evaluated.to_mesh()
  499. matrix = obj.matrix_world.copy()
  500. base = len(vertices)
  501. for vertex in mesh.vertices:
  502. vertices.append(tuple(matrix @ vertex.co))
  503. for polygon in mesh.polygons:
  504. corner_indices = tuple(base + index for index in polygon.vertices)
  505. # Reject invalid faces with fewer than three distinct indices before rebuilding.
  506. if len(set(corner_indices)) < 3:
  507. continue
  508. faces.append(corner_indices)
  509. material = obj.material_slots[polygon.material_index].material if obj.material_slots else None
  510. key = material.name if material else ''
  511. if key not in material_names:
  512. material_names[key] = len(material_names)
  513. face_material_names.append(key)
  514. evaluated.to_mesh_clear()
  515. merged_mesh = bpy.data.meshes.new('cell_merged')
  516. merged_mesh.from_pydata(vertices, [], faces)
  517. for name in material_names:
  518. merged_mesh.materials.append(bpy.data.materials[name])
  519. merged_mesh.polygons.foreach_set('material_index', [material_names[n] for n in face_material_names])
  520. edit = bmesh.new()
  521. edit.from_mesh(merged_mesh)
  522. bmesh.ops.recalc_face_normals(edit, faces=edit.faces)
  523. edit.to_mesh(merged_mesh)
  524. edit.free()
  525. merged_mesh.update()
  526. merged = bpy.data.objects.new('cell_merged', merged_mesh)
  527. collection.objects.link(merged)
  528. for obj in objects:
  529. bpy.data.objects.remove(obj, do_unlink=True)
  530. return [merged]
  531. def export_cell(path, objects, group, level):
  532. bpy.ops.object.select_all(action='DESELECT')
  533. for obj in objects:
  534. obj.select_set(True)
  535. bpy.context.view_layer.objects.active = objects[0]
  536. export_options = dict(filepath=str(path), export_format='GLB', use_selection=True,
  537. export_yup=True, export_materials='EXPORT', export_extras=True,
  538. # Bake modifiers (the asset Decimate) into the exported mesh; without
  539. # this the GLB would carry the full-detail template meshes again.
  540. export_apply=True)
  541. if args.draco:
  542. export_options['export_draco_mesh_compression_enable'] = True
  543. bpy.ops.export_scene.gltf(**export_options)
  544. with path.open('rb') as stream:
  545. header = stream.read(20)
  546. if len(header) != 20 or header[:4] != b'glTF':
  547. raise RuntimeError('Invalid GLB export ' + str(path))
  548. _, version, length, chunk_length, chunk_type = struct.unpack('<4s4I', header)
  549. if version != 2 or length != path.stat().st_size or chunk_type != 0x4E4F534A:
  550. raise RuntimeError('Invalid GLB header ' + str(path))
  551. doc = json.loads(stream.read(chunk_length))
  552. # Asset instances are one node plus their buried foundation; a massing prism is a single node.
  553. if args.merge_buildings:
  554. # Merged cell: exactly one mesh node carries the whole cell. Per-building node identity
  555. # and seated transforms are baked into world-space vertices, so only the triangle total
  556. # (below) and the scene-root shape remain meaningful here.
  557. names = [n.get('name') for n in doc['nodes'] if 'mesh' in n]
  558. if len(names) != 1:
  559. raise RuntimeError(f'{path.name}: merged cell must contain exactly one mesh node')
  560. else:
  561. expected_names = []
  562. for building, entry, _, _ in group:
  563. expected_names.append(building['id'])
  564. if entry and not (args.lod1_footprint_only and level == 'lod1'):
  565. expected_names.append(building['id'] + '_buried_foundation')
  566. names = [n.get('name') for n in doc['nodes'] if 'mesh' in n]
  567. # One node per massing prism, two per asset placement (instance + buried foundation).
  568. if len(names) != len(expected_names):
  569. raise RuntimeError(f'{path.name}: {len(names)} mesh nodes for {len(expected_names)} '
  570. 'expected building/foundation nodes')
  571. if sorted(names) != sorted(expected_names):
  572. raise RuntimeError('GLB mesh nodes do not match placed buildings and foundations: ' + str(path))
  573. nodes = {node['name']: node for node in doc['nodes']}
  574. for building, entry, sx, sy in group:
  575. x, y, top = building['positionENU']
  576. bottom = building['foundationBottom']
  577. use_asset = entry and not (args.lod1_footprint_only and level == 'lod1')
  578. checks = [(building['id'], top, (sx, 1, sy) if use_asset else None)]
  579. if use_asset:
  580. checks.append((building['id'] + '_buried_foundation', (bottom + top) / 2,
  581. (building['width'], top - bottom, building['depth'])))
  582. for name, centre, scale in checks:
  583. node = nodes[name]
  584. expected_pos = (x, centre, -y) # Blender Z-up to glTF Y-up, only once.
  585. if 'matrix' in node or any(abs(a - b) > .002 for a, b in zip(
  586. node.get('translation', (0, 0, 0)), expected_pos)):
  587. raise RuntimeError('Incorrect seated transform for ' + name + ' in ' + str(path))
  588. if scale and any(abs(a - b) > .002 for a, b in zip(node.get('scale', (1, 1, 1)), scale)):
  589. raise RuntimeError('Incorrect seated scale for ' + name + ' in ' + str(path))
  590. if set(doc['scenes'][doc.get('scene', 0)]['nodes']) != set(range(len(doc['nodes']))):
  591. raise RuntimeError('Unexpected parent transform in ' + str(path))
  592. def triangles(mesh):
  593. return sum(doc['accessors'][part['indices']]['count'] // 3 if 'indices' in part
  594. else doc['accessors'][part['attributes']['POSITION']]['count'] // 3
  595. for part in mesh['primitives'])
  596. count = sum(triangles(doc['meshes'][node['mesh']]) for node in doc['nodes'] if 'mesh' in node)
  597. if args.merge_buildings:
  598. # Merged mode: the reference is the authored merged mesh itself (degenerate faces were
  599. # dropped at collection time, so export must reproduce it one-for-one). Per-building
  600. # seated transforms were verified by the identical make_cell code path in unmerged runs.
  601. reference = sum(len(polygon.vertices) - 2 for polygon in objects[0].data.polygons)
  602. else:
  603. # Asset placements also carry a 12-triangle buried foundation box; massing prisms do not.
  604. reference = sum(expected_triangles[b['id']][level] + (12 if entry and not (args.lod1_footprint_only and level == 'lod1') else 0)
  605. for b, entry, _, _ in group)
  606. if count != reference:
  607. raise RuntimeError(f'{path.name}: {count} triangles, expected {reference}')
  608. meshes = {node['mesh'] for node in doc['nodes'] if 'mesh' in node}
  609. # Per-building material remapping is an object-level material link, so a cell whose instances
  610. # were recoloured exports one mesh variant per building; shared mesh data is not asserted here.
  611. # `uniqueMeshes` is reported so a regression towards N meshes for N nodes stays visible.
  612. return {'uri': path.name, 'bytes': path.stat().st_size, 'triangles': count,
  613. 'meshNodes': len(names), 'uniqueMeshes': len(meshes),
  614. 'merged': bool(args.merge_buildings)}
  615. # ---------------------------------------------------------------- spatial cells
  616. # Partition by positionENU into a --grid x --grid grid, then group cells into --coarse-grid parents,
  617. # exactly like the rectangular exporter so the same verifier accepts the tree.
  618. xs = [b['positionENU'][0] for b, _, _, _ in placements]
  619. ys = [b['positionENU'][1] for b, _, _, _ in placements]
  620. min_x, max_x = min(xs), max(xs)
  621. min_y, max_y = min(ys), max(ys)
  622. span_x = max(max_x - min_x, 1e-6)
  623. span_y = max(max_y - min_y, 1e-6)
  624. cells = {}
  625. for placement in placements:
  626. x, y, _ = placement[0]['positionENU']
  627. ix = min(args.grid - 1, int((x - min_x) / span_x * args.grid))
  628. iy = min(args.grid - 1, int((y - min_y) / span_y * args.grid))
  629. cells.setdefault(f'cell-{iy:02d}-{ix:02d}', []).append(placement)
  630. cells = {name: group for name, group in sorted(cells.items()) if group}
  631. if len(cells) < 2:
  632. raise RuntimeError('Not enough geographically separate building groups to demonstrate spatial LOD')
  633. hierarchical = bool(args.coarse_grid)
  634. coarse_groups, fine_coarse = {}, {}
  635. if hierarchical:
  636. for cell_id, group in cells.items():
  637. x, y, _ = group[0][0]['positionENU']
  638. ix = min(args.coarse_grid - 1, int((x - min_x) / span_x * args.coarse_grid))
  639. iy = min(args.coarse_grid - 1, int((y - min_y) / span_y * args.coarse_grid))
  640. coarse_id = f'coarse-{iy:02d}-{ix:02d}'
  641. fine_coarse[cell_id] = coarse_id
  642. coarse_groups.setdefault(coarse_id, []).extend(group)
  643. fine_tiles, manifest_cells, all_bounds = {}, [], []
  644. for cell_id, group in cells.items():
  645. exports, bounds_per_level = {}, []
  646. for level in ('lod1', 'lod0'):
  647. collection, objects, bounds = make_cell(group, level)
  648. path = OUT / f'{cell_id}-{level}.glb'
  649. exports[level] = export_cell(path, objects, group, level)
  650. bounds_per_level.append(bounds)
  651. bpy.data.collections.remove(collection, do_unlink=True)
  652. for obj in objects:
  653. bpy.data.objects.remove(obj, do_unlink=True)
  654. bounds = [[min(b[0][axis] for b in bounds_per_level) for axis in range(3)],
  655. [max(b[1][axis] for b in bounds_per_level) for axis in range(3)]]
  656. all_bounds.append(bounds)
  657. volume = {'box': box(bounds)}
  658. if hierarchical:
  659. fine_tiles[cell_id] = {'boundingVolume': volume, 'geometricError': 0,
  660. 'content': {'uri': exports['lod0']['uri']}}
  661. else:
  662. fine_tiles[cell_id] = {'boundingVolume': volume, 'geometricError': 8,
  663. 'refine': 'REPLACE', 'content': {'uri': exports['lod1']['uri']},
  664. 'children': [{'boundingVolume': volume, 'geometricError': 0,
  665. 'content': {'uri': exports['lod0']['uri']}}]}
  666. manifest_cells.append({'id': cell_id, 'count': len(group),
  667. 'buildingIds': [b['id'] for b, _, _, _ in group],
  668. 'boundsENU': {'min': bounds[0], 'max': bounds[1]}, **exports})
  669. if hierarchical:
  670. cell_tiles, manifest_coarse = [], []
  671. for coarse_id, group in coarse_groups.items():
  672. collection, objects, bounds = make_cell(group, 'lod1')
  673. path = OUT / f'{coarse_id}-lod1.glb'
  674. coarse_export = export_cell(path, objects, group, 'lod1')
  675. bpy.data.collections.remove(collection, do_unlink=True)
  676. for obj in objects:
  677. bpy.data.objects.remove(obj, do_unlink=True)
  678. children = [fine_tiles[cell_id] for cell_id, parent in fine_coarse.items() if parent == coarse_id]
  679. cell_tiles.append({'boundingVolume': {'box': box(bounds)}, 'geometricError': 16,
  680. 'refine': 'REPLACE', 'content': {'uri': coarse_export['uri']},
  681. 'children': children})
  682. manifest_coarse.append({'id': coarse_id, 'count': len(group), **coarse_export})
  683. else:
  684. cell_tiles = list(fine_tiles.values())
  685. manifest_coarse = []
  686. root_bounds = [[min(b[0][axis] for b in all_bounds) for axis in range(3)],
  687. [max(b[1][axis] for b in all_bounds) for axis in range(3)]]
  688. # The explicit root must be renderable to keep Cesium traversing its spatial children.
  689. anchor_json = json.dumps({'asset': {'version': '2.0'}, 'scene': 0, 'scenes': [{'nodes': []}]},
  690. separators=(',', ':')).encode('utf-8')
  691. anchor_json += b' ' * ((-len(anchor_json)) % 4)
  692. anchor = b'glTF' + struct.pack('<II', 2, 20 + len(anchor_json))
  693. anchor += struct.pack('<I', len(anchor_json)) + b'JSON' + anchor_json
  694. (OUT / 'root-anchor.glb').write_bytes(anchor)
  695. tileset = {'asset': {'version': '1.1', 'generator': 'Blender CAD/BIM massing LOD'},
  696. 'geometricError': 800,
  697. 'root': {'transform': plan['transform'], 'boundingVolume': {'box': box(root_bounds)},
  698. 'geometricError': 200, 'refine': 'REPLACE',
  699. 'content': {'uri': 'root-anchor.glb'}, 'children': cell_tiles}}
  700. lod1_records = manifest_coarse if hierarchical else [cell['lod1'] for cell in manifest_cells]
  701. lod0_records = [cell['lod0'] for cell in manifest_cells]
  702. manifest = {'scope': plan.get('scope', 'surveyed CAD/BIM massing sample; not full-city reconstruction'),
  703. 'origin': plan['origin'], 'notice': plan.get('notice'),
  704. 'provenance': {'source': plan.get('source'), 'plan': PLAN.name,
  705. 'gisFootprints': True, 'lod1FootprintOnly': bool(args.lod1_footprint_only),
  706. 'facadeDetails': True, 'roadsProcedural': False, 'closedRoofCaps': True},
  707. 'reduction': {'decimateLod0': args.decimate_lod0, 'decimateLod1': args.decimate_lod1,
  708. 'draco': bool(args.draco)},
  709. 'count': len(placements), 'cellCount': len(cells), 'cellIds': list(cells),
  710. 'hierarchy': {'enabled': hierarchical, 'coarseGrid': args.coarse_grid if hierarchical else None,
  711. 'coarseCellCount': len(manifest_coarse)},
  712. 'excludedUnfittedIds': rejected,
  713. 'boundsENU': {'min': root_bounds[0], 'max': root_bounds[1]},
  714. 'totals': {level: {'bytes': sum(item['bytes'] for item in (lod1_records if level == 'lod1' else lod0_records)),
  715. 'triangles': sum(item['triangles'] for item in (lod1_records if level == 'lod1' else lod0_records))}
  716. for level in ('lod1', 'lod0')},
  717. 'coarseCells': manifest_coarse,
  718. 'cells': manifest_cells}
  719. (OUT / 'tileset.json').write_text(json.dumps(tileset, indent=2), encoding='utf-8')
  720. (OUT / 'manifest.json').write_text(json.dumps(manifest, indent=2), encoding='utf-8')
  721. print('CAD_BIM_LOD', json.dumps({'count': manifest['count'], 'cells': manifest['cellCount'],
  722. 'coarse': len(manifest_coarse), 'totals': manifest['totals'],
  723. 'output': str(OUT)}))