Skip to content

Commit 4c9edde

Browse files
committed
Replaced magic numbers with vars in TransformControls. Removed radial rotation gesture (now linear only).
1 parent b863577 commit 4c9edde

File tree

1 file changed

+27
-72
lines changed

1 file changed

+27
-72
lines changed

examples/js/controls/TransformControls.js

Lines changed: 27 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ THREE.TransformControls = function ( camera, domElement ) {
420420

421421
} else if ( mode === 'rotate' ) {
422422

423-
var LINEAR_ROTATION_SPEED = 10 / worldPosition.distanceTo( _tempVector.setFromMatrixPosition( this.camera.matrixWorld ) );
423+
var ROTATION_SPEED = 20 / worldPosition.distanceTo( _tempVector.setFromMatrixPosition( this.camera.matrixWorld ) );
424424

425425
var quaternion = this.space === "local" ? worldQuaternion : _identityQuaternion;
426426

@@ -436,35 +436,21 @@ THREE.TransformControls = function ( camera, domElement ) {
436436

437437
_tempVector.copy( pointEnd ).sub( pointStart ).cross( eye ).normalize();
438438
rotationAxis.copy( _tempVector );
439-
rotationAngle = pointEnd.sub( pointStart ).dot( _tempVector.cross( eye ) ) * LINEAR_ROTATION_SPEED;
439+
rotationAngle = pointEnd.sub( pointStart ).dot( _tempVector.cross( eye ) ) * ROTATION_SPEED;
440440

441441
} else if ( axis === 'X' || axis === 'Y' || axis === 'Z' ) {
442442

443443
_alignVector.copy( unit ).applyQuaternion( quaternion );
444444

445445
rotationAxis.copy( unit );
446446

447-
var normalToCamera = Math.abs( _alignVector.dot( eye ) ) > 0.3;
448-
449-
if ( normalToCamera ) {
450-
451-
_tempVector.copy( pointEnd ).cross( pointStart );
452-
453-
var flip = {
454-
X: _tempVector.x > 0 ? -1 : 1,
455-
Y: _tempVector.y > 0 ? -1 : 1,
456-
Z: _tempVector.z > 0 ? -1 : 1
457-
}
458-
459-
rotationAngle = pointEnd.angleTo( pointStart ) * flip[ axis ];
460-
461-
} else {
462-
463-
_tempVector = unit.clone().applyQuaternion( quaternion );
464-
_tempVector2 = pointEnd.clone().sub( pointStart ).applyQuaternion( worldQuaternionStart );
465-
rotationAngle = _tempVector2.dot( _tempVector.cross( eye ) ) * LINEAR_ROTATION_SPEED;
466-
447+
_tempVector = unit.clone();
448+
_tempVector2 = pointEnd.clone().sub( pointStart );
449+
if ( space === 'local' ) {
450+
_tempVector.applyQuaternion( quaternion );
451+
_tempVector2.applyQuaternion( worldQuaternionStart );
467452
}
453+
rotationAngle = _tempVector2.dot( _tempVector.cross( eye ).normalize() ) * ROTATION_SPEED;
468454

469455
}
470456

@@ -835,18 +821,15 @@ THREE.TransformControlsGizmo = function () {
835821
var gizmoRotate = {
836822
X: [
837823
[ new THREE.Line( CircleGeometry( 1, 0.5 ), matLineRed ) ],
838-
[ new THREE.Mesh( new THREE.OctahedronBufferGeometry( 0.04, 0 ), matRed ), [ 0, 0, 0.99 ], null, [ 1, 3, 1 ], 'linear' ],
839-
[ new THREE.Mesh( new THREE.OctahedronBufferGeometry( 0.03, 0 ), matRed ), [ 0, 0, 1 ], null, [ 4, 1, 4 ], 'radial' ],
824+
[ new THREE.Mesh( new THREE.OctahedronBufferGeometry( 0.04, 0 ), matRed ), [ 0, 0, 0.99 ], null, [ 1, 3, 1 ] ],
840825
],
841826
Y: [
842827
[ new THREE.Line( CircleGeometry( 1, 0.5 ), matLineGreen ), null, [ 0, 0, -Math.PI / 2 ] ],
843-
[ new THREE.Mesh( new THREE.OctahedronBufferGeometry( 0.04, 0 ), matGreen ), [ 0, 0, 0.99 ], null, [ 3, 1, 1 ], 'linear' ],
844-
[ new THREE.Mesh( new THREE.OctahedronBufferGeometry( 0.03, 0 ), matGreen ), [ 0, 0, 1 ], null, [ 1, 4, 4 ], 'radial' ],
828+
[ new THREE.Mesh( new THREE.OctahedronBufferGeometry( 0.04, 0 ), matGreen ), [ 0, 0, 0.99 ], null, [ 3, 1, 1 ] ],
845829
],
846830
Z: [
847831
[ new THREE.Line( CircleGeometry( 1, 0.5 ), matLineBlue ), null, [ 0, Math.PI / 2, 0 ] ],
848-
[ new THREE.Mesh( new THREE.OctahedronBufferGeometry( 0.04, 0 ), matBlue ), [ 0.99, 0, 0 ], null, [ 1, 3, 1 ], 'linear' ],
849-
[ new THREE.Mesh( new THREE.OctahedronBufferGeometry( 0.03, 0 ), matBlue ), [ 1, 0, 0 ], null, [ 4, 1, 4 ], 'radial' ],
832+
[ new THREE.Mesh( new THREE.OctahedronBufferGeometry( 0.04, 0 ), matBlue ), [ 0.99, 0, 0 ], null, [ 1, 3, 1 ] ],
850833
],
851834
E: [
852835
[ new THREE.Line( CircleGeometry( 1.25, 1 ), matLineYellowTransparent ), null, [ 0, Math.PI / 2, 0 ] ],
@@ -1206,38 +1189,42 @@ THREE.TransformControlsGizmo = function () {
12061189

12071190
// Hide translate and scale axis facing the camera
12081191

1192+
var AXIS_HIDE_TRESHOLD = 0.99;
1193+
var PLANE_HIDE_TRESHOLD = 0.2;
1194+
var AXIS_FLIP_TRESHOLD = -0.4;
1195+
12091196
if ( handle.name === 'X' || handle.name === 'XYZX' ) {
1210-
if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.99 ) {
1197+
if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
12111198
handle.scale.set( 1e-10, 1e-10, 1e-10 );
12121199
handle.visible = false;
12131200
}
12141201
}
12151202
if ( handle.name === 'Y' || handle.name === 'XYZY' ) {
1216-
if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.99 ) {
1203+
if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
12171204
handle.scale.set( 1e-10, 1e-10, 1e-10 );
12181205
handle.visible = false;
12191206
}
12201207
}
12211208
if ( handle.name === 'Z' || handle.name === 'XYZZ' ) {
1222-
if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.99 ) {
1209+
if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
12231210
handle.scale.set( 1e-10, 1e-10, 1e-10 );
12241211
handle.visible = false;
12251212
}
12261213
}
12271214
if ( handle.name === 'XY' ) {
1228-
if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) < 0.2 ) {
1215+
if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
12291216
handle.scale.set( 1e-10, 1e-10, 1e-10 );
12301217
handle.visible = false;
12311218
}
12321219
}
12331220
if ( handle.name === 'YZ' ) {
1234-
if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) < 0.2 ) {
1221+
if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
12351222
handle.scale.set( 1e-10, 1e-10, 1e-10 );
12361223
handle.visible = false;
12371224
}
12381225
}
12391226
if ( handle.name === 'XZ' ) {
1240-
if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) < 0.2 ) {
1227+
if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
12411228
handle.scale.set( 1e-10, 1e-10, 1e-10 );
12421229
handle.visible = false;
12431230
}
@@ -1246,7 +1233,7 @@ THREE.TransformControlsGizmo = function () {
12461233
// Flip translate and scale axis ocluded behind another axis
12471234

12481235
if ( handle.name.search( 'X' ) !== -1 ) {
1249-
if ( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) < -0.4 ) {
1236+
if ( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
12501237
if ( handle.tag === 'fwd' ) {
12511238
handle.visible = false;
12521239
} else {
@@ -1258,7 +1245,7 @@ THREE.TransformControlsGizmo = function () {
12581245
}
12591246

12601247
if ( handle.name.search( 'Y' ) !== -1 ) {
1261-
if ( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) < -0.4 ) {
1248+
if ( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
12621249
if ( handle.tag === 'fwd' ) {
12631250
handle.visible = false;
12641251
} else {
@@ -1270,7 +1257,7 @@ THREE.TransformControlsGizmo = function () {
12701257
}
12711258

12721259
if ( handle.name.search( 'Z' ) !== -1 ) {
1273-
if ( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) < -0.4 ) {
1260+
if ( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) {
12741261
if ( handle.tag === 'fwd' ) {
12751262
handle.visible = false;
12761263
} else {
@@ -1283,38 +1270,6 @@ THREE.TransformControlsGizmo = function () {
12831270

12841271
} else if ( this.mode === 'rotate' ) {
12851272

1286-
// switch between liner/radial quaternion handle affordances
1287-
1288-
if ( handle.name === 'X' ) {
1289-
if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.3 ) {
1290-
if ( handle.tag === 'linear' ) {
1291-
handle.visible = false;
1292-
}
1293-
} else if ( handle.tag === 'radial' ) {
1294-
handle.visible = false;
1295-
}
1296-
}
1297-
1298-
if ( handle.name === 'Y' ) {
1299-
if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.3 ) {
1300-
if ( handle.tag === 'linear' ) {
1301-
handle.visible = false;
1302-
}
1303-
} else if ( handle.tag === 'radial' ) {
1304-
handle.visible = false;
1305-
}
1306-
}
1307-
1308-
if ( handle.name === 'Z' ) {
1309-
if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.3 ) {
1310-
if ( handle.tag === 'linear' ) {
1311-
handle.visible = false;
1312-
}
1313-
} else if ( handle.tag === 'radial' ) {
1314-
handle.visible = false;
1315-
}
1316-
}
1317-
13181273
// Align handles to current local or world rotation
13191274

13201275
tempQuaternion2.copy( quaternion );
@@ -1364,17 +1319,17 @@ THREE.TransformControlsGizmo = function () {
13641319

13651320
if ( handle.name === this.axis ) {
13661321

1367-
handle.material.opacity *= 2.0;
1322+
handle.material.opacity = 1.0;
13681323
handle.material.color.lerp( new THREE.Color( 1, 1, 1 ), 0.5 );
13691324

13701325
} else if ( this.axis.split('').some( function( a ) { return handle.name === a; } ) ) {
13711326

1372-
handle.material.opacity *= 2.0;
1327+
handle.material.opacity = 1.0;
13731328
handle.material.color.lerp( new THREE.Color( 1, 1, 1 ), 0.5 );
13741329

13751330
} else {
13761331

1377-
handle.material.opacity *= 0.15;
1332+
handle.material.opacity *= 0.05;
13781333

13791334
}
13801335

0 commit comments

Comments
 (0)