textured-cube-adjust.html 4.2 KB

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