首页 > 解决方案 > 如何在 3d 模型上移动轴

问题描述

用了几天的 3d 模型,我不知道如何移动轴,这样我就可以开始旋转了。例如....我想将 X 倾斜 30 度,然后我将排好队进行每个轴的旋转。如果我将 X 旋转到 30 度,它会抛出 Y 和 Z 旋转。如何在后面的代码中执行此操作的任何示例?我找到的最接近的示例是在 Manipulator 演示中,但它没有显示有关操纵器如何工作的代码。

在此处输入图像描述

  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;
    }

标签: c#wpfhelix-3d-toolkit

解决方案


推荐阅读