textured-cube-adjust.html 3.9 KB

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