TransformControls.js 23.5 KB
Newer Older
A
Aleksandar Rodic 已提交
1 2 3 4
/**
 * @author arodic / https://github.com/arodic
 */

A
Aleksandar Rodic 已提交
5
 //"use strict";
A
Aleksandar Rodic 已提交
6

A
Aleksandar Rodic 已提交
7
THREE.TransformControls = function ( camera, domElement, callback ) {
A
Aleksandar Rodic 已提交
8 9

	// TODO: Choose a better fitting intersection plane when looking at grazing angles
A
Aleksandar Rodic 已提交
10
	// TODO: Make non-uniform scale and rotate play nice in hierarchies
A
Aleksandar Rodic 已提交
11 12 13 14
	// TODO: ADD RXYZ contol

	this.camera = camera;
	this.domElement = ( domElement !== undefined ) ? domElement : document;
A
Aleksandar Rodic 已提交
15
	this.callback = callback ? callback : function() {}
A
Aleksandar Rodic 已提交
16 17

	this.active = false;
A
Aleksandar Rodic 已提交
18 19 20
	this.mode = 'translate';
	this.space = 'world';
	this.scale = 1;
A
Aleksandar Rodic 已提交
21 22

	this.snapDist = null;
23 24 25
	this.modifierAxis = new THREE.Vector3( 1, 1, 1 );
	this.gizmo = new THREE.Object3D();

A
Aleksandar Rodic 已提交
26 27 28 29 30 31 32 33 34
	var scope = this;

	var showPickers = false; // debug

	var ray = new THREE.Raycaster();
	var projector = new THREE.Projector();
	var pointerVector = new THREE.Vector3();

	var point = new THREE.Vector3();
A
Aleksandar Rodic 已提交
35 36
	var offset = new THREE.Vector3();

A
Aleksandar Rodic 已提交
37 38
	var rotation = new THREE.Vector3();
	var offsetRotation = new THREE.Vector3();
A
Aleksandar Rodic 已提交
39 40
	var scale = 1;

A
Aleksandar Rodic 已提交
41
	var lookAtMatrix = new THREE.Matrix4();
A
Aleksandar Rodic 已提交
42
	var eye = new THREE.Vector3()
43

A
Aleksandar Rodic 已提交
44 45 46 47 48 49 50 51 52 53 54
	var tempMatrix = new THREE.Matrix4();
	var tempVector = new THREE.Vector3();
	var tempQuaternion = new THREE.Quaternion();
	var unitX = new THREE.Vector3( 1, 0, 0 );
	var unitY = new THREE.Vector3( 0, 1, 0 );
	var unitZ = new THREE.Vector3( 0, 0, 1 );

	var quaternionXYZ = new THREE.Quaternion();
	var quaternionX = new THREE.Quaternion();
	var quaternionY = new THREE.Quaternion();
	var quaternionZ = new THREE.Quaternion();
A
Aleksandar Rodic 已提交
55
	var quaternionE = new THREE.Quaternion();
A
Aleksandar Rodic 已提交
56 57 58

	var oldPosition = new THREE.Vector3();
	var oldScale = new THREE.Vector3();
A
Aleksandar Rodic 已提交
59 60 61 62
	var oldRotationMatrix = new THREE.Matrix4();

	var parentRotationMatrix  = new THREE.Matrix4();
	var parentScale = new THREE.Vector3();
A
Aleksandar Rodic 已提交
63

A
Aleksandar Rodic 已提交
64 65 66
	var worldPosition = new THREE.Vector3();
	var worldRotation = new THREE.Vector3();
	var worldRotationMatrix  = new THREE.Matrix4();
A
Aleksandar Rodic 已提交
67 68 69 70 71 72
	var camPosition = new THREE.Vector3();
	var camRotation = new THREE.Vector3();

	var displayAxes = {};
	var pickerAxes = {};
	var intersectionPlanes = {};
A
Aleksandar Rodic 已提交
73
	var intersectionPlaneList = ['XY','YZ','XZ','XYZE']; // E
A
Aleksandar Rodic 已提交
74
	var currentPlane = 'XY';
A
Aleksandar Rodic 已提交
75
	var intersect, planeIntersect;
A
Aleksandar Rodic 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

	var object, name;

	// gizmo geometry
	{

		displayAxes["translate"] = new THREE.Object3D();
		displayAxes["rotate"] = new THREE.Object3D();
		displayAxes["scale"] = new THREE.Object3D();
		this.gizmo.add( displayAxes["translate"] );
		this.gizmo.add( displayAxes["rotate"] );
		this.gizmo.add( displayAxes["scale"] );
		
		pickerAxes["translate"] = new THREE.Object3D();
		pickerAxes["rotate"] = new THREE.Object3D();
		pickerAxes["scale"] = new THREE.Object3D();
		this.gizmo.add( pickerAxes["translate"] );
		this.gizmo.add( pickerAxes["rotate"] );
		this.gizmo.add( pickerAxes["scale"] );

		var HandleMaterial = function ( color ) {
			var material = new THREE.MeshBasicMaterial();
			material.side = THREE.DoubleSide;
			material.transparent = true;
			material.depthTest = false;
			material.depthWrite = false;
			material.color.setRGB( color[0], color[1], color[2] );
			material.opacity = color[3];
			return material;
		}

		var LineMaterial = function ( color ) {
			var material = new THREE.LineBasicMaterial();
			material.side = THREE.DoubleSide;
			material.transparent = true;
			material.depthTest = false;
			material.depthWrite = false;
			material.color.setRGB( color[0], color[1], color[2] );
			material.opacity = color[3];
			return material;
		}

		// materials by color
		var white = [1,1,1,0.2];
		var gray = [0.5,0.5,0.5,1];
		var red = [1,0,0,1];
		var green = [0,1,0,1];
		var blue = [0,0,1,1];
		var cyan = [0,1,1,0.2];
		var magenta = [1,0,1,0.2];
		var yellow = [1,1,0,0.2];

128
		var geometry, mesh;
A
Aleksandar Rodic 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

		// line axes

		mesh = new THREE.Line( new THREE.Geometry(), LineMaterial( red ) );
		mesh.geometry.vertices = [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 1, 0, 0 ) ];
		displayAxes['translate'].add( mesh );
		displayAxes['scale'].add( mesh.clone() );

		mesh = new THREE.Line( new THREE.Geometry(), LineMaterial( green ) );
		mesh.geometry.vertices = [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 1, 0 ) ];
		displayAxes['translate'].add( mesh );
		displayAxes['scale'].add( mesh.clone() );

		mesh = new THREE.Line( new THREE.Geometry(), LineMaterial( blue ) );
		mesh.geometry.vertices = [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, 1 ) ];
		displayAxes['translate'].add( mesh );
		displayAxes['scale'].add( mesh.clone() );

		// Translate handles

		mesh = new THREE.Mesh( new THREE.OctahedronGeometry( 0.1, 0 ), HandleMaterial( white ) );
		mesh.name = 'TXYZ';
		displayAxes['translate'].add( mesh );
		pickerAxes['translate'].add( mesh.clone() );

