mrdoob / three.js

JavaScript 3D Library.
https://threejs.org/
MIT License
102.25k stars 35.35k forks source link

Using TransformControls to move an object along the axis, the path is not correct #21840

Open superajia opened 3 years ago

superajia commented 3 years ago

Describe the bug

Using TransformControls to move an object along the axis, the path is not correct

To Reproduce

Use TransformControls to move the object along the axis. When the following three conditions are met, the path is not correct

  1. This object has a parent object that has been rotated

  2. The coordinate space is 'world'

  3. The translationSnap is a a numeric value

Code TransformControls.js line 421

if ( space === 'world' ) {
  // if ( object.parent ) {
  // object.position.add( _tempVector.setFromMatrixPosition( object.parent.matrixWorld ) );
  // }
  // if ( axis.search( 'X' ) !== - 1 ) {
  //    object.position.x = Math.round( object.position.x / this.translationSnap ) * this.translationSnap;
  // }
  // if ( axis.search( 'Y' ) !== - 1 ) {
  //    object.position.y = Math.round( object.position.y / this.translationSnap ) * this.translationSnap;
  // }
  // if ( axis.search( 'Z' ) !== - 1 ) {
  //    object.position.z = Math.round( object.position.z / this.translationSnap ) * this.translationSnap;
  // }
  // if ( object.parent ) {
  //    object.position.sub( _tempVector.setFromMatrixPosition( object.parent.matrixWorld ) );
  // }
  var vector_temp = new Vector3();
  object.getWorldPosition(vector_temp);
  if ( axis.search( 'X' ) !== - 1 ) {
    vector_temp.x = Math.round( vector_temp.x / this.translationSnap ) * this.translationSnap;
  }
  if ( axis.search( 'Y' ) !== - 1 ) {
    vector_temp.y = Math.round( vector_temp.y / this.translationSnap ) * this.translationSnap;
  }
  if ( axis.search( 'Z' ) !== - 1 ) {
    vector_temp.z = Math.round( vector_temp.z / this.translationSnap ) * this.translationSnap;
  }
  object.position.copy(object.parent.worldToLocal(vector_temp))
}

Live example

Expected behavior

Using TransformControls to move an object along the axis, the path is correct.

Screenshots

5f5b9b60161e38e6d8e23cadc066b62 40fbe4deb93aa81fe1da913d23ebedf

Platform:

Mugen87 commented 3 years ago

It seems your change is not correct. Even when applying it, you don't get the expected result if you rotate the scene in misc_controls_transform. TransformControls possibly does not fully support transformed ancestors.

/ping @arodic

superajia commented 3 years ago

Even when applying it, you don't get the expected result if you rotate the scene in misc_controls_transform

Thank you for your reply. I don't know what this sentence means "Even when applying it, you don't get the expected result if you rotate the scene in misc_controls_transform" I use the OrbitControl.js to rotate the scene and get the expected result in my way. Look forward to your reply. 3c28ac649a3ee4656abfa04285460e6

Mugen87 commented 3 years ago

I use the OrbitControl.js to rotate the scene and get the expected result in my way.

You are probably transforming the camera. Try adding scene.rotation.x = Math.PI * 0.5; in misc_controls_transform.

superajia commented 3 years ago

According to your suggestion,I tried adding scene.rotation.y = Math.PI * 0.5. However, this lead to more problems. The purpose of using Transform.js is to move objects in the scene, not scene. If you don't transform the scene,my modify will work. Look forward to your reply.

pesutoaka commented 2 years ago

This also does not work in the r137. This is what I have ended up with, the problem seams to be a parent transform. ` if ( space === 'world' ) {

                if ( object.parent ) {

                    object.getWorldPosition(_tempVector);
                    if ( axis.search( 'X' ) !== - 1 ) {
                        _tempVector.x = Math.round( _tempVector.x / this.translationSnap ) * this.translationSnap;
                    }
                    if ( axis.search( 'Y' ) !== - 1 ) {
                        _tempVector.y = Math.round( _tempVector.y / this.translationSnap ) * this.translationSnap;
                    }
                    if ( axis.search( 'Z' ) !== - 1 ) {
                        _tempVector.z = Math.round( _tempVector.z / this.translationSnap ) * this.translationSnap;
                    }
                    object.position.copy(object.parent.worldToLocal(_tempVector));

                }
                else {

                    if ( axis.search( 'X' ) !== - 1 ) {

                        object.position.x = Math.round( object.position.x / this.translationSnap ) * this.translationSnap;

                    }

                    if ( axis.search( 'Y' ) !== - 1 ) {

                        object.position.y = Math.round( object.position.y / this.translationSnap ) * this.translationSnap;

                    }

                    if ( axis.search( 'Z' ) !== - 1 ) {

                        object.position.z = Math.round( object.position.z / this.translationSnap ) * this.translationSnap;

                    }
                }

            }`

GIF 23 02 2022 17-03-13