WebVR.js 2.5 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4 5 6 7
/**
 * @author mrdoob / http://mrdoob.com
 * Based on @tojiro's vr-samples-utils.js
 */

var WEBVR = {

8
	isLatestAvailable: function () {
M
Mr.doob 已提交
9

M
Mr.doob 已提交
10
		return navigator.getVRDisplays !== undefined;
M
Mr.doob 已提交
11 12 13

	},

14 15 16 17 18 19
	isAvailable: function () {

		return navigator.getVRDisplays !== undefined || navigator.getVRDevices !== undefined;

	},

M
Mr.doob 已提交
20
	getMessage: function () {
M
Mr.doob 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

		var message;

		if ( navigator.getVRDisplays ) {

			navigator.getVRDisplays().then( function ( displays ) {

				if ( displays.length === 0 ) message = 'WebVR supported, but no VRDisplays found.';

			} );

		} else if ( navigator.getVRDevices ) {

			message = 'Your browser supports WebVR but not the latest version. See <a href="http://webvr.info">webvr.info</a> for more info.';

		} else {

			message = 'Your browser does not support WebVR. See <a href="http://webvr.info">webvr.info</a> for assistance.';

		}

		if ( message !== undefined ) {

			var container = document.createElement( 'div' );
			container.style.position = 'absolute';
			container.style.left = '0';
			container.style.top = '0';
			container.style.right = '0';
			container.style.zIndex = '999';
			container.align = 'center';

			var error = document.createElement( 'div' );
			error.style.fontFamily = 'sans-serif';
			error.style.fontSize = '16px';
			error.style.fontStyle = 'normal';
			error.style.lineHeight = '26px';
			error.style.backgroundColor = '#fff';
			error.style.color = '#000';
			error.style.padding = '10px 20px';
60
			error.style.margin = '50px';
M
Mr.doob 已提交
61 62 63 64
			error.style.display = 'inline-block';
			error.innerHTML = message;
			container.appendChild( error );

M
Mr.doob 已提交
65 66
			return container;

M
Mr.doob 已提交
67 68
		}

M
Mr.doob 已提交
69 70 71 72 73 74
	},

	getButton: function ( effect ) {

		var button = document.createElement( 'button' );
		button.style.position = 'absolute';
M
Mr.doob 已提交
75
		button.style.left = 'calc(50% - 50px)';
M
Mr.doob 已提交
76
		button.style.bottom = '20px';
M
Mr.doob 已提交
77
		button.style.width = '100px';
M
Mr.doob 已提交
78 79 80 81 82 83 84 85
		button.style.border = '0';
		button.style.padding = '8px';
		button.style.cursor = 'pointer';
		button.style.backgroundColor = '#000';
		button.style.color = '#fff';
		button.style.fontFamily = 'sans-serif';
		button.style.fontSize = '13px';
		button.style.fontStyle = 'normal';
M
Mr.doob 已提交
86
		button.style.textAlign = 'center';
M
Mr.doob 已提交
87 88 89 90
		button.style.zIndex = '999';
		button.textContent = 'ENTER VR';
		button.onclick = function() {

M
Mr.doob 已提交
91
			effect.isPresenting ? effect.exitPresent() : effect.requestPresent();
M
Mr.doob 已提交
92 93 94

		};

M
Mr.doob 已提交
95 96 97 98 99 100
		window.addEventListener( 'vrdisplaypresentchange', function ( event ) {

			button.textContent = effect.isPresenting ? 'EXIT VR' : 'ENTER VR';

		}, false );

M
Mr.doob 已提交
101 102
		return button;

M
Mr.doob 已提交
103 104 105
	}

};