Browse Source

WebXR code clean up.

Mr.doob 5 years ago
parent
commit
6854ad946d

+ 5 - 13
examples/jsm/webxr/ARButton.js

@@ -7,7 +7,7 @@ var ARButton = {
 
 	createButton: function ( renderer ) {
 
-		function showEnterXR( /*device*/ ) {
+		function showStartAR( /*device*/ ) {
 
 			var currentSession = null;
 
@@ -93,11 +93,11 @@ var ARButton = {
 
 		}
 
-		function showXRNotFound() {
+		function showWebXRNotFound() {
 
 			disableButton();
 
-			button.textContent = 'XR NOT FOUND';
+			button.textContent = 'AR NOT SUPPORTED';
 
 		}
 
@@ -127,15 +127,7 @@ var ARButton = {
 
 			navigator.xr.isSessionSupported( 'immersive-ar' ).then( function ( supported ) {
 
-				if ( supported ) {
-
-					showEnterXR();
-
-				} else {
-
-					showXRNotFound();
-
-				}
+				supported ? showStartAR() : showWebXRNotFound();
 
 			} );
 
@@ -144,7 +136,7 @@ var ARButton = {
 		} else {
 
 			var message = document.createElement( 'a' );
-			message.href = 'https://immersive-web.github.io/webxr/';
+			message.href = 'https://immersiveweb.dev/';
 
 			if ( window.isSecureContext === false ) {
 

+ 15 - 14
examples/jsm/webxr/VRButton.js

@@ -13,7 +13,7 @@ var VRButton = {
 
 		}
 
-		function showEnterXR( /*device*/ ) {
+		function showEnterVR( /*device*/ ) {
 
 			var currentSession = null;
 
@@ -100,11 +100,11 @@ var VRButton = {
 
 		}
 
-		function showXRNotFound() {
+		function showWebXRNotFound() {
 
 			disableButton();
 
-			button.textContent = 'VR NOT FOUND';
+			button.textContent = 'VR NOT SUPPORTED';
 
 		}
 
@@ -134,15 +134,7 @@ var VRButton = {
 
 			navigator.xr.isSessionSupported( 'immersive-vr' ).then( function ( supported ) {
 
-				if ( supported ) {
-
-					showEnterXR();
-
-				} else {
-
-					showXRNotFound();
-
-				}
+				supported ? showEnterVR() : showWebXRNotFound();
 
 			} );
 
@@ -151,8 +143,17 @@ var VRButton = {
 		} else {
 
 			var message = document.createElement( 'a' );
-			message.href = 'https://immersive-web.github.io/webxr/';
-			message.innerHTML = 'WEBXR NOT SUPPORTED';
+			message.href = 'https://immersiveweb.dev/';
+
+			if ( window.isSecureContext === false ) {
+
+				message.innerHTML = 'WEBXR NEEDS HTTPS'; // TODO Improve message
+
+			} else {
+
+				message.innerHTML = 'WEBXR NOT AVAILABLE';
+
+			}
 
 			message.style.left = 'calc(50% - 90px)';
 			message.style.width = '180px';

+ 9 - 9
src/renderers/WebGLRenderer.js

@@ -1,3 +1,11 @@
+/**
+ * @author supereggbert / http://www.paulbrunt.co.uk/
+ * @author mrdoob / http://mrdoob.com/
+ * @author alteredq / http://alteredqualia.com/
+ * @author szimek / https://github.com/szimek/
+ * @author tschw
+ */
+
 import {
 	RGBAFormat,
 	HalfFloatType,
@@ -38,15 +46,7 @@ import { WebGLTextures } from './webgl/WebGLTextures.js';
 import { WebGLUniforms } from './webgl/WebGLUniforms.js';
 import { WebGLUtils } from './webgl/WebGLUtils.js';
 import { WebGLMultiview } from './webgl/WebGLMultiview.js';
-import { WebXRManager } from './webvr/WebXRManager.js';
-
-/**
- * @author supereggbert / http://www.paulbrunt.co.uk/
- * @author mrdoob / http://mrdoob.com/
- * @author alteredq / http://alteredqualia.com/
- * @author szimek / https://github.com/szimek/
- * @author tschw
- */
+import { WebXRManager } from './webxr/WebXRManager.js';
 
 function WebGLRenderer( parameters ) {
 

+ 0 - 3
src/renderers/webvr/WebVRUtils.d.ts

@@ -1,3 +0,0 @@
-import { Camera } from '../../cameras/Camera';
-
-export function setProjectionFromUnion( camera: Camera, cameraL: Camera, cameraR: Camera ): void;

+ 0 - 66
src/renderers/webvr/WebVRUtils.js

@@ -1,66 +0,0 @@
-/**
- * @author jsantell / https://www.jsantell.com/
- * @author mrdoob / http://mrdoob.com/
- */
-
-import { Vector3 } from '../../math/Vector3.js';
-
-var cameraLPos = new Vector3();
-var cameraRPos = new Vector3();
-
-/**
- * Assumes 2 cameras that are parallel and share an X-axis, and that
- * the cameras' projection and world matrices have already been set.
- * And that near and far planes are identical for both cameras.
- * Visualization of this technique: https://computergraphics.stackexchange.com/a/4765
- */
-function setProjectionFromUnion( camera, cameraL, cameraR ) {
-
-	cameraLPos.setFromMatrixPosition( cameraL.matrixWorld );
-	cameraRPos.setFromMatrixPosition( cameraR.matrixWorld );
-
-	var ipd = cameraLPos.distanceTo( cameraRPos );
-
-	var projL = cameraL.projectionMatrix.elements;
-	var projR = cameraR.projectionMatrix.elements;
-
-	// VR systems will have identical far and near planes, and
-	// most likely identical top and bottom frustum extents.
-	// Use the left camera for these values.
-	var near = projL[ 14 ] / ( projL[ 10 ] - 1 );
-	var far = projL[ 14 ] / ( projL[ 10 ] + 1 );
-	var topFov = ( projL[ 9 ] + 1 ) / projL[ 5 ];
-	var bottomFov = ( projL[ 9 ] - 1 ) / projL[ 5 ];
-
-	var leftFov = ( projL[ 8 ] - 1 ) / projL[ 0 ];
-	var rightFov = ( projR[ 8 ] + 1 ) / projR[ 0 ];
-	var left = near * leftFov;
-	var right = near * rightFov;
-
-	// Calculate the new camera's position offset from the
-	// left camera. xOffset should be roughly half `ipd`.
-	var zOffset = ipd / ( - leftFov + rightFov );
-	var xOffset = zOffset * - leftFov;
-
-	// TODO: Better way to apply this offset?
-	cameraL.matrixWorld.decompose( camera.position, camera.quaternion, camera.scale );
-	camera.translateX( xOffset );
-	camera.translateZ( zOffset );
-	camera.matrixWorld.compose( camera.position, camera.quaternion, camera.scale );
-	camera.matrixWorldInverse.getInverse( camera.matrixWorld );
-
-	// Find the union of the frustum values of the cameras and scale
-	// the values so that the near plane's position does not change in world space,
-	// although must now be relative to the new union camera.
-	var near2 = near + zOffset;
-	var far2 = far + zOffset;
-	var left2 = left - xOffset;
-	var right2 = right + ( ipd - xOffset );
-	var top2 = topFov * far / far2 * near2;
-	var bottom2 = bottomFov * far / far2 * near2;
-
-	camera.projectionMatrix.makePerspective( left2, right2, top2, bottom2, near2, far2 );
-
-}
-
-export { setProjectionFromUnion };

+ 0 - 0
src/renderers/webvr/WebXRManager.d.ts → src/renderers/webxr/WebXRManager.d.ts


+ 63 - 3
src/renderers/webvr/WebXRManager.js → src/renderers/webxr/WebXRManager.js

@@ -2,14 +2,14 @@
  * @author mrdoob / http://mrdoob.com/
  */
 
+import { ArrayCamera } from '../../cameras/ArrayCamera.js';
 import { EventDispatcher } from '../../core/EventDispatcher.js';
 import { Group } from '../../objects/Group.js';
 import { Matrix4 } from '../../math/Matrix4.js';
-import { Vector4 } from '../../math/Vector4.js';
-import { ArrayCamera } from '../../cameras/ArrayCamera.js';
 import { PerspectiveCamera } from '../../cameras/PerspectiveCamera.js';
+import { Vector3 } from '../../math/Vector3.js';
+import { Vector4 } from '../../math/Vector4.js';
 import { WebGLAnimation } from '../webgl/WebGLAnimation.js';
-import { setProjectionFromUnion } from './WebVRUtils.js';
 
 function WebXRManager( renderer, gl ) {
 
@@ -181,6 +181,66 @@ function WebXRManager( renderer, gl ) {
 
 	//
 
+	var cameraLPos = new Vector3();
+	var cameraRPos = new Vector3();
+
+	/**
+	 * @author jsantell / https://www.jsantell.com/
+	 *
+	 * Assumes 2 cameras that are parallel and share an X-axis, and that
+	 * the cameras' projection and world matrices have already been set.
+	 * And that near and far planes are identical for both cameras.
+	 * Visualization of this technique: https://computergraphics.stackexchange.com/a/4765
+	 */
+	function setProjectionFromUnion( camera, cameraL, cameraR ) {
+
+		cameraLPos.setFromMatrixPosition( cameraL.matrixWorld );
+		cameraRPos.setFromMatrixPosition( cameraR.matrixWorld );
+
+		var ipd = cameraLPos.distanceTo( cameraRPos );
+
+		var projL = cameraL.projectionMatrix.elements;
+		var projR = cameraR.projectionMatrix.elements;
+
+		// VR systems will have identical far and near planes, and
+		// most likely identical top and bottom frustum extents.
+		// Use the left camera for these values.
+		var near = projL[ 14 ] / ( projL[ 10 ] - 1 );
+		var far = projL[ 14 ] / ( projL[ 10 ] + 1 );
+		var topFov = ( projL[ 9 ] + 1 ) / projL[ 5 ];
+		var bottomFov = ( projL[ 9 ] - 1 ) / projL[ 5 ];
+
+		var leftFov = ( projL[ 8 ] - 1 ) / projL[ 0 ];
+		var rightFov = ( projR[ 8 ] + 1 ) / projR[ 0 ];
+		var left = near * leftFov;
+		var right = near * rightFov;
+
+		// Calculate the new camera's position offset from the
+		// left camera. xOffset should be roughly half `ipd`.
+		var zOffset = ipd / ( - leftFov + rightFov );
+		var xOffset = zOffset * - leftFov;
+
+		// TODO: Better way to apply this offset?
+		cameraL.matrixWorld.decompose( camera.position, camera.quaternion, camera.scale );
+		camera.translateX( xOffset );
+		camera.translateZ( zOffset );
+		camera.matrixWorld.compose( camera.position, camera.quaternion, camera.scale );
+		camera.matrixWorldInverse.getInverse( camera.matrixWorld );
+
+		// Find the union of the frustum values of the cameras and scale
+		// the values so that the near plane's position does not change in world space,
+		// although must now be relative to the new union camera.
+		var near2 = near + zOffset;
+		var far2 = far + zOffset;
+		var left2 = left - xOffset;
+		var right2 = right + ( ipd - xOffset );
+		var top2 = topFov * far / far2 * near2;
+		var bottom2 = bottomFov * far / far2 * near2;
+
+		camera.projectionMatrix.makePerspective( left2, right2, top2, bottom2, near2, far2 );
+
+	}
+
 	function updateCamera( camera, parent ) {
 
 		if ( parent === null ) {