Преглед изворни кода

WebXRManager: Dispatch connected/disconnected events and pass inputSource.

Mr.doob пре 5 година
родитељ
комит
270de91664

+ 0 - 1
examples/jsm/webxr/ARButton.js

@@ -33,7 +33,6 @@ var ARButton = {
 
 				currentSession.removeEventListener( 'end', onSessionEnded );
 
-				renderer.xr.setSession( null );
 				button.textContent = 'START AR';
 
 				currentSession = null;

+ 0 - 1
examples/jsm/webxr/VRButton.js

@@ -32,7 +32,6 @@ var VRButton = {
 
 				currentSession.removeEventListener( 'end', onSessionEnded );
 
-				renderer.xr.setSession( null );
 				button.textContent = 'ENTER VR';
 
 				currentSession = null;

+ 3 - 3
src/renderers/WebGLRenderer.js

@@ -382,7 +382,7 @@ function WebGLRenderer( parameters ) {
 
 	this.setSize = function ( width, height, updateStyle ) {
 
-		if ( xr.isPresenting() ) {
+		if ( xr.isPresenting ) {
 
 			console.warn( 'THREE.WebGLRenderer: Can\'t change size while VR device is presenting.' );
 			return;
@@ -1087,7 +1087,7 @@ function WebGLRenderer( parameters ) {
 
 	function onAnimationFrame( time ) {
 
-		if ( xr.isPresenting() ) return;
+		if ( xr.isPresenting ) return;
 		if ( onAnimationFrameCallback ) onAnimationFrameCallback( time );
 
 	}
@@ -1151,7 +1151,7 @@ function WebGLRenderer( parameters ) {
 
 		if ( camera.parent === null ) camera.updateMatrixWorld();
 
-		if ( xr.enabled && xr.isPresenting() ) {
+		if ( xr.enabled && xr.isPresenting ) {
 
 			camera = xr.getCamera( camera );
 

+ 55 - 33
src/renderers/webxr/WebXRManager.js

@@ -24,13 +24,7 @@ function WebXRManager( renderer, gl ) {
 	var pose = null;
 
 	var controllers = [];
-	var sortedInputSources = [];
-
-	function isPresenting() {
-
-		return session !== null && referenceSpace !== null;
-
-	}
+	var inputSourcesMap = new Map();
 
 	//
 
@@ -50,6 +44,8 @@ function WebXRManager( renderer, gl ) {
 
 	this.enabled = false;
 
+	this.isPresenting = false;
+
 	this.getController = function ( id ) {
 
 		var controller = controllers[ id ];
@@ -72,13 +68,11 @@ function WebXRManager( renderer, gl ) {
 
 	function onSessionEvent( event ) {
 
-		for ( var i = 0; i < controllers.length; i ++ ) {
+		var controller = inputSourcesMap.get( event.inputSource );
 
-			if ( sortedInputSources[ i ] === event.inputSource ) {
+		if ( controller ) {
 
-				controllers[ i ].dispatchEvent( { type: event.type } );
-
-			}
+			controller.dispatchEvent( { type: event.type } );
 
 		}
 
@@ -86,12 +80,24 @@ function WebXRManager( renderer, gl ) {
 
 	function onSessionEnd() {
 
+		inputSourcesMap.forEach( function ( controller, inputSource ) {
+
+			controller.dispatchEvent( { type: 'disconnected', data: inputSource } );
+
+		} );
+
+		inputSourcesMap.clear();
+
+		//
+
 		renderer.setFramebuffer( null );
 		renderer.setRenderTarget( renderer.getRenderTarget() ); // Hack #15830
 		animation.stop();
 
 		scope.dispatchEvent( { type: 'sessionend' } );
 
+		scope.isPresenting = false;
+
 	}
 
 	function onRequestReferenceSpace( value ) {
@@ -103,6 +109,8 @@ function WebXRManager( renderer, gl ) {
 
 		scope.dispatchEvent( { type: 'sessionstart' } );
 
+		scope.isPresenting = true;
+
 	}
 
 	this.setFramebufferScaleFactor = function ( /* value */ ) {
@@ -157,33 +165,52 @@ function WebXRManager( renderer, gl ) {
 
 			session.addEventListener( 'inputsourceschange', updateInputSources );
 
-			updateInputSources();
-
 		}
 
 	};
 
-	function updateInputSources() {
+	function updateInputSources( event ) {
+
+		console.log( 'inputsourceschange', event, session.inputSources );
+
+		var inputSources = session.inputSources;
+
+		// Assign inputSources to available controllers
 
 		for ( var i = 0; i < controllers.length; i ++ ) {
 
-			sortedInputSources[ i ] = findInputSource( i );
+			inputSourcesMap.set( inputSources[ i ], controllers[ i ] );
 
 		}
 
-	}
+		// Notify disconnected
 
-	function findInputSource( id ) {
+		for ( var i = 0; i < event.removed.length; i ++ ) {
 
-		var inputSources = session.inputSources;
+			var inputSource = event.removed[ i ];
+			var controller = inputSourcesMap.get( inputSource );
 
-		for ( var i = 0; i < inputSources.length; i ++ ) {
+			if ( controller ) {
 
-			var inputSource = inputSources[ i ];
-			var handedness = inputSource.handedness;
+				controller.dispatchEvent( { type: 'disconnected', data: inputSource } );
+				inputSourcesMap.delete( inputSource );
+
+			}
+
+		}
+
+		// Notify connected
+
+		for ( var i = 0; i < event.added.length; i ++ ) {
+
+			var inputSource = event.added[ i ];
+			var controller = inputSourcesMap.get( inputSource );
 
-			if ( id === 0 && ( handedness === 'none' || handedness === 'right' ) ) return inputSource;
-			if ( id === 1 && ( handedness === 'left' ) ) return inputSource;
+			if ( controller ) {
+
+				controller.dispatchEvent( { type: 'connected', data: inputSource } );
+
+			}
 
 		}
 
@@ -298,8 +325,6 @@ function WebXRManager( renderer, gl ) {
 
 	};
 
-	this.isPresenting = isPresenting;
-
 	// Animation Loop
 
 	var onAnimationFrameCallback = null;
@@ -338,11 +363,13 @@ function WebXRManager( renderer, gl ) {
 
 		//
 
+		var inputSources = session.inputSources;
+
 		for ( var i = 0; i < controllers.length; i ++ ) {
 
 			var controller = controllers[ i ];
 
-			var inputSource = sortedInputSources[ i ];
+			var inputSource = inputSources[ i ];
 
 			if ( inputSource ) {
 
@@ -352,12 +379,7 @@ function WebXRManager( renderer, gl ) {
 
 					controller.matrix.fromArray( inputPose.transform.matrix );
 					controller.matrix.decompose( controller.position, controller.rotation, controller.scale );
-
-					if ( inputSource.targetRayMode === 'pointing' ) {
-
-						controller.visible = true;
-
-					}
+					controller.visible = true;
 
 					continue;