threejs-textured-cube-wait-for-all-textures.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. body {
  10. margin: 0;
  11. }
  12. #c {
  13. width: 100vw;
  14. height: 100vh;
  15. display: block;
  16. }
  17. #loading {
  18. position: fixed;
  19. top: 0;
  20. left: 0;
  21. width: 100%;
  22. height: 100%;
  23. display: flex;
  24. justify-content: center;
  25. align-items: center;
  26. }
  27. #loading .progress {
  28. margin: 1.5em;
  29. border: 1px solid white;
  30. width: 50vw;
  31. }
  32. #loading .progressbar {
  33. margin: 2px;
  34. background: white;
  35. height: 1em;
  36. transform-origin: top left;
  37. transform: scaleX(0);
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <canvas id="c"></canvas>
  43. <div id="loading">
  44. <div class="progress"><div class="progressbar"></div></div>
  45. </div>
  46. </body>
  47. <script src="resources/threejs/r102/three.min.js"></script>
  48. <script>
  49. 'use strict';
  50. /* global THREE */
  51. function main() {
  52. const canvas = document.querySelector('#c');
  53. const renderer = new THREE.WebGLRenderer({canvas: canvas});
  54. const fov = 75;
  55. const aspect = 2; // the canvas default
  56. const near = 0.1;
  57. const far = 5;
  58. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  59. camera.position.z = 2;
  60. const scene = new THREE.Scene();
  61. const boxWidth = 1;
  62. const boxHeight = 1;
  63. const boxDepth = 1;
  64. const geometry = new THREE.BoxBufferGeometry(boxWidth, boxHeight, boxDepth);
  65. const cubes = []; // just an array we can use to rotate the cubes
  66. const loadManager = new THREE.LoadingManager();
  67. const loader = new THREE.TextureLoader(loadManager);
  68. const materials = [
  69. new THREE.MeshBasicMaterial({map: loader.load('resources/images/flower-1.jpg')}),
  70. new THREE.MeshBasicMaterial({map: loader.load('resources/images/flower-2.jpg')}),
  71. new THREE.MeshBasicMaterial({map: loader.load('resources/images/flower-3.jpg')}),
  72. new THREE.MeshBasicMaterial({map: loader.load('resources/images/flower-4.jpg')}),
  73. new THREE.MeshBasicMaterial({map: loader.load('resources/images/flower-5.jpg')}),
  74. new THREE.MeshBasicMaterial({map: loader.load('resources/images/flower-6.jpg')}),
  75. ];
  76. const loadingElem = document.querySelector('#loading');
  77. const progressBarElem = loadingElem.querySelector('.progressbar');
  78. loadManager.onLoad = () => {
  79. loadingElem.style.display = 'none';
  80. const cube = new THREE.Mesh(geometry, materials);
  81. scene.add(cube);
  82. cubes.push(cube); // add to our list of cubes to rotate
  83. };
  84. loadManager.onProgress = (urlOfLastItemLoaded, itemsLoaded, itemsTotal) => {
  85. const progress = itemsLoaded / itemsTotal;
  86. progressBarElem.style.transform = `scaleX(${progress})`;
  87. };
  88. function resizeRendererToDisplaySize(renderer) {
  89. const canvas = renderer.domElement;
  90. const width = canvas.clientWidth;
  91. const height = canvas.clientHeight;
  92. const needResize = canvas.width !== width || canvas.height !== height;
  93. if (needResize) {
  94. renderer.setSize(width, height, false);
  95. }
  96. return needResize;
  97. }
  98. function render(time) {
  99. time *= 0.001;
  100. if (resizeRendererToDisplaySize(renderer)) {
  101. const canvas = renderer.domElement;
  102. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  103. camera.updateProjectionMatrix();
  104. }
  105. cubes.forEach((cube, ndx) => {
  106. const speed = .2 + ndx * .1;
  107. const rot = time * speed;
  108. cube.rotation.x = rot;
  109. cube.rotation.y = rot;
  110. });
  111. renderer.render(scene, camera);
  112. requestAnimationFrame(render);
  113. }
  114. requestAnimationFrame(render);
  115. }
  116. main();
  117. </script>
  118. </html>