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

7
THREE.TransformControls = function ( camera, domElement ) {
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 15 16
	// TODO: ADD RXYZ contol

	this.camera = camera;
	this.domElement = ( domElement !== undefined ) ? domElement : document;

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

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

A
Aleksandar Rodic 已提交
25 26
	var scope = this;

27 28
	var changeEvent = { type: 'change' };

A
Aleksandar Rodic 已提交
29 30 31 32 33 34 35
	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 已提交
36 37
	var offset = new THREE.Vector3();

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

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

A
Aleksandar Rodic 已提交
45 46 47 48 49 50 51 52 53 54 55
	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 已提交
56
	var quaternionE = new THREE.Quaternion();
A
Aleksandar Rodic 已提交
57 58 59

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

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

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

	var displayAxes = {};
	var pickerAxes = {};
	var intersectionPlanes = {};
A
Aleksandar Rodic 已提交
74
	var intersectionPlaneList = ['XY','YZ','XZ','XYZE']; // E
A
Aleksandar Rodic 已提交
75
	var currentPlane = 'XY';
A
Aleksandar Rodic 已提交
76
	var intersect, planeIntersect;
A
Aleksandar Rodic 已提交
77 78 79 80 81 82 83 84 85 86 87 88

	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"] );
89

A
Aleksandar Rodic 已提交
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
		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.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
		// Line axes

		var redColor = new THREE.Color( 0xff0000 );
		var greenColor = new THREE.Color( 0x00ff00 );
		var blueColor = new THREE.Color( 0x0000ff );

		geometry = new THREE.Geometry();
		geometry.vertices.push(
			new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 1, 0, 0 ),
			new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 1, 0 ),
			new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, 1 )
		);
		geometry.colors.push(
			redColor, redColor, greenColor, greenColor, blueColor, blueColor
		);
		material = new THREE.LineBasicMaterial( {
			vertexColors: THREE.VertexColors,
			depthTest: false,
			depthWrite: false,
			transparent: true
		} );
		mesh = new THREE.Line( geometry, material, THREE.LinePieces );
A
Aleksandar Rodic 已提交
152 153 154 155 156 157 158 159 160 161
		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() );

162 163 164
		geometry = new THREE.PlaneGeometry( 0.2, 0.2 );

		mesh = new THREE.Mesh( geometry, HandleMaterial( yellow ) );
A
Aleksandar Rodic 已提交
165 166 167 168 169 170
		mesh.position.set( 0.1, 0.1, 0 );
		bakeTransformations( mesh );
		mesh.name = 'TXY';
		displayAxes['translate'].add( mesh );
		pickerAxes['translate'].add( mesh.clone() );

171
		mesh = new THREE.Mesh( geometry, HandleMaterial( cyan ) );
A
Aleksandar Rodic 已提交
172 173 174 175 176 177
		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() );
178 179

		mesh = new THREE.Mesh( geometry, HandleMaterial( magenta ) );
A
Aleksandar Rodic 已提交
180 181 182 183 184 185 186
		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() );

187 188 189
		geometry = new THREE.CylinderGeometry( 0, 0.05, 0.2, 4, 1, true );

		mesh = new THREE.Mesh( geometry, HandleMaterial( red ) );
A
Aleksandar Rodic 已提交
190 191 192 193 194 195
		mesh.position.x = 0.9;
		mesh.rotation.z = -Math.PI/2;
		bakeTransformations( mesh );
		mesh.name = 'TX';
		displayAxes['translate'].add( mesh );

196
		mesh = new THREE.Mesh( geometry, HandleMaterial( green ) );
A
Aleksandar Rodic 已提交
197 198 199 200 201
		mesh.position.y = 0.9;
		bakeTransformations( mesh );
		mesh.name = 'TY';
		displayAxes['translate'].add( mesh );

202
		mesh = new THREE.Mesh( geometry, HandleMaterial( blue ) );
