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

6
(function () {
A
Aleksandar Rodic 已提交
7

8
	'use strict';
A
Aleksandar Rodic 已提交
9

10
	var GizmoMaterial = function ( parameters ) {
11

12
		THREE.MeshBasicMaterial.call( this );
A
Aleksandar Rodic 已提交
13

14 15 16 17
		this.depthTest = false;
		this.depthWrite = false;
		this.side = THREE.FrontSide;
		this.transparent = true;
18

19
		this.setValues( parameters );
20

21 22
		this.oldColor = this.color.clone();
		this.oldOpacity = this.opacity;
A
Aleksandar Rodic 已提交
23

24
		this.highlight = function( highlighted ) {
A
Aleksandar Rodic 已提交
25

26
			if ( highlighted ) {
A
Aleksandar Rodic 已提交
27

28 29
				this.color.setRGB( 1, 1, 0 );
				this.opacity = 1;
A
Aleksandar Rodic 已提交
30

31
			} else {
32

33 34
					this.color.copy( this.oldColor );
					this.opacity = this.oldOpacity;
35 36 37

			}

38
		};
39

40
	};
41

42
	GizmoMaterial.prototype = Object.create( THREE.MeshBasicMaterial.prototype );
43
	GizmoMaterial.prototype.constructor = GizmoMaterial;
A
Aleksandar Rodic 已提交
44

45
	var GizmoLineMaterial = function ( parameters ) {
A
Aleksandar Rodic 已提交
46

47
		THREE.LineBasicMaterial.call( this );
A
Aleksandar Rodic 已提交
48

49 50 51 52
		this.depthTest = false;
		this.depthWrite = false;
		this.transparent = true;
		this.linewidth = 1;
A
Aleksandar Rodic 已提交
53

54
		this.setValues( parameters );
A
Aleksandar Rodic 已提交
55

56 57
		this.oldColor = this.color.clone();
		this.oldOpacity = this.opacity;
58

59
		this.highlight = function( highlighted ) {
60

61
			if ( highlighted ) {
62

63 64
				this.color.setRGB( 1, 1, 0 );
				this.opacity = 1;
65

66
			} else {
67

68 69
					this.color.copy( this.oldColor );
					this.opacity = this.oldOpacity;
A
Aleksandar Rodic 已提交
70

71
			}
72

73
		};
74

75
	};
76

77
	GizmoLineMaterial.prototype = Object.create( THREE.LineBasicMaterial.prototype );
78
	GizmoLineMaterial.prototype.constructor = GizmoLineMaterial;
79

80
	THREE.TransformGizmo = function () {
81

82 83 84
		var scope = this;
		var showPickers = false; //debug
		var showActivePlane = false; //debug
85

86
		this.init = function () {
A
Aleksandar Rodic 已提交
87

88
			THREE.Object3D.call( this );
89

90 91 92
			this.handles = new THREE.Object3D();
			this.pickers = new THREE.Object3D();
			this.planes = new THREE.Object3D();
93

94 95 96
			this.add(this.handles);
			this.add(this.pickers);
			this.add(this.planes);
97

98
			//// PLANES
99

100
			var planeGeometry = new THREE.PlaneBufferGeometry( 50, 50, 2, 2 );
101 102
			var planeMaterial = new THREE.MeshBasicMaterial( { wireframe: true } );
			planeMaterial.side = THREE.DoubleSide;
103

104 105 106 107 108 109
			var planes = {
				"XY":   new THREE.Mesh( planeGeometry, planeMaterial ),
				"YZ":   new THREE.Mesh( planeGeometry, planeMaterial ),
				"XZ":   new THREE.Mesh( planeGeometry, planeMaterial ),
				"XYZE": new THREE.Mesh( planeGeometry, planeMaterial )
			};
110

111
			this.activePlane = planes["XYZE"];
112

G
gero3 已提交
113 114
			planes["YZ"].rotation.set( 0, Math.PI / 2, 0 );
			planes["XZ"].rotation.set( -Math.PI / 2, 0, 0 );
115

116 117 118 119 120
			for (var i in planes) {
				planes[i].name = i;
				this.planes.add(planes[i]);
				this.planes[i] = planes[i];
				planes[i].visible = false;
A
Aleksandar Rodic 已提交
121 122
			}

123
			//// HANDLES AND PICKERS
A
Aleksandar Rodic 已提交
124

125
			var setupGizmos = function( gizmoMap, parent ) {
A
Aleksandar Rodic 已提交
126

127
				for ( var name in gizmoMap ) {
A
Aleksandar Rodic 已提交
128

G
gero3 已提交
129
					for ( i = gizmoMap[name].length; i --;) {
130

131 132 133
						var object = gizmoMap[name][i][0];
						var position = gizmoMap[name][i][1];
						var rotation = gizmoMap[name][i][2];
A
Aleksandar Rodic 已提交
134

135
						object.name = name;
A
Aleksandar Rodic 已提交
136

137 138
						if ( position ) object.position.set( position[0], position[1], position[2] );
						if ( rotation ) object.rotation.set( rotation[0], rotation[1], rotation[2] );
139

140
						parent.add( object );
A
Aleksandar Rodic 已提交
141

142
					}
A
Aleksandar Rodic 已提交
143

144
				}
A
Aleksandar Rodic 已提交
145

146
			};
A
Aleksandar Rodic 已提交
147

148 149
			setupGizmos(this.handleGizmos, this.handles);
			setupGizmos(this.pickerGizmos, this.pickers);
150

151
			// reset Transformations
A
Aleksandar Rodic 已提交
152

153
			this.traverse(function ( child ) {
M
Mr.doob 已提交
154 155 156
				if (child instanceof THREE.Mesh) {
					child.updateMatrix();

157 158
					var tempGeometry = child.geometry.clone();
					tempGeometry.applyMatrix( child.matrix );
159
					child.geometry = tempGeometry;
160

161 162 163 164 165
					child.position.set( 0, 0, 0 );
					child.rotation.set( 0, 0, 0 );
					child.scale.set( 1, 1, 1 );
				}
			});
A
Aleksandar Rodic 已提交
166

167
		};
A
Aleksandar Rodic 已提交
168

169 170 171 172 173
		this.hide = function () {
			this.traverse(function( child ) {
				child.visible = false;
			});
		};
A
Aleksandar Rodic 已提交
174

175 176 177 178 179 180 181 182
		this.show = function () {
			this.traverse(function( child ) {
				child.visible = true;
				if (child.parent == scope.pickers ) child.visible = showPickers;
				if (child.parent == scope.planes ) child.visible = false;
			});
			this.activePlane.visible = showActivePlane;
		};
183

184 185
		this.highlight = function ( axis ) {
			this.traverse(function( child ) {
G
gero3 已提交
186
				if ( child.material && child.material.highlight ) {
187 188 189 190 191 192 193 194
					if ( child.name == axis ) {
						child.material.highlight( true );
					} else {
						child.material.highlight( false );
					}
				}
			});
		};
A
Aleksandar Rodic 已提交
195

196
	};
197

198
	THREE.TransformGizmo.prototype = Object.create( THREE.Object3D.prototype );
199
	THREE.TransformGizmo.prototype.constructor = THREE.TransformGizmo;
200

201
	THREE.TransformGizmo.prototype.update = function ( rotation, eye ) {
202

203 204 205
		var vec1 = new THREE.Vector3( 0, 0, 0 );
		var vec2 = new THREE.Vector3( 0, 1, 0 );
		var lookAtMatrix = new THREE.Matrix4();
206

207 208 209 210 211
		this.traverse(function(child) {
			if ( child.name.search("E") != -1 ) {
				child.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( eye, vec1, vec2 ) );
			} else if ( child.name.search("X") != -1 || child.name.search("Y") != -1 || child.name.search("Z") != -1 ) {
				child.quaternion.setFromEuler( rotation );
212
			}
213
		});
214

215
	};
A
Aleksandar Rodic 已提交
216

217
	THREE.TransformGizmoTranslate = function () {
A
Aleksandar Rodic 已提交
218

219
		THREE.TransformGizmo.call( this );
220

221 222 223
		var arrowGeometry = new THREE.Geometry();
		var mesh = new THREE.Mesh( new THREE.CylinderGeometry( 0, 0.05, 0.2, 12, 1, false ) );
		mesh.position.y = 0.5;
224 225 226
		mesh.updateMatrix();

		arrowGeometry.merge( mesh.geometry, mesh.matrix );
227

228 229 230 231 232 233 234 235 236 237 238
		var lineXGeometry = new THREE.Geometry();
		lineXGeometry.vertices.push( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 1, 0, 0 ) );

		var lineYGeometry = new THREE.Geometry();
		lineYGeometry.vertices.push( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 1, 0 ) );

		var lineZGeometry = new THREE.Geometry();
		lineZGeometry.vertices.push( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, 1 ) );

		this.handleGizmos = {
			X: [
G
gero3 已提交
239
				[ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0xff0000 } ) ), [ 0.5, 0, 0 ], [ 0, 0, -Math.PI / 2 ] ],
