Advertisement
Ensikology

WebVR

Dec 10th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @author mrdoob / http://mrdoob.com
  3.  * @author Mugen87 / https://github.com/Mugen87
  4.  *
  5.  * Based on @tojiro's vr-samples-utils.js
  6.  */
  7.  
  8. var WEBVR = {
  9.  
  10.     createButton: function ( renderer, options ) {
  11.  
  12.         if ( options && options.frameOfReferenceType ) {
  13.  
  14.             renderer.vr.setFrameOfReferenceType( options.frameOfReferenceType );
  15.  
  16.         }
  17.  
  18.         function showEnterVR( device ) {
  19.  
  20.             button.style.display = '';
  21.  
  22.             button.style.cursor = 'pointer';
  23.             button.style.left = 'calc(50% - 50px)';
  24.             button.style.width = '100px';
  25.  
  26.             button.textContent = 'ENTER VR';
  27.  
  28.             button.onmouseenter = function () { button.style.opacity = '1.0'; };
  29.             button.onmouseleave = function () { button.style.opacity = '0.5'; };
  30.  
  31.             button.onclick = function () {
  32.  
  33.                 device.isPresenting ? device.exitPresent() : device.requestPresent( [ { source: renderer.domElement } ] );
  34.  
  35.             };
  36.  
  37.             renderer.vr.setDevice( device );
  38.  
  39.         }
  40.  
  41.         function showEnterXR( device ) {
  42.  
  43.             var currentSession = null;
  44.  
  45.             function onSessionStarted( session ) {
  46.  
  47.                 session.addEventListener( 'end', onSessionEnded );
  48.  
  49.                 renderer.vr.setSession( session );
  50.                 button.textContent = 'EXIT VR';
  51.  
  52.                 currentSession = session;
  53.  
  54.             }
  55.  
  56.             function onSessionEnded( event ) {
  57.  
  58.                 currentSession.removeEventListener( 'end', onSessionEnded );
  59.  
  60.                 renderer.vr.setSession( null );
  61.                 button.textContent = 'ENTER VR';
  62.  
  63.                 currentSession = null;
  64.  
  65.             }
  66.  
  67.             //
  68.  
  69.             button.style.display = '';
  70.  
  71.             button.style.cursor = 'pointer';
  72.             button.style.left = 'calc(50% - 50px)';
  73.             button.style.width = '100px';
  74.  
  75.             button.textContent = 'ENTER VR';
  76.  
  77.             button.onmouseenter = function () { button.style.opacity = '1.0'; };
  78.             button.onmouseleave = function () { button.style.opacity = '0.5'; };
  79.  
  80.             button.onclick = function () {
  81.  
  82.                 if ( currentSession === null ) {
  83.  
  84.                     device.requestSession( { immersive: true, exclusive: true /* DEPRECATED */ } ).then( onSessionStarted );
  85.  
  86.                 } else {
  87.  
  88.                     currentSession.end();
  89.  
  90.                 }
  91.  
  92.             };
  93.  
  94.             renderer.vr.setDevice( device );
  95.  
  96.         }
  97.  
  98.         function showVRNotFound() {
  99.  
  100.             button.style.display = '';
  101.  
  102.             button.style.cursor = 'auto';
  103.             button.style.left = 'calc(50% - 75px)';
  104.             button.style.width = '150px';
  105.  
  106.             button.textContent = 'VR NOT FOUND';
  107.  
  108.             button.onmouseenter = null;
  109.             button.onmouseleave = null;
  110.  
  111.             button.onclick = null;
  112.  
  113.             renderer.vr.setDevice( null );
  114.  
  115.         }
  116.  
  117.         function stylizeElement( element ) {
  118.  
  119.             element.style.position = 'absolute';
  120.             element.style.bottom = '20px';
  121.             element.style.padding = '12px 6px';
  122.             element.style.border = '1px solid #fff';
  123.             element.style.borderRadius = '4px';
  124.             element.style.background = 'rgba(0,0,0,0.1)';
  125.             element.style.color = '#fff';
  126.             element.style.font = 'normal 13px sans-serif';
  127.             element.style.textAlign = 'center';
  128.             element.style.opacity = '0.5';
  129.             element.style.outline = 'none';
  130.             element.style.zIndex = '999';
  131.  
  132.         }
  133.  
  134.         if ( 'xr' in navigator ) {
  135.  
  136.             var button = document.createElement( 'button' );
  137.             button.style.display = 'none';
  138.  
  139.             stylizeElement( button );
  140.  
  141.             navigator.xr.requestDevice().then( function ( device ) {
  142.  
  143.                 device.supportsSession( { immersive: true, exclusive: true /* DEPRECATED */ } )
  144.                     .then( function () { showEnterXR( device ); } )
  145.                     .catch( showVRNotFound );
  146.  
  147.             } ).catch( showVRNotFound );
  148.  
  149.             return button;
  150.  
  151.         } else if ( 'getVRDisplays' in navigator ) {
  152.  
  153.             var button = document.createElement( 'button' );
  154.             button.style.display = 'none';
  155.  
  156.             stylizeElement( button );
  157.  
  158.             window.addEventListener( 'vrdisplayconnect', function ( event ) {
  159.  
  160.                 showEnterVR( event.display );
  161.  
  162.             }, false );
  163.  
  164.             window.addEventListener( 'vrdisplaydisconnect', function ( event ) {
  165.  
  166.                 showVRNotFound();
  167.  
  168.             }, false );
  169.  
  170.             window.addEventListener( 'vrdisplaypresentchange', function ( event ) {
  171.  
  172.                 button.textContent = event.display.isPresenting ? 'EXIT VR' : 'ENTER VR';
  173.  
  174.             }, false );
  175.  
  176.             window.addEventListener( 'vrdisplayactivate', function ( event ) {
  177.  
  178.                 event.display.requestPresent( [ { source: renderer.domElement } ] );
  179.  
  180.             }, false );
  181.  
  182.             navigator.getVRDisplays()
  183.                 .then( function ( displays ) {
  184.  
  185.                     if ( displays.length > 0 ) {
  186.  
  187.                         showEnterVR( displays[ 0 ] );
  188.  
  189.                     } else {
  190.  
  191.                         showVRNotFound();
  192.  
  193.                     }
  194.  
  195.                 } ).catch( showVRNotFound );
  196.  
  197.             return button;
  198.  
  199.         } else {
  200.  
  201.             var message = document.createElement( 'a' );
  202.             message.href = 'https://webvr.info';
  203.             message.innerHTML = 'WEBVR NOT SUPPORTED';
  204.  
  205.             message.style.left = 'calc(50% - 90px)';
  206.             message.style.width = '180px';
  207.             message.style.textDecoration = 'none';
  208.  
  209.             stylizeElement( message );
  210.  
  211.             return message;
  212.  
  213.         }
  214.  
  215.     },
  216.  
  217.     // DEPRECATED
  218.  
  219.     checkAvailability: function () {
  220.         console.warn( 'WEBVR.checkAvailability has been deprecated.' );
  221.         return new Promise( function () {} );
  222.     },
  223.  
  224.     getMessageContainer: function () {
  225.         console.warn( 'WEBVR.getMessageContainer has been deprecated.' );
  226.         return document.createElement( 'div' );
  227.     },
  228.  
  229.     getButton: function () {
  230.         console.warn( 'WEBVR.getButton has been deprecated.' );
  231.         return document.createElement( 'div' );
  232.     },
  233.  
  234.     getVRDisplay: function () {
  235.         console.warn( 'WEBVR.getVRDisplay has been deprecated.' );
  236.     }
  237.  
  238. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement