Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandruPopovici committed Nov 22, 2023
1 parent 76e40a3 commit 20d4bbf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 25 deletions.
69 changes: 48 additions & 21 deletions packages/viewer/src/modules/batching/InstancedBatchObject.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,52 @@
// /* eslint-disable camelcase */
// import { BatchObject } from './BatchObject'
/* eslint-disable camelcase */
import { BatchObject, VectorLike } from './BatchObject'

// import { Matrix4, Vector3 } from 'three'
// import { SpeckleMeshBVH } from '../objects/AccelerationStructure'
// import { NodeRenderView } from '../tree/NodeRenderView'
import { Box3, Matrix4 } from 'three'
import { NodeRenderView } from '../tree/NodeRenderView'
import { AccelerationStructure } from '../objects/AccelerationStructure'

// export class InstancedBatchObject extends BatchObject {
// protected instanceTransform: Matrix4 = new Matrix4()
export class InstancedBatchObject extends BatchObject {
protected instanceTransform: Matrix4 = new Matrix4()

// public constructor(renderView: NodeRenderView, batchIndex: number) {
// super(renderView, batchIndex)
// }
public get aabb(): Box3 {
const box = new Box3().copy(this.renderView.aabb)
box.applyMatrix4(this.transform).applyMatrix4(this.instanceTransform)
return box
}

// public buildInstanceBVH(instanceBVH?: SpeckleMeshBVH) {
// if (instanceBVH) {
// this._bvh = SpeckleMeshBVH.buildBVH(indices, localPositions)
// this._bvh.inputTransform = this.transformInv
// this._bvh.outputTransform = this.transform
// this._bvh.inputOriginTransform = new Matrix4().copy(transform)
// this._bvh.outputOriginTransfom = new Matrix4().copy(transform).invert()
// return
// } else this.buildBVH()
// }
// }
public constructor(renderView: NodeRenderView, batchIndex: number) {
super(renderView, batchIndex)
this.instanceTransform.copy(renderView.renderData.geometry.transform)
this.transform.copy(this.instanceTransform)
this.transformInv.copy(this.instanceTransform)
}

public buildInstanceBVH(accelerationStructure?: AccelerationStructure) {
if (accelerationStructure) {
this._accelerationStructure = accelerationStructure
this._accelerationStructure.inputTransform = this.transformInv
this._accelerationStructure.outputTransform = this.transform
const transform = new Matrix4().makeTranslation(
this._localOrigin.x,
this._localOrigin.y,
this._localOrigin.z
)
transform.invert()
this._accelerationStructure.inputOriginTransform = new Matrix4().copy(transform)
this._accelerationStructure.outputOriginTransfom = new Matrix4()
.copy(transform)
.invert()
} else this.buildBVH()
}

public transformTRS(
translation: VectorLike,
euler: VectorLike,
scale: VectorLike,
pivot: VectorLike
) {
super.transformTRS(translation, euler, scale, pivot)
this.transform.multiply(this.instanceTransform)
this.transformInv.multiply(this.instanceTransform)
}
}
7 changes: 4 additions & 3 deletions packages/viewer/src/modules/batching/InstancedMeshBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import {
GeometryType,
HideAllBatchUpdateRange
} from './Batch'
import { BatchObject } from './BatchObject'
import SpeckleInstancedMesh from '../objects/SpeckleInstancedMesh'
import { ObjectLayers } from '../../IViewer'
import { InstancedBatchObject } from './InstancedBatchObject'

export default class InstancedMeshBatch implements Batch {
public id: string
Expand Down Expand Up @@ -399,10 +399,11 @@ export default class InstancedMeshBatch implements Batch {

public buildBatch() {
const batchObjects = []
// let instanceBVH = null
for (let k = 0; k < this.renderViews.length; k++) {
this.renderViews[k].setBatchData(this.id, k, 1)
const batchObject = new BatchObject(this.renderViews[k], k)
batchObject.buildBVH()
const batchObject = new InstancedBatchObject(this.renderViews[k], k)

batchObjects.push(batchObject)
}

Expand Down
5 changes: 4 additions & 1 deletion packages/viewer/src/modules/objects/SpeckleInstancedMesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
import { BatchObject } from '../batching/BatchObject'
import Materials from '../materials/Materials'
import { TopLevelAccelerationStructure } from './TopLevelAccelerationStructure'
import { AccelerationStructure } from './AccelerationStructure'
import { InstancedBatchObject } from '../batching/InstancedBatchObject'

const _inverseMatrix = new Matrix4()
const _ray = new Ray()
Expand Down Expand Up @@ -47,6 +49,7 @@ const tmpInverseMatrix = /* @__PURE__ */ new Matrix4()
export default class SpeckleInstancedMesh extends InstancedMesh {
public static MeshBatchNumber = 0

private instanceAccelerationStructure: AccelerationStructure = null
private tas: TopLevelAccelerationStructure = null
private batchMaterial: Material = null
private materialCache: { [id: string]: Material } = {}
Expand Down Expand Up @@ -74,7 +77,7 @@ export default class SpeckleInstancedMesh extends InstancedMesh {
this.material = this.batchMaterial
}

public setBatchObjects(batchObjects: BatchObject[]) {
public setBatchObjects(batchObjects: InstancedBatchObject[]) {
this._batchObjects = batchObjects
for (let k = 0; k < batchObjects.length; k++) {
this.setMatrixAt(k, batchObjects[k].renderView.renderData.geometry.transform)
Expand Down

0 comments on commit 20d4bbf

Please sign in to comment.