240 241 242 243 244 245 246
				[ new THREE.Line( lineXGeometry, new GizmoLineMaterial( { color: 0xff0000 } ) ) ]
			],
			Y: [
				[ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0x00ff00 } ) ), [ 0, 0.5, 0 ] ],
				[	new THREE.Line( lineYGeometry, new GizmoLineMaterial( { color: 0x00ff00 } ) ) ]
			],
			Z: [
G
gero3 已提交
247
				[ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0x0000ff } ) ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ] ],
248 249 250 251 252 253
				[ new THREE.Line( lineZGeometry, new GizmoLineMaterial( { color: 0x0000ff } ) ) ]
			],
			XYZ: [
				[ new THREE.Mesh( new THREE.OctahedronGeometry( 0.1, 0 ), new GizmoMaterial( { color: 0xffffff, opacity: 0.25 } ) ), [ 0, 0, 0 ], [ 0, 0, 0 ] ]
			],
			XY: [
254
				[ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.29, 0.29 ), new GizmoMaterial( { color: 0xffff00, opacity: 0.25 } ) ), [ 0.15, 0.15, 0 ] ]
255 256
			],
			YZ: [
G
gero3 已提交
257
				[ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.29, 0.29 ), new GizmoMaterial( { color: 0x00ffff, opacity: 0.25 } ) ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ] ]
258 259
			],
			XZ: [
G
gero3 已提交
260
				[ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.29, 0.29 ), new GizmoMaterial( { color: 0xff00ff, opacity: 0.25 } ) ), [ 0.15, 0, 0.15 ], [ -Math.PI / 2, 0, 0 ] ]
261 262
			]
		};
263

264 265
		this.pickerGizmos = {
			X: [
G
gero3 已提交
266
				[ new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), new GizmoMaterial( { color: 0xff0000, opacity: 0.25 } ) ), [ 0.6, 0, 0 ], [ 0, 0, -Math.PI / 2 ] ]
