简体   繁体   English

如何围绕其末端旋转 ThreeJS Object3D 对象?

[英]How to rotate a ThreeJS Object3D object around its end?

Is it possible to rotate an Object3D object about its end?是否可以围绕其末端旋转 Object3D 对象? I found my object's center with const boundingBox = new THREE.Box3().setFromObject(this.object) to determine the center.我用const boundingBox = new THREE.Box3().setFromObject(this.object)找到了我的对象的中心来确定中心。 Would rotating at a point mean translating the object to one point, rotate, then translate back?在一点旋转是否意味着将对象平移到一个点,旋转,然后平移回来?

Is it possible to rotate an Object3D object about its end?是否可以围绕其末端旋转 Object3D 对象?

Yes.是的。 By default, Object3D rotates about its center, but it's possible to change rotation point by "wrapping" object in parent (Object3D) and setting position for parent (this will be also a rotation point):默认情况下,Object3D 围绕其中心旋转,但可以通过在父级 (Object3D) 中“包裹”对象并为父级设置位置(这也是一个旋转点)来更改旋转点:

function changeRotationPoint(obj, scene, position) {
  // create parent and add it to scene
  const parent = new THREE.Object3D();

  parent.add(obj);
  if (obj.parent) {
    obj.parent.add(parent);
  } else {
    // add to THREE scene if obj doesn't have a
    // parent
    scene.add(parent);
  }

  // position for rotation, 
  // for example (put your data): { x: 0.1, y: 0.1, z: 0 }
  parent.position.set(position.x, position.y, position.z);

  // correct position of obj, so only rotation point
  // will be changed
  const x = obj.position.x - position.x,
  const y = obj.position.y - position.y,
  const z = obj.position.z - position.z
  obj.position.set(x, y, z);
}

// call function
changeRotationPoint(this.object, scene, { x: 0.1, y: 0.1, z: 0 });

// rotate this.object
this.object.parent.rotation.set(0.1, 0.2, 0.3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM