diff --git a/tools/maps/sion/cga_builder.py b/tools/maps/sion/cga_builder.py index 535dfcd..e0b3d5b 100644 --- a/tools/maps/sion/cga_builder.py +++ b/tools/maps/sion/cga_builder.py @@ -10,6 +10,7 @@ per-zone watertight box-hull collision triangles. from __future__ import annotations +import base64 from collections import Counter, defaultdict from dataclasses import dataclass import hashlib @@ -945,12 +946,12 @@ def _build_roof_zones( geoms = list(merged.geoms) if hasattr(merged, "geoms") else [merged] for g in geoms: if isinstance(g, Polygon) and g.area > 0.8: - simp_r = 0.50 if micro_zone in (MICRO_FORTRESS, MICRO_OLD_TOWN, MICRO_CIVIC) else 0.90 + simp_r = 0.65 if micro_zone in (MICRO_FORTRESS, MICRO_OLD_TOWN, MICRO_CIVIC) else 1.25 gs = g.simplify(simp_r, preserve_topology=True) if isinstance(gs, Polygon) and gs.area > 0.8: g = gs - # Add gentle eave overhang on pitched slopes for larger roofs - if n_axis[1] < 0.97 and g.area > 20.0 and micro_zone in (MICRO_OLD_TOWN, MICRO_CLAVAU, MICRO_SUBURBAN, MICRO_FORTRESS, MICRO_CIVIC): + # Add gentle eave overhang on pitched slopes for hero roofs + if n_axis[1] < 0.97 and g.area > 35.0 and micro_zone in (MICRO_OLD_TOWN, MICRO_FORTRESS, MICRO_CIVIC): gb = g.buffer(0.22, join_style="bevel") if isinstance(gb, Polygon) and gb.area > g.area * 0.9: g = gb @@ -986,9 +987,16 @@ def _build_roof_zones( mat_top=roof_mat, mat_bot=pal["trim"], mat_side=pal["trim"], ao_top=245, ao_bot=165, ao_side=200, ) + l2_poly_s = poly.simplify(1.8, preserve_topology=True) + l2_poly = _clean_polygon_2d(l2_poly_s if isinstance(l2_poly_s, Polygon) else poly, min_area=0.8) or poly + l2_roof_parts = extrude_2d_poly_prism( + origin, u_axis, v_axis, n_axis, l2_poly, + -roof_thick, 0.0, + mat_top=roof_mat, mat_bot=roof_mat, mat_side=roof_mat, + ) rp = poly.representative_point() c3 = origin + rp.x * u_axis + rp.y * v_axis - out.append((parts, parts, c3)) + out.append((parts, (parts[0], parts[1], l2_roof_parts[0], parts[3]), c3)) if not out and footprints: # Flat roof slab cap over footprints @@ -1006,9 +1014,16 @@ def _build_roof_zones( mat_top=roof_mat, mat_bot=pal["slab"], mat_side=pal["trim"], ao_top=240, ao_bot=175, ao_side=205, ) + l2_fp_s = uv_poly.simplify(2.2, preserve_topology=True) + l2_uv = _clean_polygon_2d(l2_fp_s if isinstance(l2_fp_s, Polygon) else uv_poly, min_area=0.8) or uv_poly + l2_roof_parts = extrude_2d_poly_prism( + origin, u_axis, v_axis, n_axis, l2_uv, + 0.0, roof_thick, + mat_top=roof_mat, mat_bot=roof_mat, mat_side=roof_mat, + ) rp = fp.representative_point() c3 = np.array([rp.x, eave_y + roof_thick * 0.5, rp.y], dtype=np.float64) - out.append((parts, parts, c3)) + out.append((parts, (parts[0], parts[1], l2_roof_parts[0], parts[3]), c3)) return out @@ -1090,7 +1105,7 @@ def build_cga_building( bldg_center_xz = np.array([fp_cx, fp_cz], dtype=np.float64) # Final Plinth polygons (NO minimum_rotated_rectangle expansion; secondary parts kept strictly separated) - plinth_tol = 0.50 if micro_zone in (MICRO_FORTRESS, MICRO_OLD_TOWN, MICRO_CIVIC) else 1.25 + plinth_tol = 0.65 if micro_zone in (MICRO_FORTRESS, MICRO_OLD_TOWN, MICRO_CIVIC) else 1.45 plinth_fps: list[Polygon] = [] for fp in footprints: ps = fp.simplify(plinth_tol, preserve_topology=True) @@ -1210,7 +1225,7 @@ def build_cga_building( lod2_solids: list[tuple[np.ndarray, int]] = [] lod2_body_h = max(1.0, eave_y - plinth_bot_y - 0.02) for pfp in plinth_fps: - l2_fp = pfp.simplify(1.65, preserve_topology=True) + l2_fp = pfp.simplify(2.2, preserve_topology=True) l2_fp = _clean_polygon_2d(l2_fp if isinstance(l2_fp, Polygon) else pfp, min_area=1.0) or pfp l2_t, _, _, _ = extrude_2d_poly_prism( np.array([0.0, plinth_bot_y, 0.0]), u_xz, v_xz, n_up, fp_to_uv(l2_fp), @@ -1262,8 +1277,8 @@ def build_cga_building( for r_render, r_col, _ in roof_items: if len(zones) >= MAX_ZONES_PER_BUILDING: break - r_idx = add_zone(ZONE_ROOF_SEGMENT, pal["roof_class"], ZONE_FLAG_EXTERIOR, core_bit, r_render, r_col) - lod2_solids.append((zones[r_idx].col_tris, pal["roof"])) + r_idx = add_zone(ZONE_ROOF_SEGMENT, pal["roof_class"], ZONE_FLAG_EXTERIOR, core_bit, r_render, r_render) + lod2_solids.append((r_col[2], pal["roof"])) else: # 4. Standard Multi-Story Decomposition (Core + WallBays + FloorSlabs + Roofs) story_h_target = { @@ -1277,20 +1292,21 @@ def build_cga_building( }[micro_zone] n_stories = max(1, int(round(wall_h / story_h_target))) - min_edge = 2.6 if micro_zone in (MICRO_OLD_TOWN, MICRO_FORTRESS, MICRO_CIVIC) else 3.6 + hero_zone = micro_zone in (MICRO_OLD_TOWN, MICRO_FORTRESS, MICRO_CIVIC) + min_edge = 3.2 if hero_zone else 4.2 ext_pts = _merge_short_ring_edges(list(plinth_fp.exterior.coords), min_edge_len=min_edge) raw_bays_len = max(3, len(ext_pts)) - # Cap structural tiers cleanly so total city RCGA + LOD2 fits well <= 16 MiB + # Cap structural tiers cleanly so total city RCGA + LOD2 fits well <= 16 MiB (and total sion <= 85 MiB) avail_for_stories = MAX_ZONES_PER_BUILDING - (2 + n_roofs) - max_tiers = 4 if n_stories >= 8 else (3 if micro_zone in (MICRO_OLD_TOWN, MICRO_FORTRESS, MICRO_CIVIC) and n_stories >= 3 else 2) - n_tiers = min(n_stories, max_tiers, max(1, avail_for_stories // max(4, min(raw_bays_len + 1, 7)))) + max_tiers = 4 if wall_h >= 20.0 else (3 if (hero_zone and wall_h >= 9.5) else (2 if wall_h >= 5.2 else 1)) + n_tiers = min(n_stories, max_tiers, max(1, avail_for_stories // max(4, min(raw_bays_len + 1, 6)))) n_tiers = max(1, n_tiers) - max_bays_per_tier = max(3, min(7, (avail_for_stories // n_tiers) - 1)) + max_bays_per_tier = max(3, min(6 if hero_zone else 4, (avail_for_stories // n_tiers) - 1)) perim = float(plinth_fp.exterior.length) - base_bay_len = 12.0 if micro_zone in (MICRO_OLD_TOWN, MICRO_FORTRESS, MICRO_CIVIC) else (18.0 if (is_mauer or micro_zone == MICRO_RONQUOZ or perim > 70.0) else 16.0) + base_bay_len = 14.0 if hero_zone else (20.0 if (is_mauer or micro_zone == MICRO_RONQUOZ or perim > 70.0) else 18.0) bays_2d: list[tuple[np.ndarray, np.ndarray, float]] = [] for mult in (1.0, 1.4, 2.0, 3.0, 5.0, 10.0): cand = _split_ring_into_wall_bays(ext_pts, base_bay_len * mult) @@ -1329,7 +1345,7 @@ def build_cga_building( slab_inset = min(wall_thick * 0.45, 0.20) slab_polys_uv: list[Polygon] = [] for pfp in plinth_fps: - sfp = pfp.simplify(1.25, preserve_topology=True) + sfp = pfp.simplify(1.65, preserve_topology=True) if not isinstance(sfp, Polygon) or sfp.is_empty or sfp.area < 0.8: sfp = pfp sfp = sfp.buffer(-slab_inset, join_style="mitre") @@ -1370,10 +1386,9 @@ def build_cga_building( if len(zones) >= MAX_ZONES_PER_BUILDING - n_roofs - 1: break want_win = (not is_mauer) and ( - (hero_zone and blen >= 3.4 and (b_i % 2 == 0) and (t_idx == 0 or (t_idx == 1 and b_i == 0))) - or (micro_zone == MICRO_VISSIGEN and blen >= 4.8 and b_i == 0 and t_idx == 0) - or (micro_zone in (MICRO_CLAVAU, MICRO_SUBURBAN) and blen >= 5.2 and b_i == 0 and t_idx == 0) - or (micro_zone == MICRO_RONQUOZ and blen >= 7.0 and b_i == 0 and t_idx == 0) + (hero_zone and blen >= 3.8 and t_idx == 0 and (b_i == 0 or (b_i == 2 and blen >= 5.5))) + or (micro_zone in (MICRO_VISSIGEN, MICRO_CLAVAU, MICRO_SUBURBAN) and blen >= 5.2 and b_i == 0 and t_idx == 0) + or (micro_zone == MICRO_RONQUOZ and blen >= 7.5 and b_i == 0 and t_idx == 0) ) r_parts, c_parts, has_win = _build_wall_bay_geometry( sa, sb, blen, y0, bay_y_top, wall_thick, pal, micro_zone, @@ -1417,7 +1432,7 @@ def build_cga_building( prev_bay_mids = cur_bay_mids # 5. Roof Zones anchored to top-tier perimeter WallBays (never floating solely on 0.8m core) - lod2_roof_cap = 4 if micro_zone in (MICRO_FORTRESS, MICRO_CIVIC) else 2 + lod2_roof_cap = 3 if micro_zone in (MICRO_FORTRESS, MICRO_CIVIC) else 1 for r_i, (r_render, r_col, r_c3) in enumerate(roof_items): if len(zones) >= MAX_ZONES_PER_BUILDING: break @@ -1432,9 +1447,9 @@ def build_cga_building( r_sup = all_tier_supports_mask else: r_sup = plinth_bit - r_idx = add_zone(ZONE_ROOF_SEGMENT, pal["roof_class"], ZONE_FLAG_EXTERIOR, r_sup, r_render, r_col) + add_zone(ZONE_ROOF_SEGMENT, pal["roof_class"], ZONE_FLAG_EXTERIOR, r_sup, r_render, r_render) if r_i < lod2_roof_cap: - lod2_solids.append((zones[r_idx].col_tris, pal["roof"])) + lod2_solids.append((r_col[2], pal["roof"])) all_pts_bounds = np.vstack([z.render_tris.reshape(-1, 3) for z in zones] + [z.col_tris.reshape(-1, 3) for z in zones]) bmin = all_pts_bounds.min(axis=0) @@ -1897,7 +1912,7 @@ def export_city_lod2_glb(tx: int, tz: int, buildings: list[CgaBuildingResult], o primitives.append(pygltflib.Primitive( attributes=pygltflib.Attributes(POSITION=pos_acc, NORMAL=nrm_acc), indices=idx_acc, material=m_id, - extras={"solidTriCounts": solid_tri_counts}, + extras={"solidTriCountsB64": base64.b64encode(zlib.compress(np.asarray(solid_tri_counts, dtype=" dict[str, int | float | str]: blob, dtype=idx_dtype, count=idx_acc.count, offset=(idx_bv.byteOffset or 0) + (idx_acc.byteOffset or 0) ).astype(np.int64) - # Verify each individual solid component via extras["solidTriCounts"] + # Verify each individual solid component via extras["solidTriCountsB64"] / ["solidTriCounts"] tri_idx = idx.reshape(-1, 3) - solid_counts = (prim.extras or {}).get("solidTriCounts") - if not solid_counts: - solid_counts = [len(tri_idx)] + extras_dict = prim.extras or {} + if "solidTriCountsB64" in extras_dict: + solid_counts = np.frombuffer(zlib.decompress(base64.b64decode(extras_dict["solidTriCountsB64"])), dtype="