diff --git a/tools/maps/sion/cga_builder.py b/tools/maps/sion/cga_builder.py index b8afcb4..4f9687f 100644 --- a/tools/maps/sion/cga_builder.py +++ b/tools/maps/sion/cga_builder.py @@ -1311,9 +1311,9 @@ def build_cga_building( r_sup = all_tier_supports_mask add_zone(ZONE_ROOF_SEGMENT, pal["roof_class"], ZONE_FLAG_EXTERIOR, r_sup, r_render, r_col) - all_c_pts = np.vstack([z.col_tris.reshape(-1, 3) for z in zones]) - bmin = all_c_pts.min(axis=0) - bmax = all_c_pts.max(axis=0) + 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) + bmax = all_pts_bounds.max(axis=0) g_mask = 0 for z in zones: if z.flags & ZONE_FLAG_GROUND_ANCHORED: @@ -1384,6 +1384,7 @@ def _pack_zone_mesh_16b( pos_scale: np.ndarray, zone_index: int, bldg_slot: int, + allow_fallback: bool = True, ) -> tuple[bytes, bytes, int, int]: """ Pack triangle soup -> 16 B/vert vertex buffer + uint16 index buffer: @@ -1392,30 +1393,27 @@ def _pack_zone_mesh_16b( """ qpos_all = np.clip(np.round((tris - pos_origin[None, None, :]) / pos_scale[None, None, :]), -32767, 32767).astype(np.int16) - # Filter out any triangle whose quantized vertices collapse or are collinear + # Check if any triangle collapses or is collinear after int16 quantization (avoid int64 squaring overflow!) v0 = qpos_all[:, 0, :].astype(np.int64) v1 = qpos_all[:, 1, :].astype(np.int64) v2 = qpos_all[:, 2, :].astype(np.int64) - e1q = v1 - v0 - e2q = v2 - v0 - cross_q = np.cross(e1q, e2q) - valid = np.sum(cross_q * cross_q, axis=1) > 0 - if not np.any(valid): - # Fallback to single minimal valid box from raw tris bounds - bmin = tris.reshape(-1, 3).min(axis=0) - bmax = np.maximum(bmin + pos_scale * 2.0, tris.reshape(-1, 3).max(axis=0)) - box_t, box_m, box_ao, box_uv = make_box_prism( - bmin, np.array([1.0, 0.0, 0.0]), np.array([0.0, 0.0, -1.0]), np.array([0.0, 1.0, 0.0]), - 0.0, float(bmax[0] - bmin[0]), -float(bmax[2] - bmin[2]), 0.0, 0.0, float(bmax[1] - bmin[1]), - int(mats[0]), int(mats[0]), int(mats[0]), - ) - return _pack_zone_mesh_16b(box_t, box_m, box_ao, box_uv, pos_origin, pos_scale, zone_index, bldg_slot) - - tris = tris[valid] - qpos_all = qpos_all[valid] - mats = mats[valid] - ao = ao[valid] - uvs = uvs[valid] + cross_q = np.cross(v1 - v0, v2 - v0) + valid = np.any(cross_q != 0, axis=1) + if not np.all(valid): + if allow_fallback: + bmin = tris.reshape(-1, 3).min(axis=0) + bmax = np.maximum(bmin + pos_scale * 4.0, tris.reshape(-1, 3).max(axis=0)) + box_t, box_m, box_ao, box_uv = make_box_prism( + bmin, np.array([1.0, 0.0, 0.0]), np.array([0.0, 0.0, -1.0]), np.array([0.0, 1.0, 0.0]), + 0.0, float(bmax[0] - bmin[0]), -float(bmax[2] - bmin[2]), 0.0, 0.0, float(bmax[1] - bmin[1]), + int(mats[0]), int(mats[0]), int(mats[0]), + ) + return _pack_zone_mesh_16b(box_t, box_m, box_ao, box_uv, pos_origin, pos_scale, zone_index, bldg_slot, allow_fallback=False) + tris = tris[valid] + qpos_all = qpos_all[valid] + mats = mats[valid] + ao = ao[valid] + uvs = uvs[valid] e1 = tris[:, 1] - tris[:, 0] e2 = tris[:, 2] - tris[:, 0] @@ -1493,10 +1491,10 @@ def check_mesh_watertight(qpos_verts: np.ndarray, indices: np.ndarray) -> tuple[ dir_edges[(u, v)] += 1 undir_edges[(min(u, v), max(u, v))] += 1 - # Vectorized collinearity check in quantized space + # Vectorized collinearity check in quantized space (avoid int64 squaring overflow!) vp = qpos_verts[tri_idx].astype(np.int64) cr = np.cross(vp[:, 1] - vp[:, 0], vp[:, 2] - vp[:, 0]) - degenerate += int(np.sum(np.sum(cr * cr, axis=1) == 0)) + degenerate += int(np.sum(np.all(cr == 0, axis=1))) open_edges = 0 non_manifold = 0