threejs-textured-cube-6-textures.html 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. </style>
  18. </head>
  19. <body>
  20. <canvas id="c"></canvas>
  21. </body>
  22. <script type="module">
  23. import * as THREE from './resources/threejs/r115/build/three.module.js';
  24. function main() {
  25. const canvas = document.querySelector('#c');
  26. const renderer = new THREE.WebGLRenderer({canvas});
  27. const fov = 75;
  28. const aspect = 2; // the canvas default
  29. const near = 0.1;
  30. const far = 5;
  31. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  32. camera.position.z = 2;
  33. const scene = new THREE.Scene();
  34. const boxWidth = 1;
  35. const boxHeight = 1;
  36. const boxDepth = 1;
  37. const geometry = new THREE.BoxBufferGeometry(boxWidth, boxHeight, boxDepth);
  38. const cubes = []; // just an array we can use to rotate the cubes
  39. const loader = new THREE.TextureLoader();
  40. const materials = [
  41. new THREE.MeshBasicMaterial({map: loader.load('resources/images/flower-1.jpg')}),
  42. new THREE.MeshBasicMaterial({map: loader.load('resources/images/flower-2.jpg')}),
  43. new THREE.MeshBasicMaterial({map: loader.load('resources/images/flower-3.jpg')}),
  44. new THREE.MeshBasicMaterial({map: loader.load('resources/images/flower-4.jpg')}),
  45. new THREE.MeshBasicMaterial({map: loader.load('resources/images/flower-5.jpg')}),
  46. new THREE.MeshBasicMaterial({map: loader.load('resources/images/flower-6.jpg')}),
  47. ];
  48. const cube = new THREE.Mesh(geometry, materials);
  49. scene.add(cube);
  50. cubes.push(cube); // add to our list of cubes to rotate
  51. function resizeRendererToDisplaySize(renderer) {
  52. const canvas = renderer.domElement;
  53. const width = canvas.clientWidth;
  54. const height = canvas.clientHeight;
  55. const needResize = canvas.width !== width || canvas.height !== height;
  56. if (needResize) {
  57. renderer.setSize(width, height, false);
  58. }
  59. return needResize;
  60. }
  61. function render(time) {
  62. time *= 0.001;
  63. if (resizeRendererToDisplaySize(renderer)) {
  64. const canvas = renderer.domElement;
  65. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  66. camera.updateProjectionMatrix();
  67. }
  68. cubes.forEach((cube, ndx) => {
  69. const speed = .2 + ndx * .1;
  70. const rot = time * speed;
  71. cube.rotation.x = rot;
  72. cube.rotation.y = rot;
  73. });
  74. renderer.render(scene, camera);
  75. requestAnimationFrame(render);
  76. }
  77. requestAnimationFrame(render);
  78. }
  79. main();
  80. </script>
  81. </html>