Mr.doob пре 4 година
родитељ
комит
d905a21955
3 измењених фајлова са 143 додато и 28 уклоњено
  1. 11 11
      examples/js/curves/NURBSUtils.js
  2. 17 17
      examples/js/utils/SkeletonUtils.js
  3. 115 0
      examples/js/utils/WorkerPool.js

+ 11 - 11
examples/js/curves/NURBSUtils.js

@@ -115,8 +115,8 @@ returns point for given u
 
 	function calcBSplinePoint( p, U, P, u ) {
 
-		const span = this.findSpan( p, u, U );
-		const N = this.calcBasisFunctions( span, u, p, U );
+		const span = findSpan( p, u, U );
+		const N = calcBasisFunctions( span, u, p, U );
 		const C = new THREE.Vector4( 0, 0, 0, 0 );
 
 		for ( let j = 0; j <= p; ++ j ) {
@@ -279,8 +279,8 @@ returns array[n+1][p+1] with basis functions derivatives
 
 		const du = nd < p ? nd : p;
 		const CK = [];
-		const span = this.findSpan( p, u, U );
-		const nders = this.calcBasisFunctionDerivatives( span, u, p, du, U );
+		const span = findSpan( p, u, U );
+		const nders = calcBasisFunctionDerivatives( span, u, p, du, U );
 		const Pw = [];
 
 		for ( let i = 0; i < P.length; ++ i ) {
@@ -382,7 +382,7 @@ returns array with derivatives for rational curve.
 
 			for ( let i = 1; i <= k; ++ i ) {
 
-				v.sub( CK[ k - i ].clone().multiplyScalar( this.calcKoverI( k, i ) * wders[ i ] ) );
+				v.sub( CK[ k - i ].clone().multiplyScalar( calcKoverI( k, i ) * wders[ i ] ) );
 
 			}
 
@@ -408,8 +408,8 @@ returns array with derivatives.
 
 	function calcNURBSDerivatives( p, U, P, u, nd ) {
 
-		const Pders = this.calcBSplineDerivatives( p, U, P, u, nd );
-		return this.calcRationalCurveDerivatives( Pders );
+		const Pders = calcBSplineDerivatives( p, U, P, u, nd );
+		return calcRationalCurveDerivatives( Pders );
 
 	}
 	/*
@@ -426,10 +426,10 @@ returns point for given (u, v)
 
 	function calcSurfacePoint( p, q, U, V, P, u, v, target ) {
 
-		const uspan = this.findSpan( p, u, U );
-		const vspan = this.findSpan( q, v, V );
-		const Nu = this.calcBasisFunctions( uspan, u, p, U );
-		const Nv = this.calcBasisFunctions( vspan, v, q, V );
+		const uspan = findSpan( p, u, U );
+		const vspan = findSpan( q, v, V );
+		const Nu = calcBasisFunctions( uspan, u, p, U );
+		const Nv = calcBasisFunctions( vspan, v, q, V );
 		const temp = [];
 
 		for ( let l = 0; l <= q; ++ l ) {

+ 17 - 17
examples/js/utils/SkeletonUtils.js

@@ -14,8 +14,8 @@
 		options.useTargetMatrix = options.useTargetMatrix !== undefined ? options.useTargetMatrix : false;
 		options.hip = options.hip !== undefined ? options.hip : 'hip';
 		options.names = options.names || {};
-		const sourceBones = source.isObject3D ? source.skeleton.bones : this.getBones( source ),
-			bones = target.isObject3D ? target.skeleton.bones : this.getBones( target );
+		const sourceBones = source.isObject3D ? source.skeleton.bones : getBones( source ),
+			bones = target.isObject3D ? target.skeleton.bones : getBones( target );
 		let bindBones, bone, name, boneTo, bonesPosition; // reset bones
 
 		if ( target.isObject3D ) {
@@ -82,7 +82,7 @@
 
 			bone = bones[ i ];
 			name = options.names[ bone.name ] || bone.name;
-			boneTo = this.getBoneByName( name, sourceBones );
+			boneTo = getBoneByName( name, sourceBones );
 			globalMatrix.copy( bone.matrixWorld );
 
 			if ( boneTo ) {
@@ -174,7 +174,7 @@
 
 		if ( ! source.isObject3D ) {
 
-			source = this.getHelperFromSkeleton( source );
+			source = getHelperFromSkeleton( source );
 
 		}
 
@@ -182,7 +182,7 @@
 			delta = 1 / options.fps,
 			convertedTracks = [],
 			mixer = new THREE.AnimationMixer( source ),
-			bones = this.getBones( target.skeleton ),
+			bones = getBones( target.skeleton ),
 			boneDatas = [];
 		let positionOffset, bone, boneTo, boneData, name;
 		mixer.clipAction( clip ).play();
@@ -192,12 +192,12 @@
 		for ( let i = 0; i < numFrames; ++ i ) {
 
 			const time = i * delta;
-			this.retarget( target, source, options );
+			retarget( target, source, options );
 
 			for ( let j = 0; j < bones.length; ++ j ) {
 
 				name = options.names[ bones[ j ].name ] || bones[ j ].name;
-				boneTo = this.getBoneByName( name, source.skeleton );
+				boneTo = getBoneByName( name, source.skeleton );
 
 				if ( boneTo ) {
 
@@ -299,14 +299,14 @@
 
 		if ( ! source.isObject3D ) {
 
-			source = this.getHelperFromSkeleton( source );
+			source = getHelperFromSkeleton( source );
 
 		}
 
 		const nameKeys = Object.keys( options.names ),
 			nameValues = Object.values( options.names ),
-			sourceBones = source.isObject3D ? source.skeleton.bones : this.getBones( source ),
-			bones = target.isObject3D ? target.skeleton.bones : this.getBones( target ),
+			sourceBones = source.isObject3D ? source.skeleton.bones : getBones( source ),
+			bones = target.isObject3D ? target.skeleton.bones : getBones( target ),
 			offsets = [];
 		let bone, boneTo, name, i;
 		target.skeleton.pose();
@@ -315,12 +315,12 @@
 
 			bone = bones[ i ];
 			name = options.names[ bone.name ] || bone.name;
-			boneTo = this.getBoneByName( name, sourceBones );
+			boneTo = getBoneByName( name, sourceBones );
 
 			if ( boneTo && name !== options.hip ) {
 
-				const boneParent = this.getNearestBone( bone.parent, nameKeys ),
-					boneToParent = this.getNearestBone( boneTo.parent, nameValues );
+				const boneParent = getNearestBone( bone.parent, nameKeys ),
+					boneToParent = getNearestBone( boneTo.parent, nameValues );
 				boneParent.updateMatrixWorld();
 				boneToParent.updateMatrixWorld();
 				targetParentPos.setFromMatrixPosition( boneParent.matrixWorld );
@@ -346,7 +346,7 @@
 
 	function renameBones( skeleton, names ) {
 
-		const bones = this.getBones( skeleton );
+		const bones = getBones( skeleton );
 
 		for ( let i = 0; i < bones.length; ++ i ) {
 
@@ -372,7 +372,7 @@
 
 	function getBoneByName( name, skeleton ) {
 
-		for ( let i = 0, bones = this.getBones( skeleton ); i < bones.length; i ++ ) {
+		for ( let i = 0, bones = getBones( skeleton ); i < bones.length; i ++ ) {
 
 			if ( name === bones[ i ].name ) return bones[ i ];
 
@@ -423,8 +423,8 @@
 
 	function getEqualsBonesNames( skeleton, targetSkeleton ) {
 
-		const sourceBones = this.getBones( skeleton ),
-			targetBones = this.getBones( targetSkeleton ),
+		const sourceBones = getBones( skeleton ),
+			targetBones = getBones( targetSkeleton ),
 			bones = [];
 
 		search: for ( let i = 0; i < sourceBones.length; i ++ ) {

+ 115 - 0
examples/js/utils/WorkerPool.js

@@ -0,0 +1,115 @@
+( function () {
+
+	/**
+ * @author Deepkolos / https://github.com/deepkolos
+ */
+	class WorkerPool {
+
+		constructor( pool = 4 ) {
+
+			this.pool = pool;
+			this.queue = [];
+			this.workers = [];
+			this.workersResolve = [];
+			this.workerStatus = 0;
+
+		}
+
+		_initWorker( workerId ) {
+
+			if ( ! this.workers[ workerId ] ) {
+
+				const worker = this.workerCreator();
+				worker.addEventListener( 'message', this._onMessage.bind( this, workerId ) );
+				this.workers[ workerId ] = worker;
+
+			}
+
+		}
+
+		_getIdleWorker() {
+
+			for ( let i = 0; i < this.pool; i ++ ) if ( ! ( this.workerStatus & 1 << i ) ) return i;
+
+			return - 1;
+
+		}
+
+		_onMessage( workerId, msg ) {
+
+			const resolve = this.workersResolve[ workerId ];
+			resolve && resolve( msg );
+
+			if ( this.queue.length ) {
+
+				const {
+					resolve,
+					msg,
+					transfer
+				} = this.queue.shift();
+				this.workersResolve[ workerId ] = resolve;
+				this.workers[ workerId ].postMessage( msg, transfer );
+
+			} else {
+
+				this.workerStatus ^= 1 << workerId;
+
+			}
+
+		}
+
+		setWorkerCreator( workerCreator ) {
+
+			this.workerCreator = workerCreator;
+
+		}
+
+		setWorkerLimit( pool ) {
+
+			this.pool = pool;
+
+		}
+
+		postMessage( msg, transfer ) {
+
+			return new Promise( resolve => {
+
+				const workerId = this._getIdleWorker();
+
+				if ( workerId !== - 1 ) {
+
+					this._initWorker( workerId );
+
+					this.workerStatus |= 1 << workerId;
+					this.workersResolve[ workerId ] = resolve;
+					this.workers[ workerId ].postMessage( msg, transfer );
+
+				} else {
+
+					this.queue.push( {
+						resolve,
+						msg,
+						transfer
+					} );
+
+				}
+
+			} );
+
+		}
+
+		dispose() {
+
+			this.workers.forEach( worker => worker.terminate() );
+			this.workersResolve.length = 0;
+			this.workers.length = 0;
+			this.queue.length = 0;
+			this.workerStatus = 0;
+
+		}
+
+	}
+
+	THREE.WorkerPool = WorkerPool;
+
+} )();