webxr_vr_video.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - 360 stereo video</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> vr - 360 stereo video<br />
  13. stereoscopic panoramic render by <a href="http://pedrofe.com/rendering-for-oculus-rift-with-arnold/" target="_blank" rel="noopener">pedrofe</a>. scene from <a href="http://www.meryproject.com/" target="_blank" rel="noopener">mery project</a>.
  14. </div>
  15. <script src="js/vr/HelioWebXRPolyfill.js"></script>
  16. <video id="video" loop muted crossOrigin="anonymous" playsinline style="display:none">
  17. <source src="textures/MaryOculus.webm">
  18. <source src="textures/MaryOculus.mp4">
  19. </video>
  20. <script type="module">
  21. import * as THREE from '../build/three.module.js';
  22. import { VRButton } from './jsm/webxr/VRButton.js';
  23. var camera, scene, renderer;
  24. init();
  25. animate();
  26. function init() {
  27. var container = document.getElementById( 'container' );
  28. container.addEventListener( 'click', function () {
  29. video.play();
  30. } );
  31. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 2000 );
  32. camera.layers.enable( 1 ); // render left view when no stereo available
  33. // video
  34. var video = document.getElementById( 'video' );
  35. video.play();
  36. var texture = new THREE.Texture( video );
  37. texture.generateMipmaps = false;
  38. texture.minFilter = THREE.NearestFilter;
  39. texture.magFilter = THREE.NearestFilter;
  40. texture.format = THREE.RGBFormat;
  41. setInterval( function () {
  42. if ( video.readyState >= video.HAVE_CURRENT_DATA ) {
  43. texture.needsUpdate = true;
  44. }
  45. }, 1000 / 24 );
  46. scene = new THREE.Scene();
  47. scene.background = new THREE.Color( 0x101010 );
  48. // left
  49. var geometry = new THREE.SphereBufferGeometry( 500, 60, 40 );
  50. // invert the geometry on the x-axis so that all of the faces point inward
  51. geometry.scale( - 1, 1, 1 );
  52. var uvs = geometry.attributes.uv.array;
  53. for ( var i = 0; i < uvs.length; i += 2 ) {
  54. uvs[ i ] *= 0.5;
  55. }
  56. var material = new THREE.MeshBasicMaterial( { map: texture } );
  57. var mesh = new THREE.Mesh( geometry, material );
  58. mesh.rotation.y = - Math.PI / 2;
  59. mesh.layers.set( 1 ); // display in left eye only
  60. scene.add( mesh );
  61. // right
  62. var geometry = new THREE.SphereBufferGeometry( 500, 60, 40 );
  63. geometry.scale( - 1, 1, 1 );
  64. var uvs = geometry.attributes.uv.array;
  65. for ( var i = 0; i < uvs.length; i += 2 ) {
  66. uvs[ i ] *= 0.5;
  67. uvs[ i ] += 0.5;
  68. }
  69. var material = new THREE.MeshBasicMaterial( { map: texture } );
  70. var mesh = new THREE.Mesh( geometry, material );
  71. mesh.rotation.y = - Math.PI / 2;
  72. mesh.layers.set( 2 ); // display in right eye only
  73. scene.add( mesh );
  74. //
  75. renderer = new THREE.WebGLRenderer();
  76. renderer.setPixelRatio( window.devicePixelRatio );
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. renderer.xr.enabled = true;
  79. container.appendChild( renderer.domElement );
  80. document.body.appendChild( VRButton.createButton( renderer, { referenceSpaceType: 'local' } ) );
  81. //
  82. window.addEventListener( 'resize', onWindowResize, false );
  83. }
  84. function onWindowResize() {
  85. camera.aspect = window.innerWidth / window.innerHeight;
  86. camera.updateProjectionMatrix();
  87. renderer.setSize( window.innerWidth, window.innerHeight );
  88. }
  89. function animate() {
  90. renderer.setAnimationLoop( render );
  91. }
  92. function render() {
  93. renderer.render( scene, camera );
  94. }
  95. </script>
  96. </body>
  97. </html>