154 155 156
		geometry = new THREE.PlaneGeometry( 0.2, 0.2 );

		mesh = new THREE.Mesh( geometry, HandleMaterial( yellow ) );
A
Aleksandar Rodic 已提交
157 158 159 160 161 162
		mesh.position.set( 0.1, 0.1, 0 );
		bakeTransformations( mesh );
		mesh.name = 'TXY';
		displayAxes['translate'].add( mesh );
		pickerAxes['translate'].add( mesh.clone() );

163
		mesh = new THREE.Mesh( geometry, HandleMaterial( cyan ) );
A
Aleksandar Rodic 已提交
164 165 166 167 168 169
		mesh.position.set( 0, 0.1, 0.1 );
		mesh.rotation.y = Math.PI/2;
		bakeTransformations( mesh );
		mesh.name = 'TYZ';
		displayAxes['translate'].add( mesh );
		pickerAxes['translate'].add( mesh.clone() );
170 171

		mesh = new THREE.Mesh( geometry, HandleMaterial( magenta ) );
A
Aleksandar Rodic 已提交
172 173 174 175 176 177 178
		mesh.position.set( 0.1, 0, 0.1 );
		mesh.rotation.x = Math.PI/2;
		bakeTransformations( mesh );
		mesh.name = 'TXZ';
		displayAxes['translate'].add( mesh );
		pickerAxes['translate'].add( mesh.clone() );

179 180 181
		geometry = new THREE.CylinderGeometry( 0, 0.05, 0.2, 4, 1, true );

		mesh = new THREE.Mesh( geometry, HandleMaterial( red ) );
A
Aleksandar Rodic 已提交
182 183 184 185 186 187
		mesh.position.x = 0.9;
		mesh.rotation.z = -Math.PI/2;
		bakeTransformations( mesh );
		mesh.name = 'TX';
		displayAxes['translate'].add( mesh );

188
		mesh = new THREE.Mesh( geometry, HandleMaterial( green ) );
A
Aleksandar Rodic 已提交
189 190 191 192 193
		mesh.position.y = 0.9;
		bakeTransformations( mesh );
		mesh.name = 'TY';
		displayAxes['translate'].add( mesh );

194
		mesh = new THREE.Mesh( geometry, HandleMaterial( blue ) );
A
Aleksandar Rodic 已提交
195 196 197 198 199 200
		mesh.position.z = 0.9;
		mesh.rotation.x = Math.PI/2;
		bakeTransformations( mesh );
		mesh.name = 'TZ';
		displayAxes['translate'].add( mesh );

