From 6fb2f5bcc43a433f0f219f6ae818985a2344f650 Mon Sep 17 00:00:00 2001 From: Lajos Meszaros Date: Sun, 2 Jun 2024 15:15:30 +0200 Subject: [PATCH] feat(Polygons): move bounding box related methods from ArxMap to Polygons --- src/ArxMap.ts | 58 ++++--------------------------------------------- src/Polygons.ts | 52 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 55 deletions(-) diff --git a/src/ArxMap.ts b/src/ArxMap.ts index fcc5b324..dbe90a0c 100644 --- a/src/ArxMap.ts +++ b/src/ArxMap.ts @@ -14,7 +14,7 @@ import { ArxVertex, } from 'arx-convert/types' import { getCellCoords, MAP_DEPTH_IN_CELLS, MAP_WIDTH_IN_CELLS, QuadrupleOf } from 'arx-convert/utils' -import { Box3, Object3D } from 'three' +import { Object3D } from 'three' import { Audio } from '@src/Audio.js' import { Entities } from '@src/Entities.js' import { Entity } from '@src/Entity.js' @@ -108,14 +108,6 @@ export class ArxMap { ], } - cashedBBox: { - numberOfPolygons: number - value: Box3 - } = { - numberOfPolygons: 0, - value: new Box3(), - } - constructor(dlf?: ArxDLF, fts?: ArxFTS, llf?: ArxLLF, areNormalsCalculated = false) { if (typeof dlf === 'undefined' || typeof fts === 'undefined' || typeof llf === 'undefined') { return @@ -244,11 +236,11 @@ export class ArxMap { throw new MapFinalizedError() } - const numberOfRemovedPolygons = $(this.polygons).clearSelection().selectOutOfBounds().delete() + const removedPolygons = $(this.polygons).clearSelection().selectOutOfBounds().delete() - if (numberOfRemovedPolygons > 0) { + if (removedPolygons.length > 0) { console.warn( - `[warning] ArxMap: Removed ${numberOfRemovedPolygons} polygons what are outside the 0..16000 boundary on the X or Z axis`, + `[warning] ArxMap: Removed ${removedPolygons.length} polygons what are outside the 0..16000 boundary on the X or Z axis`, ) } @@ -616,46 +608,4 @@ export class ArxMap { // TODO: adjust fts anchor linked anchor indices // TODO: adjust fts polygon texture container ids } - - getBoundingBox() { - // TODO: this isn't ideal when only a vertex gets changed, but not the number of polygons - if (this.cashedBBox.numberOfPolygons === this.polygons.length) { - return this.cashedBBox.value - } - - const box = new Box3() - - for (const polygon of this.polygons) { - for (let i = 0; i < (polygon.isQuad() ? 4 : 3); i++) { - box.expandByPoint(polygon.vertices[i]) - } - } - - this.cashedBBox.numberOfPolygons = this.polygons.length - this.cashedBBox.value = box - - return box - } - - getCenter() { - const bb = this.getBoundingBox() - const center = new Vector3() - bb.getCenter(center) - return center - } - - getHeight() { - const { max, min } = this.getBoundingBox() - return max.y - min.y - } - - getWidth() { - const { max, min } = this.getBoundingBox() - return max.x - min.x - } - - getDepth() { - const { max, min } = this.getBoundingBox() - return max.z - min.z - } } diff --git a/src/Polygons.ts b/src/Polygons.ts index bb363949..d0fe4208 100644 --- a/src/Polygons.ts +++ b/src/Polygons.ts @@ -7,7 +7,7 @@ import { TripleOf, isTiled, } from 'arx-convert/utils' -import { Mesh, MeshBasicMaterial, Object3D, BufferAttribute } from 'three' +import { Mesh, MeshBasicMaterial, Object3D, BufferAttribute, Box3 } from 'three' import { Color } from '@src/Color.js' import { Material } from '@src/Material.js' import { Polygon, TransparencyType } from '@src/Polygon.js' @@ -43,6 +43,14 @@ type VertexWithMaterialIndex = { } export class Polygons extends Array { + cashedBBox: { + numberOfPolygons: number + value: Box3 + } = { + numberOfPolygons: 0, + value: new Box3(), + } + async exportTextures(settings: Settings) { const texturesToExport: { tileable: Record @@ -409,4 +417,46 @@ export class Polygons extends Array { return colors } + + getBoundingBox() { + // TODO: this isn't ideal when only a vertex gets changed, but not the number of polygons + if (this.cashedBBox.numberOfPolygons === this.length) { + return this.cashedBBox.value + } + + const bbox = new Box3() + + for (const polygon of this) { + const { min, max } = polygon.getBoundingBox() + bbox.expandByPoint(min) + bbox.expandByPoint(max) + } + + this.cashedBBox.numberOfPolygons = this.length + this.cashedBBox.value = bbox + + return bbox + } + + getCenter() { + const bb = this.getBoundingBox() + const center = new Vector3() + bb.getCenter(center) + return center + } + + getHeight() { + const { max, min } = this.getBoundingBox() + return max.y - min.y + } + + getWidth() { + const { max, min } = this.getBoundingBox() + return max.x - min.x + } + + getDepth() { + const { max, min } = this.getBoundingBox() + return max.z - min.z + } }