textured-cube-wait-for-texture.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 - Wait for Texture</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. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../../build/three.module.js"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. function main() {
  36. const canvas = document.querySelector('#c');
  37. const renderer = new THREE.WebGLRenderer({antialias: true, canvas});
  38. const fov = 75;
  39. const aspect = 2; // the canvas default
  40. const near = 0.1;
  41. const far = 5;
  42. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  43. camera.position.z = 2;
  44. const scene = new THREE.Scene();
  45. const boxWidth = 1;
  46. const boxHeight = 1;
  47. const boxDepth = 1;
  48. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  49. const cubes = []; // just an array we can use to rotate the cubes
  50. const loader = new THREE.TextureLoader();
  51. loader.load('resources/images/wall.jpg', (texture) => {
  52. const material = new THREE.MeshBasicMaterial({
  53. map: texture,
  54. });
  55. const cube = new THREE.Mesh(geometry, material);
  56. scene.add(cube);
  57. cubes.push(cube); // add to our list of cubes to rotate
  58. });
  59. function resizeRendererToDisplaySize(renderer) {
  60. const canvas = renderer.domElement;
  61. const width = canvas.clientWidth;
  62. const height = canvas.clientHeight;
  63. const needResize = canvas.width !== width || canvas.height !== height;
  64. if (needResize) {
  65. renderer.setSize(width, height, false);
  66. }
  67. return needResize;
  68. }
  69. function render(time) {
  70. time *= 0.001;
  71. if (resizeRendererToDisplaySize(renderer)) {
  72. const canvas = renderer.domElement;
  73. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  74. camera.updateProjectionMatrix();
  75. }
  76. cubes.forEach((cube, ndx) => {
  77. const speed = .2 + ndx * .1;
  78. const rot = time * speed;
  79. cube.rotation.x = rot;
  80. cube.rotation.y = rot;
  81. });
  82. renderer.render(scene, camera);
  83. requestAnimationFrame(render);
  84. }
  85. requestAnimationFrame(render);
  86. }
  87. main();
  88. </script>
  89. </html>