201 202 203
		geometry = new THREE.CylinderGeometry( 0.04, 0.04, 0.8, 4, 1, false );

		mesh = new THREE.Mesh( geometry, HandleMaterial( red ) );
A
Aleksandar Rodic 已提交
204 205 206 207 208 209
		mesh.position.x = 0.6;
		mesh.rotation.z = -Math.PI/2;
		bakeTransformations( mesh );
		mesh.name = 'TX';
		pickerAxes['translate'].add( mesh );

210
		mesh = new THREE.Mesh( geometry, HandleMaterial( green ) );
A
Aleksandar Rodic 已提交
211 212 213 214 215
		mesh.position.y = 0.6;
		bakeTransformations( mesh );
		mesh.name = 'TY';
		pickerAxes['translate'].add( mesh );

216
		mesh = new THREE.Mesh( geometry, HandleMaterial( blue ) );
A
Aleksandar Rodic 已提交
217 218 219 220 221 222 223 224
		mesh.position.z = 0.6;
		mesh.rotation.x = Math.PI/2;
		bakeTransformations( mesh );
		mesh.name = 'TZ';
		pickerAxes['translate'].add( mesh );

		// scale manipulators

225 226 227
		geometry = new THREE.CubeGeometry( 0.1, 0.1, 0.1 );

		mesh = new THREE.Mesh( geometry, HandleMaterial( white ) );
A
Aleksandar Rodic 已提交
228 229 230 231
		mesh.name = 'SXYZ';
		displayAxes['scale'].add( mesh );
		pickerAxes['scale'].add( mesh.clone() );

232
		mesh = new THREE.Mesh( geometry, HandleMaterial( red ) );
A
Aleksandar Rodic 已提交
233 234 235 236 237 238
		mesh.position.set( 1, 0, 0 );
		bakeTransformations( mesh );
		mesh.name = 'SX';
		displayAxes['scale'].add( mesh );
		pickerAxes['scale'].add( mesh.clone() );

239
		mesh = new THREE.Mesh( geometry, HandleMaterial( green ) );
A
Aleksandar Rodic 已提交
240 241 242 243 244 245
		mesh.position.set( 0, 1, 0 );
		bakeTransformations( mesh );
		mesh.name = 'SY';
		displayAxes['scale'].add( mesh );
		pickerAxes['scale'].add( mesh.clone() );

246
		mesh = new THREE.Mesh( geometry, HandleMaterial( blue ) );
A
Aleksandar Rodic 已提交
247 248 249 250 251 252 253 254
		mesh.position.set( 0, 0, 1 );
		bakeTransformations( mesh );
		mesh.name = 'SZ';
		displayAxes['scale'].add( mesh );
		pickerAxes['scale'].add( mesh.clone() );

		// rotate manipulators

255 256 257
		var Circle = function ( radius, facing, arc ) {

			geometry = new THREE.Geometry();
A
Aleksandar Rodic 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
			arc = arc ? arc : 1;
			for ( var i = 0; i <= 64 * arc; ++i ) {
				if ( facing == 'x' ) geometry.vertices.push( new THREE.Vector3( 0, Math.cos( i / 32 * Math.PI ), Math.sin( i / 32 * Math.PI ) ).multiplyScalar(radius) );
				if ( facing == 'y' ) geometry.vertices.push( new THREE.Vector3( Math.cos( i / 32 * Math.PI ), 0, Math.sin( i / 32 * Math.PI ) ).multiplyScalar(radius) );
				if ( facing == 'z' ) geometry.vertices.push( new THREE.Vector3( Math.sin( i / 32 * Math.PI ), Math.cos( i / 32 * Math.PI ), 0 ).multiplyScalar(radius) );
			}

			return geometry;
		}

		mesh = new THREE.Line( Circle( 1, 'x', 0.5 ), LineMaterial( red ) );
		mesh.name = 'RX';
		displayAxes['rotate'].add( mesh );

		mesh = new THREE.Line( Circle( 1, 'y', 0.5 ), LineMaterial( green ) );
		mesh.name = 'RY';
		displayAxes['rotate'].add( mesh );

		mesh = new THREE.Line( Circle( 1, 'z', 0.5 ), LineMaterial( blue ) );
		mesh.name = 'RZ';
		displayAxes['rotate'].add( mesh );

		mesh = new THREE.Line( Circle( 1, 'z' ), LineMaterial( gray ) );
		mesh.name = 'RXYZE';
		displayAxes['rotate'].add( mesh );

		mesh = new THREE.Line( Circle( 1.1, 'z' ), LineMaterial( [1,1,0,1] ) );
		mesh.name = 'RE';
		displayAxes['rotate'].add( mesh );

288 289 290
		geometry = new THREE.TorusGeometry( 1, 0.05, 4, 6, Math.PI );

		mesh = new THREE.Mesh( geometry, HandleMaterial( cyan ) );
