textured-cube-adjust.html 4.2 KB

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