ViveController.js 3.1 KB
Newer Older
M
Mr.doob 已提交
1 2
/**
 * @author mrdoob / http://mrdoob.com
S
Stewart 已提交
3
 * @author stewdio / http://stewd.io
M
Mr.doob 已提交
4 5
 */

6
THREE.ViveController = function ( id ) {
M
Mr.doob 已提交
7 8 9

	THREE.Object3D.call( this );

10 11
	var scope = this;
	var gamepad;
S
Stewart 已提交
12

13 14 15 16 17
	var axes = [ 0, 0 ];
	var thumbpadIsPressed = false;
	var triggerIsPressed = false;
	var gripsArePressed = false;
	var menuIsPressed = false;
M
Mr.doob 已提交
18

M
Mr.doob 已提交
19 20 21 22 23 24 25 26 27 28 29
	function findGamepad( id ) {

		// Iterate across gamepads as Vive Controllers may not be
		// in position 0 and 1.

		var gamepads = navigator.getGamepads();

		for ( var i = 0, j = 0; i < 4; i ++ ) {

			var gamepad = gamepads[ i ];

V
Vbitz 已提交
30
			if ( gamepad && (gamepad.id === 'OpenVR Gamepad' || gamepad.id === 'Oculus Touch (Left)' || gamepad.id === 'Oculus Touch (Right)') ) {
M
Mr.doob 已提交
31 32 33 34 35 36 37 38 39 40 41

				if ( j === id ) return gamepad;

				j ++;

			}

		}

	}

M
Mr.doob 已提交
42 43
	this.matrixAutoUpdate = false;
	this.standingMatrix = new THREE.Matrix4();
44 45

	this.getGamepad = function () {
M
Mr.doob 已提交
46

S
Stewart 已提交
47
		return gamepad;
M
Mr.doob 已提交
48

49 50 51
	};

	this.getButtonState = function ( button ) {
M
Mr.doob 已提交
52

53 54 55 56
		if ( button === 'thumbpad' ) return thumbpadIsPressed;
		if ( button === 'trigger' ) return triggerIsPressed;
		if ( button === 'grips' ) return gripsArePressed;
		if ( button === 'menu' ) return menuIsPressed;
M
Mr.doob 已提交
57

58
	};
S
Stewart 已提交
59

60
	this.update = function () {
S
Stewart 已提交
61

M
Mr.doob 已提交
62
		gamepad = findGamepad( id );
M
Mr.doob 已提交
63

64 65 66
		if ( gamepad !== undefined && gamepad.pose !== undefined ) {

			if ( gamepad.pose === null ) return; // No user action yet
M
Mr.doob 已提交
67

S
Stewart 已提交
68 69
			//  Position and orientation.

70 71
			var pose = gamepad.pose;

72 73
			if ( pose.position !== null ) scope.position.fromArray( pose.position );
			if ( pose.orientation !== null ) scope.quaternion.fromArray( pose.orientation );
M
Mr.doob 已提交
74 75 76 77 78
			scope.matrix.compose( scope.position, scope.quaternion, scope.scale );
			scope.matrix.multiplyMatrices( scope.standingMatrix, scope.matrix );
			scope.matrixWorldNeedsUpdate = true;
			scope.visible = true;

S
Stewart 已提交
79
			//  Thumbpad and Buttons.
M
Mr.doob 已提交
80

81
			if ( axes[ 0 ] !== gamepad.axes[ 0 ] || axes[ 1 ] !== gamepad.axes[ 1 ] ) {
M
Mr.doob 已提交
82

83 84 85
				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 } );
S
Stewart 已提交
86 87

			}
88

S
Stewart 已提交
89 90 91
			if ( thumbpadIsPressed !== gamepad.buttons[ 0 ].pressed ) {

				thumbpadIsPressed = gamepad.buttons[ 0 ].pressed;
92
				scope.dispatchEvent( { type: thumbpadIsPressed ? 'thumbpaddown' : 'thumbpadup' } );
S
Stewart 已提交
93 94

			}
95

S
Stewart 已提交
96 97 98
			if ( triggerIsPressed !== gamepad.buttons[ 1 ].pressed ) {

				triggerIsPressed = gamepad.buttons[ 1 ].pressed;
99
				scope.dispatchEvent( { type: triggerIsPressed ? 'triggerdown' : 'triggerup' } );
S
Stewart 已提交
100 101

			}
102

S
Stewart 已提交
103
			if ( gripsArePressed !== gamepad.buttons[ 2 ].pressed ) {
M
Mr.doob 已提交
104

S
Stewart 已提交
105
				gripsArePressed = gamepad.buttons[ 2 ].pressed;
106
				scope.dispatchEvent( { type: gripsArePressed ? 'gripsdown' : 'gripsup' } );
M
Mr.doob 已提交
107

S
Stewart 已提交
108
			}
109

S
Stewart 已提交
110 111 112
			if ( menuIsPressed !== gamepad.buttons[ 3 ].pressed ) {

				menuIsPressed = gamepad.buttons[ 3 ].pressed;
113
				scope.dispatchEvent( { type: menuIsPressed ? 'menudown' : 'menuup' } );
S
Stewart 已提交
114 115 116

			}

117 118 119 120
		} else {

			scope.visible = false;

S
Stewart 已提交
121 122
		}

123 124 125
	};

};
M
Mr.doob 已提交
126 127 128

THREE.ViveController.prototype = Object.create( THREE.Object3D.prototype );
THREE.ViveController.prototype.constructor = THREE.ViveController;