A
Aleksandar Rodic 已提交
203 204 205 206 207 208
		mesh.position.z = 0.9;
		mesh.rotation.x = Math.PI/2;
		bakeTransformations( mesh );
		mesh.name = 'TZ';
		displayAxes['translate'].add( mesh );

209
		geometry = new THREE.CylinderGeometry( 0.1, 0.1, 0.8, 4, 1, false );
210 211

		mesh = new THREE.Mesh( geometry, HandleMaterial( red ) );
A
Aleksandar Rodic 已提交
212 213 214 215 216 217
		mesh.position.x = 0.6;
		mesh.rotation.z = -Math.PI/2;
		bakeTransformations( mesh );
		mesh.name = 'TX';
		pickerAxes['translate'].add( mesh );

218
		mesh = new THREE.Mesh( geometry, HandleMaterial( green ) );
A
Aleksandar Rodic 已提交
219 220 221 222 223
		mesh.position.y = 0.6;
		bakeTransformations( mesh );
		mesh.name = 'TY';
		pickerAxes['translate'].add( mesh );

224
		mesh = new THREE.Mesh( geometry, HandleMaterial( blue ) );
A
Aleksandar Rodic 已提交
225 226 227 228 229 230 231 232
		mesh.position.z = 0.6;
		mesh.rotation.x = Math.PI/2;
		bakeTransformations( mesh );
		mesh.name = 'TZ';
		pickerAxes['translate'].add( mesh );

		// scale manipulators

233 234 235
		geometry = new THREE.CubeGeometry( 0.1, 0.1, 0.1 );

		mesh = new THREE.Mesh( geometry, HandleMaterial( white ) );
A
Aleksandar Rodic 已提交
236 237 238 239
		mesh.name = 'SXYZ';
		displayAxes['scale'].add( mesh );
		pickerAxes['scale'].add( mesh.clone() );

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

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

254
		mesh = new THREE.Mesh( geometry, HandleMaterial( blue ) );
A
Aleksandar Rodic 已提交
255 256 257 258 259 260 261 262
		mesh.position.set( 0, 0, 1 );
		bakeTransformations( mesh );
		mesh.name = 'SZ';
		displayAxes['scale'].add( mesh );
		pickerAxes['scale'].add( mesh.clone() );

		// rotate manipulators

263 264 265
		var Circle = function ( radius, facing, arc ) {

			geometry = new THREE.Geometry();
A
Aleksandar Rodic 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
			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 );

296 297 298
		geometry = new THREE.TorusGeometry( 1, 0.05, 4, 6, Math.PI );

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

305 306
		mesh = new THREE.Mesh( geometry, HandleMaterial( magenta ) );
		mesh.rotation.z = Math.PI;
A
Aleksandar Rodic 已提交
307 308 309 310 311
		mesh.rotation.x = -Math.PI/2;
		bakeTransformations( mesh );
		mesh.name = 'RY';
		pickerAxes['rotate'].add( mesh );

312
		mesh = new THREE.Mesh( geometry, HandleMaterial( yellow ) );
A
Aleksandar Rodic 已提交
313 314
		mesh.rotation.z = -Math.PI/2;
		bakeTransformations( mesh );
