webvr-libs
Version:
WebVR libraries
132 lines (72 loc) • 2.18 kB
JavaScript
/**
* @author dmarcos / https://github.com/dmarcos
* @author mrdoob / http://mrdoob.com
*/
THREE.VRControls = function ( object, onError ) {
var scope = this;
var vrInput;
function gotVRDevices( devices ) {
for ( var i = 0; i < devices.length; i ++ ) {
if ( ( 'VRDisplay' in window && devices[ i ] instanceof VRDisplay ) ||
( 'PositionSensorVRDevice' in window && devices[ i ] instanceof PositionSensorVRDevice ) ) {
vrInput = devices[ i ];
break; // We keep the first we encounter
}
}
if ( !vrInput ) {
if ( onError ) onError( 'VR input not available.' );
}
}
if ( navigator.getVRDisplays ) {
navigator.getVRDisplays().then( gotVRDevices );
} else if ( navigator.getVRDevices ) {
// Deprecated API.
navigator.getVRDevices().then( gotVRDevices );
}
// the Rift SDK returns the position in meters
// this scale factor allows the user to define how meters
// are converted to scene units.
this.scale = 1;
this.update = function () {
if ( vrInput ) {
if ( vrInput.getPose ) {
var pose = vrInput.getPose();
if ( pose.orientation !== null ) {
object.quaternion.fromArray( pose.orientation );
}
if ( pose.position !== null ) {
object.position.fromArray( pose.position ).multiplyScalar( scope.scale );
}
} else {
// Deprecated API.
var state = vrInput.getState();
if ( state.orientation !== null ) {
object.quaternion.copy( state.orientation );
}
if ( state.position !== null ) {
object.position.copy( state.position ).multiplyScalar( scope.scale );
}
}
}
};
this.resetSensor = function () {
if ( vrInput ) {
if ( vrInput.resetPose !== undefined ) {
vrInput.resetPose();
} else if ( vrInput.resetSensor !== undefined ) {
// Deprecated API.
vrInput.resetSensor();
} else if ( vrInput.zeroSensor !== undefined ) {
// Really deprecated API.
vrInput.zeroSensor();
}
}
};
this.zeroSensor = function () {
console.warn( 'THREE.VRControls: .zeroSensor() is now .resetSensor().' );
this.resetSensor();
};
this.dispose = function () {
vrInput = null;
};
};