webxr_vr_panorama.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - panorama</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. <script src="js/vr/HelioWebXRPolyfill.js"></script>
  11. <script type="module">
  12. import * as THREE from '../build/three.module.js';
  13. import { VRButton } from './jsm/webxr/VRButton.js';
  14. var camera;
  15. var renderer;
  16. var scene;
  17. init();
  18. animate();
  19. function init() {
  20. renderer = new THREE.WebGLRenderer();
  21. renderer.setPixelRatio( window.devicePixelRatio );
  22. renderer.setSize( window.innerWidth, window.innerHeight );
  23. renderer.xr.enabled = true;
  24. document.body.appendChild( renderer.domElement );
  25. document.body.appendChild( VRButton.createButton( renderer, { referenceSpaceType: 'local' } ) );
  26. //
  27. scene = new THREE.Scene();
  28. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  29. camera.layers.enable( 1 );
  30. var geometry = new THREE.BoxBufferGeometry( 100, 100, 100 );
  31. geometry.scale( 1, 1, - 1 );
  32. var textures = getTexturesFromAtlasFile( "textures/cube/sun_temple_stripe_stereo.jpg", 12 );
  33. var materials = [];
  34. for ( var i = 0; i < 6; i ++ ) {
  35. materials.push( new THREE.MeshBasicMaterial( { map: textures[ i ] } ) );
  36. }
  37. var skyBox = new THREE.Mesh( geometry, materials );
  38. skyBox.layers.set( 1 );
  39. scene.add( skyBox );
  40. var materialsR = [];
  41. for ( var i = 6; i < 12; i ++ ) {
  42. materialsR.push( new THREE.MeshBasicMaterial( { map: textures[ i ] } ) );
  43. }
  44. var skyBoxR = new THREE.Mesh( geometry, materialsR );
  45. skyBoxR.layers.set( 2 );
  46. scene.add( skyBoxR );
  47. window.addEventListener( 'resize', onWindowResize, false );
  48. }
  49. function getTexturesFromAtlasFile( atlasImgUrl, tilesNum ) {
  50. var textures = [];
  51. for ( var i = 0; i < tilesNum; i ++ ) {
  52. textures[ i ] = new THREE.Texture();
  53. }
  54. var loader = new THREE.ImageLoader();
  55. loader.load( atlasImgUrl, function ( imageObj ) {
  56. var canvas, context;
  57. var tileWidth = imageObj.height;
  58. for ( var i = 0; i < textures.length; i ++ ) {
  59. canvas = document.createElement( 'canvas' );
  60. context = canvas.getContext( '2d' );
  61. canvas.height = tileWidth;
  62. canvas.width = tileWidth;
  63. context.drawImage( imageObj, tileWidth * i, 0, tileWidth, tileWidth, 0, 0, tileWidth, tileWidth );
  64. textures[ i ].image = canvas;
  65. textures[ i ].needsUpdate = true;
  66. }
  67. } );
  68. return textures;
  69. }
  70. function onWindowResize() {
  71. camera.aspect = window.innerWidth / window.innerHeight;
  72. camera.updateProjectionMatrix();
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. }
  75. function animate() {
  76. renderer.setAnimationLoop( render );
  77. }
  78. function render() {
  79. renderer.render( scene, camera );
  80. }
  81. </script>
  82. </body>
  83. </html>