Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

three-mesh-bvh

Package Overview
Dependencies
Maintainers
1
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

three-mesh-bvh - npm Package Compare versions

Comparing version 0.7.4 to 0.7.5

2

package.json
{
"name": "three-mesh-bvh",
"version": "0.7.4",
"version": "0.7.5",
"description": "A BVH implementation to speed up raycasting against three.js meshes.",

@@ -5,0 +5,0 @@ "module": "src/index.js",

@@ -373,6 +373,6 @@ # three-mesh-bvh

```js
raycast( ray : Ray, side : FrontSide | BackSide | DoubleSide = FrontSide ) : Array<RaycastHit>
raycast( ray : Ray, side : FrontSide | BackSide | DoubleSide = FrontSide, near : Number = 0, far : Number = Infinity ) : Array<RaycastHit>
```
```js
raycast( ray : Ray, material : Array<Material> | Material ) : Array<RaycastHit>
raycast( ray : Ray, material? : Array<Material> | Material, near : Number = 0, far : Number = Infinity ) : Array<RaycastHit>
```

@@ -387,6 +387,6 @@

```js
raycastFirst( ray : Ray, side : FrontSide | BackSide | DoubleSide = FrontSide ) : RaycastHit
raycastFirst( ray : Ray, side : FrontSide | BackSide | DoubleSide = FrontSide, near : Number = 0, far : Number = Infinity ) : RaycastHit
```
```js
raycastFirst( ray : Ray, material : Array<Material> | Material ) : RaycastHit
raycastFirst( ray : Ray, material : Array<Material> | Material, near : Number = 0, far : Number = Infinity ) : RaycastHit
```

@@ -393,0 +393,0 @@

@@ -11,6 +11,6 @@ import { intersectRay } from '../utils/intersectUtils.js';

function raycast_indirect( bvh, root, side, ray, intersects ) {
function raycast_indirect( bvh, root, side, ray, intersects, near, far ) {
BufferStack.setBuffer( bvh._roots[ root ] );
_raycast( 0, bvh, side, ray, intersects );
_raycast( 0, bvh, side, ray, intersects, near, far );
BufferStack.clearBuffer();

@@ -20,3 +20,3 @@

function _raycast( nodeIndex32, bvh, side, ray, intersects ) {
function _raycast( nodeIndex32, bvh, side, ray, intersects, near, far ) {

@@ -31,3 +31,3 @@ const { float32Array, uint16Array, uint32Array } = BufferStack;

intersectTris_indirect( bvh, side, ray, offset, count, intersects );
intersectTris_indirect( bvh, side, ray, offset, count, intersects, near, far );

@@ -38,5 +38,5 @@

const leftIndex = LEFT_NODE( nodeIndex32 );
if ( intersectRay( leftIndex, float32Array, ray ) ) {
if ( intersectRay( leftIndex, float32Array, ray, near, far ) ) {
_raycast( leftIndex, bvh, side, ray, intersects );
_raycast( leftIndex, bvh, side, ray, intersects, near, far );

@@ -46,5 +46,5 @@ }

const rightIndex = RIGHT_NODE( nodeIndex32, uint32Array );
if ( intersectRay( rightIndex, float32Array, ray ) ) {
if ( intersectRay( rightIndex, float32Array, ray, near, far ) ) {
_raycast( rightIndex, bvh, side, ray, intersects );
_raycast( rightIndex, bvh, side, ray, intersects, near, far );

@@ -51,0 +51,0 @@ }

@@ -11,6 +11,6 @@ import { intersectRay } from '../utils/intersectUtils.js';

function raycast( bvh, root, side, ray, intersects ) {
function raycast( bvh, root, side, ray, intersects, near, far ) {
BufferStack.setBuffer( bvh._roots[ root ] );
_raycast( 0, bvh, side, ray, intersects );
_raycast( 0, bvh, side, ray, intersects, near, far );
BufferStack.clearBuffer();

@@ -20,3 +20,3 @@

function _raycast( nodeIndex32, bvh, side, ray, intersects ) {
function _raycast( nodeIndex32, bvh, side, ray, intersects, near, far ) {

@@ -32,3 +32,3 @@ const { float32Array, uint16Array, uint32Array } = BufferStack;

intersectTris( bvh, side, ray, offset, count, intersects );
intersectTris( bvh, side, ray, offset, count, intersects, near, far );

@@ -39,5 +39,5 @@

const leftIndex = LEFT_NODE( nodeIndex32 );
if ( intersectRay( leftIndex, float32Array, ray ) ) {
if ( intersectRay( leftIndex, float32Array, ray, near, far ) ) {
_raycast( leftIndex, bvh, side, ray, intersects );
_raycast( leftIndex, bvh, side, ray, intersects, near, far );

@@ -47,5 +47,5 @@ }

const rightIndex = RIGHT_NODE( nodeIndex32, uint32Array );
if ( intersectRay( rightIndex, float32Array, ray ) ) {
if ( intersectRay( rightIndex, float32Array, ray, near, far ) ) {
_raycast( rightIndex, bvh, side, ray, intersects );
_raycast( rightIndex, bvh, side, ray, intersects, near, far );

@@ -52,0 +52,0 @@ }

@@ -7,6 +7,6 @@ import { intersectRay } from '../utils/intersectUtils.js';

export function raycast/* @echo INDIRECT_STRING */( bvh, root, side, ray, intersects ) {
export function raycast/* @echo INDIRECT_STRING */( bvh, root, side, ray, intersects, near, far ) {
BufferStack.setBuffer( bvh._roots[ root ] );
_raycast( 0, bvh, side, ray, intersects );
_raycast( 0, bvh, side, ray, intersects, near, far );
BufferStack.clearBuffer();

@@ -16,3 +16,3 @@

function _raycast( nodeIndex32, bvh, side, ray, intersects ) {
function _raycast( nodeIndex32, bvh, side, ray, intersects, near, far ) {

@@ -29,7 +29,7 @@ const { float32Array, uint16Array, uint32Array } = BufferStack;

intersectTris_indirect( bvh, side, ray, offset, count, intersects );
intersectTris_indirect( bvh, side, ray, offset, count, intersects, near, far );
/* @else */
intersectTris( bvh, side, ray, offset, count, intersects );
intersectTris( bvh, side, ray, offset, count, intersects, near, far );

@@ -41,5 +41,5 @@ /* @endif */

const leftIndex = LEFT_NODE( nodeIndex32 );
if ( intersectRay( leftIndex, float32Array, ray ) ) {
if ( intersectRay( leftIndex, float32Array, ray, near, far ) ) {
_raycast( leftIndex, bvh, side, ray, intersects );
_raycast( leftIndex, bvh, side, ray, intersects, near, far );

@@ -49,5 +49,5 @@ }

const rightIndex = RIGHT_NODE( nodeIndex32, uint32Array );
if ( intersectRay( rightIndex, float32Array, ray ) ) {
if ( intersectRay( rightIndex, float32Array, ray, near, far ) ) {
_raycast( rightIndex, bvh, side, ray, intersects );
_raycast( rightIndex, bvh, side, ray, intersects, near, far );

@@ -54,0 +54,0 @@ }

@@ -13,6 +13,6 @@ import { IS_LEAF, OFFSET, COUNT, SPLIT_AXIS, LEFT_NODE, RIGHT_NODE } from '../utils/nodeBufferUtils.js';

function raycastFirst_indirect( bvh, root, side, ray ) {
function raycastFirst_indirect( bvh, root, side, ray, near, far ) {
BufferStack.setBuffer( bvh._roots[ root ] );
const result = _raycastFirst( 0, bvh, side, ray );
const result = _raycastFirst( 0, bvh, side, ray, near, far );
BufferStack.clearBuffer();

@@ -24,3 +24,3 @@

function _raycastFirst( nodeIndex32, bvh, side, ray ) {
function _raycastFirst( nodeIndex32, bvh, side, ray, near, far ) {

@@ -36,3 +36,3 @@ const { float32Array, uint16Array, uint32Array } = BufferStack;

return intersectClosestTri_indirect( bvh, side, ray, offset, count );
return intersectClosestTri_indirect( bvh, side, ray, offset, count, near, far );

@@ -63,4 +63,4 @@

const c1Intersection = intersectRay( c1, float32Array, ray );
const c1Result = c1Intersection ? _raycastFirst( c1, bvh, side, ray ) : null;
const c1Intersection = intersectRay( c1, float32Array, ray, near, far );
const c1Result = c1Intersection ? _raycastFirst( c1, bvh, side, ray, near, far ) : null;

@@ -88,4 +88,4 @@ // if we got an intersection in the first node and it's closer than the second node's bounding

// intersection in the second, so check the second node and then take the better of the two
const c2Intersection = intersectRay( c2, float32Array, ray );
const c2Result = c2Intersection ? _raycastFirst( c2, bvh, side, ray ) : null;
const c2Intersection = intersectRay( c2, float32Array, ray, near, far );
const c2Result = c2Intersection ? _raycastFirst( c2, bvh, side, ray, near, far ) : null;

@@ -92,0 +92,0 @@ if ( c1Result && c2Result ) {

@@ -13,6 +13,6 @@ import { IS_LEAF, OFFSET, COUNT, SPLIT_AXIS, LEFT_NODE, RIGHT_NODE } from '../utils/nodeBufferUtils.js';

function raycastFirst( bvh, root, side, ray ) {
function raycastFirst( bvh, root, side, ray, near, far ) {
BufferStack.setBuffer( bvh._roots[ root ] );
const result = _raycastFirst( 0, bvh, side, ray );
const result = _raycastFirst( 0, bvh, side, ray, near, far );
BufferStack.clearBuffer();

@@ -24,3 +24,3 @@

function _raycastFirst( nodeIndex32, bvh, side, ray ) {
function _raycastFirst( nodeIndex32, bvh, side, ray, near, far ) {

@@ -37,3 +37,3 @@ const { float32Array, uint16Array, uint32Array } = BufferStack;

return intersectClosestTri( bvh, side, ray, offset, count );
return intersectClosestTri( bvh, side, ray, offset, count, near, far );

@@ -64,4 +64,4 @@

const c1Intersection = intersectRay( c1, float32Array, ray );
const c1Result = c1Intersection ? _raycastFirst( c1, bvh, side, ray ) : null;
const c1Intersection = intersectRay( c1, float32Array, ray, near, far );
const c1Result = c1Intersection ? _raycastFirst( c1, bvh, side, ray, near, far ) : null;

@@ -89,4 +89,4 @@ // if we got an intersection in the first node and it's closer than the second node's bounding

// intersection in the second, so check the second node and then take the better of the two
const c2Intersection = intersectRay( c2, float32Array, ray );
const c2Result = c2Intersection ? _raycastFirst( c2, bvh, side, ray ) : null;
const c2Intersection = intersectRay( c2, float32Array, ray, near, far );
const c2Result = c2Intersection ? _raycastFirst( c2, bvh, side, ray, near, far ) : null;

@@ -93,0 +93,0 @@ if ( c1Result && c2Result ) {

@@ -9,6 +9,6 @@ import { COUNT, OFFSET, LEFT_NODE, RIGHT_NODE, IS_LEAF, SPLIT_AXIS } from '../utils/nodeBufferUtils.js';

export function raycastFirst/* @echo INDIRECT_STRING */( bvh, root, side, ray ) {
export function raycastFirst/* @echo INDIRECT_STRING */( bvh, root, side, ray, near, far ) {
BufferStack.setBuffer( bvh._roots[ root ] );
const result = _raycastFirst( 0, bvh, side, ray );
const result = _raycastFirst( 0, bvh, side, ray, near, far );
BufferStack.clearBuffer();

@@ -20,3 +20,3 @@

function _raycastFirst( nodeIndex32, bvh, side, ray ) {
function _raycastFirst( nodeIndex32, bvh, side, ray, near, far ) {

@@ -34,7 +34,7 @@ const { float32Array, uint16Array, uint32Array } = BufferStack;

return intersectClosestTri_indirect( bvh, side, ray, offset, count );
return intersectClosestTri_indirect( bvh, side, ray, offset, count, near, far );
/* @else */
return intersectClosestTri( bvh, side, ray, offset, count );
return intersectClosestTri( bvh, side, ray, offset, count, near, far );

@@ -66,4 +66,4 @@ /* @endif */

const c1Intersection = intersectRay( c1, float32Array, ray );
const c1Result = c1Intersection ? _raycastFirst( c1, bvh, side, ray ) : null;
const c1Intersection = intersectRay( c1, float32Array, ray, near, far );
const c1Result = c1Intersection ? _raycastFirst( c1, bvh, side, ray, near, far ) : null;

@@ -91,4 +91,4 @@ // if we got an intersection in the first node and it's closer than the second node's bounding

// intersection in the second, so check the second node and then take the better of the two
const c2Intersection = intersectRay( c2, float32Array, ray );
const c2Result = c2Intersection ? _raycastFirst( c2, bvh, side, ray ) : null;
const c2Intersection = intersectRay( c2, float32Array, ray, near, far );
const c2Result = c2Intersection ? _raycastFirst( c2, bvh, side, ray, near, far ) : null;

@@ -95,0 +95,0 @@ if ( c1Result && c2Result ) {

@@ -163,4 +163,3 @@ import { BufferAttribute, Box3, FrontSide } from 'three';

const { _indirectBuffer } = this;
this.resolveTriangleIndex = options.indirect ? i => _indirectBuffer[ i ] : i => i;
this.resolveTriangleIndex = options.indirect ? i => this._indirectBuffer[ i ] : i => i;

@@ -215,3 +214,3 @@ }

/* Core Cast Functions */
raycast( ray, materialOrSide = FrontSide ) {
raycast( ray, materialOrSide = FrontSide, near = 0, far = Infinity ) {

@@ -232,3 +231,3 @@ const roots = this._roots;

raycastFunc( this, i, materialSide, ray, intersects );
raycastFunc( this, i, materialSide, ray, intersects, near, far );

@@ -252,3 +251,3 @@ if ( isArrayMaterial ) {

raycastFirst( ray, materialOrSide = FrontSide ) {
raycastFirst( ray, materialOrSide = FrontSide, near = 0, far = Infinity ) {

@@ -268,3 +267,3 @@ const roots = this._roots;

const materialSide = isArrayMaterial ? materialOrSide[ groups[ i ].materialIndex ].side : side;
const result = raycastFirstFunc( this, i, materialSide, ray );
const result = raycastFirstFunc( this, i, materialSide, ray, near, far );
if ( result != null && ( closestResult == null || result.distance < closestResult.distance ) ) {

@@ -271,0 +270,0 @@

@@ -5,3 +5,3 @@ /**

*/
export function intersectRay( nodeIndex32, array, ray ) {
export function intersectRay( nodeIndex32, array, ray, near, far ) {

@@ -71,3 +71,3 @@ let tmin, tmax, tymin, tymax, tzmin, tzmax;

// if ( tzmin > tmin || tmin !== tmin ) tmin = tzmin; // Uncomment this line if add the distance check
if ( tzmin > tmin || tmin !== tmin ) tmin = tzmin;

@@ -78,6 +78,4 @@ if ( tzmax < tmax || tmax !== tmax ) tmax = tzmax;

if ( tmax < 0 ) return false;
return tmin <= far && tmax >= near;
return true;
}

@@ -9,3 +9,3 @@ import { intersectTri } from '../../utils/ThreeRayIntersectUtilities.js';

function intersectTris_indirect( bvh, side, ray, offset, count, intersections ) {
function intersectTris_indirect( bvh, side, ray, offset, count, intersections, near, far ) {

@@ -16,3 +16,3 @@ const { geometry, _indirectBuffer } = bvh;

let vi = _indirectBuffer ? _indirectBuffer[ i ] : i;
intersectTri( geometry, side, ray, vi, intersections );
intersectTri( geometry, side, ray, vi, intersections, near, far );

@@ -24,3 +24,3 @@

function intersectClosestTri_indirect( bvh, side, ray, offset, count ) {
function intersectClosestTri_indirect( bvh, side, ray, offset, count, near, far ) {

@@ -33,3 +33,3 @@ const { geometry, _indirectBuffer } = bvh;

let intersection;
intersection = intersectTri( geometry, side, ray, _indirectBuffer ? _indirectBuffer[ i ] : i );
intersection = intersectTri( geometry, side, ray, _indirectBuffer ? _indirectBuffer[ i ] : i, null, near, far );

@@ -36,0 +36,0 @@

@@ -9,3 +9,3 @@ import { intersectTri } from '../../utils/ThreeRayIntersectUtilities.js';

function intersectTris( bvh, side, ray, offset, count, intersections ) {
function intersectTris( bvh, side, ray, offset, count, intersections, near, far ) {

@@ -16,3 +16,3 @@ const { geometry, _indirectBuffer } = bvh;

intersectTri( geometry, side, ray, i, intersections );
intersectTri( geometry, side, ray, i, intersections, near, far );

@@ -24,3 +24,3 @@

function intersectClosestTri( bvh, side, ray, offset, count ) {
function intersectClosestTri( bvh, side, ray, offset, count, near, far ) {

@@ -34,3 +34,3 @@ const { geometry, _indirectBuffer } = bvh;

intersection = intersectTri( geometry, side, ray, i );
intersection = intersectTri( geometry, side, ray, i, null, near, far );

@@ -37,0 +37,0 @@

@@ -5,3 +5,3 @@ /* eslint-disable indent */

export function intersectTris/* @echo INDIRECT_STRING */( bvh, side, ray, offset, count, intersections ) {
export function intersectTris/* @echo INDIRECT_STRING */( bvh, side, ray, offset, count, intersections, near, far ) {

@@ -14,7 +14,7 @@ const { geometry, _indirectBuffer } = bvh;

let vi = _indirectBuffer ? _indirectBuffer[ i ] : i;
intersectTri( geometry, side, ray, vi, intersections );
intersectTri( geometry, side, ray, vi, intersections, near, far );
/* @else */
intersectTri( geometry, side, ray, i, intersections );
intersectTri( geometry, side, ray, i, intersections, near, far );

@@ -27,3 +27,3 @@ /* @endif */

export function intersectClosestTri/* @echo INDIRECT_STRING */( bvh, side, ray, offset, count ) {
export function intersectClosestTri/* @echo INDIRECT_STRING */( bvh, side, ray, offset, count, near, far ) {

@@ -38,7 +38,7 @@ const { geometry, _indirectBuffer } = bvh;

intersection = intersectTri( geometry, side, ray, _indirectBuffer ? _indirectBuffer[ i ] : i );
intersection = intersectTri( geometry, side, ray, _indirectBuffer ? _indirectBuffer[ i ] : i, null, near, far );
/* @else */
intersection = intersectTri( geometry, side, ray, i );
intersection = intersectTri( geometry, side, ray, i, null, near, far );

@@ -45,0 +45,0 @@ /* @endif */

@@ -56,5 +56,5 @@ import { BufferGeometry, Vector3, Side, Material, Ray, Sphere, Matrix4, Color,

raycast( ray: Ray, materialOrSide: Side | Array<Material> | Material ): Array<Intersection>
raycast( ray: Ray, materialOrSide?: Side | Array<Material> | Material, near?: number, far?: number ): Array<Intersection>
raycastFirst( ray: Ray, materialOrSide: Side | Array<Material> | Material ): Intersection;
raycastFirst( ray: Ray, materialOrSide?: Side | Array<Material> | Material, near?: number, far?: number ): Intersection;

@@ -61,0 +61,0 @@ intersectsSphere( sphere: Sphere ): boolean;

@@ -1,2 +0,2 @@

import { Ray, Matrix4, Mesh } from 'three';
import { Ray, Matrix4, Mesh, Vector3 } from 'three';
import { convertRaycastIntersect } from './GeometryRayIntersectUtilities.js';

@@ -6,3 +6,5 @@ import { MeshBVH } from '../core/MeshBVH.js';

const ray = /* @__PURE__ */ new Ray();
const direction = /* @__PURE__ */ new Vector3();
const tmpInverseMatrix = /* @__PURE__ */ new Matrix4();
const worldScale = /* @__PURE__ */ new Vector3();
const origMeshRaycastFunc = Mesh.prototype.raycast;

@@ -19,6 +21,13 @@

this.getWorldScale( worldScale );
direction.copy( ray.direction ).multiply( worldScale );
const scaleFactor = direction.length();
const near = raycaster.near / scaleFactor;
const far = raycaster.far / scaleFactor;
const bvh = this.geometry.boundsTree;
if ( raycaster.firstHitOnly === true ) {
const hit = convertRaycastIntersect( bvh.raycastFirst( ray, this.material ), this, raycaster );
const hit = convertRaycastIntersect( bvh.raycastFirst( ray, this.material, near, far ), this, raycaster );
if ( hit ) {

@@ -32,3 +41,3 @@

const hits = bvh.raycast( ray, this.material );
const hits = bvh.raycast( ray, this.material, near, far );
for ( let i = 0, l = hits.length; i < l; i ++ ) {

@@ -35,0 +44,0 @@

@@ -15,12 +15,4 @@ // converts the given BVH raycast intersection to align with the three.js raycast

if ( hit.distance < raycaster.near || hit.distance > raycaster.far ) {
return hit;
return null;
} else {
return hit;
}
}

@@ -18,3 +18,3 @@ import { Vector3, Vector2, Triangle, DoubleSide, BackSide } from 'three';

const _intersectionPoint = /* @__PURE__ */ new Vector3();
function checkIntersection( ray, pA, pB, pC, point, side ) {
function checkIntersection( ray, pA, pB, pC, point, side, near, far ) {

@@ -36,2 +36,4 @@ let intersect;

if ( distance < near || distance > far ) return null;
return {

@@ -46,3 +48,3 @@

function checkBufferGeometryIntersection( ray, position, normal, uv, uv1, a, b, c, side ) {
function checkBufferGeometryIntersection( ray, position, normal, uv, uv1, a, b, c, side, near, far ) {

@@ -53,3 +55,3 @@ _vA.fromBufferAttribute( position, a );

const intersection = checkIntersection( ray, _vA, _vB, _vC, _intersectionPoint, side );
const intersection = checkIntersection( ray, _vA, _vB, _vC, _intersectionPoint, side, near, far );

@@ -113,3 +115,3 @@ if ( intersection ) {

// https://github.com/mrdoob/three.js/blob/0aa87c999fe61e216c1133fba7a95772b503eddf/src/objects/Mesh.js#L258
function intersectTri( geo, side, ray, tri, intersections ) {
function intersectTri( geo, side, ray, tri, intersections, near, far ) {

@@ -131,3 +133,3 @@ const triOffset = tri * 3;

const { position, normal, uv, uv1 } = geo.attributes;
const intersection = checkBufferGeometryIntersection( ray, position, normal, uv, uv1, a, b, c, side );
const intersection = checkBufferGeometryIntersection( ray, position, normal, uv, uv1, a, b, c, side, near, far );

@@ -134,0 +136,0 @@ if ( intersection ) {

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc