提交 08e94604 编写于 作者: S Stewart 提交者: Mr.doob

Added Vive controller events. (#9586)

* Added Vive controller events.

* Reformatted for Doob-style.

* Fewer public variables, more API-like.

* Fixed how axisChanged is reported.
上级 7714fe8f
/** /**
* @author mrdoob / http://mrdoob.com * @author mrdoob / http://mrdoob.com
* @author stewdio / http://stewd.io
*/ */
THREE.ViveController = function ( id ) { THREE.ViveController = function( id ) {
THREE.Object3D.call( this ); THREE.Object3D.call( this );
var scope = this; var
var gamepad; scope = this,
gamepad,
axes = [ 0, 0 ],
thumbpadIsPressed = false,
triggerIsPressed = false,
gripsArePressed = false,
menuIsPressed = false;
function dispatchViveControllerEvent( name, custom ) {
var data = {};
data.id = id;
data.gamepad = gamepad;
data.instance = scope;
if ( custom !== undefined ) Object.assign( data, custom );
window.dispatchEvent( new CustomEvent( 'viveController' + name, { detail: data } ) );
}
this.getGamepad = function () { return gamepad; };
this.matrixAutoUpdate = false; this.matrixAutoUpdate = false;
this.standingMatrix = new THREE.Matrix4(); this.standingMatrix = new THREE.Matrix4();
this.getGamepad = function() {
function update() { return gamepad;
requestAnimationFrame( update ); }
this.getButtonState = function( button ) {
gamepad = navigator.getGamepads()[ id ]; return scope[ button + ( button === 'grips' ? 'ArePressed' : 'IsPressed' ) ];
}
this.update = function() {
var pose;
gamepad = navigator.getGamepads()[ id ];
if ( gamepad !== undefined && gamepad.pose !== null ) { if ( gamepad !== undefined && gamepad.pose !== null ) {
var pose = gamepad.pose;
// Position and orientation.
pose = gamepad.pose;
scope.position.fromArray( pose.position ); scope.position.fromArray( pose.position );
scope.quaternion.fromArray( pose.orientation ); scope.quaternion.fromArray( pose.orientation );
scope.matrix.compose( scope.position, scope.quaternion, scope.scale ); scope.matrix.compose( scope.position, scope.quaternion, scope.scale );
scope.matrix.multiplyMatrices( scope.standingMatrix, scope.matrix ); scope.matrix.multiplyMatrices( scope.standingMatrix, scope.matrix );
scope.matrixWorldNeedsUpdate = true; scope.matrixWorldNeedsUpdate = true;
scope.visible = true; scope.visible = true;
} else {
scope.visible = false; // 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.
dispatchViveControllerEvent( 'AxisChanged', { axes: axes } );
}
if ( thumbpadIsPressed !== gamepad.buttons[ 0 ].pressed ) {
thumbpadIsPressed = gamepad.buttons[ 0 ].pressed;
dispatchViveControllerEvent( thumbpadIsPressed ? 'ThumbpadPressed' : 'ThumbpadReleased' );
}
if ( triggerIsPressed !== gamepad.buttons[ 1 ].pressed ) {
triggerIsPressed = gamepad.buttons[ 1 ].pressed;
dispatchViveControllerEvent( triggerIsPressed ? 'TriggerPressed' : 'TriggerReleased' );
}
if ( gripsArePressed !== gamepad.buttons[ 2 ].pressed ) {
update(); gripsArePressed = gamepad.buttons[ 2 ].pressed;
dispatchViveControllerEvent( gripsArePressed ? 'GripsPressed' : 'GripsReleased' );
}; }
if ( menuIsPressed !== gamepad.buttons[ 3 ].pressed ) {
menuIsPressed = gamepad.buttons[ 3 ].pressed;
dispatchViveControllerEvent( menuIsPressed ? 'MenuPressed' : 'MenuReleased' );
}
}
else scope.visible = false;
}
}
THREE.ViveController.prototype = Object.create( THREE.Object3D.prototype ); THREE.ViveController.prototype = Object.create( THREE.Object3D.prototype );
THREE.ViveController.prototype.constructor = THREE.ViveController; THREE.ViveController.prototype.constructor = THREE.ViveController;
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册