A
Aleksandar Rodic 已提交
291
		mesh.rotation.z = -Math.PI/2;
A
Aleksandar Rodic 已提交
292 293 294 295 296
		mesh.rotation.y = -Math.PI/2;
		bakeTransformations( mesh );
		mesh.name = 'RX';
		pickerAxes['rotate'].add( mesh );

297 298
		mesh = new THREE.Mesh( geometry, HandleMaterial( magenta ) );
		mesh.rotation.z = Math.PI;
A
Aleksandar Rodic 已提交
299 300 301 302 303
		mesh.rotation.x = -Math.PI/2;
		bakeTransformations( mesh );
		mesh.name = 'RY';
		pickerAxes['rotate'].add( mesh );

304
		mesh = new THREE.Mesh( geometry, HandleMaterial( yellow ) );
A
Aleksandar Rodic 已提交
305 306
		mesh.rotation.z = -Math.PI/2;
		bakeTransformations( mesh );
A
Aleksandar Rodic 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
		mesh.name = 'RZ';
		pickerAxes['rotate'].add( mesh );

		mesh = new THREE.Mesh( new THREE.SphereGeometry( 0.95, 12, 12 ), HandleMaterial( white ) );
		mesh.name = 'RXYZ';
		pickerAxes['rotate'].add( mesh );

		mesh = new THREE.Mesh( new THREE.TorusGeometry( 1.12, 0.07, 4, 12 ), HandleMaterial( yellow ) );
		mesh.name = 'RE';
		pickerAxes['rotate'].add( mesh );

		mesh = null;

	}

	// intersection planes
	{
324

A
Aleksandar Rodic 已提交
325 326 327 328 329
		var planes = new THREE.Object3D();
		this.gizmo.add(planes);

		for ( var i in intersectionPlaneList ){

A
Aleksandar Rodic 已提交
330
			intersectionPlanes[intersectionPlaneList[i]] = new THREE.Mesh( new THREE.PlaneGeometry( 500, 500 ) );
A
Aleksandar Rodic 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
			intersectionPlanes[intersectionPlaneList[i]].material.side = THREE.DoubleSide;
			intersectionPlanes[intersectionPlaneList[i]].name = intersectionPlaneList[i];
			intersectionPlanes[intersectionPlaneList[i]].visible = false;
			planes.add(intersectionPlanes[intersectionPlaneList[i]]);

		}

		intersectionPlanes['YZ'].rotation.set( 0, Math.PI/2, 0 );
		intersectionPlanes['XZ'].rotation.set( -Math.PI/2, 0, 0 );
		bakeTransformations(intersectionPlanes['YZ']);
		bakeTransformations(intersectionPlanes['XZ']);

	}


346
	this.attatch = function ( object ) {
A
Aleksandar Rodic 已提交
347

348
		this.object = object;
A
Aleksandar Rodic 已提交
349
	 	this.updateMode();
350

A
Aleksandar Rodic 已提交
351 352
		this.domElement.addEventListener( 'mousedown', onMouseDown, false );
		document.addEventListener( 'keydown', onKeyDown, false );
A
Aleksandar Rodic 已提交
353

354
	}
A
Aleksandar Rodic 已提交
355

356
	this.detatch = function ( object ) {
A
Aleksandar Rodic 已提交
357 358 359 360 361

	 	this.hide();

		this.domElement.removeEventListener( 'mousedown', onMouseDown, false );
		document.removeEventListener( 'keydown', onKeyDown, false );
A
Aleksandar Rodic 已提交
362

363
	}
A
Aleksandar Rodic 已提交
364

