Skip to content

Commit

Permalink
Bounding box updates
Browse files Browse the repository at this point in the history
  • Loading branch information
benc-uk committed Mar 6, 2024
1 parent 84207ae commit 78a5d1e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/renderable/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down Expand Up @@ -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]
}
}

/*
Expand Down
9 changes: 9 additions & 0 deletions src/renderable/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 78a5d1e

Please sign in to comment.