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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. <!-- Import maps polyfill -->
  49. <!-- Remove this when import maps will be widely supported -->
  50. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  51. <script type="importmap">
  52. {
  53. "imports": {
  54. "three": "../../build/three.module.js"
  55. }
  56. }
  57. </script>
  58. <script type="module">
  59. import * as THREE from 'three';
  60. function main() {
  61. const canvas = document.querySelector( '#c' );
  62. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  63. renderer.useLegacyLights = false;
  64. const fov = 75;
  65. const aspect = 2; // the canvas default
  66. const near = 0.1;
  67. const far = 5;
  68. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  69. camera.position.z = 2;
  70. const scene = new THREE.Scene();
  71. const boxWidth = 1;
  72. const boxHeight = 1;
  73. const boxDepth = 1;
  74. const geometry = new THREE.BoxGeometry( boxWidth, boxHeight, boxDepth );
  75. const cubes = []; // just an array we can use to rotate the cubes
  76. const loadManager = new THREE.LoadingManager();
  77. const loader = new THREE.TextureLoader( loadManager );
  78. const materials = [
  79. new THREE.MeshBasicMaterial( { map: loadColorTexture( 'resources/images/flower-1.jpg' ) } ),
  80. new THREE.MeshBasicMaterial( { map: loadColorTexture( 'resources/images/flower-2.jpg' ) } ),
  81. new THREE.MeshBasicMaterial( { map: loadColorTexture( 'resources/images/flower-3.jpg' ) } ),
  82. new THREE.MeshBasicMaterial( { map: loadColorTexture( 'resources/images/flower-4.jpg' ) } ),
  83. new THREE.MeshBasicMaterial( { map: loadColorTexture( 'resources/images/flower-5.jpg' ) } ),
  84. new THREE.MeshBasicMaterial( { map: loadColorTexture( 'resources/images/flower-6.jpg' ) } ),
  85. ];
  86. const loadingElem = document.querySelector( '#loading' );
  87. const progressBarElem = loadingElem.querySelector( '.progressbar' );
  88. loadManager.onLoad = () => {
  89. loadingElem.style.display = 'none';
  90. const cube = new THREE.Mesh( geometry, materials );
  91. scene.add( cube );
  92. cubes.push( cube ); // add to our list of cubes to rotate
  93. };
  94. loadManager.onProgress = ( urlOfLastItemLoaded, itemsLoaded, itemsTotal ) => {
  95. const progress = itemsLoaded / itemsTotal;
  96. progressBarElem.style.transform = `scaleX(${progress})`;
  97. };
  98. function resizeRendererToDisplaySize( renderer ) {
  99. const canvas = renderer.domElement;
  100. const width = canvas.clientWidth;
  101. const height = canvas.clientHeight;
  102. const needResize = canvas.width !== width || canvas.height !== height;
  103. if ( needResize ) {
  104. renderer.setSize( width, height, false );
  105. }
  106. return needResize;
  107. }
  108. function loadColorTexture( path ) {
  109. const texture = loader.load( path );
  110. texture.colorSpace = THREE.SRGBColorSpace;
  111. return texture;
  112. }
  113. function render( time ) {
  114. time *= 0.001;
  115. if ( resizeRendererToDisplaySize( renderer ) ) {
  116. const canvas = renderer.domElement;
  117. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  118. camera.updateProjectionMatrix();
  119. }
  120. cubes.forEach( ( cube, ndx ) => {
  121. const speed = .2 + ndx * .1;
  122. const rot = time * speed;
  123. cube.rotation.x = rot;
  124. cube.rotation.y = rot;
  125. } );
  126. renderer.render( scene, camera );
  127. requestAnimationFrame( render );
  128. }
  129. requestAnimationFrame( render );
  130. }
  131. main();
  132. </script>
  133. </html>