three-mesh-bvh
Advanced tools
Comparing version 0.7.3 to 0.7.4
{ | ||
"name": "three-mesh-bvh", | ||
"version": "0.7.3", | ||
"version": "0.7.4", | ||
"description": "A BVH implementation to speed up raycasting against three.js meshes.", | ||
@@ -5,0 +5,0 @@ "module": "src/index.js", |
@@ -1,2 +0,1 @@ | ||
import { Vector3 } from 'three'; | ||
import { intersectRay } from '../utils/intersectUtils.js'; | ||
@@ -12,3 +11,2 @@ import { IS_LEAF, OFFSET, COUNT, LEFT_NODE, RIGHT_NODE } from '../utils/nodeBufferUtils.js'; | ||
const _boxIntersection = /* @__PURE__ */ new Vector3(); | ||
function raycast_indirect( bvh, root, side, ray, intersects ) { | ||
@@ -38,3 +36,3 @@ | ||
const leftIndex = LEFT_NODE( nodeIndex32 ); | ||
if ( intersectRay( leftIndex, float32Array, ray, _boxIntersection ) ) { | ||
if ( intersectRay( leftIndex, float32Array, ray ) ) { | ||
@@ -46,3 +44,3 @@ _raycast( leftIndex, bvh, side, ray, intersects ); | ||
const rightIndex = RIGHT_NODE( nodeIndex32, uint32Array ); | ||
if ( intersectRay( rightIndex, float32Array, ray, _boxIntersection ) ) { | ||
if ( intersectRay( rightIndex, float32Array, ray ) ) { | ||
@@ -49,0 +47,0 @@ _raycast( rightIndex, bvh, side, ray, intersects ); |
@@ -1,2 +0,1 @@ | ||
import { Vector3 } from 'three'; | ||
import { intersectRay } from '../utils/intersectUtils.js'; | ||
@@ -12,3 +11,2 @@ import { IS_LEAF, OFFSET, COUNT, LEFT_NODE, RIGHT_NODE } from '../utils/nodeBufferUtils.js'; | ||
const _boxIntersection = /* @__PURE__ */ new Vector3(); | ||
function raycast( bvh, root, side, ray, intersects ) { | ||
@@ -39,3 +37,3 @@ | ||
const leftIndex = LEFT_NODE( nodeIndex32 ); | ||
if ( intersectRay( leftIndex, float32Array, ray, _boxIntersection ) ) { | ||
if ( intersectRay( leftIndex, float32Array, ray ) ) { | ||
@@ -47,3 +45,3 @@ _raycast( leftIndex, bvh, side, ray, intersects ); | ||
const rightIndex = RIGHT_NODE( nodeIndex32, uint32Array ); | ||
if ( intersectRay( rightIndex, float32Array, ray, _boxIntersection ) ) { | ||
if ( intersectRay( rightIndex, float32Array, ray ) ) { | ||
@@ -50,0 +48,0 @@ _raycast( rightIndex, bvh, side, ray, intersects ); |
@@ -1,2 +0,1 @@ | ||
import { Vector3 } from 'three'; | ||
import { intersectRay } from '../utils/intersectUtils.js'; | ||
@@ -8,3 +7,2 @@ import { COUNT, OFFSET, LEFT_NODE, RIGHT_NODE, IS_LEAF } from '../utils/nodeBufferUtils.js'; | ||
const _boxIntersection = /* @__PURE__ */ new Vector3(); | ||
export function raycast/* @echo INDIRECT_STRING */( bvh, root, side, ray, intersects ) { | ||
@@ -41,3 +39,3 @@ | ||
const leftIndex = LEFT_NODE( nodeIndex32 ); | ||
if ( intersectRay( leftIndex, float32Array, ray, _boxIntersection ) ) { | ||
if ( intersectRay( leftIndex, float32Array, ray ) ) { | ||
@@ -49,3 +47,3 @@ _raycast( leftIndex, bvh, side, ray, intersects ); | ||
const rightIndex = RIGHT_NODE( nodeIndex32, uint32Array ); | ||
if ( intersectRay( rightIndex, float32Array, ray, _boxIntersection ) ) { | ||
if ( intersectRay( rightIndex, float32Array, ray ) ) { | ||
@@ -52,0 +50,0 @@ _raycast( rightIndex, bvh, side, ray, intersects ); |
@@ -1,2 +0,1 @@ | ||
import { Vector3 } from 'three'; | ||
import { IS_LEAF, OFFSET, COUNT, SPLIT_AXIS, LEFT_NODE, RIGHT_NODE } from '../utils/nodeBufferUtils.js'; | ||
@@ -11,4 +10,5 @@ import { BufferStack } from '../utils/BufferStack.js'; | ||
/***********************************************************/ | ||
const _boxIntersection = /* @__PURE__ */ new Vector3(); | ||
const _xyzFields = [ 'x', 'y', 'z' ]; | ||
function raycastFirst_indirect( bvh, root, side, ray ) { | ||
@@ -61,3 +61,3 @@ | ||
const c1Intersection = intersectRay( c1, float32Array, ray, _boxIntersection ); | ||
const c1Intersection = intersectRay( c1, float32Array, ray ); | ||
const c1Result = c1Intersection ? _raycastFirst( c1, bvh, side, ray ) : null; | ||
@@ -86,3 +86,3 @@ | ||
// intersection in the second, so check the second node and then take the better of the two | ||
const c2Intersection = intersectRay( c2, float32Array, ray, _boxIntersection ); | ||
const c2Intersection = intersectRay( c2, float32Array, ray ); | ||
const c2Result = c2Intersection ? _raycastFirst( c2, bvh, side, ray ) : null; | ||
@@ -89,0 +89,0 @@ |
@@ -1,2 +0,1 @@ | ||
import { Vector3 } from 'three'; | ||
import { IS_LEAF, OFFSET, COUNT, SPLIT_AXIS, LEFT_NODE, RIGHT_NODE } from '../utils/nodeBufferUtils.js'; | ||
@@ -11,4 +10,5 @@ import { BufferStack } from '../utils/BufferStack.js'; | ||
/***********************************************************/ | ||
const _boxIntersection = /* @__PURE__ */ new Vector3(); | ||
const _xyzFields = [ 'x', 'y', 'z' ]; | ||
function raycastFirst( bvh, root, side, ray ) { | ||
@@ -62,3 +62,3 @@ | ||
const c1Intersection = intersectRay( c1, float32Array, ray, _boxIntersection ); | ||
const c1Intersection = intersectRay( c1, float32Array, ray ); | ||
const c1Result = c1Intersection ? _raycastFirst( c1, bvh, side, ray ) : null; | ||
@@ -87,3 +87,3 @@ | ||
// intersection in the second, so check the second node and then take the better of the two | ||
const c2Intersection = intersectRay( c2, float32Array, ray, _boxIntersection ); | ||
const c2Intersection = intersectRay( c2, float32Array, ray ); | ||
const c2Result = c2Intersection ? _raycastFirst( c2, bvh, side, ray ) : null; | ||
@@ -90,0 +90,0 @@ |
@@ -1,2 +0,1 @@ | ||
import { Vector3 } from 'three'; | ||
import { COUNT, OFFSET, LEFT_NODE, RIGHT_NODE, IS_LEAF, SPLIT_AXIS } from '../utils/nodeBufferUtils.js'; | ||
@@ -7,4 +6,5 @@ import { BufferStack } from '../utils/BufferStack.js'; | ||
import { intersectClosestTri_indirect } from '../utils/iterationUtils_indirect.generated.js'; | ||
const _boxIntersection = /* @__PURE__ */ new Vector3(); | ||
const _xyzFields = [ 'x', 'y', 'z' ]; | ||
export function raycastFirst/* @echo INDIRECT_STRING */( bvh, root, side, ray ) { | ||
@@ -64,3 +64,3 @@ | ||
const c1Intersection = intersectRay( c1, float32Array, ray, _boxIntersection ); | ||
const c1Intersection = intersectRay( c1, float32Array, ray ); | ||
const c1Result = c1Intersection ? _raycastFirst( c1, bvh, side, ray ) : null; | ||
@@ -89,3 +89,3 @@ | ||
// intersection in the second, so check the second node and then take the better of the two | ||
const c2Intersection = intersectRay( c2, float32Array, ray, _boxIntersection ); | ||
const c2Intersection = intersectRay( c2, float32Array, ray ); | ||
const c2Result = c2Intersection ? _raycastFirst( c2, bvh, side, ray ) : null; | ||
@@ -92,0 +92,0 @@ |
@@ -58,3 +58,3 @@ import { BufferAttribute, Box3, FrontSide } from 'three'; | ||
roots: rootData.map( root => root.slice() ), | ||
index: indexAttribute.array.slice(), | ||
index: indexAttribute ? indexAttribute.array.slice() : null, | ||
indirectBuffer: indirectBuffer ? indirectBuffer.slice() : null, | ||
@@ -67,3 +67,3 @@ }; | ||
roots: rootData, | ||
index: indexAttribute.array, | ||
index: indexAttribute ? indexAttribute.array : null, | ||
indirectBuffer: indirectBuffer, | ||
@@ -70,0 +70,0 @@ }; |
@@ -1,10 +0,80 @@ | ||
import { Box3 } from 'three'; | ||
import { arrayToBox } from '../../utils/ArrayBoxUtilities.js'; | ||
/** | ||
* This function performs intersection tests similar to Ray.intersectBox in three.js, | ||
* with the difference that the box values are read from an array to improve performance. | ||
*/ | ||
export function intersectRay( nodeIndex32, array, ray ) { | ||
const _boundingBox = /* @__PURE__ */ new Box3(); | ||
export function intersectRay( nodeIndex32, array, ray, target ) { | ||
let tmin, tmax, tymin, tymax, tzmin, tzmax; | ||
arrayToBox( nodeIndex32, array, _boundingBox ); | ||
return ray.intersectBox( _boundingBox, target ); | ||
const invdirx = 1 / ray.direction.x, | ||
invdiry = 1 / ray.direction.y, | ||
invdirz = 1 / ray.direction.z; | ||
const ox = ray.origin.x; | ||
const oy = ray.origin.y; | ||
const oz = ray.origin.z; | ||
let minx = array[ nodeIndex32 ]; | ||
let maxx = array[ nodeIndex32 + 3 ]; | ||
let miny = array[ nodeIndex32 + 1 ]; | ||
let maxy = array[ nodeIndex32 + 3 + 1 ]; | ||
let minz = array[ nodeIndex32 + 2 ]; | ||
let maxz = array[ nodeIndex32 + 3 + 2 ]; | ||
if ( invdirx >= 0 ) { | ||
tmin = ( minx - ox ) * invdirx; | ||
tmax = ( maxx - ox ) * invdirx; | ||
} else { | ||
tmin = ( maxx - ox ) * invdirx; | ||
tmax = ( minx - ox ) * invdirx; | ||
} | ||
if ( invdiry >= 0 ) { | ||
tymin = ( miny - oy ) * invdiry; | ||
tymax = ( maxy - oy ) * invdiry; | ||
} else { | ||
tymin = ( maxy - oy ) * invdiry; | ||
tymax = ( miny - oy ) * invdiry; | ||
} | ||
if ( ( tmin > tymax ) || ( tymin > tmax ) ) return false; | ||
if ( tymin > tmin || isNaN( tmin ) ) tmin = tymin; | ||
if ( tymax < tmax || isNaN( tmax ) ) tmax = tymax; | ||
if ( invdirz >= 0 ) { | ||
tzmin = ( minz - oz ) * invdirz; | ||
tzmax = ( maxz - oz ) * invdirz; | ||
} else { | ||
tzmin = ( maxz - oz ) * invdirz; | ||
tzmax = ( minz - oz ) * invdirz; | ||
} | ||
if ( ( tmin > tzmax ) || ( tzmin > tmax ) ) return false; | ||
// if ( tzmin > tmin || tmin !== tmin ) tmin = tzmin; // Uncomment this line if add the distance check | ||
if ( tzmax < tmax || tmax !== tmax ) tmax = tzmax; | ||
//return point closest to the ray (positive side) | ||
if ( tmax < 0 ) return false; | ||
return true; | ||
} |
@@ -12,4 +12,7 @@ import { | ||
// account for error | ||
progress = Math.min( progress, 1 ); | ||
const currTime = performance.now(); | ||
if ( currTime - prevTime >= 10 || progress === 1.0 ) { | ||
if ( currTime - prevTime >= 10 && progress !== 1.0 ) { | ||
@@ -61,5 +64,11 @@ postMessage( { | ||
const serialized = MeshBVH.serialize( bvh, { copyIndexBuffer: false } ); | ||
const toTransfer = [ serialized.index.buffer, position.buffer, ...serialized.roots ] | ||
.filter( v => ( typeof SharedArrayBuffer === 'undefined' ) || ! ( v instanceof SharedArrayBuffer ) ); | ||
let toTransfer = [ position.buffer, ...serialized.roots ]; | ||
if ( serialized.index ) { | ||
toTransfer.push( serialized.index.buffer ); | ||
} | ||
toTransfer = toTransfer.filter( v => ( typeof SharedArrayBuffer === 'undefined' ) || ! ( v instanceof SharedArrayBuffer ) ); | ||
if ( bvh._indirectBuffer ) { | ||
@@ -66,0 +75,0 @@ |
@@ -56,11 +56,15 @@ import { Box3, BufferAttribute } from 'three'; | ||
geometry.attributes.position.array = position; | ||
if ( geometry.index ) { | ||
if ( serialized.index ) { | ||
geometry.index.array = serialized.index; | ||
if ( geometry.index ) { | ||
} else { | ||
geometry.index.array = serialized.index; | ||
const newIndex = new BufferAttribute( serialized.index, 1, false ); | ||
geometry.setIndex( newIndex ); | ||
} else { | ||
const newIndex = new BufferAttribute( serialized.index, 1, false ); | ||
geometry.setIndex( newIndex ); | ||
} | ||
} | ||
@@ -74,2 +78,8 @@ | ||
if ( options.onProgress ) { | ||
options.onProgress( data.progress ); | ||
} | ||
resolve( bvh ); | ||
@@ -76,0 +86,0 @@ worker.onmessage = null; |
@@ -250,4 +250,7 @@ import { MathUtils, BufferGeometry, BufferAttribute } from 'three'; | ||
// account for error | ||
progress = Math.min( progress, 1 ); | ||
const currTime = performance.now(); | ||
if ( currTime - prevTime >= 10 || progress === 1.0 ) { | ||
if ( currTime - prevTime >= 10 && progress !== 1.0 ) { | ||
@@ -254,0 +257,0 @@ postMessage( { |
@@ -6,2 +6,3 @@ import { Box3, BufferAttribute } from 'three'; | ||
import { GenerateMeshBVHWorker } from './GenerateMeshBVHWorker.js'; | ||
import { ensureIndex } from '../core/build/geometryUtils.js'; | ||
@@ -31,2 +32,8 @@ const DEFAULT_WORKER_COUNT = typeof navigator !== 'undefined' ? navigator.hardwareConcurrency : 4; | ||
if ( ! geometry.index && ! options.indirect ) { | ||
ensureIndex( geometry, options ); | ||
} | ||
if ( | ||
@@ -68,11 +75,15 @@ geometry.getAttribute( 'position' ).isInterleavedBufferAttribute || | ||
geometry.attributes.position.array = position; | ||
if ( geometry.index ) { | ||
if ( serialized.index ) { | ||
geometry.index.array = serialized.index; | ||
if ( geometry.index ) { | ||
} else { | ||
geometry.index.array = serialized.index; | ||
const newIndex = new BufferAttribute( serialized.index, 1, false ); | ||
geometry.setIndex( newIndex ); | ||
} else { | ||
const newIndex = new BufferAttribute( serialized.index, 1, false ); | ||
geometry.setIndex( newIndex ); | ||
} | ||
} | ||
@@ -86,2 +97,8 @@ | ||
if ( options.onProgress ) { | ||
options.onProgress( data.progress ); | ||
} | ||
resolve( bvh ); | ||
@@ -88,0 +105,0 @@ worker.onmessage = null; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1542744
17603