Skip to content

Commit

Permalink
Merge pull request #595 from gkjohnson/update-bvhcast
Browse files Browse the repository at this point in the history
Update bvhcast
  • Loading branch information
gkjohnson authored Oct 6, 2023
2 parents fae5205 + f0ccb8b commit 2994d47
Show file tree
Hide file tree
Showing 5 changed files with 414 additions and 595 deletions.
2 changes: 1 addition & 1 deletion example/edgeIntersect.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function render() {

const edge = new THREE.Line3();
const results = [];
mesh1.geometry.boundsTree.bvhcast( mesh2.geometry.boundsTree, matrix2to1, {
mesh1.geometry.boundsTree.bvhcast_new( mesh2.geometry.boundsTree, matrix2to1, {

intersectsTriangles( triangle1, triangle2 ) {

Expand Down
103 changes: 99 additions & 4 deletions src/core/MeshBVH.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import { raycast } from './cast/raycast.generated.js';
import { raycastFirst } from './cast/raycastFirst.generated.js';
import { intersectsGeometry } from './cast/intersectsGeometry.generated.js';
import { closestPointToGeometry } from './cast/closestPointToGeometry.generated.js';
import { bvhcast } from './cast/bvhcast.generated.js';

import { iterateOverTriangles_indirect } from './utils/iterationUtils_indirect.generated.js';
import { refit_indirect } from './cast/refit_indirect.generated.js';
import { raycast_indirect } from './cast/raycast_indirect.generated.js';
import { raycastFirst_indirect } from './cast/raycastFirst_indirect.generated.js';
import { intersectsGeometry_indirect } from './cast/intersectsGeometry_indirect.generated.js';
import { closestPointToGeometry_indirect } from './cast/closestPointToGeometry_indirect.generated.js';
import { bvhcast_indirect } from './cast/bvhcast_indirect.generated.js';
import { isSharedArrayBufferSupported } from '../utils/BufferUtils.js';
import { setTriangle } from '../utils/TriangleUtilities.js';
import { bvhcast } from './cast/bvhcast.js';

const obb = /* @__PURE__ */ new OrientedBox();
const tempBox = /* @__PURE__ */ new Box3();
Expand Down Expand Up @@ -373,11 +373,106 @@ export class MeshBVH {

bvhcast( otherBvh, matrixToLocal, callbacks ) {

const bvhcastFunc = this.indirect ? bvhcast_indirect : bvhcast;
return bvhcastFunc( this, otherBvh, matrixToLocal, callbacks );
let {
intersectsRanges,
intersectsTriangles,
} = callbacks;

const triangle1 = ExtendedTrianglePool.getPrimitive();
const indexAttr1 = this.geometry.index;
const positionAttr1 = this.geometry.attributes.position;
const assignTriangle1 = this.indirect ?
i1 => {


const ti = this.resolveTriangleIndex( i1 );
setTriangle( triangle1, ti * 3, indexAttr1, positionAttr1 );

} :
i1 => {

setTriangle( triangle1, i1 * 3, indexAttr1, positionAttr1 );

};

const triangle2 = ExtendedTrianglePool.getPrimitive();
const indexAttr2 = otherBvh.geometry.index;
const positionAttr2 = otherBvh.geometry.attributes.position;
const assignTriangle2 = otherBvh.indirect ?
i2 => {

const ti2 = otherBvh.resolveTriangleIndex( i2 );
setTriangle( triangle2, ti2 * 3, indexAttr2, positionAttr2 );

} :
i2 => {

setTriangle( triangle2, i2 * 3, indexAttr2, positionAttr2 );

};

// generate triangle callback if needed
if ( intersectsTriangles ) {

const iterateOverDoubleTriangles = ( offset1, count1, offset2, count2, depth1, index1, depth2, index2 ) => {

for ( let i2 = offset2, l2 = offset2 + count2; i2 < l2; i2 ++ ) {

assignTriangle2( i2 );

triangle2.a.applyMatrix4( matrixToLocal );
triangle2.b.applyMatrix4( matrixToLocal );
triangle2.c.applyMatrix4( matrixToLocal );
triangle2.needsUpdate = true;

for ( let i1 = offset1, l1 = offset1 + count1; i1 < l1; i1 ++ ) {

assignTriangle1( i1 );

triangle1.needsUpdate = true;

if ( intersectsTriangles( triangle1, triangle2, i1, i2, depth1, index1, depth2, index2 ) ) {

return true;

}

}

}

return false;

};

if ( intersectsRanges ) {

const originalIntersectsRanges = intersectsRanges;
intersectsRanges = function ( offset1, count1, offset2, count2, depth1, index1, depth2, index2 ) {

if ( ! originalIntersectsRanges( offset1, count1, offset2, count2, depth1, index1, depth2, index2 ) ) {

return iterateOverDoubleTriangles( offset1, count1, offset2, count2, depth1, index1, depth2, index2 );

}

return true;

};

} else {

intersectsRanges = iterateOverDoubleTriangles;

}

}

return bvhcast( this, otherBvh, matrixToLocal, intersectsRanges );

}


/* Derived Cast Functions */
intersectsBox( box, boxToMesh ) {

Expand Down
Loading

0 comments on commit 2994d47

Please sign in to comment.