A
Aleksandar Rodic 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
		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
	{
332

A
Aleksandar Rodic 已提交
333 334 335 336 337
		var planes = new THREE.Object3D();
		this.gizmo.add(planes);

		for ( var i in intersectionPlaneList ){

A
Aleksandar Rodic 已提交
338
			intersectionPlanes[intersectionPlaneList[i]] = new THREE.Mesh( new THREE.PlaneGeometry( 500, 500 ) );
A
Aleksandar Rodic 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352
			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']);

	}

353
	this.attatch = function ( object ) {
A
Aleksandar Rodic 已提交
354

355
		this.object = object;
356
	 	this.setMode( scope.mode );
357

A
Aleksandar Rodic 已提交
358 359
		this.domElement.addEventListener( 'mousedown', onMouseDown, false );
		document.addEventListener( 'keydown', onKeyDown, false );
A
Aleksandar Rodic 已提交
360

361
	}
A
Aleksandar Rodic 已提交
362

363
	this.detatch = function ( object ) {
A
Aleksandar Rodic 已提交
364 365 366 367 368

	 	this.hide();

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

370
	}
A
Aleksandar Rodic 已提交
371

372
	this.update = function () {
A
Aleksandar Rodic 已提交
373

A
Aleksandar Rodic 已提交
374 375 376
		this.object.updateMatrixWorld();
		worldPosition.getPositionFromMatrix( this.object.matrixWorld );
		worldRotation.setEulerFromRotationMatrix( tempMatrix.extractRotation(this.object.matrixWorld ));
A
Aleksandar Rodic 已提交
377

A
Aleksandar Rodic 已提交
378 379 380
		this.camera.updateMatrixWorld();
		camPosition.getPositionFromMatrix( this.camera.matrixWorld );
		camRotation.setEulerFromRotationMatrix( tempMatrix.extractRotation( this.camera.matrixWorld ));
A
Aleksandar Rodic 已提交
381

A
Aleksandar Rodic 已提交
382 383 384
		scale = worldPosition.distanceTo( camPosition ) / 10 * this.scale;
		this.gizmo.position.copy( worldPosition )
		this.gizmo.scale.set( scale, scale, scale );
A
Aleksandar Rodic 已提交
385

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

A
Aleksandar Rodic 已提交
388
			for ( var j in this.gizmo.children[i].children ) {
A
Aleksandar Rodic 已提交
389 390 391 392 393

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

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

A
Aleksandar Rodic 已提交
395
					lookAtMatrix.lookAt( camPosition, worldPosition, tempVector.set( 0, 1, 0 ));
A
Aleksandar Rodic 已提交
396 397 398 399
					object.rotation.setEulerFromRotationMatrix( lookAtMatrix );

				} else {

A
Aleksandar Rodic 已提交
400
					eye.copy( camPosition ).sub( worldPosition ).normalize();
A
Aleksandar Rodic 已提交
401 402 403

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

A
Aleksandar Rodic 已提交
404
						tempQuaternion.setFromEuler( worldRotation );
A
Aleksandar Rodic 已提交
405

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

A
Aleksandar Rodic 已提交
408 409
							tempMatrix.makeRotationFromQuaternion( tempQuaternion ).getInverse( tempMatrix );
							eye.applyProjection( tempMatrix );
A
Aleksandar Rodic 已提交
410

A
Aleksandar Rodic 已提交
411 412
							if ( name == 'RX' ) {
								quaternionX.setFromAxisAngle( unitX, Math.atan2( -eye.y, eye.z ) );
413
								tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
A
Aleksandar Rodic 已提交
414
							}
A
Aleksandar Rodic 已提交
415

A
Aleksandar Rodic 已提交
416 417
							if ( name == 'RY' ) {
								quaternionY.setFromAxisAngle( unitY, Math.atan2( eye.x, eye.z ) );
418
								tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
A
Aleksandar Rodic 已提交
419 420 421 422
							}

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

A
Aleksandar Rodic 已提交
426
						}
427

A
Aleksandar Rodic 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
						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 已提交
451

A
Aleksandar Rodic 已提交
452 453
		}

454 455 456
	}

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

A
Aleksandar Rodic 已提交
458
	 	for ( var i in displayAxes ) {
A
Aleksandar Rodic 已提交
459

A
Aleksandar Rodic 已提交
460
		 	for ( var j in displayAxes[i].children ) {
A
Aleksandar Rodic 已提交
461 462 463 464 465 466 467

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

		 	}

	 	}

