webgl_panorama_cube.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. renderer.useLegacyLights = false;
  39. container.appendChild( renderer.domElement );
  40. scene = new THREE.Scene();
  41. camera = new THREE.PerspectiveCamera( 90, window.innerWidth / window.innerHeight, 0.1, 100 );
  42. camera.position.z = 0.01;
  43. controls = new OrbitControls( camera, renderer.domElement );
  44. controls.enableZoom = false;
  45. controls.enablePan = false;
  46. controls.enableDamping = true;
  47. controls.rotateSpeed = - 0.25;
  48. const textures = getTexturesFromAtlasFile( 'textures/cube/sun_temple_stripe.jpg', 6 );
  49. const materials = [];
  50. for ( let i = 0; i < 6; i ++ ) {
  51. materials.push( new THREE.MeshBasicMaterial( { map: textures[ i ] } ) );
  52. }
  53. const skyBox = new THREE.Mesh( new THREE.BoxGeometry( 1, 1, 1 ), materials );
  54. skyBox.geometry.scale( 1, 1, - 1 );
  55. scene.add( skyBox );
  56. window.addEventListener( 'resize', onWindowResize );
  57. }
  58. function getTexturesFromAtlasFile( atlasImgUrl, tilesNum ) {
  59. const textures = [];
  60. for ( let i = 0; i < tilesNum; i ++ ) {
  61. textures[ i ] = new THREE.Texture();
  62. }
  63. new THREE.ImageLoader()
  64. .load( atlasImgUrl, ( image ) => {
  65. let canvas, context;
  66. const tileWidth = image.height;
  67. for ( let i = 0; i < textures.length; i ++ ) {
  68. canvas = document.createElement( 'canvas' );
  69. context = canvas.getContext( '2d' );
  70. canvas.height = tileWidth;
  71. canvas.width = tileWidth;
  72. context.drawImage( image, tileWidth * i, 0, tileWidth, tileWidth, 0, 0, tileWidth, tileWidth );
  73. textures[ i ].colorSpace = THREE.SRGBColorSpace;
  74. textures[ i ].image = canvas;
  75. textures[ i ].needsUpdate = true;
  76. }
  77. } );
  78. return textures;
  79. }
  80. function onWindowResize() {
  81. camera.aspect = window.innerWidth / window.innerHeight;
  82. camera.updateProjectionMatrix();
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. }
  85. function animate() {
  86. requestAnimationFrame( animate );
  87. controls.update(); // required when damping is enabled
  88. renderer.render( scene, camera );
  89. }
  90. </script>
  91. </body>
  92. </html>