webgl_panorama_cube.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - 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. <div id="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - cube panorama demo
  13. </div>
  14. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. let camera, controls;
  29. let renderer;
  30. let scene;
  31. init();
  32. animate();
  33. function init() {
  34. const container = document.getElementById( 'container' );
  35. renderer = new THREE.WebGLRenderer();
  36. renderer.setPixelRatio( window.devicePixelRatio );
  37. renderer.setSize( window.innerWidth, window.innerHeight );
  38. container.appendChild( renderer.domElement );
  39. scene = new THREE.Scene();
  40. camera = new THREE.PerspectiveCamera( 90, window.innerWidth / window.innerHeight, 0.1, 100 );
  41. camera.position.z = 0.01;
  42. controls = new OrbitControls( camera, renderer.domElement );
  43. controls.enableZoom = false;
  44. controls.enablePan = false;
  45. controls.enableDamping = true;
  46. controls.rotateSpeed = - 0.25;
  47. const textures = getTexturesFromAtlasFile( 'textures/cube/sun_temple_stripe.jpg', 6 );
  48. const materials = [];
  49. for ( let i = 0; i < 6; i ++ ) {
  50. materials.push( new THREE.MeshBasicMaterial( { map: textures[ i ] } ) );
  51. }
  52. const skyBox = new THREE.Mesh( new THREE.BoxGeometry( 1, 1, 1 ), materials );
  53. skyBox.geometry.scale( 1, 1, - 1 );
  54. scene.add( skyBox );
  55. window.addEventListener( 'resize', onWindowResize );
  56. }
  57. function getTexturesFromAtlasFile( atlasImgUrl, tilesNum ) {
  58. const textures = [];
  59. for ( let i = 0; i < tilesNum; i ++ ) {
  60. textures[ i ] = new THREE.Texture();
  61. }
  62. new THREE.ImageLoader()
  63. .load( atlasImgUrl, ( image ) => {
  64. let canvas, context;
  65. const tileWidth = image.height;
  66. for ( let i = 0; i < textures.length; i ++ ) {
  67. canvas = document.createElement( 'canvas' );
  68. context = canvas.getContext( '2d' );
  69. canvas.height = tileWidth;
  70. canvas.width = tileWidth;
  71. context.drawImage( image, tileWidth * i, 0, tileWidth, tileWidth, 0, 0, tileWidth, tileWidth );
  72. textures[ i ].colorSpace = THREE.SRGBColorSpace;
  73. textures[ i ].image = canvas;
  74. textures[ i ].needsUpdate = true;
  75. }
  76. } );
  77. return textures;
  78. }
  79. function onWindowResize() {
  80. camera.aspect = window.innerWidth / window.innerHeight;
  81. camera.updateProjectionMatrix();
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. }
  84. function animate() {
  85. requestAnimationFrame( animate );
  86. controls.update(); // required when damping is enabled
  87. renderer.render( scene, camera );
  88. }
  89. </script>
  90. </body>
  91. </html>