如何在3D模型上移动轴

时间:2018-08-26 22:55:47

标签: c# wpf helix-3d-toolkit

几天后进入3D模型,我不知道如何移动轴,因此无法开始旋转。例如...。我想将X倾斜30度,然后我将排成一行进行每个轴旋转。如果我将X旋转到30度,则会偏离Y和Z旋转。在后面的代码中如何执行此操作的任何示例?我发现的最接近的示例是在Manipulator演示中,但没有显示有关该Manipulator如何工作的代码。

enter image description here

  private Transform3DGroup GetTransforms(Model3D model)
    {
        var transforms = new Transform3DGroup();
        // Rotation around X
        transforms.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), 0)));
        // Rotation around Y 
        transforms.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), 0)));
        // Rotation around Z
        transforms.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), 0)));
        // Translate transform (if required)
        transforms.Children.Add(new TranslateTransform3D());
        model.Transform = transforms;
        return transforms;
    }

 private void SetRotation(double amountX, double amountY, double amountZ, Model3D model, Point3D center)
    {
        // Suppose we have a function that gives us all the transforms
        // applied to this object
        var transforms = GetTransforms(model);
        var translation = transforms.Children[3];

        // Suppose GetCenter() obtains the center point of an object
        // in Point3D format
        var translatedCenter = translation.Transform(center);

        if (!(transforms.Children[0] is RotateTransform3D rotX)) throw new ArgumentNullException(nameof(rotX));
        if (!(transforms.Children[1] is RotateTransform3D rotY)) throw new ArgumentNullException(nameof(rotY));
        if (!(transforms.Children[2] is RotateTransform3D rotZ)) throw new ArgumentNullException(nameof(rotZ));

        // Set the center point of transformation to the translated center of the object
        rotX.CenterX = rotY.CenterX = rotZ.CenterX = translatedCenter.X;
        rotX.CenterY = rotY.CenterY = rotZ.CenterY = translatedCenter.Y;
        rotX.CenterZ = rotY.CenterZ = rotZ.CenterZ = translatedCenter.Z;

        // Apply the angle amounts
        ((AxisAngleRotation3D) rotX.Rotation).Angle = amountX;
        ((AxisAngleRotation3D) rotY.Rotation).Angle = amountY;
        ((AxisAngleRotation3D) rotZ.Rotation).Angle = amountZ;
    }

0 个答案:

没有答案