365
	this.updateGizmo = function() {
A
Aleksandar Rodic 已提交
366

A
Aleksandar Rodic 已提交
367 368 369
		this.object.updateMatrixWorld();
		worldPosition.getPositionFromMatrix( this.object.matrixWorld );
		worldRotation.setEulerFromRotationMatrix( tempMatrix.extractRotation(this.object.matrixWorld ));
A
Aleksandar Rodic 已提交
370

A
Aleksandar Rodic 已提交
371 372 373
		this.camera.updateMatrixWorld();
		camPosition.getPositionFromMatrix( this.camera.matrixWorld );
		camRotation.setEulerFromRotationMatrix( tempMatrix.extractRotation( this.camera.matrixWorld ));
A
Aleksandar Rodic 已提交
374

A
Aleksandar Rodic 已提交
375 376 377
		scale = worldPosition.distanceTo( camPosition ) / 10 * this.scale;
		this.gizmo.position.copy( worldPosition )
		this.gizmo.scale.set( scale, scale, scale );
A
Aleksandar Rodic 已提交
378

A
Aleksandar Rodic 已提交
379
		for ( var i in this.gizmo.children ) {
380

A
Aleksandar Rodic 已提交
381
			for ( var j in this.gizmo.children[i].children ) {
A
Aleksandar Rodic 已提交
382 383 384 385 386

				object = this.gizmo.children[i].children[j];
				name = object.name;

				if ( name.search('E') != -1 ){
387

A
Aleksandar Rodic 已提交
388
					lookAtMatrix.lookAt( camPosition, worldPosition, tempVector.set( 0, 1, 0 ));
A
Aleksandar Rodic 已提交
389 390 391 392
					object.rotation.setEulerFromRotationMatrix( lookAtMatrix );

				} else {

A
Aleksandar Rodic 已提交
393
					eye.copy( camPosition ).sub( worldPosition ).normalize();
A
Aleksandar Rodic 已提交
394 395 396

					if ( this.space == 'local' ) {

A
Aleksandar Rodic 已提交
397
						tempQuaternion.setFromEuler( worldRotation );
A
Aleksandar Rodic 已提交
398

A
Aleksandar Rodic 已提交
399
						if ( name.search('R') != -1 ){
A
Aleksandar Rodic 已提交
400

A
Aleksandar Rodic 已提交
401 402
							tempMatrix.makeRotationFromQuaternion( tempQuaternion ).getInverse( tempMatrix );
							eye.applyProjection( tempMatrix );
A
Aleksandar Rodic 已提交
403

A
Aleksandar Rodic 已提交
404 405
							if ( name == 'RX' ) {
								quaternionX.setFromAxisAngle( unitX, Math.atan2( -eye.y, eye.z ) );
406
								tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
A
Aleksandar Rodic 已提交
407
							}
A
Aleksandar Rodic 已提交
408

A
Aleksandar Rodic 已提交
409 410
							if ( name == 'RY' ) {
								quaternionY.setFromAxisAngle( unitY, Math.atan2( eye.x, eye.z ) );
411
								tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
A
Aleksandar Rodic 已提交
412 413 414 415
							}

							if ( name == 'RZ' ) {
								quaternionZ.setFromAxisAngle( unitZ, Math.atan2( eye.y, eye.x ) );
416
								tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
A
Aleksandar Rodic 已提交
417
							}
418

A
Aleksandar Rodic 已提交
419
						}
420

A
Aleksandar Rodic 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
						object.rotation.setEulerFromQuaternion( tempQuaternion );

					} else if ( this.space == 'world' ) {

						object.rotation.set( 0, 0, 0 );

						if ( name == 'RX' ) {
							object.rotation.setX( Math.atan2( -eye.y, eye.z ) );
						}

						if ( name == 'RY' ) {
							object.rotation.setY( Math.atan2( eye.x, eye.z ) );
						}

						if ( name == 'RZ' ) {
							object.rotation.setZ( Math.atan2( eye.y, eye.x ) );
						}

					}

				}

			}
A
Aleksandar Rodic 已提交
444

A
Aleksandar Rodic 已提交
445 446
		}

A
Aleksandar Rodic 已提交
447
		this.callback();
A
Aleksandar Rodic 已提交
448

449 450 451
	}

	this.hide = function () {
A
Aleksandar Rodic 已提交
452

A
Aleksandar Rodic 已提交
453
	 	for ( var i in displayAxes ) {
A
Aleksandar Rodic 已提交
454

A
Aleksandar Rodic 已提交
455
		 	for ( var j in displayAxes[i].children ) {
A
Aleksandar Rodic 已提交
456 457 458 459 460 461 462

		 		displayAxes[i].children[j].visible = false;

		 	}

	 	}

A
Aleksandar Rodic 已提交
463
	 	for ( var i in pickerAxes ) {
A
Aleksandar Rodic 已提交
464

A
Aleksandar Rodic 已提交
465
		 	for ( var j in pickerAxes[i].children ) {
A
Aleksandar Rodic 已提交
466 467 468 469 470 471 472

		 		pickerAxes[i].children[j].visible = false;

		 	}

	 	}

473
	}
A
Aleksandar Rodic 已提交
474

475
	this.updateMode = function() {
A
Aleksandar Rodic 已提交
476

477
		this.hide();
A
Aleksandar Rodic 已提交
478

479
		if ( scope.mode == 'scale' ) scope.space = 'local';
A
Aleksandar Rodic 已提交
480

A
Aleksandar Rodic 已提交
481
	 	for ( var i in displayAxes[this.mode].children ) {
A
Aleksandar Rodic 已提交
482 483

 			displayAxes[this.mode].children[i].visible = true;
484

A
Aleksandar Rodic 已提交
485 486
	 	}

A
Aleksandar Rodic 已提交
487
	 	for ( var i in pickerAxes[this.mode].children ) {
A
Aleksandar Rodic 已提交
488 489

 			pickerAxes[this.mode].children[i].visible = showPickers;
490

A
Aleksandar Rodic 已提交
491 492 493 494
	 	}

	 	scope.updateGizmo();

495
	}
A
Aleksandar Rodic 已提交
496

