WebVR.js 5.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
 * Based on @tojiro's vr-samples-utils.js
 */

8
THREE.WEBVR = {
M
Mr.doob 已提交
9

10
	createButton: function ( renderer, options ) {
M
Mugen87 已提交
11

12 13
		console.warn( 'WEBVR.js has been deprecated. Use VRButton.js instead.' );

14
		if ( options && options.referenceSpaceType ) {
15

16
			renderer.vr.setReferenceSpaceType( options.referenceSpaceType );
17 18

		}
M
Mr.doob 已提交
19

M
Mr.doob 已提交
20
		function showEnterVR( device ) {
M
Mugen87 已提交
21

M
Mr.doob 已提交
22 23
			button.style.display = '';

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

M
Mr.doob 已提交
28
			button.textContent = 'ENTER VR';
M
Mugen87 已提交
29

30 31 32 33 34 35 36 37 38 39 40
			button.onmouseenter = function () {

				button.style.opacity = '1.0';

			};

			button.onmouseleave = function () {

				button.style.opacity = '0.5';

			};
M
Mr.doob 已提交
41

42 43
			button.onclick = function () {

M
Mr.doob 已提交
44
				device.isPresenting ? device.exitPresent() : device.requestPresent( [ { source: renderer.domElement } ] );
M
Mr.doob 已提交
45

M
Mr.doob 已提交
46 47
			};

48
			renderer.vr.setDevice( device );
M
Mr.doob 已提交
49 50 51

		}

52
		function showEnterXR( /*device*/ ) {
M
Mr.doob 已提交
53 54 55 56 57 58 59

			var currentSession = null;

			function onSessionStarted( session ) {

				session.addEventListener( 'end', onSessionEnded );

60
				renderer.vr.setSession( session );
61
				button.textContent = 'EXIT XR';
M
Mr.doob 已提交
62 63 64 65 66

				currentSession = session;

			}

67
			function onSessionEnded( /*event*/ ) {
M
Mr.doob 已提交
68

M
Mr.doob 已提交
69 70
				currentSession.removeEventListener( 'end', onSessionEnded );

M
Mr.doob 已提交
71
				renderer.vr.setSession( null );
72
				button.textContent = 'ENTER XR';
M
Mr.doob 已提交
73

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

M
Mr.doob 已提交
76 77 78 79 80 81 82 83 84 85
			}

			//

			button.style.display = '';

			button.style.cursor = 'pointer';
			button.style.left = 'calc(50% - 50px)';
			button.style.width = '100px';

86
			button.textContent = 'ENTER XR';
M
Mr.doob 已提交
87

88 89 90 91 92 93 94 95 96 97 98
			button.onmouseenter = function () {

				button.style.opacity = '1.0';

			};

			button.onmouseleave = function () {

				button.style.opacity = '0.5';

			};
M
Mr.doob 已提交
99 100

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

M
Mr.doob 已提交
102
				if ( currentSession === null ) {
M
Mr.doob 已提交
103

104 105 106 107 108 109 110 111 112
					// WebXR's requestReferenceSpace only works if the corresponding feature
					// was requested at session creation time. For simplicity, just ask for
					// the interesting ones as optional features, but be aware that the
					// requestReferenceSpace call will fail if it turns out to be unavailable.
					// ('local' is always available for immersive sessions and doesn't need to
					// be requested separately.)

					var sessionInit = { optionalFeatures: [ 'local-floor', 'bounded-floor' ] };
					navigator.xr.requestSession( 'immersive-vr', sessionInit ).then( onSessionStarted );
M
Mr.doob 已提交
113 114 115

				} else {

M
Mr.doob 已提交
116
					currentSession.end();
M
Mr.doob 已提交
117 118

				}
119 120 121

			};

M
Mr.doob 已提交
122
		}
M
Mugen87 已提交
123

124
		function disableButton() {
M
Mugen87 已提交
125

M
Mr.doob 已提交
126 127
			button.style.display = '';

M
Mr.doob 已提交
128 129 130
			button.style.cursor = 'auto';
			button.style.left = 'calc(50% - 75px)';
			button.style.width = '150px';
M
Mugen87 已提交
131

M
Mr.doob 已提交
132 133 134
			button.onmouseenter = null;
			button.onmouseleave = null;

135 136
			button.onclick = null;

137 138 139 140 141 142 143 144
		}

		function showVRNotFound() {

			disableButton();

			button.textContent = 'VR NOT FOUND';

145 146
			renderer.vr.setDevice( null );

M
Mr.doob 已提交
147 148
		}

149 150 151 152 153 154 155 156
		function showXRNotFound() {

			disableButton();

			button.textContent = 'XR NOT FOUND';

		}

M
Mr.doob 已提交
157 158 159 160 161 162 163
		function stylizeElement( element ) {

			element.style.position = 'absolute';
			element.style.bottom = '20px';
			element.style.padding = '12px 6px';
			element.style.border = '1px solid #fff';
			element.style.borderRadius = '4px';
M
Mr.doob 已提交
164
			element.style.background = 'rgba(0,0,0,0.1)';
M
Mr.doob 已提交
165 166 167 168
			element.style.color = '#fff';
			element.style.font = 'normal 13px sans-serif';
			element.style.textAlign = 'center';
			element.style.opacity = '0.5';
M
Mr.doob 已提交
169
			element.style.outline = 'none';
M
Mr.doob 已提交
170 171
			element.style.zIndex = '999';

M
Mr.doob 已提交
172
		}
M
Mugen87 已提交
173

M
Mr.doob 已提交
174
		if ( 'xr' in navigator ) {
M
Mr.doob 已提交
175 176 177 178 179 180

			var button = document.createElement( 'button' );
			button.style.display = 'none';

			stylizeElement( button );

T
Takahiro 已提交
181
			navigator.xr.isSessionSupported( 'immersive-vr' ).then( function ( supported ) {
T
Takahiro 已提交
182

183
				if ( supported ) {
T
Takahiro 已提交
184

185
					showEnterXR();
T
Takahiro 已提交
186

187
				} else {
T
Takahiro 已提交
188

189
					showXRNotFound();
T
Takahiro 已提交
190

191
				}
T
Takahiro 已提交
192

193
			} );
M
Mr.doob 已提交
194 195 196 197

			return button;

		} else if ( 'getVRDisplays' in navigator ) {
M
Mugen87 已提交
198

M
Mr.doob 已提交
199
			var button = document.createElement( 'button' );
M
Mr.doob 已提交
200
			button.style.display = 'none';
M
Mr.doob 已提交
201 202

			stylizeElement( button );
M
Mugen87 已提交
203

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

206
				showEnterVR( event.display );
M
Mugen87 已提交
207

M
Mr.doob 已提交
208
			}, false );
