/**
 * three-utils.ts — shared Three.js helpers for the massing scenes.
 *
 * `disposeObject` was lifted VERBATIM from MassingScene.tsx so the ПТИЦА cockpit
 * sandbox and the §7 «Концепция» 3D viewer share one teardown helper (no
 * copy-drift). Disposes every geometry/material under an Object3D — call before
 * dropping a Group from the scene to free GPU memory.
 */

import type * as THREE from "three";

/** Dispose every geometry/material under an Object3D. */
export function disposeObject(obj: THREE.Object3D): void {
  obj.traverse((o) => {
    const mesh = o as Partial<THREE.Mesh> & Partial<THREE.Line>;
    if (mesh.geometry) mesh.geometry.dispose();
    if (mesh.material) {
      const mats = Array.isArray(mesh.material)
        ? mesh.material
        : [mesh.material];
      mats.forEach((m) => m.dispose());
    }
  });
}
