Browse Source

Merge pull request #17673 from mrdoob/webvr

Web*RManager: Ensure input sources are sorted
Mr.doob 5 years ago
parent
commit
db5bfed9b0
2 changed files with 33 additions and 16 deletions
  1. 4 3
      src/renderers/webvr/WebVRManager.js
  2. 29 13
      src/renderers/webvr/WebXRManager.js

+ 4 - 3
src/renderers/webvr/WebVRManager.js

@@ -109,7 +109,7 @@ function WebVRManager( renderer ) {
 
 		var gamepads = navigator.getGamepads && navigator.getGamepads();
 
-		for ( var i = 0, j = 0, l = gamepads.length; i < l; i ++ ) {
+		for ( var i = 0, l = gamepads.length; i < l; i ++ ) {
 
 			var gamepad = gamepads[ i ];
 
@@ -119,9 +119,10 @@ function WebVRManager( renderer ) {
 				gamepad.id.startsWith( 'HTC Vive Focus' ) ||
 				gamepad.id.startsWith( 'Spatial Controller' ) ) ) {
 
-				if ( j === id ) return gamepad;
+				var hand = gamepad.hand;
 
-				j ++;
+				if ( id === 0 && ( hand === '' || hand === 'right' ) ) return gamepad;
+				if ( id === 1 && ( hand === 'left' ) ) return gamepad;
 
 			}
 

+ 29 - 13
src/renderers/webvr/WebXRManager.js

@@ -25,7 +25,7 @@ function WebXRManager( renderer, gl ) {
 	var pose = null;
 
 	var controllers = [];
-	var inputSources = [];
+	var sortedInputSources = [];
 
 	function isPresenting() {
 
@@ -75,7 +75,7 @@ function WebXRManager( renderer, gl ) {
 
 		for ( var i = 0; i < controllers.length; i ++ ) {
 
-			if ( inputSources[ i ] === event.inputSource ) {
+			if ( sortedInputSources[ i ] === event.inputSource ) {
 
 				controllers[ i ].dispatchEvent( { type: event.type } );
 
@@ -142,25 +142,41 @@ function WebXRManager( renderer, gl ) {
 
 			//
 
-			inputSources = session.inputSources;
+			session.addEventListener( 'inputsourceschange', updateInputSources );
 
-			session.addEventListener( 'inputsourceschange', function () {
+			updateInputSources();
 
-				inputSources = session.inputSources;
-				console.log( inputSources );
+		}
 
-				for ( var i = 0; i < controllers.length; i ++ ) {
+	};
 
-					var controller = controllers[ i ];
-					controller.userData.inputSource = inputSources[ i ];
+	function updateInputSources() {
 
-				}
+		for ( var i = 0; i < controllers.length; i ++ ) {
 
-			} );
+			sortedInputSources[ i ] = findInputSource( i );
 
 		}
 
-	};
+	}
+
+	function findInputSource( id ) {
+
+		var inputSources = session.inputSources;
+
+		for ( var i = 0; i < inputSources.length; i ++ ) {
+
+			var inputSource = inputSources[ i ];
+			var handedness = inputSource.handedness;
+
+			if ( id === 0 && ( handedness === 'none' || handedness === 'right' ) ) return inputSource;
+			if ( id === 1 && ( handedness === 'left' ) ) return inputSource;
+
+		}
+
+	}
+
+	//
 
 	function updateCamera( camera, parent ) {
 
@@ -259,7 +275,7 @@ function WebXRManager( renderer, gl ) {
 
 			var controller = controllers[ i ];
 
-			var inputSource = inputSources[ i ];
+			var inputSource = sortedInputSources[ i ];
 
 			if ( inputSource ) {