267 268 269 270 271
			],
			Y: [
				[ new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), new GizmoMaterial( { color: 0x00ff00, opacity: 0.25 } ) ), [ 0, 0.6, 0 ] ]
			],
			Z: [
G
gero3 已提交
272
				[ new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), new GizmoMaterial( { color: 0x0000ff, opacity: 0.25 } ) ), [ 0, 0, 0.6 ], [ Math.PI / 2, 0, 0 ] ]
273 274 275 276 277
			],
			XYZ: [
				[ new THREE.Mesh( new THREE.OctahedronGeometry( 0.2, 0 ), new GizmoMaterial( { color: 0xffffff, opacity: 0.25 } ) ) ]
			],
			XY: [
278
				[ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.4, 0.4 ), new GizmoMaterial( { color: 0xffff00, opacity: 0.25 } ) ), [ 0.2, 0.2, 0 ] ]
279 280
			],
			YZ: [
G
gero3 已提交
281
				[ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.4, 0.4 ), new GizmoMaterial( { color: 0x00ffff, opacity: 0.25 } ) ), [ 0, 0.2, 0.2 ], [ 0, Math.PI / 2, 0 ] ]
282 283
			],
			XZ: [
G
gero3 已提交
284
				[ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.4, 0.4 ), new GizmoMaterial( { color: 0xff00ff, opacity: 0.25 } ) ), [ 0.2, 0, 0.2 ], [ -Math.PI / 2, 0, 0 ] ]
285 286
			]
		};
A
Aleksandar Rodic 已提交
287

288
		this.setActivePlane = function ( axis, eye ) {
289

290
			var tempMatrix = new THREE.Matrix4();
291
			eye.applyMatrix4( tempMatrix.getInverse( tempMatrix.extractRotation( this.planes[ "XY" ].matrixWorld ) ) );
292

293 294 295 296
			if ( axis == "X" ) {
				this.activePlane = this.planes[ "XY" ];
				if ( Math.abs(eye.y) > Math.abs(eye.z) ) this.activePlane = this.planes[ "XZ" ];
			}
297

G
gero3 已提交
298
			if ( axis == "Y" ) {
299 300 301
				this.activePlane = this.planes[ "XY" ];
				if ( Math.abs(eye.x) > Math.abs(eye.z) ) this.activePlane = this.planes[ "YZ" ];
			}
302

G
gero3 已提交
303
			if ( axis == "Z" ) {
304 305 306
				this.activePlane = this.planes[ "XZ" ];
				if ( Math.abs(eye.x) > Math.abs(eye.y) ) this.activePlane = this.planes[ "YZ" ];
			}
307

308
			if ( axis == "XYZ" ) this.activePlane = this.planes[ "XYZE" ];
A
Aleksandar Rodic 已提交
309

310
			if ( axis == "XY" ) this.activePlane = this.planes[ "XY" ];
A
Aleksandar Rodic 已提交
311

312
			if ( axis == "YZ" ) this.activePlane = this.planes[ "YZ" ];
A
Aleksandar Rodic 已提交
313

314
			if ( axis == "XZ" ) this.activePlane = this.planes[ "XZ" ];
315

316 317
			this.hide();
			this.show();
A
Aleksandar Rodic 已提交
318

319
		};
A
Aleksandar Rodic 已提交
320

321
		this.init();
A
Aleksandar Rodic 已提交
322

323
	};
A
Aleksandar Rodic 已提交
324

325
	THREE.TransformGizmoTranslate.prototype = Object.create( THREE.TransformGizmo.prototype );
326
	THREE.TransformGizmoTranslate.prototype.constructor = THREE.TransformGizmoTranslate;
A
Aleksandar Rodic 已提交
327

328
	THREE.TransformGizmoRotate = function () {
A
Aleksandar Rodic 已提交
329

330
		THREE.TransformGizmo.call( this );
331

332
		var CircleGeometry = function ( radius, facing, arc ) {
333

334 335
				var geometry = new THREE.Geometry();
				arc = arc ? arc : 1;
G
gero3 已提交
336
				for ( var i = 0; i <= 64 * arc; ++ i ) {
337 338 339 340
					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) );
				}
341

342 343
				return geometry;
		};
A
Aleksandar Rodic 已提交
344

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
		this.handleGizmos = {
			X: [
				[ new THREE.Line( new CircleGeometry(1,'x',0.5), new GizmoLineMaterial( { color: 0xff0000 } ) ) ]
			],
			Y: [
				[ new THREE.Line( new CircleGeometry(1,'y',0.5), new GizmoLineMaterial( { color: 0x00ff00 } ) ) ]
			],
			Z: [
				[ new THREE.Line( new CircleGeometry(1,'z',0.5), new GizmoLineMaterial( { color: 0x0000ff } ) ) ]
			],
			E: [
				[ new THREE.Line( new CircleGeometry(1.25,'z',1), new GizmoLineMaterial( { color: 0xcccc00 } ) ) ]
			],
			XYZE: [
				[ new THREE.Line( new CircleGeometry(1,'z',1), new GizmoLineMaterial( { color: 0x787878 } ) ) ]
			]
		};
362

