import { getObjectVoxelGroups } from "../graphics/voxelGroup"; import type { ObjectInstance, ObjectType, RuntimeObjectInstance, RuntimeObjectType, V3, World } from "../../types"; import { buildObjectTrimesh } from "../graphics/mesh"; export function computeBoundingBox(objectType: ObjectType): { min: V3; max: V3 } { if (!objectType.voxels.length) return { min: [0, 0, 0], max: [1, 1, 1] }; let minX = Infinity, minY = Infinity, minZ = Infinity; let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity; for (const v of objectType.voxels) { minX = Math.min(minX, v.position[0]); minY = Math.min(minY, v.position[1]); minZ = Math.min(minZ, v.position[2]); maxX = Math.max(maxX, v.position[0] + 1); maxY = Math.max(maxY, v.position[1] + 1); maxZ = Math.max(maxZ, v.position[2] + 1); } return { min: [minX, minY, minZ], max: [maxX, maxY, maxZ] }; } export function populateRuntimeObjectType(value: ObjectType, world: World): RuntimeObjectType { return { ...value, runtime: { voxelGroups: getObjectVoxelGroups(value, world.voxelTypes), colliderMesh: buildObjectTrimesh(value, world.voxelTypes), boundingBox: computeBoundingBox(value), updatedAt: 0, }, }; } export function populateRuntimeObject(value: ObjectInstance, _world: World): RuntimeObjectInstance { return { ...value, runtime: { rb: () => null, }, }; } export function depopulateRuntimeObjectType(value: RuntimeObjectType, _world: World): ObjectType { const { runtime: _, ...result } = value; return result; } export function depopulateRuntimeObject(value: RuntimeObjectInstance, _world: World): ObjectInstance { const { runtime: _, ...result } = value; return result; }