Spaces:
Running
Running
File size: 3,176 Bytes
6cd9596 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
/**
* @author mrdoob / http://mrdoob.com
* @author stewdio / http://stewd.io
*/
THREE.ViveController = function ( id ) {
THREE.Object3D.call( this );
var scope = this;
var gamepad;
var axes = [ 0, 0 ];
var thumbpadIsPressed = false;
var triggerIsPressed = false;
var gripsArePressed = false;
var menuIsPressed = false;
function findGamepad( id ) {
// Iterate across gamepads as Vive Controllers may not be
// in position 0 and 1.
var gamepads = navigator.getGamepads && navigator.getGamepads();
for ( var i = 0, j = 0; i < gamepads.length; i ++ ) {
var gamepad = gamepads[ i ];
if ( gamepad && ( gamepad.id === 'OpenVR Gamepad' || gamepad.id.startsWith( 'Oculus Touch' ) || gamepad.id.startsWith( 'Spatial Controller' ) ) ) {
if ( j === id ) return gamepad;
j ++;
}
}
}
this.matrixAutoUpdate = false;
this.standingMatrix = new THREE.Matrix4();
this.getGamepad = function () {
return gamepad;
};
this.getButtonState = function ( button ) {
if ( button === 'thumbpad' ) return thumbpadIsPressed;
if ( button === 'trigger' ) return triggerIsPressed;
if ( button === 'grips' ) return gripsArePressed;
if ( button === 'menu' ) return menuIsPressed;
};
this.update = function () {
gamepad = findGamepad( id );
if ( gamepad !== undefined && gamepad.pose !== undefined ) {
if ( gamepad.pose === null ) return; // No user action yet
// Position and orientation.
var pose = gamepad.pose;
if ( pose.position !== null ) scope.position.fromArray( pose.position );
if ( pose.orientation !== null ) scope.quaternion.fromArray( pose.orientation );
scope.matrix.compose( scope.position, scope.quaternion, scope.scale );
scope.matrix.premultiply( scope.standingMatrix );
scope.matrixWorldNeedsUpdate = true;
scope.visible = true;
// Thumbpad and Buttons.
if ( axes[ 0 ] !== gamepad.axes[ 0 ] || axes[ 1 ] !== gamepad.axes[ 1 ] ) {
axes[ 0 ] = gamepad.axes[ 0 ]; // X axis: -1 = Left, +1 = Right.
axes[ 1 ] = gamepad.axes[ 1 ]; // Y axis: -1 = Bottom, +1 = Top.
scope.dispatchEvent( { type: 'axischanged', axes: axes } );
}
if ( thumbpadIsPressed !== gamepad.buttons[ 0 ].pressed ) {
thumbpadIsPressed = gamepad.buttons[ 0 ].pressed;
scope.dispatchEvent( { type: thumbpadIsPressed ? 'thumbpaddown' : 'thumbpadup', axes: axes } );
}
if ( triggerIsPressed !== gamepad.buttons[ 1 ].pressed ) {
triggerIsPressed = gamepad.buttons[ 1 ].pressed;
scope.dispatchEvent( { type: triggerIsPressed ? 'triggerdown' : 'triggerup' } );
}
if ( gripsArePressed !== gamepad.buttons[ 2 ].pressed ) {
gripsArePressed = gamepad.buttons[ 2 ].pressed;
scope.dispatchEvent( { type: gripsArePressed ? 'gripsdown' : 'gripsup' } );
}
if ( menuIsPressed !== gamepad.buttons[ 3 ].pressed ) {
menuIsPressed = gamepad.buttons[ 3 ].pressed;
scope.dispatchEvent( { type: menuIsPressed ? 'menudown' : 'menuup' } );
}
} else {
scope.visible = false;
}
};
};
THREE.ViveController.prototype = Object.create( THREE.Object3D.prototype );
THREE.ViveController.prototype.constructor = THREE.ViveController;
|