textured-cube-adjust.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 - Adjustments</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. "three/addons/": "../../examples/jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three';
  36. import {GUI} from 'three/addons/libs/lil-gui.module.min.js';
  37. function main() {
  38. const canvas = document.querySelector('#c');
  39. const renderer = new THREE.WebGLRenderer({antialias: true, canvas});
  40. const fov = 75;
  41. const aspect = 2; // the canvas default
  42. const near = 0.1;
  43. const far = 5;
  44. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  45. camera.position.z = 2;
  46. const scene = new THREE.Scene();
  47. const boxWidth = 1;
  48. const boxHeight = 1;
  49. const boxDepth = 1;
  50. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  51. const cubes = []; // just an array we can use to rotate the cubes
  52. const loader = new THREE.TextureLoader();
  53. const texture = loader.load('resources/images/wall.jpg');
  54. const material = new THREE.MeshBasicMaterial({
  55. map: texture,
  56. });
  57. const cube = new THREE.Mesh(geometry, material);
  58. scene.add(cube);
  59. cubes.push(cube); // add to our list of cubes to rotate
  60. class DegRadHelper {
  61. constructor(obj, prop) {
  62. this.obj = obj;
  63. this.prop = prop;
  64. }
  65. get value() {
  66. return THREE.MathUtils.radToDeg(this.obj[this.prop]);
  67. }
  68. set value(v) {
  69. this.obj[this.prop] = THREE.MathUtils.degToRad(v);
  70. }
  71. }
  72. class StringToNumberHelper {
  73. constructor(obj, prop) {
  74. this.obj = obj;
  75. this.prop = prop;
  76. }
  77. get value() {
  78. return this.obj[this.prop];
  79. }
  80. set value(v) {
  81. this.obj[this.prop] = parseFloat(v);
  82. }
  83. }
  84. const wrapModes = {
  85. 'ClampToEdgeWrapping': THREE.ClampToEdgeWrapping,
  86. 'RepeatWrapping': THREE.RepeatWrapping,
  87. 'MirroredRepeatWrapping': THREE.MirroredRepeatWrapping,
  88. };
  89. function updateTexture() {
  90. texture.needsUpdate = true;
  91. }
  92. const gui = new GUI();
  93. gui.add(new StringToNumberHelper(texture, 'wrapS'), 'value', wrapModes)
  94. .name('texture.wrapS')
  95. .onChange(updateTexture);
  96. gui.add(new StringToNumberHelper(texture, 'wrapT'), 'value', wrapModes)
  97. .name('texture.wrapT')
  98. .onChange(updateTexture);
  99. gui.add(texture.repeat, 'x', 0, 5, .01).name('texture.repeat.x');
  100. gui.add(texture.repeat, 'y', 0, 5, .01).name('texture.repeat.y');
  101. gui.add(texture.offset, 'x', -2, 2, .01).name('texture.offset.x');
  102. gui.add(texture.offset, 'y', -2, 2, .01).name('texture.offset.y');
  103. gui.add(texture.center, 'x', -.5, 1.5, .01).name('texture.center.x');
  104. gui.add(texture.center, 'y', -.5, 1.5, .01).name('texture.center.y');
  105. gui.add(new DegRadHelper(texture, 'rotation'), 'value', -360, 360)
  106. .name('texture.rotation');
  107. function resizeRendererToDisplaySize(renderer) {
  108. const canvas = renderer.domElement;
  109. const width = canvas.clientWidth;
  110. const height = canvas.clientHeight;
  111. const needResize = canvas.width !== width || canvas.height !== height;
  112. if (needResize) {
  113. renderer.setSize(width, height, false);
  114. }
  115. return needResize;
  116. }
  117. function render(time) {
  118. time *= 0.001;
  119. if (resizeRendererToDisplaySize(renderer)) {
  120. const canvas = renderer.domElement;
  121. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  122. camera.updateProjectionMatrix();
  123. }
  124. cubes.forEach((cube, ndx) => {
  125. const speed = .2 + ndx * .1;
  126. const rot = time * speed;
  127. cube.rotation.x = rot;
  128. cube.rotation.y = rot;
  129. });
  130. renderer.render(scene, camera);
  131. requestAnimationFrame(render);
  132. }
  133. requestAnimationFrame(render);
  134. }
  135. main();
  136. </script>
  137. </html>