我想要实现的是几何体围绕枢轴点的旋转,并使几何体具有新的定义。我不想继续编辑rotationZ,但我想让当前的rotationZ成为新的rotationZ 0。

这样,当我创建新的旋转任务时,它将从新给定的枢轴点和新给定的rad开始。

我尝试过的方法,但是旋转点移动了:

// Add cube to do calculations
var box = new THREE.Box3().setFromObject( o );
var size = box.getSize();
var offsetZ = size.z / 2;

o.geometry.translate(0, -offsetZ, 0)

// Do ratation
o.rotateZ(CalcUtils.degreeToRad(degree));

o.geometry.translate(0, offsetZ, 0)

我还尝试添加一个组并旋转该组,然后删除该组。但是我需要保持旋转状态而没有所有其他对象。我创建的代码
var box = new THREE.Box3().setFromObject( o );
    var size = box.size();

    var geometry = new THREE.BoxGeometry( 20, 20, 20 );
    var material = new THREE.MeshBasicMaterial( { color: 0xcc0000 } );

    var cube = new THREE.Mesh( geometry, material );

    cube.position.x = o.position.x;
    cube.position.y = 0; // Height / 2
    cube.position.z = -size.z / 2;

    o.position.x = 0;
    o.position.y = 0;
    o.position.z = size.z / 2;

    cube.add(o);

    scene.add(cube);

    // Do ratation
    cube.rotateY(CalcUtils.degreeToRad(degree));

    // Remove cube, and go back to single object
    var position = o.getWorldPosition();

    scene.add(o)
    scene.remove(cube);
    console.log(o);

    o.position.x = position.x;
    o.position.y = position.y;
    o.position.z = position.z;

所以我的问题是,如何将当前旋转保存为新的0旋转点。使旋转最终

编辑
我添加了我想做的图像。该对象是绿色的。我的世界为0点(黑色)。我有一个0点的对象(红色)。我有旋转点(蓝色)。

如何围绕蓝点旋转对象?

javascript - 三个JS枢轴点-LMLPHP

最佳答案

我不建议您更新顶点,因为您会遇到法线的麻烦(除非您也使它们保持最新状态)。基本上,执行转换矩阵所要执行的操作非常麻烦。

您通过平移,旋转和取消平移非常接近,因此您处在正确的轨道上。有一些内置方法可以帮助使此操作变得简单。

// obj - your object (THREE.Object3D or derived)
// point - the point of rotation (THREE.Vector3)
// axis - the axis of rotation (normalized THREE.Vector3)
// theta - radian value of rotation
// pointIsWorld - boolean indicating the point is in world coordinates (default = false)
function rotateAboutPoint(obj, point, axis, theta, pointIsWorld){
    pointIsWorld = (pointIsWorld === undefined)? false : pointIsWorld;

    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
}

此方法完成后,旋转/位置 IS 仍然存在。下次调用该方法时,它将把对象从其当前状态转换为下一步您定义的输入。

另请注意使用世界坐标的补偿。通过将对象的position vector 转换为正确的坐标系,可以使用世界坐标或局部空间中的点。尽管您的观察可能有所不同,但最好在您的点和对象处于不同坐标系时以这种方式使用它。

09-05 06:07