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

var WEBVR = {

M
Mr.doob 已提交
10
	createButton: function ( renderer ) {
M
Mugen87 已提交
11

M
Mr.doob 已提交
12
		function showEnterVR() {
M
Mugen87 已提交
13

M
Mr.doob 已提交
14 15 16
			button.style.cursor = 'pointer';
			button.style.left = 'calc(50% - 50px)';
			button.style.width = '100px';
M
Mugen87 已提交
17

M
Mr.doob 已提交
18
			button.textContent = 'ENTER VR';
M
Mugen87 已提交
19

M
Mr.doob 已提交
20
		}
M
Mugen87 已提交
21

M
Mr.doob 已提交
22
		function showVRNotFound() {
M
Mugen87 已提交
23

M
Mr.doob 已提交
24 25 26
			button.style.cursor = 'auto';
			button.style.left = 'calc(50% - 75px)';
			button.style.width = '150px';
M
Mugen87 已提交
27

M
Mr.doob 已提交
28
			button.textContent = 'VR NOT FOUND';
29

M
Mr.doob 已提交
30
		}
M
Mugen87 已提交
31

M
Mr.doob 已提交
32
		if ( 'getVRDisplays' in navigator ) {
M
Mugen87 已提交
33

M
Mr.doob 已提交
34 35 36 37 38 39 40 41 42 43 44 45
			var button = document.createElement( 'button' );
			button.style.position = 'absolute';
			button.style.bottom = '20px';
			button.style.border = '1px solid #fff';
			button.style.padding = '12px 6px';
			button.style.backgroundColor = 'transparent';
			button.style.color = '#fff';
			button.style.fontFamily = 'sans-serif';
			button.style.fontSize = '13px';
			button.style.fontStyle = 'normal';
			button.style.textAlign = 'center';
			button.style.zIndex = '999';
M
Mugen87 已提交
46

M
Mr.doob 已提交
47
			showVRNotFound();
M
Mugen87 已提交
48

M
Mr.doob 已提交
49
			window.addEventListener( 'vrdisplayconnect', function ( event ) {
M
Mr.doob 已提交
50

M
Mr.doob 已提交
51
				console.log( event.display.isConnected );
M
Mr.doob 已提交
52

M
Mr.doob 已提交
53
				var display = event.display;
M
Mr.doob 已提交
54

M
Mr.doob 已提交
55
				showEnterVR();
M
Mr.doob 已提交
56

M
Mr.doob 已提交
57
				button.onclick = function () {
M
Mr.doob 已提交
58

M
Mr.doob 已提交
59
					display.isPresenting ? display.exitPresent() : display.requestPresent( [ { source: renderer.domElement } ] );
M
Mr.doob 已提交
60

M
Mr.doob 已提交
61
				};
M
Mr.doob 已提交
62

M
Mr.doob 已提交
63
				renderer.vr.setDevice( display );
M
Mugen87 已提交
64

M
Mr.doob 已提交
65
			}, false );
M
Mugen87 已提交
66

M
Mr.doob 已提交
67
			window.addEventListener( 'vrdisplaydisconnect', function ( event ) {
68

M
Mr.doob 已提交
69 70
				console.log( event );
				console.log( event.display.isConnected );
71

M
Mr.doob 已提交
72
				showVRNotFound();
73

M
Mr.doob 已提交
74
				button.onclick = null;
M
Mr.doob 已提交
75

M
Mr.doob 已提交
76
				renderer.vr.setDevice( null );
M
Mr.doob 已提交
77

M
Mr.doob 已提交
78
			}, false );
M
Mr.doob 已提交
79

M
Mr.doob 已提交
80
			window.addEventListener( 'vrdisplaypresentchange', function ( event ) {
M
Mr.doob 已提交
81

M
Mr.doob 已提交
82
				var display = event.display;
83

84
				button.textContent = display.isPresenting ? 'EXIT VR' : 'ENTER VR';
85 86 87

			}, false );

M
Mr.doob 已提交
88 89
			return button;

90 91
		} else {

M
Mr.doob 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
			var error = document.createElement( 'div' );
			error.style.position = 'absolute';
			error.style.left = 'calc(50% - 150px)';
			error.style.bottom = '20px';
			error.style.width = '300px';
			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';
			error.style.textAlign = 'center';
			error.style.zIndex = '999';
			error.innerHTML = 'Your browser does not support WebVR. Check <a href="https://webvr.info">webvr.info</a> for assistance.';

			return error;
109 110

		}
M
Mr.doob 已提交
111

M
Mr.doob 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	},

	// DEPRECATED

	checkAvailability: function () {
		console.warn( 'WEBVR.checkAvailability has been deprecated.' );
		return new Promise( function () {} );
	},

	getMessageContainer: function () {
		console.warn( 'WEBVR.getMessageContainer has been deprecated.' );
		return document.createElement( 'div' );
	},

	getButton: function () {
		console.warn( 'WEBVR.getButton has been deprecated.' );
		return document.createElement( 'div' );
	},
M
Mr.doob 已提交
130

M
Mr.doob 已提交
131 132
	getVRDisplay: function () {
		console.warn( 'WEBVR.getVRDisplay has been deprecated.' );
M
Mr.doob 已提交
133 134 135
	}

};