webgl_panorama_cube.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  17. var camera, controls;
  18. var renderer;
  19. var scene;
  20. init();
  21. animate();
  22. function init() {
  23. var container = document.getElementById( 'container' );
  24. renderer = new THREE.WebGLRenderer();
  25. renderer.setPixelRatio( window.devicePixelRatio );
  26. renderer.setSize( window.innerWidth, window.innerHeight );
  27. container.appendChild( renderer.domElement );
  28. scene = new THREE.Scene();
  29. camera = new THREE.PerspectiveCamera( 90, window.innerWidth / window.innerHeight, 0.1, 100 );
  30. camera.position.z = 0.01;
  31. controls = new OrbitControls( camera, renderer.domElement );
  32. controls.enableZoom = false;
  33. controls.enablePan = false;
  34. controls.enableDamping = true;
  35. controls.rotateSpeed = - 0.25;
  36. var textures = getTexturesFromAtlasFile( "textures/cube/sun_temple_stripe.jpg", 6 );
  37. var materials = [];
  38. for ( var i = 0; i < 6; i ++ ) {
  39. materials.push( new THREE.MeshBasicMaterial( { map: textures[ i ] } ) );
  40. }
  41. var skyBox = new THREE.Mesh( new THREE.BoxBufferGeometry( 1, 1, 1 ), materials );
  42. skyBox.geometry.scale( 1, 1, - 1 );
  43. scene.add( skyBox );
  44. window.addEventListener( 'resize', onWindowResize, false );
  45. }
  46. function getTexturesFromAtlasFile( atlasImgUrl, tilesNum ) {
  47. var textures = [];
  48. for ( var i = 0; i < tilesNum; i ++ ) {
  49. textures[ i ] = new THREE.Texture();
  50. }
  51. var imageObj = new Image();
  52. imageObj.onload = function () {
  53. var canvas, context;
  54. var tileWidth = imageObj.height;
  55. for ( var i = 0; i < textures.length; i ++ ) {
  56. canvas = document.createElement( 'canvas' );
  57. context = canvas.getContext( '2d' );
  58. canvas.height = tileWidth;
  59. canvas.width = tileWidth;
  60. context.drawImage( imageObj, tileWidth * i, 0, tileWidth, tileWidth, 0, 0, tileWidth, tileWidth );
  61. textures[ i ].image = canvas;
  62. textures[ i ].needsUpdate = true;
  63. }
  64. };
  65. imageObj.src = atlasImgUrl;
  66. return textures;
  67. }
  68. function onWindowResize() {
  69. camera.aspect = window.innerWidth / window.innerHeight;
  70. camera.updateProjectionMatrix();
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. }
  73. function animate() {
  74. requestAnimationFrame( animate );
  75. controls.update(); // required when damping is enabled
  76. renderer.render( scene, camera );
  77. }
  78. </script>
  79. </body>
  80. </html>