threejs-textured-cube-adjust.html 3.9 KB

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