threejs-textured-cube-adjust.html 3.9 KB

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