363 364
		this.pickerGizmos = {
			X: [
G
gero3 已提交
365
				[ new THREE.Mesh( new THREE.TorusGeometry( 1, 0.12, 4, 12, Math.PI ), new GizmoMaterial( { color: 0xff0000, opacity: 0.25 } ) ), [ 0, 0, 0 ], [ 0, -Math.PI / 2, -Math.PI / 2 ] ]
366 367
			],
			Y: [
G
gero3 已提交
368
				[ new THREE.Mesh( new THREE.TorusGeometry( 1, 0.12, 4, 12, Math.PI ), new GizmoMaterial( { color: 0x00ff00, opacity: 0.25 } ) ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ] ]
369 370
			],
			Z: [
G
gero3 已提交
371
				[ new THREE.Mesh( new THREE.TorusGeometry( 1, 0.12, 4, 12, Math.PI ), new GizmoMaterial( { color: 0x0000ff, opacity: 0.25 } ) ), [ 0, 0, 0 ], [ 0, 0, -Math.PI / 2 ] ]
372 373 374 375 376 377 378 379
			],
			E: [
				[ new THREE.Mesh( new THREE.TorusGeometry( 1.25, 0.12, 2, 24 ), new GizmoMaterial( { color: 0xffff00, opacity: 0.25 } ) ) ]
			],
			XYZE: [
				[ new THREE.Mesh( new THREE.Geometry() ) ]// TODO
			]
		};
A
Aleksandar Rodic 已提交
380

381
		this.setActivePlane = function ( axis ) {
A
Aleksandar Rodic 已提交
382

383
			if ( axis == "E" ) this.activePlane = this.planes[ "XYZE" ];
384

385
			if ( axis == "X" ) this.activePlane = this.planes[ "YZ" ];
A
Aleksandar Rodic 已提交
386

387
			if ( axis == "Y" ) this.activePlane = this.planes[ "XZ" ];
A
Aleksandar Rodic 已提交
388

389
			if ( axis == "Z" ) this.activePlane = this.planes[ "XY" ];
A
Aleksandar Rodic 已提交
390

391 392
			this.hide();
			this.show();
A
Aleksandar Rodic 已提交
393

394
		};
A
Aleksandar Rodic 已提交
395

396
		this.update = function ( rotation, eye2 ) {
A
Aleksandar Rodic 已提交
397

398
			THREE.TransformGizmo.prototype.update.apply( this, arguments );
A
Aleksandar Rodic 已提交
399

400 401 402 403
			var group = {
				handles: this["handles"],
				pickers: this["pickers"],
			};
A
Aleksandar Rodic 已提交
404

405 406 407 408 409 410 411 412 413 414
			var tempMatrix = new THREE.Matrix4();
			var worldRotation = new THREE.Euler( 0, 0, 1 );
			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 quaternionX = new THREE.Quaternion();
			var quaternionY = new THREE.Quaternion();
			var quaternionZ = new THREE.Quaternion();
			var eye = eye2.clone();
A
Aleksandar Rodic 已提交
415

416 417
			worldRotation.copy( this.planes["XY"].rotation );
			tempQuaternion.setFromEuler( worldRotation );
418

419
			tempMatrix.makeRotationFromQuaternion( tempQuaternion ).getInverse( tempMatrix );
420
			eye.applyMatrix4( tempMatrix );
421

422
			this.traverse(function(child) {
A
Aleksandar Rodic 已提交
423

424
				tempQuaternion.setFromEuler( worldRotation );
A
Aleksandar Rodic 已提交
425

426
				if ( child.name == "X" ) {
427 428
					quaternionX.setFromAxisAngle( unitX, Math.atan2( -eye.y, eye.z ) );
					tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
429
					child.quaternion.copy( tempQuaternion );
A
Aleksandar Rodic 已提交
430 431
				}

432
				if ( child.name == "Y" ) {
433 434
					quaternionY.setFromAxisAngle( unitY, Math.atan2( eye.x, eye.z ) );
					tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
435
					child.quaternion.copy( tempQuaternion );
436 437
				}

438
				if ( child.name == "Z" ) {
439 440
					quaternionZ.setFromAxisAngle( unitZ, Math.atan2( eye.y, eye.x ) );
					tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
441
					child.quaternion.copy( tempQuaternion );
442
				}
A
Aleksandar Rodic 已提交
443

444
			});
A
Aleksandar Rodic 已提交
445

446
		};
447

448
		this.init();
A
Aleksandar Rodic 已提交
449

450
	};
A
Aleksandar Rodic 已提交
451

452
	THREE.TransformGizmoRotate.prototype = Object.create( THREE.TransformGizmo.prototype );
453
	THREE.TransformGizmoRotate.prototype.constructor = THREE.TransformGizmoRotate;
454 455 456 457 458 459 460 461

	THREE.TransformGizmoScale = function () {

		THREE.TransformGizmo.call( this );

		var arrowGeometry = new THREE.Geometry();
		var mesh = new THREE.Mesh( new THREE.BoxGeometry( 0.125, 0.125, 0.125 ) );
		mesh.position.y = 0.5;
462 463 464
		mesh.updateMatrix();

		arrowGeometry.merge( mesh.geometry, mesh.matrix );
465 466 467 468 469 470 471 472 473 474 475 476

		var lineXGeometry = new THREE.Geometry();
		lineXGeometry.vertices.push( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 1, 0, 0 ) );

		var lineYGeometry = new THREE.Geometry();
		lineYGeometry.vertices.push( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 1, 0 ) );

		var lineZGeometry = new THREE.Geometry();
		lineZGeometry.vertices.push( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, 1 ) );

		this.handleGizmos = {
			X: [
G
gero3 已提交
477
				[ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0xff0000 } ) ), [ 0.5, 0, 0 ], [ 0, 0, -Math.PI / 2 ] ],
478 479 480 481 482 483 484
				[ new THREE.Line( lineXGeometry, new GizmoLineMaterial( { color: 0xff0000 } ) ) ]
			],
			Y: [
				[ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0x00ff00 } ) ), [ 0, 0.5, 0 ] ],
				[ new THREE.Line( lineYGeometry, new GizmoLineMaterial( { color: 0x00ff00 } ) ) ]
			],
			Z: [
G
gero3 已提交
485
				[ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0x0000ff } ) ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ] ],
