11import { Helpers } from './Unity.js' ;
22
3+ const wristToHandDistance = 0.05 ;
4+
35const zeroVector = new THREE . Vector3 ( ) ;
46const forwardVector = new THREE . Vector3 ( 0 , 0 , 1 ) ;
57const leftRotation = new THREE . Quaternion ( ) . setFromAxisAngle ( new THREE . Vector3 ( 0 , 1 , 0 ) , Math . PI / 2 ) ;
@@ -13,6 +15,7 @@ const localVector2 = new THREE.Vector3();
1315const localVector3 = new THREE . Vector3 ( ) ;
1416const localVector4 = new THREE . Vector3 ( ) ;
1517const localVector5 = new THREE . Vector3 ( ) ;
18+ const localVector6 = new THREE . Vector3 ( ) ;
1619const localQuaternion = new THREE . Quaternion ( ) ;
1720const localQuaternion2 = new THREE . Quaternion ( ) ;
1821const localQuaternion3 = new THREE . Quaternion ( ) ;
@@ -46,28 +49,18 @@ const localMatrix = new THREE.Matrix4();
4649 Helpers . updateMatrixWorld ( this . arm . upperArm ) ;
4750
4851 const upperArmPosition = Helpers . getWorldPosition ( this . arm . upperArm , localVector ) ;
49- const handPositionDistance = this . target . position . distanceTo ( upperArmPosition ) ;
50- // let handPosition;
51- // if (handPositionDistance < this.armLength) {
52- const handPosition = this . target . position ;
53- /* } else {
54- handPosition = this.arm.upperArm.position.add(
55- this.target.position.clone().sub(this.arm.upperArm.position).normalize().multiplyScalar(this.armLength)
56- );
57- } */
5852 const handRotation = this . target . quaternion ;
53+ const handPosition = localVector2 . copy ( this . target . position ) . add ( localVector3 . set ( 0 , 0 , wristToHandDistance ) . applyQuaternion ( handRotation ) ) ;
5954
6055 const shoulderRotation = Helpers . getWorldQuaternion ( this . shoulder . transform , localQuaternion ) ;
6156 const shoulderRotationInverse = localQuaternion2 . copy ( shoulderRotation ) . inverse ( ) ;
6257
6358 const hypotenuseDistance = this . upperArmLength ;
6459 const directDistance = upperArmPosition . distanceTo ( handPosition ) / 2 ;
6560 const offsetDistance = hypotenuseDistance > directDistance ? Math . sqrt ( hypotenuseDistance * hypotenuseDistance - directDistance * directDistance ) : 0 ;
66- // console.log('offset distance', this.upperArmLength, this.lowerArmLength, hypotenuseDistance, directDistance, offsetDistance);
67- // const outFactor = targetEuler.x < 0 ? (1 - Math.min(Math.max(-targetEuler.x/(Math.PI/4), 0), 1)) : 1;
68- const offsetDirection = localVector2 . copy ( handPosition ) . sub ( upperArmPosition )
61+ const offsetDirection = localVector3 . copy ( handPosition ) . sub ( upperArmPosition )
6962 . normalize ( )
70- . cross ( localVector3 . set ( - 1 , 0 , 0 ) . applyQuaternion ( shoulderRotation ) ) ;
63+ . cross ( localVector4 . set ( - 1 , 0 , 0 ) . applyQuaternion ( shoulderRotation ) ) ;
7164
7265 const targetEuler = localEuler . setFromQuaternion (
7366 localQuaternion3
@@ -102,13 +95,13 @@ const localMatrix = new THREE.Matrix4();
10295 . applyAxisAngle ( forwardVector , targetEuler . z )
10396 . applyQuaternion ( shoulderRotation ) ;
10497
105- const elbowPosition = localVector3 . copy ( upperArmPosition ) . add ( handPosition ) . divideScalar ( 2 )
106- . add ( localVector4 . copy ( offsetDirection ) . multiplyScalar ( offsetDistance ) ) ;
107- const upVector = localVector4 . set ( this . left ? - 1 : 1 , 0 , 0 ) . applyQuaternion ( shoulderRotation ) ;
98+ const elbowPosition = localVector4 . copy ( upperArmPosition ) . add ( handPosition ) . divideScalar ( 2 )
99+ . add ( localVector5 . copy ( offsetDirection ) . multiplyScalar ( offsetDistance ) ) ;
100+ const upVector = localVector5 . set ( this . left ? - 1 : 1 , 0 , 0 ) . applyQuaternion ( shoulderRotation ) ;
108101 this . arm . upperArm . quaternion . setFromRotationMatrix (
109102 localMatrix . lookAt (
110103 zeroVector ,
111- localVector5 . copy ( elbowPosition ) . sub ( upperArmPosition ) ,
104+ localVector6 . copy ( elbowPosition ) . sub ( upperArmPosition ) ,
112105 upVector
113106 )
114107 )
@@ -120,7 +113,7 @@ const localMatrix = new THREE.Matrix4();
120113 this . arm . lowerArm . quaternion . setFromRotationMatrix (
121114 localMatrix . lookAt (
122115 zeroVector ,
123- localVector5 . copy ( handPosition ) . sub ( elbowPosition ) ,
116+ localVector6 . copy ( handPosition ) . sub ( elbowPosition ) ,
124117 upVector
125118 )
126119 )
0 commit comments