diff --git a/src/renderable/builder.ts b/src/renderable/builder.ts index 6b80551..0ab2594 100644 --- a/src/renderable/builder.ts +++ b/src/renderable/builder.ts @@ -62,14 +62,31 @@ export class BuilderPart { private indexCount: number = 0 private normalData: number[] = [] private texcoordData: number[] = [] + private _boundingBox: number[] + + public get boundingBox(): number[] { + return this._boundingBox + } private _triCount: number = 0 + public get triangleCount(): number { return this._triCount } private _customArrayData: twgl.Arrays | undefined + constructor() { + this._boundingBox = [ + Number.MAX_VALUE, + Number.MAX_VALUE, + Number.MAX_VALUE, + Number.MIN_VALUE, + Number.MIN_VALUE, + Number.MIN_VALUE, + ] + } + private addVertex(x: number, y: number, z: number): number { this.vertexData.push(x, y, z) return this.vertexCount++ @@ -112,6 +129,16 @@ export class BuilderPart { this.addNormal([n[0], n[1], n[2]]) this.texcoordData.push(...tc1, ...tc2, ...tc3) + + // Update bounding box + for (const v of [v1, v2, v3]) { + if (v[0] < this._boundingBox[0]) this._boundingBox[0] = v[0] + if (v[1] < this._boundingBox[1]) this._boundingBox[1] = v[1] + if (v[2] < this._boundingBox[2]) this._boundingBox[2] = v[2] + if (v[0] > this._boundingBox[3]) this._boundingBox[3] = v[0] + if (v[1] > this._boundingBox[4]) this._boundingBox[4] = v[1] + if (v[2] > this._boundingBox[5]) this._boundingBox[5] = v[2] + } } /* diff --git a/src/renderable/model.ts b/src/renderable/model.ts index 29b3451..13bf16b 100644 --- a/src/renderable/model.ts +++ b/src/renderable/model.ts @@ -201,6 +201,15 @@ export class Model implements Renderable { const partBuffers = builderPart.build(gl) if (!partBuffers) continue + // Update bounding box + const bb = builderPart.boundingBox + if (bb[0] < model._boundingBox[0]) model._boundingBox[0] = bb[0] + if (bb[1] < model._boundingBox[1]) model._boundingBox[1] = bb[1] + if (bb[2] < model._boundingBox[2]) model._boundingBox[2] = bb[2] + if (bb[3] > model._boundingBox[3]) model._boundingBox[3] = bb[3] + if (bb[4] > model._boundingBox[4]) model._boundingBox[4] = bb[4] + if (bb[5] > model._boundingBox[5]) model._boundingBox[5] = bb[5] + model.triCount += builderPart.triangleCount model.parts.push(new ModelPart(partBuffers, partName)) model.materials[partName] = builder.materials.get(partName) ?? model.materials.__default