486 487 488 489 490 491
				[ new THREE.Line( lineZGeometry, new GizmoLineMaterial( { color: 0x0000ff } ) ) ]
			],
			XYZ: [
				[ new THREE.Mesh( new THREE.BoxGeometry( 0.125, 0.125, 0.125 ), new GizmoMaterial( { color: 0xffffff, opacity: 0.25 } ) ) ]
			]
		};
A
Aleksandar Rodic 已提交
492

493 494
		this.pickerGizmos = {
			X: [
G
gero3 已提交
495
				[ new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), new GizmoMaterial( { color: 0xff0000, opacity: 0.25 } ) ), [ 0.6, 0, 0 ], [ 0, 0, -Math.PI / 2 ] ]
496 497 498 499 500
			],
			Y: [
				[ new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), new GizmoMaterial( { color: 0x00ff00, opacity: 0.25 } ) ), [ 0, 0.6, 0 ] ]
			],
			Z: [
G
gero3 已提交
501
				[ new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), new GizmoMaterial( { color: 0x0000ff, opacity: 0.25 } ) ), [ 0, 0, 0.6 ], [ Math.PI / 2, 0, 0 ] ]
502 503 504 505 506
			],
			XYZ: [
				[ new THREE.Mesh( new THREE.BoxGeometry( 0.4, 0.4, 0.4 ), new GizmoMaterial( { color: 0xffffff, opacity: 0.25 } ) ) ]
			]
		};
507

508
		this.setActivePlane = function ( axis, eye ) {
A
Aleksandar Rodic 已提交
509

510
			var tempMatrix = new THREE.Matrix4();
511
			eye.applyMatrix4( tempMatrix.getInverse( tempMatrix.extractRotation( this.planes[ "XY" ].matrixWorld ) ) );
A
Aleksandar Rodic 已提交
512

513 514 515 516
			if ( axis == "X" ) {
				this.activePlane = this.planes[ "XY" ];
				if ( Math.abs(eye.y) > Math.abs(eye.z) ) this.activePlane = this.planes[ "XZ" ];
			}
A
Aleksandar Rodic 已提交
517

G
gero3 已提交
518
			if ( axis == "Y" ) {
519 520 521
				this.activePlane = this.planes[ "XY" ];
				if ( Math.abs(eye.x) > Math.abs(eye.z) ) this.activePlane = this.planes[ "YZ" ];
			}
A
Aleksandar Rodic 已提交
522

G
gero3 已提交
523
			if ( axis == "Z" ) {
524 525 526
				this.activePlane = this.planes[ "XZ" ];
				if ( Math.abs(eye.x) > Math.abs(eye.y) ) this.activePlane = this.planes[ "YZ" ];
			}
527

528
			if ( axis == "XYZ" ) this.activePlane = this.planes[ "XYZE" ];
529

530 531
			this.hide();
			this.show();
A
Aleksandar Rodic 已提交
532

533
		};
A
Aleksandar Rodic 已提交
534

535
		this.init();
536

537
	};
A
Aleksandar Rodic 已提交
538

539
	THREE.TransformGizmoScale.prototype = Object.create( THREE.TransformGizmo.prototype );
540
	THREE.TransformGizmoScale.prototype.constructor = THREE.TransformGizmoScale;
A
Aleksandar Rodic 已提交
541

542
	THREE.TransformControls = function ( camera, domElement ) {
A
Aleksandar Rodic 已提交
543

544 545
		// TODO: Make non-uniform scale and rotate play nice in hierarchies
		// TODO: ADD RXYZ contol
A
Aleksandar Rodic 已提交
546

547
		THREE.Object3D.call( this );
A
Aleksandar Rodic 已提交
548

549
		domElement = ( domElement !== undefined ) ? domElement : document;
550

551 552 553 554
		this.gizmo = {};
		this.gizmo["translate"] = new THREE.TransformGizmoTranslate();
		this.gizmo["rotate"] = new THREE.TransformGizmoRotate();
		this.gizmo["scale"] = new THREE.TransformGizmoScale();
555

556 557 558
		this.add(this.gizmo["translate"]);
		this.add(this.gizmo["rotate"]);
		this.add(this.gizmo["scale"]);
559

560 561 562
		this.gizmo["translate"].hide();
		this.gizmo["rotate"].hide();
		this.gizmo["scale"].hide();
563

564
		this.object = undefined;
565
		this.snap = null;
566 567
		this.space = "world";
		this.size = 1;
568
		this.axis = null;
569

570
		var scope = this;
571

572 573 574
		var _dragging = false;
		var _mode = "translate";
		var _plane = "XY";
575

576
		var changeEvent = { type: "change" };
577 578
		var mouseDownEvent = { type: "mouseDown" };
		var mouseUpEvent = { type: "mouseUp", mode: _mode };
579
		var objectChangeEvent = { type: "objectChange" };
A
Aleksandar Rodic 已提交
580

581 582
		var ray = new THREE.Raycaster();
		var pointerVector = new THREE.Vector3();
A
Aleksandar Rodic 已提交
583

584 585
		var point = new THREE.Vector3();
		var offset = new THREE.Vector3();
A
Aleksandar Rodic 已提交
586

587 588 589
		var rotation = new THREE.Vector3();
		var offsetRotation = new THREE.Vector3();
		var scale = 1;
590

591 592
		var lookAtMatrix = new THREE.Matrix4();
		var eye = new THREE.Vector3();
593

594 595 596 597 598 599
		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 );
600

