webvr_video.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webvr - video</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  7. <!-- Origin Trial Token, feature = WebVR (For Chrome M62+), origin = https://threejs.org, expires = 2018-04-03 -->
  8. <meta http-equiv="origin-trial" data-feature="WebVR (For Chrome M62+)" data-expires="2018-04-03" content="AqIrNtaoJSmDZatgEEW2ehuK9ITIiFk3gZ3/gC6Vp0gF85NDYl7IzVPj4Bh7O6SIcr3ICOm6kocYSaS+BPkazgYAAABQeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJWUjEuMU02MiIsImV4cGlyeSI6MTUyMjc2MzAwMn0=">
  9. <style>
  10. body {
  11. font-family: Monospace;
  12. background-color: #f0f0f0;
  13. margin: 0px;
  14. overflow: hidden;
  15. }
  16. #info {
  17. position: absolute;
  18. top: 10px;
  19. width: 100%;
  20. color: #fff;
  21. text-align: center;
  22. }
  23. a {
  24. color: #ff0
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="container"></div>
  30. <div id="info">
  31. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - vr video<br />
  32. 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>.
  33. </div>
  34. <script src="../build/three.js"></script>
  35. <script src="js/vr/WebVR.js"></script>
  36. <script>
  37. var camera, scene, renderer;
  38. var video, texture;
  39. init();
  40. animate();
  41. function init() {
  42. var container = document.getElementById( 'container' );
  43. container.addEventListener( 'click', function () {
  44. video.play();
  45. } );
  46. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 2000 );
  47. camera.layers.enable( 1 ); // render left view when no stereo available
  48. // video
  49. video = document.createElement( 'video' );
  50. video.crossOrigin = 'anonymous';
  51. video.loop = true;
  52. video.muted = true;
  53. video.src = 'textures/MaryOculus.webm';
  54. video.setAttribute( 'webkit-playsinline', 'webkit-playsinline' );
  55. video.play();
  56. texture = new THREE.Texture( video );
  57. texture.generateMipmaps = false;
  58. texture.minFilter = THREE.NearestFilter;
  59. texture.maxFilter = THREE.NearestFilter;
  60. texture.format = THREE.RGBFormat;
  61. setInterval( function () {
  62. if ( video.readyState >= video.HAVE_CURRENT_DATA ) {
  63. texture.needsUpdate = true;
  64. }
  65. }, 1000 / 24 );
  66. scene = new THREE.Scene();
  67. scene.background = new THREE.Color( 0x101010 );
  68. // left
  69. var geometry = new THREE.SphereGeometry( 500, 60, 40 );
  70. // invert the geometry on the x-axis so that all of the faces point inward
  71. geometry.scale( - 1, 1, 1 );
  72. var uvs = geometry.faceVertexUvs[ 0 ];
  73. for ( var i = 0; i < uvs.length; i ++ ) {
  74. for ( var j = 0; j < 3; j ++ ) {
  75. uvs[ i ][ j ].x *= 0.5;
  76. }
  77. }
  78. var material = new THREE.MeshBasicMaterial( { map: texture } );
  79. var mesh = new THREE.Mesh( geometry, material );
  80. mesh.rotation.y = - Math.PI / 2;
  81. mesh.layers.set( 1 ); // display in left eye only
  82. scene.add( mesh );
  83. // right
  84. var geometry = new THREE.SphereGeometry( 500, 60, 40 );
  85. geometry.scale( - 1, 1, 1 );
  86. var uvs = geometry.faceVertexUvs[ 0 ];
  87. for ( var i = 0; i < uvs.length; i ++ ) {
  88. for ( var j = 0; j < 3; j ++ ) {
  89. uvs[ i ][ j ].x *= 0.5;
  90. uvs[ i ][ j ].x += 0.5;
  91. }
  92. }
  93. var material = new THREE.MeshBasicMaterial( { map: texture } );
  94. var mesh = new THREE.Mesh( geometry, material );
  95. mesh.rotation.y = - Math.PI / 2;
  96. mesh.layers.set( 2 ); // display in right eye only
  97. scene.add( mesh );
  98. //
  99. renderer = new THREE.WebGLRenderer();
  100. renderer.setPixelRatio( window.devicePixelRatio );
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. renderer.vr.enabled = true;
  103. renderer.vr.userHeight = 0; // TOFIX
  104. container.appendChild( renderer.domElement );
  105. document.body.appendChild( WEBVR.createButton( renderer ) );
  106. //
  107. window.addEventListener( 'resize', onWindowResize, false );
  108. }
  109. function onWindowResize() {
  110. camera.aspect = window.innerWidth / window.innerHeight;
  111. camera.updateProjectionMatrix();
  112. renderer.setSize( window.innerWidth, window.innerHeight );
  113. }
  114. function animate() {
  115. renderer.animate( render );
  116. }
  117. function render() {
  118. renderer.render( scene, camera );
  119. }
  120. </script>
  121. </body>
  122. </html>