textured-cube-wait-for-all-textures.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Textured Cube - 6 Textures</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. #loading {
  19. position: fixed;
  20. top: 0;
  21. left: 0;
  22. width: 100%;
  23. height: 100%;
  24. display: flex;
  25. justify-content: center;
  26. align-items: center;
  27. }
  28. #loading .progress {
  29. margin: 1.5em;
  30. border: 1px solid white;
  31. width: 50vw;
  32. }
  33. #loading .progressbar {
  34. margin: 2px;
  35. background: white;
  36. height: 1em;
  37. transform-origin: top left;
  38. transform: scaleX(0);
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <canvas id="c"></canvas>
  44. <div id="loading">
  45. <div class="progress"><div class="progressbar"></div></div>
  46. </div>
  47. </body>
  48. <script type="importmap">
  49. {
  50. "imports": {
  51. "three": "../../build/three.module.js"
  52. }
  53. }
  54. </script>
  55. <script type="module">
  56. import * as THREE from 'three';
  57. function main() {
  58. const canvas = document.querySelector( '#c' );
  59. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  60. const fov = 75;
  61. const aspect = 2; // the canvas default
  62. const near = 0.1;
  63. const far = 5;
  64. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  65. camera.position.z = 2;
  66. const scene = new THREE.Scene();
  67. const boxWidth = 1;
  68. const boxHeight = 1;
  69. const boxDepth = 1;
  70. const geometry = new THREE.BoxGeometry( boxWidth, boxHeight, boxDepth );
  71. const cubes = []; // just an array we can use to rotate the cubes
  72. const loadManager = new THREE.LoadingManager();
  73. const loader = new THREE.TextureLoader( loadManager );
  74. const materials = [
  75. new THREE.MeshBasicMaterial( { map: loadColorTexture( 'resources/images/flower-1.jpg' ) } ),
  76. new THREE.MeshBasicMaterial( { map: loadColorTexture( 'resources/images/flower-2.jpg' ) } ),
  77. new THREE.MeshBasicMaterial( { map: loadColorTexture( 'resources/images/flower-3.jpg' ) } ),
  78. new THREE.MeshBasicMaterial( { map: loadColorTexture( 'resources/images/flower-4.jpg' ) } ),
  79. new THREE.MeshBasicMaterial( { map: loadColorTexture( 'resources/images/flower-5.jpg' ) } ),
  80. new THREE.MeshBasicMaterial( { map: loadColorTexture( 'resources/images/flower-6.jpg' ) } ),
  81. ];
  82. const loadingElem = document.querySelector( '#loading' );
  83. const progressBarElem = loadingElem.querySelector( '.progressbar' );
  84. loadManager.onLoad = () => {
  85. loadingElem.style.display = 'none';
  86. const cube = new THREE.Mesh( geometry, materials );
  87. scene.add( cube );
  88. cubes.push( cube ); // add to our list of cubes to rotate
  89. };
  90. loadManager.onProgress = ( urlOfLastItemLoaded, itemsLoaded, itemsTotal ) => {
  91. const progress = itemsLoaded / itemsTotal;
  92. progressBarElem.style.transform = `scaleX(${progress})`;
  93. };
  94. function resizeRendererToDisplaySize( renderer ) {
  95. const canvas = renderer.domElement;
  96. const width = canvas.clientWidth;
  97. const height = canvas.clientHeight;
  98. const needResize = canvas.width !== width || canvas.height !== height;
  99. if ( needResize ) {
  100. renderer.setSize( width, height, false );
  101. }
  102. return needResize;
  103. }
  104. function loadColorTexture( path ) {
  105. const texture = loader.load( path );
  106. texture.colorSpace = THREE.SRGBColorSpace;
  107. return texture;
  108. }
  109. function render( time ) {
  110. time *= 0.001;
  111. if ( resizeRendererToDisplaySize( renderer ) ) {
  112. const canvas = renderer.domElement;
  113. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  114. camera.updateProjectionMatrix();
  115. }
  116. cubes.forEach( ( cube, ndx ) => {
  117. const speed = .2 + ndx * .1;
  118. const rot = time * speed;
  119. cube.rotation.x = rot;
  120. cube.rotation.y = rot;
  121. } );
  122. renderer.render( scene, camera );
  123. requestAnimationFrame( render );
  124. }
  125. requestAnimationFrame( render );
  126. }
  127. main();
  128. </script>
  129. </html>