Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BatchedMesh: Add support for raycasting #27146

Merged
merged 16 commits into from
Nov 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 61 additions & 2 deletions examples/jsm/objects/BatchedMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const _frustum = new Frustum();
const _box = new Box3();
const _sphere = new Sphere();
const _vector = new Vector3();
const _mesh = new Mesh();
const _batchIntersects = [];

// @TODO: SkinnedMesh support?
// @TODO: Future work if needed. Move into the core. Can be optimized more with WEBGL_multi_draw.
Expand Down Expand Up @@ -694,9 +696,66 @@ class BatchedMesh extends Mesh {

}

raycast() {
raycast( raycaster, intersects ) {

console.warn( 'BatchedMesh: Raycast function not implemented.' );
const visible = this._visible;
const active = this._active;
const drawRanges = this._drawRanges;
const geometryCount = this._geometryCount;
const matrixWorld = this.matrixWorld;
const batchGeometry = this.geometry;

// iterate over each geometry
_mesh.material = this.material;
_mesh.geometry.index = batchGeometry.index;
_mesh.geometry.attributes = batchGeometry.attributes;
if ( _mesh.geometry.boundingBox === null ) {

_mesh.geometry.boundingBox = new Box3();

}

if ( _mesh.geometry.boundingSphere === null ) {

_mesh.geometry.boundingSphere = new Sphere();

}

for ( let i = 0; i < geometryCount; i ++ ) {

if ( ! visible[ i ] || ! active[ i ] ) {

continue;

}

const drawRange = drawRanges[ i ];
_mesh.geometry.setDrawRange( drawRange.start, drawRange.count );

// ge the intersects
this.getMatrixAt( i, _mesh.matrixWorld ).premultiply( matrixWorld );
this.getBoundingBoxAt( i, _mesh.geometry.boundingBox );
this.getBoundingSphereAt( i, _mesh.geometry.boundingSphere );
_mesh.raycast( raycaster, _batchIntersects );

// add batch id to the intersects
for ( let j = 0, l = _batchIntersects.length; j < l; j ++ ) {

const intersect = _batchIntersects[ j ];
intersect.object = this;
intersect.batchId = i;
intersects.push( intersect );

}

_batchIntersects.length = 0;

}

_mesh.material = null;
_mesh.geometry.index = null;
_mesh.geometry.attributes = {};
_mesh.geometry.setDrawRange( 0, Infinity );

}

Expand Down