Skip to content

Commit db5bfed

Browse files
authored
Merge pull request #17673 from mrdoob/webvr
Web*RManager: Ensure input sources are sorted
2 parents d8b2f1a + c07456a commit db5bfed

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

src/renderers/webvr/WebVRManager.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function WebVRManager( renderer ) {
109109

110110
var gamepads = navigator.getGamepads && navigator.getGamepads();
111111

112-
for ( var i = 0, j = 0, l = gamepads.length; i < l; i ++ ) {
112+
for ( var i = 0, l = gamepads.length; i < l; i ++ ) {
113113

114114
var gamepad = gamepads[ i ];
115115

@@ -119,9 +119,10 @@ function WebVRManager( renderer ) {
119119
gamepad.id.startsWith( 'HTC Vive Focus' ) ||
120120
gamepad.id.startsWith( 'Spatial Controller' ) ) ) {
121121

122-
if ( j === id ) return gamepad;
122+
var hand = gamepad.hand;
123123

124-
j ++;
124+
if ( id === 0 && ( hand === '' || hand === 'right' ) ) return gamepad;
125+
if ( id === 1 && ( hand === 'left' ) ) return gamepad;
125126

126127
}
127128

src/renderers/webvr/WebXRManager.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function WebXRManager( renderer, gl ) {
2525
var pose = null;
2626

2727
var controllers = [];
28-
var inputSources = [];
28+
var sortedInputSources = [];
2929

3030
function isPresenting() {
3131

@@ -75,7 +75,7 @@ function WebXRManager( renderer, gl ) {
7575

7676
for ( var i = 0; i < controllers.length; i ++ ) {
7777

78-
if ( inputSources[ i ] === event.inputSource ) {
78+
if ( sortedInputSources[ i ] === event.inputSource ) {
7979

8080
controllers[ i ].dispatchEvent( { type: event.type } );
8181

@@ -142,25 +142,41 @@ function WebXRManager( renderer, gl ) {
142142

143143
//
144144

145-
inputSources = session.inputSources;
145+
session.addEventListener( 'inputsourceschange', updateInputSources );
146146

147-
session.addEventListener( 'inputsourceschange', function () {
147+
updateInputSources();
148148

149-
inputSources = session.inputSources;
150-
console.log( inputSources );
149+
}
151150

152-
for ( var i = 0; i < controllers.length; i ++ ) {
151+
};
153152

154-
var controller = controllers[ i ];
155-
controller.userData.inputSource = inputSources[ i ];
153+
function updateInputSources() {
156154

157-
}
155+
for ( var i = 0; i < controllers.length; i ++ ) {
158156

159-
} );
157+
sortedInputSources[ i ] = findInputSource( i );
160158

161159
}
162160

163-
};
161+
}
162+
163+
function findInputSource( id ) {
164+
165+
var inputSources = session.inputSources;
166+
167+
for ( var i = 0; i < inputSources.length; i ++ ) {
168+
169+
var inputSource = inputSources[ i ];
170+
var handedness = inputSource.handedness;
171+
172+
if ( id === 0 && ( handedness === 'none' || handedness === 'right' ) ) return inputSource;
173+
if ( id === 1 && ( handedness === 'left' ) ) return inputSource;
174+
175+
}
176+
177+
}
178+
179+
//
164180

165181
function updateCamera( camera, parent ) {
166182

@@ -259,7 +275,7 @@ function WebXRManager( renderer, gl ) {
259275

260276
var controller = controllers[ i ];
261277

262-
var inputSource = inputSources[ i ];
278+
var inputSource = sortedInputSources[ i ];
263279

264280
if ( inputSource ) {
265281

0 commit comments

Comments
 (0)