601 602 603 604 605
		var quaternionXYZ = new THREE.Quaternion();
		var quaternionX = new THREE.Quaternion();
		var quaternionY = new THREE.Quaternion();
		var quaternionZ = new THREE.Quaternion();
		var quaternionE = new THREE.Quaternion();
606

607 608 609
		var oldPosition = new THREE.Vector3();
		var oldScale = new THREE.Vector3();
		var oldRotationMatrix = new THREE.Matrix4();
610

611 612
		var parentRotationMatrix  = new THREE.Matrix4();
		var parentScale = new THREE.Vector3();
613

614 615 616 617 618
		var worldPosition = new THREE.Vector3();
		var worldRotation = new THREE.Euler();
		var worldRotationMatrix  = new THREE.Matrix4();
		var camPosition = new THREE.Vector3();
		var camRotation = new THREE.Euler();
619

620 621
		domElement.addEventListener( "mousedown", onPointerDown, false );
		domElement.addEventListener( "touchstart", onPointerDown, false );
622

623 624
		domElement.addEventListener( "mousemove", onPointerHover, false );
		domElement.addEventListener( "touchmove", onPointerHover, false );
625

626 627
		domElement.addEventListener( "mousemove", onPointerMove, false );
		domElement.addEventListener( "touchmove", onPointerMove, false );
A
Aleksandar Rodic 已提交
628

629 630 631 632 633
		domElement.addEventListener( "mouseup", onPointerUp, false );
		domElement.addEventListener( "mouseout", onPointerUp, false );
		domElement.addEventListener( "touchend", onPointerUp, false );
		domElement.addEventListener( "touchcancel", onPointerUp, false );
		domElement.addEventListener( "touchleave", onPointerUp, false );
A
Aleksandar Rodic 已提交
634

635
		this.attach = function ( object ) {
A
Aleksandar Rodic 已提交
636

637
			scope.object = object;
A
Aleksandar Rodic 已提交
638

639 640 641 642
			this.gizmo["translate"].hide();
			this.gizmo["rotate"].hide();
			this.gizmo["scale"].hide();
			this.gizmo[_mode].show();
A
Aleksandar Rodic 已提交
643

644
			scope.update();
A
Aleksandar Rodic 已提交
645

646
		};
A
Aleksandar Rodic 已提交
647

648
		this.detach = function ( object ) {
A
Aleksandar Rodic 已提交
649

650
			scope.object = undefined;
651
			this.axis = null;
A
Aleksandar Rodic 已提交
652

653 654 655
			this.gizmo["translate"].hide();
			this.gizmo["rotate"].hide();
			this.gizmo["scale"].hide();
A
Aleksandar Rodic 已提交
656

657
		};
A
Aleksandar Rodic 已提交
658

659
		this.setMode = function ( mode ) {
A
Aleksandar Rodic 已提交
660

661
			_mode = mode ? mode : _mode;
A
Aleksandar Rodic 已提交
662

663
			if ( _mode == "scale" ) scope.space = "local";
A
Aleksandar Rodic 已提交
664

665 666
			this.gizmo["translate"].hide();
			this.gizmo["rotate"].hide();
667
			this.gizmo["scale"].hide();
668
			this.gizmo[_mode].show();
A
Aleksandar Rodic 已提交
669

670 671
			this.update();
			scope.dispatchEvent( changeEvent );
A
Aleksandar Rodic 已提交
672

673
		};
674

675
		this.setSnap = function ( snap ) {
676

677
			scope.snap = snap;
678

679
		};
A
Aleksandar Rodic 已提交
680

681
		this.setSize = function ( size ) {
682

683 684 685
			scope.size = size;
			this.update();
			scope.dispatchEvent( changeEvent );
686

687
		};
688

689
		this.setSpace = function ( space ) {
690

691 692 693
			scope.space = space;
			this.update();
			scope.dispatchEvent( changeEvent );
694

695
		};
696

697
		this.update = function () {
698

699
			if ( scope.object === undefined ) return;
700

701 702 703
			scope.object.updateMatrixWorld();
			worldPosition.setFromMatrixPosition( scope.object.matrixWorld );
			worldRotation.setFromRotationMatrix( tempMatrix.extractRotation( scope.object.matrixWorld ) );
704

705 706 707
			camera.updateMatrixWorld();
			camPosition.setFromMatrixPosition( camera.matrixWorld );
			camRotation.setFromRotationMatrix( tempMatrix.extractRotation( camera.matrixWorld ) );
708

709 710 711
			scale = worldPosition.distanceTo( camPosition ) / 6 * scope.size;
			this.position.copy( worldPosition );
			this.scale.set( scale, scale, scale );
712

713
			eye.copy( camPosition ).sub( worldPosition ).normalize();
714

715 716
			if ( scope.space == "local" )
				this.gizmo[_mode].update( worldRotation, eye );
717

718 719
			else if ( scope.space == "world" )
				this.gizmo[_mode].update( new THREE.Euler(), eye );
720

721
			this.gizmo[_mode].highlight( scope.axis );
722

723
		};
724

725
		function onPointerHover( event ) {
726

727
			if ( scope.object === undefined || _dragging === true ) return;
728

729
			event.preventDefault();
730

731
			var pointer = event.changedTouches ? event.changedTouches[ 0 ] : event;
732 733 734

			var intersect = intersectObjects( pointer, scope.gizmo[_mode].pickers.children );

735 736
			var axis = null;

737 738
			if ( intersect ) {

739
				axis = intersect.object.name;
740

741 742 743
			}

			if ( scope.axis !== axis ) {
744

745
				scope.axis = axis;
746 747 748 749
				scope.update();
				scope.dispatchEvent( changeEvent );

			}
750 751 752

		}