497
	this.setIntersectionPlane = function () {
A
Aleksandar Rodic 已提交
498

499
		if ( isActive("X") || isActive("Y") ) {
A
Aleksandar Rodic 已提交
500

501
			currentPlane = 'XY';
A
Aleksandar Rodic 已提交
502

503
		}
A
Aleksandar Rodic 已提交
504

505
		if ( isActive("Z") ) {
A
Aleksandar Rodic 已提交
506

507
			currentPlane = 'YZ';
A
Aleksandar Rodic 已提交
508

509
		}
A
Aleksandar Rodic 已提交
510

511
		if ( isActive("XZ") ) {
A
Aleksandar Rodic 已提交
512

513
			currentPlane = 'XZ';
A
Aleksandar Rodic 已提交
514

515
		}
A
Aleksandar Rodic 已提交
516

517
		if ( isActive("XYZ") || isActive("E") ) {
A
Aleksandar Rodic 已提交
518

519
			currentPlane = 'XYZE';
A
Aleksandar Rodic 已提交
520

521
		}
A
Aleksandar Rodic 已提交
522

523
	 	if ( isActive("RX") ) {
A
Aleksandar Rodic 已提交
524

525
			currentPlane = 'YZ';
A
Aleksandar Rodic 已提交
526

527
		}
A
Aleksandar Rodic 已提交
528

529
		if ( isActive("RY") ) {
A
Aleksandar Rodic 已提交
530

531
			currentPlane = 'XZ';
A
Aleksandar Rodic 已提交
532

533
		} 
A
Aleksandar Rodic 已提交
534

535
		if ( isActive("RZ") ) {
A
Aleksandar Rodic 已提交
536

537
			currentPlane = 'XY';
A
Aleksandar Rodic 已提交
538

539
		}
A
Aleksandar Rodic 已提交
540

541
		scope.updateGizmo();
A
Aleksandar Rodic 已提交
542

543
	}
A
Aleksandar Rodic 已提交
544

