textured-cube-adjust.html 4.3 KB

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