753
		function onPointerDown( event ) {
754

755
			if ( scope.object === undefined || _dragging === true ) return;
756

757 758
			event.preventDefault();
			event.stopPropagation();
A
Aleksandar Rodic 已提交
759

760
			var pointer = event.changedTouches ? event.changedTouches[ 0 ] : event;
761

762
			if ( pointer.button === 0 || pointer.button === undefined ) {
A
Aleksandar Rodic 已提交
763

764
				var intersect = intersectObjects( pointer, scope.gizmo[_mode].pickers.children );
A
Aleksandar Rodic 已提交
765

766
				if ( intersect ) {
A
Aleksandar Rodic 已提交
767

768
					scope.dispatchEvent( mouseDownEvent );
D
Daniel Taub 已提交
769

770
					scope.axis = intersect.object.name;
A
Aleksandar Rodic 已提交
771

772
					scope.update();
A
Aleksandar Rodic 已提交
773

774
					eye.copy( camPosition ).sub( worldPosition ).normalize();
A
Aleksandar Rodic 已提交
775

776
					scope.gizmo[_mode].setActivePlane( scope.axis, eye );
777

G
gero3 已提交
778
					var planeIntersect = intersectObjects( pointer, [ scope.gizmo[_mode].activePlane ] );
779

780 781
					oldPosition.copy( scope.object.position );
					oldScale.copy( scope.object.scale );
A
Aleksandar Rodic 已提交
782

783 784
					oldRotationMatrix.extractRotation( scope.object.matrix );
					worldRotationMatrix.extractRotation( scope.object.matrixWorld );
A
Aleksandar Rodic 已提交
785

786 787
					parentRotationMatrix.extractRotation( scope.object.parent.matrixWorld );
					parentScale.setFromMatrixScale( tempMatrix.getInverse( scope.object.parent.matrixWorld ) );
A
Aleksandar Rodic 已提交
788

789
					offset.copy( planeIntersect.point );
A
Aleksandar Rodic 已提交
790

791
				}
A
Aleksandar Rodic 已提交
792 793 794

			}

795 796
			_dragging = true;

A
Aleksandar Rodic 已提交
797 798
		}

