Browse Source

Introduced getHand() on WebXRManager

Fernando Serrano 5 years ago
parent
commit
8831e1d2d7
2 changed files with 68 additions and 11 deletions
  1. 52 11
      src/renderers/webxr/WebXRController.js
  2. 16 0
      src/renderers/webxr/WebXRManager.js

+ 52 - 11
src/renderers/webxr/WebXRController.js

@@ -8,6 +8,7 @@ function WebXRController() {
 
 	this._targetRay = null;
 	this._grip = null;
+	this._hand = null;
 
 }
 
@@ -15,6 +16,20 @@ Object.assign( WebXRController.prototype, {
 
 	constructor: WebXRController,
 
+	getHandSpace: function () {
+
+		if ( this._hand === null ) {
+
+			this._hand = new Group();
+			this._hand.matrixAutoUpdate = false;
+			this._hand.visible = false;
+
+		}
+
+		return this._hand;
+
+	},
+
 	getTargetRaySpace: function () {
 
 		if ( this._targetRay === null ) {
@@ -57,6 +72,12 @@ Object.assign( WebXRController.prototype, {
 
 		}
 
+		if ( this._hand !== null ) {
+
+			this._hand.dispatchEvent( event );
+
+		}
+
 		return this;
 
 	},
@@ -77,6 +98,12 @@ Object.assign( WebXRController.prototype, {
 
 		}
 
+		if ( this._hand !== null ) {
+
+			this._hand.visible = false;
+
+		}
+
 		return this;
 
 	},
@@ -85,33 +112,41 @@ Object.assign( WebXRController.prototype, {
 
 		let inputPose = null;
 		let gripPose = null;
+		let handPose = null;
 
 		const targetRay = this._targetRay;
 		const grip = this._grip;
+		const hand = this._hand;
 
 		if ( inputSource ) {
 
-			if ( targetRay !== null ) {
+			if ( inputSource.hand ) {
+
+			} else {
+
+				if ( targetRay !== null ) {
 
-				inputPose = frame.getPose( inputSource.targetRaySpace, referenceSpace );
+					inputPose = frame.getPose( inputSource.targetRaySpace, referenceSpace );
 
-				if ( inputPose !== null ) {
+					if ( inputPose !== null ) {
 
-					targetRay.matrix.fromArray( inputPose.transform.matrix );
-					targetRay.matrix.decompose( targetRay.position, targetRay.rotation, targetRay.scale );
+						targetRay.matrix.fromArray( inputPose.transform.matrix );
+						targetRay.matrix.decompose( targetRay.position, targetRay.rotation, targetRay.scale );
+
+					}
 
 				}
 
-			}
+				if ( grip !== null && inputSource.gripSpace ) {
 
-			if ( grip !== null && inputSource.gripSpace ) {
+					gripPose = frame.getPose( inputSource.gripSpace, referenceSpace );
 
-				gripPose = frame.getPose( inputSource.gripSpace, referenceSpace );
+					if ( gripPose !== null ) {
 
-				if ( gripPose !== null ) {
+						grip.matrix.fromArray( gripPose.transform.matrix );
+						grip.matrix.decompose( grip.position, grip.rotation, grip.scale );
 
-					grip.matrix.fromArray( gripPose.transform.matrix );
-					grip.matrix.decompose( grip.position, grip.rotation, grip.scale );
+					}
 
 				}
 
@@ -131,6 +166,12 @@ Object.assign( WebXRController.prototype, {
 
 		}
 
+		if ( hand !== null ) {
+
+			hand.visible = ( handPose !== null );
+
+		}
+
 		return this;
 
 	}

+ 16 - 0
src/renderers/webxr/WebXRManager.js

@@ -81,6 +81,21 @@ function WebXRManager( renderer, gl ) {
 
 	};
 
+	this.getHand = function ( index ) {
+
+		let controller = controllers[ index ];
+
+		if ( controller === undefined ) {
+
+			controller = new WebXRController();
+			controllers[ index ] = controller;
+
+		}
+
+		return controller.getHandSpace();
+
+	};
+
 	//
 
 	function onSessionEvent( event ) {
@@ -449,6 +464,7 @@ function WebXRManager( renderer, gl ) {
 		//
 
 		const inputSources = session.inputSources;
+		//console.log(inputSources);
 
 		for ( let i = 0; i < controllers.length; i ++ ) {