545
	function onMouseDown( event ) {
A
Aleksandar Rodic 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558

		event.preventDefault();

		scope.domElement.focus();

		scope.updateGizmo();

		if ( event.button === 0 ) {

			scope.updateGizmo();

			intersect = intersectObjects( pickerAxes[scope.mode].children );

A
Aleksandar Rodic 已提交
559
			if ( intersect ) {
A
Aleksandar Rodic 已提交
560

A
Aleksandar Rodic 已提交
561
				scope.active = intersect.object.name;
A
Aleksandar Rodic 已提交
562 563 564 565 566

				scope.setIntersectionPlane();

				planeIntersect = intersectObjects( [intersectionPlanes[currentPlane]] );

A
Aleksandar Rodic 已提交
567 568
				if ( planeIntersect ) {

569 570
					oldPosition.copy( scope.object.position );
					oldScale.copy( scope.object.scale );
A
Aleksandar Rodic 已提交
571

572 573
					oldRotationMatrix.extractRotation( scope.object.matrix );
					worldRotationMatrix.extractRotation( scope.object.matrixWorld );
A
Aleksandar Rodic 已提交
574

575 576
					parentRotationMatrix.extractRotation( scope.object.parent.matrixWorld );
					parentScale.getScaleFromMatrix( tempMatrix.getInverse( scope.object.parent.matrixWorld ) );
A
Aleksandar Rodic 已提交
577

A
Aleksandar Rodic 已提交
578 579 580
					offset.copy( planeIntersect.point );

				}
A
Aleksandar Rodic 已提交
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597

			}

		}

		document.addEventListener( 'mousemove', onMouseMove, false );
		document.addEventListener( 'mouseup', onMouseUp, false );
		document.addEventListener( 'mouseout', onMouseUp, false );

	};

	function onMouseMove( event ) {

		if ( scope.active ) {

			planeIntersect = intersectObjects( [intersectionPlanes[currentPlane]] );

A
Aleksandar Rodic 已提交
598
			if ( planeIntersect ) {
A
Aleksandar Rodic 已提交
599

A
Aleksandar Rodic 已提交
600
				point.copy( planeIntersect.point );
A
Aleksandar Rodic 已提交
601

A
Aleksandar Rodic 已提交
602
				if ( ( scope.mode == 'translate' ) && isActive("T") ) {
603

A
Aleksandar Rodic 已提交
604 605
					point.sub( offset );
					point.multiply(parentScale);
A
Aleksandar Rodic 已提交
606 607 608

					if ( scope.space == 'local' ) {

A
Aleksandar Rodic 已提交
609 610 611 612 613 614
						point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );

						if ( !(isActive("X")) || scope.modifierAxis.x != 1 ) point.x = 0;
						if ( !(isActive("Y")) || scope.modifierAxis.y != 1 ) point.y = 0;
						if ( !(isActive("Z")) || scope.modifierAxis.z != 1 ) point.z = 0;
						if ( isActive("XYZ") ) point.set( 0, 0, 0 );
A
Aleksandar Rodic 已提交
615

A
Aleksandar Rodic 已提交
616
						point.applyMatrix4( oldRotationMatrix );
A
Aleksandar Rodic 已提交
617 618

						scope.object.position.copy( oldPosition );
A
Aleksandar Rodic 已提交
619
						scope.object.position.add( point );
A
Aleksandar Rodic 已提交
620 621 622

					} 

A
Aleksandar Rodic 已提交
623
					if ( scope.space == 'world' || isActive("XYZ") ) {
A
Aleksandar Rodic 已提交
624

A
Aleksandar Rodic 已提交
625 626 627
						if ( !(isActive("X")) || scope.modifierAxis.x != 1 ) point.x = 0;
						if ( !(isActive("Y")) || scope.modifierAxis.y != 1 ) point.y = 0;
						if ( !(isActive("Z")) || scope.modifierAxis.z != 1 ) point.z = 0;
A
Aleksandar Rodic 已提交
628

A
Aleksandar Rodic 已提交
629
						point.applyMatrix4( tempMatrix.getInverse( parentRotationMatrix ) );
A
Aleksandar Rodic 已提交
630 631 632

						scope.object.position.copy( oldPosition );
						scope.object.position.add( point );
633

A
Aleksandar Rodic 已提交
634
						if ( scope.snapDist ) {
A
Aleksandar Rodic 已提交
635
							if ( isActive("X") ) scope.object.position.x = Math.round( scope.object.position.x / scope.snapDist ) * scope.snapDist;
636 637
							if ( isActive("Y") ) scope.object.position.y = Math.round( scope.object.position.y / scope.snapDist ) * scope.snapDist;
							if ( isActive("Z") ) scope.object.position.z = Math.round( scope.object.position.z / scope.snapDist ) * scope.snapDist;
A
Aleksandar Rodic 已提交
638 639 640 641
						}

					}

A
Aleksandar Rodic 已提交
642
				} else if ( ( scope.mode == 'scale') && isActive("S") ) {
A
Aleksandar Rodic 已提交
643

A
Aleksandar Rodic 已提交
644 645
					point.sub( offset );
					point.multiply(parentScale);
A
Aleksandar Rodic 已提交
646 647 648

					if ( scope.space == 'local' ) {

A
Aleksandar Rodic 已提交
649
						if ( isActive("XYZ")) {
650

A
Aleksandar Rodic 已提交
651
							scale = 1 + ( ( point.y ) / 50 );
A
Aleksandar Rodic 已提交
652 653 654 655 656 657

							scope.object.scale.x = oldScale.x * scale;
							scope.object.scale.y = oldScale.y * scale;
							scope.object.scale.z = oldScale.z * scale;

						} else {
658

A
Aleksandar Rodic 已提交
659
							point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
A
Aleksandar Rodic 已提交
660

A
Aleksandar Rodic 已提交
661 662 663 664 665 666 667
							if ( !(isActive("X")) || scope.modifierAxis.x != 1 ) point.x = 0;
							if ( !(isActive("Y")) || scope.modifierAxis.y != 1 ) point.y = 0;
							if ( !(isActive("Z")) || scope.modifierAxis.z != 1 ) point.z = 0;

							if ( isActive("X") ) scope.object.scale.x = oldScale.x * ( 1 + point.x / 50 );
							if ( isActive("Y") ) scope.object.scale.y = oldScale.y * ( 1 + point.y / 50 );
							if ( isActive("Z") ) scope.object.scale.z = oldScale.z * ( 1 + point.z / 50 );
A
Aleksandar Rodic 已提交
668 669 670

						}

A
Aleksandar Rodic 已提交
671
					}
A
Aleksandar Rodic 已提交
672

A
Aleksandar Rodic 已提交
673
				} else if ( ( scope.mode == 'rotate' ) && isActive("R") ) {
A
Aleksandar Rodic 已提交
674

A
Aleksandar Rodic 已提交
675 676 677 678
					point.sub( worldPosition );
					point.multiply(parentScale);
					tempVector.copy(offset).sub( worldPosition );
					tempVector.multiply(parentScale);
A
Aleksandar Rodic 已提交
679

A
Aleksandar Rodic 已提交
680
					if ( scope.active == "RE" ) {
A
Aleksandar Rodic 已提交
681

A
Aleksandar Rodic 已提交
682 683
						point.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
						tempVector.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
A
Aleksandar Rodic 已提交
684

A
Aleksandar Rodic 已提交
685 686
						rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
						offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
A
Aleksandar Rodic 已提交
687

A
Aleksandar Rodic 已提交
688
						tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
A
Aleksandar Rodic 已提交
689

A
Aleksandar Rodic 已提交
690 691 692 693 694
						quaternionE.setFromAxisAngle( eye, rotation.z - offsetRotation.z );
						quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );

						tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionE );
						tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
695

A
Aleksandar Rodic 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
						scope.object.rotation.setEulerFromQuaternion( tempQuaternion );

					} else if ( scope.active == "RXYZ" ) {

						// TODO

					} else if ( scope.space == 'local' ) {

						point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );

						tempVector.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );

						rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
						offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );

						quaternionXYZ.setFromRotationMatrix( oldRotationMatrix );
A
Aleksandar Rodic 已提交
712 713 714 715
						quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
						quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
						quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );

A
Aleksandar Rodic 已提交
716 717 718
						if ( scope.active == "RX" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionX );
						if ( scope.active == "RY" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionY );
						if ( scope.active == "RZ" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionZ );
719

A
Aleksandar Rodic 已提交
720 721
						scope.object.rotation.setEulerFromQuaternion( quaternionXYZ );

A
Aleksandar Rodic 已提交
722
					} else if ( scope.space == 'world' ) {
A
Aleksandar Rodic 已提交
723 724

						rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
A
Aleksandar Rodic 已提交
725 726 727
						offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );

						tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
A
Aleksandar Rodic 已提交
728 729 730 731

						quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
						quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
						quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
A
Aleksandar Rodic 已提交
732
						quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
A
Aleksandar Rodic 已提交
733

A
Aleksandar Rodic 已提交
734 735 736
						if ( scope.active == "RX" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
						if ( scope.active == "RY" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
						if ( scope.active == "RZ" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
A
Aleksandar Rodic 已提交
737

A
Aleksandar Rodic 已提交
738
						tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
739

A
Aleksandar Rodic 已提交
740
						scope.object.rotation.setEulerFromQuaternion( tempQuaternion );
A
Aleksandar Rodic 已提交
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759

					}

				}

			}

		}

		scope.updateGizmo();

	}

	function onMouseUp( event ) {

		scope.active = false;

		document.removeEventListener( 'mousemove', onMouseMove, false );
		document.removeEventListener( 'mouseup', onMouseUp, false );
A
Aleksandar Rodic 已提交
760
		document.removeEventListener( 'mouseout', onMouseUp, false );
A
Aleksandar Rodic 已提交
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775

	}

	function onKeyDown( event ) {

 
		if ( event.keyCode == 87 ) { // W

			if ( scope.mode == 'translate' ) scope.space = ( scope.space == 'world' ) ? 'local' : 'world';
			scope.mode = 'translate';

		}

		if ( event.keyCode == 69 ) { // E

776
			if ( scope.mode == 'rotate' ) scope.space = ( scope.space == 'world' ) ? 'local' : 'world';
A
Aleksandar Rodic 已提交
777 778 779 780 781
			scope.mode = 'rotate';

		}

		if ( event.keyCode == 82 ) { // R
782

A
Aleksandar Rodic 已提交
783 784 785 786 787
			scope.mode = 'scale';
			scope.space = 'local';

		}

A
Aleksandar Rodic 已提交
788
		if ( event.keyCode == 187 || event.keyCode == 107 ) { // +,=,num+
789

A
Aleksandar Rodic 已提交
790 791 792 793 794
			scope.scale += 0.1

		}

		if ( event.keyCode == 189 || event.keyCode == 109) { // -,_,num-
795

A
Aleksandar Rodic 已提交
796 797 798 799 800
			scope.scale -= 0.1
			scope.scale = Math.max( scope.scale, 0.1 );

		}

A
Aleksandar Rodic 已提交
801 802 803 804 805 806 807 808 809 810 811 812 813 814
		scope.updateMode();

	}

	function intersectObjects( objects ) {

		pointerVector.set(
			( event.layerX / scope.domElement.offsetWidth ) * 2 - 1,
			- ( event.layerY / scope.domElement.offsetHeight ) * 2 + 1,
			0.5
		);

		projector.unprojectVector( pointerVector, scope.camera );
		ray.set( camPosition, pointerVector.sub( camPosition ).normalize() );
815

A
Aleksandar Rodic 已提交
816 817
		var intersections = ray.intersectObjects( objects, true );
		return intersections[0] ? intersections[0] : false;
818

A
Aleksandar Rodic 已提交
819 820
	}

A
Aleksandar Rodic 已提交
821 822 823
	function isActive( name ) {
		if ( scope.active.search( name ) != -1 ) return true;
		else return false;
A
Aleksandar Rodic 已提交
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
	}

	function bakeTransformations( object ) {

		var tempGeometry = new THREE.Geometry();
		THREE.GeometryUtils.merge( tempGeometry, object );
		object.setGeometry( tempGeometry );
		object.position.set( 0, 0, 0 );
		object.rotation.set( 0, 0, 0 );
		object.scale.set( 1, 1, 1 );

	}

};

839
THREE.TransformControls.prototype = Object.create( THREE.EventDispatcher.prototype );