799
		function onPointerMove( event ) {
A
Aleksandar Rodic 已提交
800

801
			if ( scope.object === undefined || scope.axis === null || _dragging === false ) return;
A
Aleksandar Rodic 已提交
802

803 804
			event.preventDefault();
			event.stopPropagation();
A
Aleksandar Rodic 已提交
805

G
gero3 已提交
806
			var pointer = event.changedTouches ? event.changedTouches[0] : event;
A
Aleksandar Rodic 已提交
807

G
gero3 已提交
808
			var planeIntersect = intersectObjects( pointer, [ scope.gizmo[_mode].activePlane ] );
A
Aleksandar Rodic 已提交
809

810
			point.copy( planeIntersect.point );
A
Aleksandar Rodic 已提交
811

812
			if ( _mode == "translate" ) {
A
Aleksandar Rodic 已提交
813

814 815
				point.sub( offset );
				point.multiply(parentScale);
A
Aleksandar Rodic 已提交
816

817
				if ( scope.space == "local" ) {
A
Aleksandar Rodic 已提交
818

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

821 822 823
					if ( scope.axis.search("X") == -1 ) point.x = 0;
					if ( scope.axis.search("Y") == -1 ) point.y = 0;
					if ( scope.axis.search("Z") == -1 ) point.z = 0;
A
Aleksandar Rodic 已提交
824

825
					point.applyMatrix4( oldRotationMatrix );
A
Aleksandar Rodic 已提交
826

827 828
					scope.object.position.copy( oldPosition );
					scope.object.position.add( point );
A
Aleksandar Rodic 已提交
829

D
Daniel Taub 已提交
830
				}
A
Aleksandar Rodic 已提交
831

832
				if ( scope.space == "world" || scope.axis.search("XYZ") != -1 ) {
A
Aleksandar Rodic 已提交
833

834 835 836
					if ( scope.axis.search("X") == -1 ) point.x = 0;
					if ( scope.axis.search("Y") == -1 ) point.y = 0;
					if ( scope.axis.search("Z") == -1 ) point.z = 0;
A
Aleksandar Rodic 已提交
837

838
					point.applyMatrix4( tempMatrix.getInverse( parentRotationMatrix ) );
A
Aleksandar Rodic 已提交
839

840 841
					scope.object.position.copy( oldPosition );
					scope.object.position.add( point );
842

843
				}
844

845
				if ( scope.snap !== null ) {
846

847 848 849
					if ( scope.axis.search("X") != -1 ) scope.object.position.x = Math.round( scope.object.position.x / scope.snap ) * scope.snap;
					if ( scope.axis.search("Y") != -1 ) scope.object.position.y = Math.round( scope.object.position.y / scope.snap ) * scope.snap;
					if ( scope.axis.search("Z") != -1 ) scope.object.position.z = Math.round( scope.object.position.z / scope.snap ) * scope.snap;
850

851
				}
852

853
			} else if ( _mode == "scale" ) {
A
Aleksandar Rodic 已提交
854

855 856
				point.sub( offset );
				point.multiply(parentScale);
A
Aleksandar Rodic 已提交
857

858
				if ( scope.space == "local" ) {
A
Aleksandar Rodic 已提交
859

860
					if ( scope.axis == "XYZ") {
861

862
						scale = 1 + ( ( point.y ) / 50 );
A
Aleksandar Rodic 已提交
863

864 865 866
						scope.object.scale.x = oldScale.x * scale;
						scope.object.scale.y = oldScale.y * scale;
						scope.object.scale.z = oldScale.z * scale;
A
Aleksandar Rodic 已提交
867

868
					} else {
869

870
						point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
A
Aleksandar Rodic 已提交
871

872 873 874
						if ( scope.axis == "X" ) scope.object.scale.x = oldScale.x * ( 1 + point.x / 50 );
						if ( scope.axis == "Y" ) scope.object.scale.y = oldScale.y * ( 1 + point.y / 50 );
						if ( scope.axis == "Z" ) scope.object.scale.z = oldScale.z * ( 1 + point.z / 50 );
A
Aleksandar Rodic 已提交
875

876
					}
A
Aleksandar Rodic 已提交
877

878
				}
A
Aleksandar Rodic 已提交
879

880
			} else if ( _mode == "rotate" ) {
A
Aleksandar Rodic 已提交
881

882 883 884 885
				point.sub( worldPosition );
				point.multiply(parentScale);
				tempVector.copy(offset).sub( worldPosition );
				tempVector.multiply(parentScale);
A
Aleksandar Rodic 已提交
886

887
				if ( scope.axis == "E" ) {
A
Aleksandar Rodic 已提交
888

889 890
					point.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
					tempVector.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
A
Aleksandar Rodic 已提交
891

892 893
					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 已提交
894

895
					tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
A
Aleksandar Rodic 已提交
896

897 898
					quaternionE.setFromAxisAngle( eye, rotation.z - offsetRotation.z );
					quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
899

900 901
					tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionE );
					tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
A
Aleksandar Rodic 已提交
902

903
					scope.object.quaternion.copy( tempQuaternion );
904

905
				} else if ( scope.axis == "XYZE" ) {
A
Aleksandar Rodic 已提交
906

907
					quaternionE.setFromEuler( point.clone().cross(tempVector).normalize() ); // rotation axis
908

909 910 911
					tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
					quaternionX.setFromAxisAngle( quaternionE, - point.clone().angleTo(tempVector) );
					quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
912

913 914
					tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
					tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
A
Aleksandar Rodic 已提交
915

916
					scope.object.quaternion.copy( tempQuaternion );
A
Aleksandar Rodic 已提交
917

918
				} else if ( scope.space == "local" ) {
A
Aleksandar Rodic 已提交
919

920
					point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
A
Aleksandar Rodic 已提交
921

922
					tempVector.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
A
Aleksandar Rodic 已提交
923

924 925
					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 已提交
926

927 928 929 930
					quaternionXYZ.setFromRotationMatrix( oldRotationMatrix );
					quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
					quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
					quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
931

932 933 934
					if ( scope.axis == "X" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionX );
					if ( scope.axis == "Y" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionY );
					if ( scope.axis == "Z" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionZ );
A
Aleksandar Rodic 已提交
935

936
					scope.object.quaternion.copy( quaternionXYZ );
A
Aleksandar Rodic 已提交
937

938
				} else if ( scope.space == "world" ) {
A
Aleksandar Rodic 已提交
939

940 941
					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 已提交
942

943
					tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
A
Aleksandar Rodic 已提交
944

945 946 947 948
					quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
					quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
					quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
					quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
A
Aleksandar Rodic 已提交
949

950 951 952
					if ( scope.axis == "X" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
					if ( scope.axis == "Y" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
					if ( scope.axis == "Z" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
953

954
					tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
A
Aleksandar Rodic 已提交
955

956
					scope.object.quaternion.copy( tempQuaternion );
A
Aleksandar Rodic 已提交
957

958
				}
A
Aleksandar Rodic 已提交
959 960 961

			}

962
			scope.update();
963
			scope.dispatchEvent( changeEvent );
964
			scope.dispatchEvent( objectChangeEvent );
A
Aleksandar Rodic 已提交
965

966
		}
A
Aleksandar Rodic 已提交
967

968
		function onPointerUp( event ) {
A
Aleksandar Rodic 已提交
969

D
Daniel Taub 已提交
970
			if ( _dragging && ( scope.axis !== null ) ) {
971 972
				mouseUpEvent.mode = _mode;
				scope.dispatchEvent( mouseUpEvent )
973
			}
974 975
			_dragging = false;
			onPointerHover( event );
A
Aleksandar Rodic 已提交
976

977
		}
A
Aleksandar Rodic 已提交
978

979
		function intersectObjects( pointer, objects ) {
A
Aleksandar Rodic 已提交
980

981
			var rect = domElement.getBoundingClientRect();
982 983 984 985 986
			var x = ( pointer.clientX - rect.left ) / rect.width;
			var y = ( pointer.clientY - rect.top ) / rect.height;

			pointerVector.set( ( x * 2 ) - 1, - ( y * 2 ) + 1, 0.5 );
			pointerVector.unproject( camera );
A
Aleksandar Rodic 已提交
987

988
			ray.set( camPosition, pointerVector.sub( camPosition ).normalize() );
A
Aleksandar Rodic 已提交
989

990 991
			var intersections = ray.intersectObjects( objects, true );
			return intersections[0] ? intersections[0] : false;
992

993
		}
994

995
	};
A
Aleksandar Rodic 已提交
996

997
	THREE.TransformControls.prototype = Object.create( THREE.Object3D.prototype );
998
	THREE.TransformControls.prototype.constructor = THREE.TransformControls;
A
Aleksandar Rodic 已提交
999

D
Daniel Taub 已提交
1000
}());