textured-cube-adjust.html 4.1 KB

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