A
Aleksandar Rodic 已提交
468
	 	for ( var i in pickerAxes ) {
A
Aleksandar Rodic 已提交
469

A
Aleksandar Rodic 已提交
470
		 	for ( var j in pickerAxes[i].children ) {
A
Aleksandar Rodic 已提交
471 472 473 474 475 476 477

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

		 	}

	 	}

478
	}
A
Aleksandar Rodic 已提交
479

480 481 482
	this.setMode = function ( value ) {

		scope.mode = value;
A
Aleksandar Rodic 已提交
483

484
		this.hide();
A
Aleksandar Rodic 已提交
485

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

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

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

A
Aleksandar Rodic 已提交
492 493
	 	}

A
Aleksandar Rodic 已提交
494
	 	for ( var i in pickerAxes[this.mode].children ) {
A
Aleksandar Rodic 已提交
495 496

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

A
Aleksandar Rodic 已提交
498 499
	 	}

500
	 	scope.update();
A
Aleksandar Rodic 已提交
501

502
	}
A
Aleksandar Rodic 已提交
503

504
	this.setIntersectionPlane = function () {
A
Aleksandar Rodic 已提交
505

506
		if ( isActive("X") || isActive("Y") ) {
A
Aleksandar Rodic 已提交
507

508
			currentPlane = 'XY';
A
Aleksandar Rodic 已提交
509

510
		}
A
Aleksandar Rodic 已提交
511

512
		if ( isActive("Z") ) {
A
Aleksandar Rodic 已提交
513

514
			currentPlane = 'YZ';
A
Aleksandar Rodic 已提交
515

516
		}
A
Aleksandar Rodic 已提交
517

518
		if ( isActive("XZ") ) {
A
Aleksandar Rodic 已提交
519

520
			currentPlane = 'XZ';
A
Aleksandar Rodic 已提交
521

522
		}
A
Aleksandar Rodic 已提交
523

524
		if ( isActive("XYZ") || isActive("E") ) {
A
Aleksandar Rodic 已提交
525

526
			currentPlane = 'XYZE';
A
Aleksandar Rodic 已提交
527

528
		}
A
Aleksandar Rodic 已提交
529

530
	 	if ( isActive("RX") ) {
A
Aleksandar Rodic 已提交
531

532
			currentPlane = 'YZ';
A
Aleksandar Rodic 已提交
533

534
		}
A
Aleksandar Rodic 已提交
535

536
		if ( isActive("RY") ) {
A
Aleksandar Rodic 已提交
537

538
			currentPlane = 'XZ';
A
Aleksandar Rodic 已提交
539

540
		} 
A
Aleksandar Rodic 已提交
541

542
		if ( isActive("RZ") ) {
A
Aleksandar Rodic 已提交
543

544
			currentPlane = 'XY';
A
Aleksandar Rodic 已提交
545

546
		}
A
Aleksandar Rodic 已提交
547

548
		scope.update();
A
Aleksandar Rodic 已提交
549

550
	}
A
Aleksandar Rodic 已提交
551

