wasabia / three_dart

three.js rewrite by Dart, Dart 3D library. an easy to use, lightweight, cross-platform, general purpose 3D library.
MIT License
444 stars 77 forks source link

Spin on the y axis,but the center of the rotation is not the center point of the 3D model itself #150

Closed changning420 closed 5 months ago

changning420 commented 6 months ago

I want to rotate on itself y axis.I render the interface via obj+mtl. I want an object3D to rotate on its own y axis, but the center of rotation is incorrect. Do I need to re-export the obj+mtl file? The center of rotation is not the center point of a 3d object. autorotation becomes revolution

  apertureRingObject.rotateY(rotate);
Knightro63 commented 6 months ago

Hi @changning420,

Depending on your obj file your rotation of your mesh is at the center of the volume. I would import your file into blender and see what the center is there. If it is off in blender you will need to modify your files.

or you could try to find the center point and do something like this

void rotateAboutPoint(Object3D obj, Vector3 point, Vector3 axis, double theta, [bool pointIsWorld = false]){
    if(pointIsWorld){
        obj.parent.localToWorld(obj.position); // compensate for world coordinate
    }

    obj.position.sub(point); // remove the offset
    obj.position.applyAxisAngle(axis, theta); // rotate the POSITION
    obj.position.add(point); // re-add the offset

    if(pointIsWorld){
        obj.parent.worldToLocal(obj.position); // undo world coordinates compensation
    }

    obj.rotateOnAxis(axis, theta); // rotate the OBJECT
}

Hope this helps

changning420 commented 6 months ago

@Knightro63 Yes! That's right. That works very well. Thank you

void _focusCenter() {
    var foundFocusGeometry = false;
    var focusCenter = three.Vector3();
    regulatingRingObject.traverse((child) {
      if (child is three.Mesh) {
        three.Mesh ms = child;
        if (ms.geometry != null && foundFocusGeometry == false) {
          var geometry = child.geometry!;
          geometry.computeBoundingBox();
          geometry.boundingBox!.getCenter(focusCenter);
          geometry.center();
          foundFocusGeometry = true;
        }
      }
    });
    regulatingRingObject.position.set(0.05, 27.5, 0);
  }
void _apertureCenter() {
    var center = three.Vector3();
    var foundGeometry = false;
    apertureRingObject.traverse((child) {
      if (child is three.Mesh) {
        three.Mesh ms = child;
        if (ms.geometry != null && foundGeometry == false) {
          var geometry = child.geometry!;
          geometry.computeBoundingBox();
          geometry.boundingBox!.getCenter(center);
          geometry.center();
          foundGeometry = true;
        }
      }
    });
    apertureRingObject.children.first.position.set(0.5, 4, 0.5);

  }

I used the above two methods and achieved the same results.Spin on the y axis. But the words will come out misaligned.why?

image

link