123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - transform controls</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="info">
- "W" translate | "E" rotate | "R" scale | "+" increase size | "-" decrease size<br />
- "Q" toggle world/local space | Hold "Shift" down to snap to grid<br />
- "X" toggle X | "Y" toggle Y | "Z" toggle Z | "Spacebar" toggle enabled
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { TransformControls } from './jsm/controls/TransformControls.js';
- var camera, scene, renderer, control, orbit;
- init();
- render();
- function init() {
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- //
- camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 3000 );
- camera.position.set( 1000, 500, 1000 );
- camera.lookAt( 0, 200, 0 );
- scene = new THREE.Scene();
- scene.add( new THREE.GridHelper( 1000, 10 ) );
- var light = new THREE.DirectionalLight( 0xffffff, 2 );
- light.position.set( 1, 1, 1 );
- scene.add( light );
- var texture = new THREE.TextureLoader().load( 'textures/crate.gif', render );
- texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
- var geometry = new THREE.BoxBufferGeometry( 200, 200, 200 );
- var material = new THREE.MeshLambertMaterial( { map: texture, transparent: true } );
- orbit = new OrbitControls( camera, renderer.domElement );
- orbit.update();
- orbit.addEventListener( 'change', render );
- control = new TransformControls( camera, renderer.domElement );
- control.addEventListener( 'change', render );
- control.addEventListener( 'dragging-changed', function ( event ) {
- orbit.enabled = ! event.value;
- } );
- var mesh = new THREE.Mesh( geometry, material );
- scene.add( mesh );
- control.attach( mesh );
- scene.add( control );
- window.addEventListener( 'resize', onWindowResize, false );
- window.addEventListener( 'keydown', function ( event ) {
- switch ( event.keyCode ) {
- case 81: // Q
- control.setSpace( control.space === "local" ? "world" : "local" );
- break;
- case 16: // Shift
- control.setTranslationSnap( 100 );
- control.setRotationSnap( THREE.MathUtils.degToRad( 15 ) );
- control.setScaleSnap( 0.25 );
- break;
- case 87: // W
- control.setMode( "translate" );
- break;
- case 69: // E
- control.setMode( "rotate" );
- break;
- case 82: // R
- control.setMode( "scale" );
- break;
- case 187:
- case 107: // +, =, num+
- control.setSize( control.size + 0.1 );
- break;
- case 189:
- case 109: // -, _, num-
- control.setSize( Math.max( control.size - 0.1, 0.1 ) );
- break;
- case 88: // X
- control.showX = ! control.showX;
- break;
- case 89: // Y
- control.showY = ! control.showY;
- break;
- case 90: // Z
- control.showZ = ! control.showZ;
- break;
- case 32: // Spacebar
- control.enabled = ! control.enabled;
- break;
- }
- } );
- window.addEventListener( 'keyup', function ( event ) {
- switch ( event.keyCode ) {
- case 17: // Ctrl
- control.setTranslationSnap( null );
- control.setRotationSnap( null );
- control.setScaleSnap( null );
- break;
- }
- } );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- render();
- }
- function render() {
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|