M
Mugen87 已提交
209

210
			window.addEventListener( 'vrdisplaydisconnect', function ( /*event*/ ) {
211

M
Mr.doob 已提交
212
				showVRNotFound();
213

M
Mr.doob 已提交
214
			}, false );
M
Mr.doob 已提交
215

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

218
				button.textContent = event.display.isPresenting ? 'EXIT VR' : 'ENTER VR';
219 220 221

			}, false );

222 223 224 225 226 227
			window.addEventListener( 'vrdisplayactivate', function ( event ) {

				event.display.requestPresent( [ { source: renderer.domElement } ] );

			}, false );

228 229 230
			navigator.getVRDisplays()
				.then( function ( displays ) {

M
Mr.doob 已提交
231 232 233 234 235 236 237 238 239
					if ( displays.length > 0 ) {

						showEnterVR( displays[ 0 ] );

					} else {

						showVRNotFound();

					}
240

241
				} ).catch( showVRNotFound );
242

M
Mr.doob 已提交
243 244
			return button;

245 246
		} else {

M
Mr.doob 已提交
247
			var message = document.createElement( 'a' );
248 249
			message.href = 'https://webvr.info';
			message.innerHTML = 'WEBVR NOT SUPPORTED';
250

M
Mr.doob 已提交
251 252
			message.style.left = 'calc(50% - 90px)';
			message.style.width = '180px';
M
Mr.doob 已提交
253 254 255
			message.style.textDecoration = 'none';

			stylizeElement( message );
M
Mr.doob 已提交
256 257

			return message;
258 259

		}
M
Mr.doob 已提交
260

M
Mr.doob 已提交
261 262 263
	}

};