webgl_materials_texture_rotation.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - texture - rotation</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - texture rotation
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import { GUI } from './jsm/libs/dat.gui.module.js';
  16. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  17. var mesh, renderer, scene, camera;
  18. var gui;
  19. var API = {
  20. offsetX: 0,
  21. offsetY: 0,
  22. repeatX: 0.25,
  23. repeatY: 0.25,
  24. rotation: Math.PI / 4, // positive is counter-clockwise
  25. centerX: 0.5,
  26. centerY: 0.5
  27. };
  28. init();
  29. function init() {
  30. renderer = new THREE.WebGLRenderer();
  31. renderer.setPixelRatio( window.devicePixelRatio );
  32. renderer.setSize( window.innerWidth, window.innerHeight );
  33. document.body.appendChild( renderer.domElement );
  34. scene = new THREE.Scene();
  35. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  36. camera.position.set( 10, 15, 25 );
  37. scene.add( camera );
  38. var controls = new OrbitControls( camera, renderer.domElement );
  39. controls.addEventListener( 'change', render );
  40. controls.minDistance = 20;
  41. controls.maxDistance = 50;
  42. controls.maxPolarAngle = Math.PI / 2;
  43. var geometry = new THREE.BoxBufferGeometry( 10, 10, 10 );
  44. new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg', function ( texture ) {
  45. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  46. texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
  47. //texture.matrixAutoUpdate = false; // default true; set to false to update texture.matrix manually
  48. var material = new THREE.MeshBasicMaterial( { map: texture } );
  49. mesh = new THREE.Mesh( geometry, material );
  50. scene.add( mesh );
  51. updateUvTransform();
  52. initGui();
  53. render();
  54. } );
  55. window.addEventListener( 'resize', onWindowResize, false );
  56. }
  57. function render() {
  58. renderer.render( scene, camera );
  59. }
  60. function onWindowResize() {
  61. camera.aspect = window.innerWidth / window.innerHeight;
  62. camera.updateProjectionMatrix();
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. render();
  65. }
  66. function updateUvTransform() {
  67. var texture = mesh.material.map;
  68. if ( texture.matrixAutoUpdate === true ) {
  69. texture.offset.set( API.offsetX, API.offsetY );
  70. texture.repeat.set( API.repeatX, API.repeatY );
  71. texture.center.set( API.centerX, API.centerY );
  72. texture.rotation = API.rotation; // rotation is around [ 0.5, 0.5 ]
  73. } else {
  74. // one way...
  75. //texture.matrix.setUvTransform( API.offsetX, API.offsetY, API.repeatX, API.repeatY, API.rotation, API.centerX, API.centerY );
  76. // another way...
  77. texture.matrix
  78. .identity()
  79. .translate( - API.centerX, - API.centerY )
  80. .rotate( API.rotation ) // I don't understand how rotation can preceed scale, but it seems to be required...
  81. .scale( API.repeatX, API.repeatY )
  82. .translate( API.centerX, API.centerY )
  83. .translate( API.offsetX, API.offsetY );
  84. }
  85. render();
  86. }
  87. function initGui() {
  88. gui = new GUI();
  89. gui.add( API, 'offsetX', 0.0, 1.0 ).name( 'offset.x' ).onChange( updateUvTransform );
  90. gui.add( API, 'offsetY', 0.0, 1.0 ).name( 'offset.y' ).onChange( updateUvTransform );
  91. gui.add( API, 'repeatX', 0.25, 2.0 ).name( 'repeat.x' ).onChange( updateUvTransform );
  92. gui.add( API, 'repeatY', 0.25, 2.0 ).name( 'repeat.y' ).onChange( updateUvTransform );
  93. gui.add( API, 'rotation', - 2.0, 2.0 ).name( 'rotation' ).onChange( updateUvTransform );
  94. gui.add( API, 'centerX', 0.0, 1.0 ).name( 'center.x' ).onChange( updateUvTransform );
  95. gui.add( API, 'centerY', 0.0, 1.0 ).name( 'center.y' ).onChange( updateUvTransform );
  96. }
  97. </script>
  98. </body>
  99. </html>