mirror of
https://github.com/wassname/DefinitelyTyped.git
synced 2026-08-20 12:00:53 +08:00
add webvr-api definition
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
/// <reference path="./webvr-api.d.ts" />
|
||||
|
||||
function fieldOfViewToProjectionMatrix(fov: WebVRApi.VRFieldOfView, zNear: number, zFar: number) {
|
||||
var upTan = Math.tan(fov.upDegrees * Math.PI/180.0);
|
||||
var downTan = Math.tan(fov.downDegrees * Math.PI/180.0);
|
||||
var leftTan = Math.tan(fov.leftDegrees * Math.PI/180.0);
|
||||
var rightTan = Math.tan(fov.rightDegrees * Math.PI/180.0);
|
||||
var xScale = 2.0 / (leftTan + rightTan);
|
||||
var yScale = 2.0 / (upTan + downTan);
|
||||
|
||||
var out = new Float32Array(16);
|
||||
out[0] = xScale;
|
||||
out[1] = 0.0;
|
||||
out[2] = 0.0;
|
||||
out[3] = 0.0;
|
||||
out[4] = 0.0;
|
||||
out[5] = yScale;
|
||||
out[6] = 0.0;
|
||||
out[7] = 0.0;
|
||||
out[8] = -((leftTan - rightTan) * xScale * 0.5);
|
||||
out[9] = ((upTan - downTan) * yScale * 0.5);
|
||||
out[10] = -(zNear + zFar) / (zFar - zNear);
|
||||
out[11] = -1.0;
|
||||
out[12] = 0.0;
|
||||
out[13] = 0.0;
|
||||
out[14] = -(2.0 * zFar * zNear) / (zFar - zNear);
|
||||
out[15] = 0.0;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
var hmd: HMDVRDevice;
|
||||
var leftEyeParams = hmd.getEyeParameters("left");
|
||||
var rightEyeParams = hmd.getEyeParameters("right");
|
||||
var leftEyeRect = leftEyeParams.renderRect;
|
||||
var rightEyeRect = rightEyeParams.renderRect;
|
||||
|
||||
var canvas: HTMLCanvasElement;
|
||||
canvas.width = rightEyeRect.x + rightEyeRect.width;
|
||||
canvas.height = Math.max(leftEyeRect.y + leftEyeRect.height, rightEyeRect.y + rightEyeRect.height);
|
||||
|
||||
var gHMD: WebVRApi.VRDevice;
|
||||
var gPositionSensor: WebVRApi.VRDevice;
|
||||
|
||||
navigator.getVRDevices().then(function(devices) {
|
||||
for (var i = 0; i < devices.length; ++i) {
|
||||
if (devices[i] instanceof HMDVRDevice) {
|
||||
gHMD = devices[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (gHMD) {
|
||||
for (var i = 0; i < devices.length; ++i) {
|
||||
if (devices[i] instanceof PositionSensorVRDevice &&
|
||||
devices[i].hardwareUnitId == gHMD.hardwareUnitId) {
|
||||
gPositionSensor = devices[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
Vendored
+175
@@ -0,0 +1,175 @@
|
||||
// Type definitions for WebVR API
|
||||
// Project: http://mozvr.github.io/webvr-spec/webvr.html
|
||||
// Definitions by: Toshiya Nakakura <https://github.com/nakakura>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
/// <reference path="../geometry-dom/geometry-dom.d.ts" />
|
||||
/// <reference path="../es6-promise/es6-promise.d.ts" />
|
||||
|
||||
declare type VREye = string;
|
||||
|
||||
declare module WebVRApi {
|
||||
export interface VRFieldOfViewReadOnly {
|
||||
upDegrees: number;
|
||||
rightDegrees: number;
|
||||
downDegrees: number;
|
||||
leftDegrees: number;
|
||||
}
|
||||
|
||||
export interface VRFieldOfViewInit {
|
||||
upDegrees: number;
|
||||
rightDegrees: number;
|
||||
downDegrees: number;
|
||||
leftDegrees: number;
|
||||
}
|
||||
|
||||
export interface VRFieldOfView extends VRFieldOfViewReadOnly {
|
||||
constructor(upDegrees:number, rightDegrees:number, downDegrees:number, leftDegrees:number): VRFieldOfView;
|
||||
upDegrees: number;
|
||||
rightDegrees: number;
|
||||
downDegrees: number;
|
||||
leftDegrees: number;
|
||||
}
|
||||
|
||||
export interface VRPositionState {
|
||||
/**
|
||||
* Monotonically increasing value that allows the author to determine if position state data been updated from the hardware.
|
||||
*/
|
||||
timeStamp: number;
|
||||
/**
|
||||
* True if the position attribute is valid. If false position MUST be null.
|
||||
*/
|
||||
hasPosition: boolean;
|
||||
/**
|
||||
* Position of the sensor at timeStamp as a 3D vector.
|
||||
*/
|
||||
position?: GeometryDom.DOMPoint;
|
||||
/**
|
||||
* Linear velocity of the sensor at timeStamp.
|
||||
*/
|
||||
linearVelocity?: GeometryDom.DOMPoint;
|
||||
/**
|
||||
* Linear acceleration of the sensor at timeStamp.
|
||||
*/
|
||||
linearAcceleration?: GeometryDom.DOMPoint;
|
||||
/**
|
||||
* True if the orientation attribute is valid. If false orientation MUST be null.
|
||||
*/
|
||||
hasOrientation: boolean;
|
||||
/**
|
||||
* Orientation of the sensor at timeStamp as a quaternion.
|
||||
*/
|
||||
orientation?: GeometryDom.DOMPoint;
|
||||
/**
|
||||
* Angular velocity of the sensor at timeStamp.
|
||||
*/
|
||||
angularVelocity?: GeometryDom.DOMPoint;
|
||||
/**
|
||||
* Angular acceleration of the sensor at timeStamp.
|
||||
*/
|
||||
angularAcceleration?: GeometryDom.DOMPoint;
|
||||
}
|
||||
|
||||
export interface VREyeParameters {
|
||||
/**
|
||||
* Describes the minimum supported field of view for the eye.
|
||||
*/
|
||||
minimumFieldOfView: VRFieldOfView;
|
||||
/**
|
||||
* Describes the maximum supported field of view for the eye.
|
||||
*/
|
||||
maximumFieldOfView: VRFieldOfView;
|
||||
/**
|
||||
* Describes the recommended field of view for the eye.
|
||||
*/
|
||||
recommendedFieldOfView: VRFieldOfView;
|
||||
/**
|
||||
* Offset from the center of the users head to the eye in meters.
|
||||
*/
|
||||
eyeTranslation: GeometryDom.DOMPoint;
|
||||
/**
|
||||
* The current field of view for the eye, as specified by setFieldOfView.
|
||||
*/
|
||||
currentFieldOfView: VRFieldOfView;
|
||||
/**
|
||||
* Describes the viewport of a canvas into which visuals for this eye should be rendered.
|
||||
*/
|
||||
renderRect: GeometryDom.DOMRect;
|
||||
}
|
||||
|
||||
export interface VRDevice {
|
||||
/**
|
||||
* An identifier for the distinct hardware unit that this VRDevice is a part of.
|
||||
*/
|
||||
hardwareUnitId: string;
|
||||
/**
|
||||
* An identifier for this distinct sensor/device on a physical hardware device.
|
||||
*/
|
||||
deviceId: string;
|
||||
/**
|
||||
* A user-readable name identifying the device.
|
||||
*/
|
||||
deviceName: string;
|
||||
}
|
||||
}
|
||||
|
||||
declare class HMDVRDevice implements WebVRApi.VRDevice {
|
||||
/**
|
||||
* An identifier for the distinct hardware unit that this VRDevice is a part of.
|
||||
*/
|
||||
hardwareUnitId: string;
|
||||
/**
|
||||
* An identifier for this distinct sensor/device on a physical hardware device.
|
||||
*/
|
||||
deviceId: string;
|
||||
/**
|
||||
* A user-readable name identifying the device.
|
||||
*/
|
||||
deviceName: string;
|
||||
|
||||
/**
|
||||
* Return the current VREyeParameters for the given eye.
|
||||
*/
|
||||
getEyeParameters(whichEye: VREye): WebVRApi.VREyeParameters;
|
||||
/**
|
||||
* Set the field of view for both eyes. If
|
||||
*/
|
||||
setFieldOfView(leftFOV?: WebVRApi.VRFieldOfViewInit, rightFOV?: WebVRApi.VRFieldOfViewInit, zNear?: number, zFar?: number): void;
|
||||
}
|
||||
|
||||
declare class PositionSensorVRDevice implements WebVRApi.VRDevice {
|
||||
/**
|
||||
* An identifier for the distinct hardware unit that this VRDevice is a part of.
|
||||
*/
|
||||
hardwareUnitId: string;
|
||||
/**
|
||||
* An identifier for this distinct sensor/device on a physical hardware device.
|
||||
*/
|
||||
deviceId: string;
|
||||
/**
|
||||
* A user-readable name identifying the device.
|
||||
*/
|
||||
deviceName: string;
|
||||
|
||||
/**
|
||||
* Return a VRPositionState dictionary containing the state of this position sensor state for the current frame (if within a requestAnimationFrame context) or for the previous frame.
|
||||
*/
|
||||
getState(): WebVRApi.VRPositionState;
|
||||
/**
|
||||
* Return the current instantaneous sensor state.
|
||||
*/
|
||||
getImmediateState(): WebVRApi.VRPositionState;
|
||||
|
||||
/**
|
||||
* Reset this sensor, treating its current position and orientation yaw as the "origin/zero" values.
|
||||
*/
|
||||
resetSensor(): void;
|
||||
}
|
||||
|
||||
interface Navigator {
|
||||
/**
|
||||
* Return a Promise which resolves to a list of available VRDevices.
|
||||
*/
|
||||
getVRDevices(): Promise<Array<WebVRApi.VRDevice>>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user