旋转我的 Model3D 对象会改变其缩放方式,使用户感到困惑

问题描述 投票:0回答:1

我有代码可以旋转和缩放下面的模型。

我的问题是,在旋转模型后,如果我根据模型的旋转方式来更改宽度、高度或深度,则它不会按预期工作。例如,如果模型向后躺并直立翻转,则高度和深度基本上与另一个一样起作用。这对我来说成为一个问题,因为我打算在我或管理员用户进行初始处理后让用户编辑比例和旋转。所以我的问题是有什么方法可以保持直观或创建一个副本,就像它从未旋转或类似的顺序?任何指示将不胜感激。

我当前的代码

    private void ApplyScalingAndRotation(Model3D model, double width, double height, double depth, double xRot, double yRot, double zRot)
    {
        // Retrieve the bounds of the model
        var bounds = model.Bounds;

        // Calculate the center point of the model
        var modelCenter = new Point3D(
            bounds.X + bounds.SizeX / 2,
            bounds.Y + bounds.SizeY / 2,
            bounds.Z + bounds.SizeZ / 2
        );

        // Original dimensions of the model
        double originalWidth = bounds.SizeX;
        double originalHeight = bounds.SizeY;
        double originalDepth = bounds.SizeZ;

        // Calculate scaling factors for each dimension
        double widthScaleFactor = width / originalWidth;
        double heightScaleFactor = height / originalHeight;
        double depthScaleFactor = depth / originalDepth;

        // Create a scale transformation without any rotation
        ScaleTransform3D scaleTransform = new ScaleTransform3D(
            widthScaleFactor, heightScaleFactor, depthScaleFactor,
            modelCenter.X, modelCenter.Y, modelCenter.Z
        );

        // Convert degrees to radians for rotation
        double rotationXDegrees = xRot;
        double rotationYDegrees = yRot;
        double rotationZDegrees = zRot;

        // Create rotation transformations
        RotateTransform3D rotateXTransform = new RotateTransform3D(new AxisAngleRotation3D(new System.Windows.Media.Media3D.Vector3D(1, 0, 0), rotationXDegrees));
        RotateTransform3D rotateYTransform = new RotateTransform3D(new AxisAngleRotation3D(new System.Windows.Media.Media3D.Vector3D(0, 1, 0), rotationYDegrees));
        RotateTransform3D rotateZTransform = new RotateTransform3D(new AxisAngleRotation3D(new System.Windows.Media.Media3D.Vector3D(0, 0, 1), rotationZDegrees));

        // Combine the rotation transformations into one Transform3DGroup
        Transform3DGroup rotationGroup = new Transform3DGroup();
        rotationGroup.Children.Add(rotateXTransform);
        rotationGroup.Children.Add(rotateYTransform);
        rotationGroup.Children.Add(rotateZTransform);

        // Combine the scale and rotation transformations into one Transform3DGroup
        Transform3DGroup combinedTransform = new Transform3DGroup();
        combinedTransform.Children.Add(scaleTransform);
        combinedTransform.Children.Add(rotationGroup);

        // Apply the combined transformation to the model
        model.Transform = combinedTransform;

    }
c# wpf 3d
1个回答
0
投票

我最终用我想要与另一个 Model3DGroup 旋转的模型包裹每个模型并旋转它。这解决了我的问题。

    private void ApplyScalingAndRotation(Model3D model, double width, double height, double depth, double xRot, double yRot, double zRot)
    {
        var model3DGroup = new Model3DGroup();


        //This should hopefully make it start in the back left corner
        TranslateTransform3D translation = new TranslateTransform3D(-0.5, -0.5, -0.5);

        // Apply the new translation transformation to the model
        model3DGroup.Transform = translation;

        model3DGroup.Children.Add(model);

        // Retrieve the bounds of the model
        var bounds = model3DGroup.Bounds;

        //Calculate the center point of the model
        var modelCenter = new Point3D(
            bounds.X + bounds.SizeX / 2,
            bounds.Y + bounds.SizeY / 2,
            bounds.Z + bounds.SizeZ / 2
        );

        // Original dimensions of the model
        double originalWidth = bounds.SizeX;
        double originalHeight = bounds.SizeY;
        double originalDepth = bounds.SizeZ;

        // Calculate scaling factors for each dimension
        double widthScaleFactor = width / originalWidth;
        double heightScaleFactor = height / originalHeight;
        double depthScaleFactor = depth / originalDepth;

        // Create a scale transformation without any rotation
        ScaleTransform3D scaleTransform = new ScaleTransform3D(
            widthScaleFactor, heightScaleFactor, depthScaleFactor,
            modelCenter.X, modelCenter.Y, modelCenter.Z
        );

        // Create rotation transformations
        RotateTransform3D rotateXTransform = new RotateTransform3D(new AxisAngleRotation3D(new System.Windows.Media.Media3D.Vector3D(1, 0, 0), xRot));
        RotateTransform3D rotateYTransform = new RotateTransform3D(new AxisAngleRotation3D(new System.Windows.Media.Media3D.Vector3D(0, 1, 0), yRot));
        RotateTransform3D rotateZTransform = new RotateTransform3D(new AxisAngleRotation3D(new System.Windows.Media.Media3D.Vector3D(0, 0, 1), zRot));

        // Combine the rotation transformations into one Transform3DGroup
        Transform3DGroup rotationGroup = new Transform3DGroup();
        rotationGroup.Children.Add(rotateXTransform);
        rotationGroup.Children.Add(rotateYTransform);
        rotationGroup.Children.Add(rotateZTransform);

        // Combine the scale and rotation transformations into one Transform3DGroup
        Transform3DGroup combinedTransform = new Transform3DGroup();
        combinedTransform.Children.Add(scaleTransform);
        combinedTransform.Children.Add(rotationGroup);

        // Apply the combined transformation to the model
        model3DGroup.Transform = combinedTransform;

        ContentVisual.Content = model3DGroup;


        //So it is a model 3d group


        //Change camera not sure we need this.

        bounds = model.Bounds;

        modelCenter = new Point3D(bounds.X + bounds.SizeX / 2,
            bounds.Y + bounds.SizeY / 2,
            bounds.Z + bounds.SizeZ / 2);

        var modelSize = Math.Sqrt(bounds.SizeX * bounds.SizeX +
                                  bounds.SizeY * bounds.SizeY +
                                  bounds.SizeZ * bounds.SizeZ);


        Camera1.TargetPosition = modelCenter;
        Camera1.Distance = modelSize * 2;

    }
© www.soinside.com 2019 - 2024. All rights reserved.