diff --git a/editor/js/Viewport.js b/editor/js/Viewport.js index b3e006c2e6750b359ac296ec456f41d592ed4a14..16f6b515139b72a216bc6aaf835c4d3f04e90bc8 100644 --- a/editor/js/Viewport.js +++ b/editor/js/Viewport.js @@ -283,7 +283,7 @@ var Viewport = function ( editor ) { signals.snapChanged.add( function ( dist ) { - transformControls.setSnap( dist ); + transformControls.setTranslationSnap( dist ); } ); diff --git a/examples/js/controls/TransformControls.js b/examples/js/controls/TransformControls.js index 990d49b315a3e815d7e0423ed69d749571ca2345..ee42c7cb748e6ae68e41b9e2b4b5202ccd0763bc 100644 --- a/examples/js/controls/TransformControls.js +++ b/examples/js/controls/TransformControls.js @@ -617,7 +617,8 @@ this.object = undefined; this.visible = false; - this.snap = null; + this.translationSnap = null; + this.rotationSnap = null; this.space = "world"; this.size = 1; this.axis = null; @@ -750,9 +751,15 @@ }; - this.setSnap = function ( snap ) { + this.setTranslationSnap = function ( translationSnap ) { - scope.snap = snap; + scope.translationSnap = translationSnap; + + }; + + this.setRotationSnap = function ( rotationSnap ) { + + scope.rotationSnap = rotationSnap; }; @@ -930,11 +937,23 @@ } - if ( scope.snap !== null ) { + if ( scope.translationSnap !== null ) { - 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; + if ( scope.space === "local" ) { + + scope.object.position.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) ); + + } + + if ( scope.axis.search( "X" ) !== - 1 ) scope.object.position.x = Math.round( scope.object.position.x / scope.translationSnap ) * scope.translationSnap; + if ( scope.axis.search( "Y" ) !== - 1 ) scope.object.position.y = Math.round( scope.object.position.y / scope.translationSnap ) * scope.translationSnap; + if ( scope.axis.search( "Z" ) !== - 1 ) scope.object.position.z = Math.round( scope.object.position.z / scope.translationSnap ) * scope.translationSnap; + + if ( scope.space === "local" ) { + + scope.object.position.applyMatrix4( worldRotationMatrix ); + + } } @@ -1013,9 +1032,20 @@ offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) ); quaternionXYZ.setFromRotationMatrix( oldRotationMatrix ); - quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x ); - quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y ); - quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z ); + + if ( scope.rotationSnap !== null ) { + + quaternionX.setFromAxisAngle( unitX, Math.round( ( rotation.x - offsetRotation.x ) / scope.rotationSnap ) * scope.rotationSnap ); + quaternionY.setFromAxisAngle( unitY, Math.round( ( rotation.y - offsetRotation.y ) / scope.rotationSnap ) * scope.rotationSnap ); + quaternionZ.setFromAxisAngle( unitZ, Math.round( ( rotation.z - offsetRotation.z ) / scope.rotationSnap ) * scope.rotationSnap ); + + } else { + + quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x ); + quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y ); + quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z ); + + } if ( scope.axis === "X" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionX ); if ( scope.axis === "Y" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionY ); @@ -1030,9 +1060,20 @@ tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) ); - quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x ); - quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y ); - quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z ); + if ( scope.rotationSnap !== null ) { + + quaternionX.setFromAxisAngle( unitX, Math.round( ( rotation.x - offsetRotation.x ) / scope.rotationSnap ) * scope.rotationSnap ); + quaternionY.setFromAxisAngle( unitY, Math.round( ( rotation.y - offsetRotation.y ) / scope.rotationSnap ) * scope.rotationSnap ); + quaternionZ.setFromAxisAngle( unitZ, Math.round( ( rotation.z - offsetRotation.z ) / scope.rotationSnap ) * scope.rotationSnap ); + + } else { + + quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x ); + quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y ); + quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z ); + + } + quaternionXYZ.setFromRotationMatrix( worldRotationMatrix ); if ( scope.axis === "X" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX ); diff --git a/examples/misc_controls_transform.html b/examples/misc_controls_transform.html index 497dca99c6e956cf41252f6dce2570422e6b6748..bee7323cb26399f4b9d21a8177ef33eb3c75dd32 100644 --- a/examples/misc_controls_transform.html +++ b/examples/misc_controls_transform.html @@ -26,7 +26,7 @@
"W" translate | "E" rotate | "R" scale | "+" increase size | "-" decrease size
- Press "Q" to toggle world/local space + Press "Q" to toggle world/local space, keep "Ctrl" down to snap to grid
@@ -79,30 +79,56 @@ window.addEventListener( 'resize', onWindowResize, false ); window.addEventListener( 'keydown', function ( event ) { - //console.log(event.which); - switch ( event.keyCode ) { - case 81: // Q - control.setSpace( control.space == "local" ? "world" : "local" ); - break; - case 87: // W - control.setMode( "translate" ); - break; - case 69: // E - control.setMode( "rotate" ); - break; - case 82: // R - control.setMode( "scale" ); - break; - case 187: - case 107: // +,=,num+ - control.setSize( control.size + 0.1 ); - break; - case 189: - case 109: // -,_,num- - control.setSize( Math.max(control.size - 0.1, 0.1 ) ); - break; - } - }); + + switch ( event.keyCode ) { + + case 81: // Q + control.setSpace( control.space === "local" ? "world" : "local" ); + break; + + case 17: // Ctrl + control.setTranslationSnap( 100 ); + control.setRotationSnap( THREE.Math.degToRad( 15 ) ); + break; + + case 87: // W + control.setMode( "translate" ); + break; + + case 69: // E + control.setMode( "rotate" ); + break; + + case 82: // R + control.setMode( "scale" ); + break; + + case 187: + case 107: // +, =, num+ + control.setSize( control.size + 0.1 ); + break; + + case 189: + case 109: // -, _, num- + control.setSize( Math.max( control.size - 0.1, 0.1 ) ); + break; + + } + + }); + + window.addEventListener( 'keyup', function ( event ) { + + switch ( event.keyCode ) { + + case 17: // Ctrl + control.setTranslationSnap( null ); + control.setRotationSnap( null ); + break; + + } + + }); }