webgl_panorama_cube.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. <style>
  8. html, body {
  9. background-color: #000;
  10. margin: 0px;
  11. padding: 0px;
  12. overflow: hidden;
  13. }
  14. #info {
  15. position: absolute;
  16. top: 0px; width: 100%;
  17. color: #ffffff;
  18. padding: 5px;
  19. font-family:Monospace;
  20. font-size:13px;
  21. font-weight: bold;
  22. text-align:center;
  23. }
  24. a {
  25. color: #ffffff;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div id="container"></div>
  31. <div id="info">
  32. <a href="http://threejs.org" target="_blank">three.js webgl</a> - cube panorama demo.
  33. </div>
  34. <script src="../build/three.js"></script>
  35. <script src="js/controls/OrbitControls.js"></script>
  36. <script>
  37. var camera, controls;
  38. var renderer;
  39. var scene;
  40. init();
  41. animate();
  42. function init() {
  43. var container = document.getElementById( 'container' );
  44. renderer = new THREE.WebGLRenderer();
  45. renderer.setPixelRatio( window.devicePixelRatio );
  46. renderer.setSize( window.innerWidth, window.innerHeight );
  47. container.appendChild( renderer.domElement );
  48. scene = new THREE.Scene();
  49. camera = new THREE.PerspectiveCamera( 90, window.innerWidth / window.innerHeight, 0.1, 100 );
  50. camera.position.z = 0.01;
  51. controls = new THREE.OrbitControls( camera );
  52. controls.enableZoom = false;
  53. controls.enablePan = false;
  54. var textures = getTexturesFromAtlasFile( "textures/cube/sun_temple_stripe.jpg", 6 );
  55. var materials = [];
  56. for ( var i = 0; i < 6; i ++ ) {
  57. materials.push( new THREE.MeshBasicMaterial( { map: textures[ i ] } ) );
  58. }
  59. var skyBox = new THREE.Mesh( new THREE.CubeGeometry( 1, 1, 1 ), new THREE.MeshFaceMaterial( materials ) );
  60. skyBox.applyMatrix( new THREE.Matrix4().makeScale( 1, 1, - 1 ) );
  61. scene.add( skyBox );
  62. window.addEventListener( 'resize', onWindowResize, false );
  63. }
  64. function getTexturesFromAtlasFile( atlasImgUrl, tilesNum ) {
  65. var textures = [];
  66. for ( var i = 0; i < tilesNum; i ++ ) {
  67. textures[ i ] = new THREE.Texture();
  68. }
  69. var imageObj = new Image();
  70. imageObj.onload = function() {
  71. var canvas, context;
  72. var tileWidth = imageObj.height;
  73. for ( var i = 0; i < textures.length; i ++ ) {
  74. canvas = document.createElement( 'canvas' );
  75. context = canvas.getContext( '2d' );
  76. canvas.height = tileWidth;
  77. canvas.width = tileWidth;
  78. context.drawImage( imageObj, tileWidth * i, 0, tileWidth, tileWidth, 0, 0, tileWidth, tileWidth );
  79. textures[ i ].image = canvas
  80. textures[ i ].needsUpdate = true;
  81. }
  82. };
  83. imageObj.src = atlasImgUrl;
  84. return textures;
  85. }
  86. function onWindowResize() {
  87. camera.aspect = window.innerWidth / window.innerHeight;
  88. camera.updateProjectionMatrix();
  89. renderer.setSize( window.innerWidth, window.innerHeight );
  90. }
  91. function animate() {
  92. controls.update();
  93. renderer.render( scene, camera );
  94. requestAnimationFrame( animate );
  95. }
  96. </script>
  97. </body>
  98. </html>