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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. const fov = 75;
  64. const aspect = 2; // the canvas default
  65. const near = 0.1;
  66. const far = 5;
  67. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  68. camera.position.z = 2;
  69. const scene = new THREE.Scene();
  70. const boxWidth = 1;
  71. const boxHeight = 1;
  72. const boxDepth = 1;
  73. const geometry = new THREE.BoxGeometry( boxWidth, boxHeight, boxDepth );
  74. const cubes = []; // just an array we can use to rotate the cubes
  75. const loadManager = new THREE.LoadingManager();
  76. const loader = new THREE.TextureLoader( loadManager );
  77. const materials = [
  78. new THREE.MeshBasicMaterial( { map: loadColorTexture( 'resources/images/flower-1.jpg' ) } ),
  79. new THREE.MeshBasicMaterial( { map: loadColorTexture( 'resources/images/flower-2.jpg' ) } ),
  80. new THREE.MeshBasicMaterial( { map: loadColorTexture( 'resources/images/flower-3.jpg' ) } ),
  81. new THREE.MeshBasicMaterial( { map: loadColorTexture( 'resources/images/flower-4.jpg' ) } ),
  82. new THREE.MeshBasicMaterial( { map: loadColorTexture( 'resources/images/flower-5.jpg' ) } ),
  83. new THREE.MeshBasicMaterial( { map: loadColorTexture( 'resources/images/flower-6.jpg' ) } ),
  84. ];
  85. const loadingElem = document.querySelector( '#loading' );
  86. const progressBarElem = loadingElem.querySelector( '.progressbar' );
  87. loadManager.onLoad = () => {
  88. loadingElem.style.display = 'none';
  89. const cube = new THREE.Mesh( geometry, materials );
  90. scene.add( cube );
  91. cubes.push( cube ); // add to our list of cubes to rotate
  92. };
  93. loadManager.onProgress = ( urlOfLastItemLoaded, itemsLoaded, itemsTotal ) => {
  94. const progress = itemsLoaded / itemsTotal;
  95. progressBarElem.style.transform = `scaleX(${progress})`;
  96. };
  97. function resizeRendererToDisplaySize( renderer ) {
  98. const canvas = renderer.domElement;
  99. const width = canvas.clientWidth;
  100. const height = canvas.clientHeight;
  101. const needResize = canvas.width !== width || canvas.height !== height;
  102. if ( needResize ) {
  103. renderer.setSize( width, height, false );
  104. }
  105. return needResize;
  106. }
  107. function loadColorTexture( path ) {
  108. const texture = loader.load( path );
  109. texture.colorSpace = THREE.SRGBColorSpace;
  110. return texture;
  111. }
  112. function render( time ) {
  113. time *= 0.001;
  114. if ( resizeRendererToDisplaySize( renderer ) ) {
  115. const canvas = renderer.domElement;
  116. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  117. camera.updateProjectionMatrix();
  118. }
  119. cubes.forEach( ( cube, ndx ) => {
  120. const speed = .2 + ndx * .1;
  121. const rot = time * speed;
  122. cube.rotation.x = rot;
  123. cube.rotation.y = rot;
  124. } );
  125. renderer.render( scene, camera );
  126. requestAnimationFrame( render );
  127. }
  128. requestAnimationFrame( render );
  129. }
  130. main();
  131. </script>
  132. </html>