| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 | import {	Box3,	Line3,	Plane,	Sphere,	Triangle,	Vector3} from '../../../build/three.module.js';import { Capsule } from '../math/Capsule.js';const _v1 = new Vector3();const _v2 = new Vector3();const _plane = new Plane();const _line1 = new Line3();const _line2 = new Line3();const _sphere = new Sphere();const _capsule = new Capsule();class Octree {	constructor( box ) {		this.triangles = [];		this.box = box;		this.subTrees = [];	}	addTriangle( triangle ) {		if ( ! this.bounds ) this.bounds = new Box3();		this.bounds.min.x = Math.min( this.bounds.min.x, triangle.a.x, triangle.b.x, triangle.c.x );		this.bounds.min.y = Math.min( this.bounds.min.y, triangle.a.y, triangle.b.y, triangle.c.y );		this.bounds.min.z = Math.min( this.bounds.min.z, triangle.a.z, triangle.b.z, triangle.c.z );		this.bounds.max.x = Math.max( this.bounds.max.x, triangle.a.x, triangle.b.x, triangle.c.x );		this.bounds.max.y = Math.max( this.bounds.max.y, triangle.a.y, triangle.b.y, triangle.c.y );		this.bounds.max.z = Math.max( this.bounds.max.z, triangle.a.z, triangle.b.z, triangle.c.z );		this.triangles.push( triangle );		return this;	}	calcBox() {		this.box = this.bounds.clone();		// offset small ammount to account for regular grid		this.box.min.x -= 0.01;		this.box.min.y -= 0.01;		this.box.min.z -= 0.01;		return this;	}	split( level ) {		if ( ! this.box ) return;		const subTrees = [];		const halfsize = _v2.copy( this.box.max ).sub( this.box.min ).multiplyScalar( 0.5 );		for ( let x = 0; x < 2; x ++ ) {			for ( let y = 0; y < 2; y ++ ) {				for ( let z = 0; z < 2; z ++ ) {					const box = new Box3();					const v = _v1.set( x, y, z );					box.min.copy( this.box.min ).add( v.multiply( halfsize ) );					box.max.copy( box.min ).add( halfsize );					subTrees.push( new Octree( box ) );				}			}		}		let triangle;		while ( triangle = this.triangles.pop() ) {			for ( let i = 0; i < subTrees.length; i ++ ) {				if ( subTrees[ i ].box.intersectsTriangle( triangle ) ) {					subTrees[ i ].triangles.push( triangle );				}			}		}		for ( let i = 0; i < subTrees.length; i ++ ) {			const len = subTrees[ i ].triangles.length;			if ( len > 8 && level < 16 ) {				subTrees[ i ].split( level + 1 );			}			if ( len !== 0 ) {				this.subTrees.push( subTrees[ i ] );			}		}		return this;	}	build() {		this.calcBox();		this.split( 0 );		return this;	}	getRayTriangles( ray, triangles ) {		for ( let i = 0; i < this.subTrees.length; i ++ ) {			const subTree = this.subTrees[ i ];			if ( ! ray.intersectsBox( subTree.box ) ) continue;			if ( subTree.triangles.length > 0 ) {				for ( let j = 0; j < subTree.triangles.length; j ++ ) {					if ( triangles.indexOf( subTree.triangles[ j ] ) === - 1 ) triangles.push( subTree.triangles[ j ] );				}			} else {				subTree.getRayTriangles( ray, triangles );			}		}		return triangles;	}	triangleCapsuleIntersect( capsule, triangle ) {		triangle.getPlane( _plane );		const d1 = _plane.distanceToPoint( capsule.start ) - capsule.radius;		const d2 = _plane.distanceToPoint( capsule.end ) - capsule.radius;		if ( ( d1 > 0 && d2 > 0 ) || ( d1 < - capsule.radius && d2 < - capsule.radius ) ) {			return false;		}		const delta = Math.abs( d1 / ( Math.abs( d1 ) + Math.abs( d2 ) ) );		const intersectPoint = _v1.copy( capsule.start ).lerp( capsule.end, delta );		if ( triangle.containsPoint( intersectPoint ) ) {			return { normal: _plane.normal.clone(), point: intersectPoint.clone(), depth: Math.abs( Math.min( d1, d2 ) ) };		}		const r2 = capsule.radius * capsule.radius;		const line1 = _line1.set( capsule.start, capsule.end );		const lines = [			[ triangle.a, triangle.b ],			[ triangle.b, triangle.c ],			[ triangle.c, triangle.a ]		];		for ( let i = 0; i < lines.length; i ++ ) {			const line2 = _line2.set( lines[ i ][ 0 ], lines[ i ][ 1 ] );			const [ point1, point2 ] = capsule.lineLineMinimumPoints( line1, line2 );			if ( point1.distanceToSquared( point2 ) < r2 ) {				return { normal: point1.clone().sub( point2 ).normalize(), point: point2.clone(), depth: capsule.radius - point1.distanceTo( point2 ) };			}		}		return false;	}	triangleSphereIntersect( sphere, triangle ) {		triangle.getPlane( _plane );		if ( ! sphere.intersectsPlane( _plane ) ) return false;		const depth = Math.abs( _plane.distanceToSphere( sphere ) );		const r2 = sphere.radius * sphere.radius - depth * depth;		const plainPoint = _plane.projectPoint( sphere.center, _v1 );		if ( triangle.containsPoint( sphere.center ) ) {			return { normal: _plane.normal.clone(), point: plainPoint.clone(), depth: Math.abs( _plane.distanceToSphere( sphere ) ) };		}		const lines = [			[ triangle.a, triangle.b ],			[ triangle.b, triangle.c ],			[ triangle.c, triangle.a ]		];		for ( let i = 0; i < lines.length; i ++ ) {			_line1.set( lines[ i ][ 0 ], lines[ i ][ 1 ] );			_line1.closestPointToPoint( plainPoint, true, _v2 );			const d = _v2.distanceToSquared( sphere.center );			if ( d < r2 ) {				return { normal: sphere.center.clone().sub( _v2 ).normalize(), point: _v2.clone(), depth: sphere.radius - Math.sqrt( d ) };			}		}		return false;	}	getSphereTriangles( sphere, triangles ) {		for ( let i = 0; i < this.subTrees.length; i ++ ) {			const subTree = this.subTrees[ i ];			if ( ! sphere.intersectsBox( subTree.box ) ) continue;			if ( subTree.triangles.length > 0 ) {				for ( let j = 0; j < subTree.triangles.length; j ++ ) {					if ( triangles.indexOf( subTree.triangles[ j ] ) === - 1 ) triangles.push( subTree.triangles[ j ] );				}			} else {				subTree.getSphereTriangles( sphere, triangles );			}		}	}	getCapsuleTriangles( capsule, triangles ) {		for ( let i = 0; i < this.subTrees.length; i ++ ) {			const subTree = this.subTrees[ i ];			if ( ! capsule.intersectsBox( subTree.box ) ) continue;			if ( subTree.triangles.length > 0 ) {				for ( let j = 0; j < subTree.triangles.length; j ++ ) {					if ( triangles.indexOf( subTree.triangles[ j ] ) === - 1 ) triangles.push( subTree.triangles[ j ] );				}			} else {				subTree.getCapsuleTriangles( capsule, triangles );			}		}	}	sphereIntersect( sphere ) {		_sphere.copy( sphere );		const triangles = [];		let result, hit = false;		this.getSphereTriangles( sphere, triangles );		for ( let i = 0; i < triangles.length; i ++ ) {			if ( result = this.triangleSphereIntersect( _sphere, triangles[ i ] ) ) {				hit = true;				_sphere.center.add( result.normal.multiplyScalar( result.depth ) );			}		}		if ( hit ) {			const collisionVector = _sphere.center.clone().sub( sphere.center );			const depth = collisionVector.length();			return { normal: collisionVector.normalize(), depth: depth };		}		return false;	}	capsuleIntersect( capsule ) {		_capsule.copy( capsule );		const triangles = [];		let result, hit = false;		this.getCapsuleTriangles( _capsule, triangles );		for ( let i = 0; i < triangles.length; i ++ ) {			if ( result = this.triangleCapsuleIntersect( _capsule, triangles[ i ] ) ) {				hit = true;				_capsule.translate( result.normal.multiplyScalar( result.depth ) );			}		}		if ( hit ) {			const collisionVector = _capsule.getCenter( new Vector3() ).sub( capsule.getCenter( _v1 ) );			const depth = collisionVector.length();			return { normal: collisionVector.normalize(), depth: depth };		}		return false;	}	rayIntersect( ray ) {		if ( ray.direction.length() === 0 ) return;		const triangles = [];		let triangle, position, distance = 1e100;		this.getRayTriangles( ray, triangles );		for ( let i = 0; i < triangles.length; i ++ ) {			const result = ray.intersectTriangle( triangles[ i ].a, triangles[ i ].b, triangles[ i ].c, true, _v1 );			if ( result ) {				const newdistance = result.sub( ray.origin ).length();				if ( distance > newdistance ) {					position = result.clone().add( ray.origin );					distance = newdistance;					triangle = triangles[ i ];				}			}		}		return distance < 1e100 ? { distance: distance, triangle: triangle, position: position } : false;	}	fromGraphNode( group ) {		group.updateWorldMatrix( true, true );		group.traverse( ( obj ) => {			if ( obj.isMesh === true ) {				let geometry, isTemp = false;				if ( obj.geometry.index !== null ) {					isTemp = true;					geometry = obj.geometry.toNonIndexed();				} else {					geometry = obj.geometry;				}				const positionAttribute = geometry.getAttribute( 'position' );				for ( let i = 0; i < positionAttribute.count; i += 3 ) {					const v1 = new Vector3().fromBufferAttribute( positionAttribute, i );					const v2 = new Vector3().fromBufferAttribute( positionAttribute, i + 1 );					const v3 = new Vector3().fromBufferAttribute( positionAttribute, i + 2 );					v1.applyMatrix4( obj.matrixWorld );					v2.applyMatrix4( obj.matrixWorld );					v3.applyMatrix4( obj.matrixWorld );					this.addTriangle( new Triangle( v1, v2, v3 ) );				}				if ( isTemp ) {					geometry.dispose();				}			}		} );		this.build();		return this;	}}export { Octree };
 |