misc_controls_transform.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - transform controls</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. "W" translate | "E" rotate | "R" scale | "+" increase size | "-" decrease size<br />
  12. "Q" toggle world/local space | Hold "Shift" down to snap to grid<br />
  13. "X" toggle X | "Y" toggle Y | "Z" toggle Z | "Spacebar" toggle enabled
  14. </div>
  15. <script type="module">
  16. import * as THREE from '../build/three.module.js';
  17. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  18. import { TransformControls } from './jsm/controls/TransformControls.js';
  19. var camera, scene, renderer, control, orbit;
  20. init();
  21. render();
  22. function init() {
  23. renderer = new THREE.WebGLRenderer();
  24. renderer.setPixelRatio( window.devicePixelRatio );
  25. renderer.setSize( window.innerWidth, window.innerHeight );
  26. document.body.appendChild( renderer.domElement );
  27. //
  28. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 3000 );
  29. camera.position.set( 1000, 500, 1000 );
  30. camera.lookAt( 0, 200, 0 );
  31. scene = new THREE.Scene();
  32. scene.add( new THREE.GridHelper( 1000, 10 ) );
  33. var light = new THREE.DirectionalLight( 0xffffff, 2 );
  34. light.position.set( 1, 1, 1 );
  35. scene.add( light );
  36. var texture = new THREE.TextureLoader().load( 'textures/crate.gif', render );
  37. texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
  38. var geometry = new THREE.BoxBufferGeometry( 200, 200, 200 );
  39. var material = new THREE.MeshLambertMaterial( { map: texture, transparent: true } );
  40. orbit = new OrbitControls( camera, renderer.domElement );
  41. orbit.update();
  42. orbit.addEventListener( 'change', render );
  43. control = new TransformControls( camera, renderer.domElement );
  44. control.addEventListener( 'change', render );
  45. control.addEventListener( 'dragging-changed', function ( event ) {
  46. orbit.enabled = ! event.value;
  47. } );
  48. var mesh = new THREE.Mesh( geometry, material );
  49. scene.add( mesh );
  50. control.attach( mesh );
  51. scene.add( control );
  52. window.addEventListener( 'resize', onWindowResize, false );
  53. window.addEventListener( 'keydown', function ( event ) {
  54. switch ( event.keyCode ) {
  55. case 81: // Q
  56. control.setSpace( control.space === "local" ? "world" : "local" );
  57. break;
  58. case 16: // Shift
  59. control.setTranslationSnap( 100 );
  60. control.setRotationSnap( THREE.MathUtils.degToRad( 15 ) );
  61. control.setScaleSnap( 0.25 );
  62. break;
  63. case 87: // W
  64. control.setMode( "translate" );
  65. break;
  66. case 69: // E
  67. control.setMode( "rotate" );
  68. break;
  69. case 82: // R
  70. control.setMode( "scale" );
  71. break;
  72. case 187:
  73. case 107: // +, =, num+
  74. control.setSize( control.size + 0.1 );
  75. break;
  76. case 189:
  77. case 109: // -, _, num-
  78. control.setSize( Math.max( control.size - 0.1, 0.1 ) );
  79. break;
  80. case 88: // X
  81. control.showX = ! control.showX;
  82. break;
  83. case 89: // Y
  84. control.showY = ! control.showY;
  85. break;
  86. case 90: // Z
  87. control.showZ = ! control.showZ;
  88. break;
  89. case 32: // Spacebar
  90. control.enabled = ! control.enabled;
  91. break;
  92. }
  93. } );
  94. window.addEventListener( 'keyup', function ( event ) {
  95. switch ( event.keyCode ) {
  96. case 17: // Ctrl
  97. control.setTranslationSnap( null );
  98. control.setRotationSnap( null );
  99. control.setScaleSnap( null );
  100. break;
  101. }
  102. } );
  103. }
  104. function onWindowResize() {
  105. camera.aspect = window.innerWidth / window.innerHeight;
  106. camera.updateProjectionMatrix();
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. render();
  109. }
  110. function render() {
  111. renderer.render( scene, camera );
  112. }
  113. </script>
  114. </body>
  115. </html>