threejs how to rotate around object's own center,instead of world center

前端 未结 5 726
渐次进展
渐次进展 2020-11-27 03:27

\"enter

two objects in the scene. the cube rotate axis should be the cube\'s center,th

5条回答
  •  执念已碎
    2020-11-27 03:39

    If your mesh is not rotating around its center, it is because the geometry vertices are offset from the origin.

    You can automate repositioning by using a bounding box to define a reasonable center, and then offset the mesh's position like so:

    var box = new THREE.Box3().setFromObject( mesh );
    box.center( mesh.position ); // this re-sets the mesh position
    mesh.position.multiplyScalar( - 1 );
    

    Then add the mesh to a pivot object:

    var pivot = new THREE.Group();
    scene.add( pivot );
    pivot.add( mesh );
    

    In your animation loop, rotate the pivot;

    pivot.rotation.y += 0.01;
    

    EDIT: A different solution is to translate the geometry vertices so the geometry is centered around, or near, the origin:

    geometry.translate( distX, distY, distZ );
    

    Or, alternatively, you could just call:

    geometry.center();
    

    which centers the vertices of the geometry for you based on the geometry's bounding box.

    three.js r.97

提交回复
热议问题