552
	function onMouseDown( event ) {
A
Aleksandar Rodic 已提交
553 554 555 556 557 558 559

		event.preventDefault();

		scope.domElement.focus();

		if ( event.button === 0 ) {

M
Mr.doob 已提交
560
			intersect = intersectObjects( event, pickerAxes[scope.mode].children );
A
Aleksandar Rodic 已提交
561

A
Aleksandar Rodic 已提交
562
			if ( intersect ) {
A
Aleksandar Rodic 已提交
563

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

				scope.setIntersectionPlane();

M
Mr.doob 已提交
568
				planeIntersect = intersectObjects( event, [intersectionPlanes[currentPlane]] );
A
Aleksandar Rodic 已提交
569

A
Aleksandar Rodic 已提交
570 571
				if ( planeIntersect ) {

572 573
					oldPosition.copy( scope.object.position );
					oldScale.copy( scope.object.scale );
A
Aleksandar Rodic 已提交
574

575 576
					oldRotationMatrix.extractRotation( scope.object.matrix );
					worldRotationMatrix.extractRotation( scope.object.matrixWorld );
A
Aleksandar Rodic 已提交
577

578 579
					parentRotationMatrix.extractRotation( scope.object.parent.matrixWorld );
					parentScale.getScaleFromMatrix( tempMatrix.getInverse( scope.object.parent.matrixWorld ) );
A
Aleksandar Rodic 已提交
580

A
Aleksandar Rodic 已提交
581 582 583
					offset.copy( planeIntersect.point );

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

			}

		}

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

	};

	function onMouseMove( event ) {

		if ( scope.active ) {

M
Mr.doob 已提交
598
			planeIntersect = intersectObjects( event, [intersectionPlanes[currentPlane]] );
A
Aleksandar Rodic 已提交
599

A
Aleksandar Rodic 已提交
600
			if ( planeIntersect ) {
A
Aleksandar Rodic 已提交
601

A
Aleksandar Rodic 已提交
602
				point.copy( planeIntersect.point );
A
Aleksandar Rodic 已提交
603

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

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

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

A
Aleksandar Rodic 已提交
611 612 613 614 615 616
						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 已提交
617

A
Aleksandar Rodic 已提交
618
						point.applyMatrix4( oldRotationMatrix );
A
Aleksandar Rodic 已提交
619 620

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

					} 

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

A
Aleksandar Rodic 已提交
627 628 629
						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 已提交
630

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

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

A
Aleksandar Rodic 已提交
636
						if ( scope.snapDist ) {
A
Aleksandar Rodic 已提交
637
							if ( isActive("X") ) scope.object.position.x = Math.round( scope.object.position.x / scope.snapDist ) * scope.snapDist;
638 639
							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 已提交
640 641 642 643
						}

					}

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

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

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

A
Aleksandar Rodic 已提交
651
						if ( isActive("XYZ")) {
652

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

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

						} else {
660

A
Aleksandar Rodic 已提交
661
							point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
A
Aleksandar Rodic 已提交
662

A
Aleksandar Rodic 已提交
663 664 665 666 667 668 669
							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 已提交
670 671 672

						}

A
Aleksandar Rodic 已提交
673
					}
A
Aleksandar Rodic 已提交
674

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

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

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

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

A
Aleksandar Rodic 已提交
687 688
						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 已提交
689

A
Aleksandar Rodic 已提交
690
						tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
A
Aleksandar Rodic 已提交
691

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

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

A
Aleksandar Rodic 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
						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 已提交
714 715 716 717
						quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
						quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
						quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );

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

A
Aleksandar Rodic 已提交
722 723
						scope.object.rotation.setEulerFromQuaternion( quaternionXYZ );

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

						rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
A
Aleksandar Rodic 已提交
727 728 729
						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 已提交
730 731 732 733

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

A
Aleksandar Rodic 已提交
736 737 738
						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 已提交
739

A
Aleksandar Rodic 已提交
740
						tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
741

A
Aleksandar Rodic 已提交
742
						scope.object.rotation.setEulerFromQuaternion( tempQuaternion );
A
Aleksandar Rodic 已提交
743 744 745 746 747 748 749

					}

				}

			}

750
			scope.update();
751
			scope.dispatchEvent( changeEvent );
A
Aleksandar Rodic 已提交
752

753
		}
A
Aleksandar Rodic 已提交
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776

	}

	function onMouseUp( event ) {

		scope.active = false;

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

	}

	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

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

		}

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

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

		}

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

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

		}

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

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

		}

802
		scope.setMode( scope.mode );
803
		scope.dispatchEvent( changeEvent );
A
Aleksandar Rodic 已提交
804 805 806

	}

M
Mr.doob 已提交
807
	function intersectObjects( event, objects ) {
A
Aleksandar Rodic 已提交
808 809 810 811 812 813 814 815 816

		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() );
817

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

A
Aleksandar Rodic 已提交
821 822
	}

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

	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 );

	}

};

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