From 97f251cef19e557391588889493103c15fffde12 Mon Sep 17 00:00:00 2001 From: Toshiya Nakakura Date: Sun, 5 Jul 2015 13:16:47 +0900 Subject: [PATCH] add webvr-api definition --- webvr-api/webvr-api-test.ts | 62 +++++++++++++ webvr-api/webvr-api.d.ts | 175 ++++++++++++++++++++++++++++++++++++ 2 files changed, 237 insertions(+) create mode 100644 webvr-api/webvr-api-test.ts create mode 100644 webvr-api/webvr-api.d.ts diff --git a/webvr-api/webvr-api-test.ts b/webvr-api/webvr-api-test.ts new file mode 100644 index 0000000000..df2a72438a --- /dev/null +++ b/webvr-api/webvr-api-test.ts @@ -0,0 +1,62 @@ +/// + +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; + } + } + } +}); diff --git a/webvr-api/webvr-api.d.ts b/webvr-api/webvr-api.d.ts new file mode 100644 index 0000000000..16918cf303 --- /dev/null +++ b/webvr-api/webvr-api.d.ts @@ -0,0 +1,175 @@ +// Type definitions for WebVR API +// Project: http://mozvr.github.io/webvr-spec/webvr.html +// Definitions by: Toshiya Nakakura +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// +/// + +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>; +} +