首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Helix工具包自动旋转

Helix工具包自动旋转
EN

Stack Overflow用户
提问于 2020-07-13 18:14:18
回答 1查看 713关注 0票数 1

在过去的两个星期里,我一直在研究这个问题,但我一直无法找到解决办法。我正在加载一个三维模型从STL文件,并试图旋转的三维模型自动围绕1轴。这个想法就像一个缓慢移动的动画,在模型的Y轴周围显示360度的视图。

XAML:

代码语言:javascript
复制
<Grid>
    <StackPanel x:Name="myViewPort">
        <helix:HelixViewport3D x:Name="viewPort3d" ZoomExtentsWhenLoaded="true" RotateAroundMouseDownPoint="true" CameraRotationMode="Turntable" Height="1000" ShowViewCube="false">
            <helix:DefaultLights/>
            <ModelVisual3D x:Name="visualModel"/>
        </helix:HelixViewport3D>
    </StackPanel>
</Grid>

C#:

代码语言:javascript
复制
public void load3dModel()
    {
        StLReader stlReader = new StLReader();
        Model3DGroup MyModel = stlReader.Read(MODEL_PATH);

        /* Auto Rotate Here */

        System.Windows.Media.Media3D.Material mat = MaterialHelper.CreateMaterial(new SolidColorBrush(Color.FromRgb(255, 255, 255)));

        foreach (System.Windows.Media.Media3D.GeometryModel3D geometryModel in MyModel.Children)
        {
            geometryModel.Material = mat;
            geometryModel.BackMaterial = mat;
        }

        visualModel.Content = MyModel;
    }

这方面的棘手之处在于,我需要能够只使用代码来旋转模型。生成的模型可以是数以百计的模型之一,它将取决于应用程序的模型将是什么。因此,代码需要能够处理围绕同一轴旋转,我可以保证当3D模型被导出到STL文件时,它将沿着X轴平坦。

-更新

代码语言:javascript
复制
public void load3dModel()
    {
        StLReader stlReader = new StLReader();
        Model3DGroup MyModel = stlReader.Read(MODEL_PATH);

        System.Windows.Media.Media3D.Material mat = MaterialHelper.CreateMaterial(new SolidColorBrush(Color.FromRgb(255, 255, 255)));

        foreach (System.Windows.Media.Media3D.GeometryModel3D geometryModel in MyModel.Children)
        {
            geometryModel.Material = mat;
            geometryModel.BackMaterial = mat;
        }

        visualModel.Content = MyModel;

        /* Auto Rotate Here */
        GeometryModel3D geoModel = new GeometryModel3D()
        {
            Transform = new RotateTransform3D()
            {
                Rotation = new AxisAngleRotation3D()
                {
                    Axis = new Vector3D(0, 1, 0),
                    Angle = 0
                }
            }
        };

        MyModel.Children.Add(geoModel);

        var Rotation3DAnimation = new Rotation3DAnimation();

        var FromAxis = new AxisAngleRotation3D()
        {
            Axis = new Vector3D(0, 1, 0),
            Angle = 0
        };

        var ToAxis = new AxisAngleRotation3D()
        {
            Axis = new Vector3D(0, 1, 0),
            Angle = 359
        };

        Rotation3DAnimation.From = FromAxis;
        Rotation3DAnimation.To = ToAxis;
        Rotation3DAnimation.Duration = Duration.Forever; //ADDED DURATION, Still did not work!

        var rotateStoryboard = new Storyboard
        {
            Duration = new Timespan(0, 0, 12),
            RepeatBehavior = RepeatBehavior.Forever,
        };

        Storyboard.SetTarget(Rotation3DAnimation, geoModel.Transform);
        Storyboard.SetTargetProperty(Rotation3DAnimation, new PropertyPath("Rotation"));

        rotateStoryboard.Children.Add(Rotation3DAnimation);
        rotateStoryboard.Begin();
    }

这不管用..。什么都没变?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-17 21:30:46

我不确定我是否正确地理解了您要完成的任务,所以如果我误解了您的意思,请告诉我:在您显示的代码中,您正在加载几个GeometryModel3D,所以您是想让它们都旋转,还是只加载一个?

使其旋转的一种方法是通过GeometryModel3D的Transform属性。

您必须设置一个DispatcherTimer,并更新每个滴答的旋转角度:

我根据您提供的内容做了一个例子,其中我让一个3D模型旋转:

代码语言:javascript
复制
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Load3dModel();
        this.timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(10) };
        this.timer.Tick += Timer_Tick;
        this.timer.Start();
    }

    /// <summary>
    /// Change the rotation
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Timer_Tick(object sender, EventArgs e)
    {
        if (this.angle >= 360)
        {
            this.angle = 0;
        }
        else
        {
            //Nothing to do
        }
        this.angle = this.angle + 0.25;
        //You can adapt the code if you have many children 
        GeometryModel3D geometryModel3D = (GeometryModel3D)((Model3DGroup)visualModel.Content).Children.First();
        if (geometryModel3D.Transform is RotateTransform3D rotateTransform3 && rotateTransform3.Rotation is AxisAngleRotation3D rotation)
        {
            rotation.Angle = this.angle;
        }
        else
        {
            ///Initialize the Transform (I didn't do it in my example but you could do this initialization in <see Load3dModel/>)
            geometryModel3D.Transform = new RotateTransform3D()
            {
                Rotation = new AxisAngleRotation3D()
                {
                    Axis = new Vector3D(0, 1, 0),
                    Angle = this.angle,
                }
            };
        }
    }

    private DispatcherTimer timer;

    public void Load3dModel()
    {
        StLReader stlReader = new StLReader();
        /*
        Model3DGroup MyModel = stlReader.Read(OrLoadFromPath));
        */
        Model3DGroup myModel = new Model3DGroup();
        // Create a mesh builder and add a box to it
        var meshBuilder = new MeshBuilder(false, false);
        meshBuilder.AddBox(new Point3D(0, 0, 1), 1, 2, 0.5);
        meshBuilder.AddBox(new Rect3D(0, 0, 1.2, 0.5, 1, 0.4));

        // Create a mesh from the builder (and freeze it)
        var mesh = meshBuilder.ToMesh(true);

        // Add 3 models to the group (using the same mesh, that's why we had to freeze it)
        myModel.Children.Add(new GeometryModel3D { Geometry = mesh});
        myModel.Children.Add(new GeometryModel3D { Geometry = mesh, Transform = new TranslateTransform3D(-2, 0, 0)});
        myModel.Children.Add(new GeometryModel3D { Geometry = mesh, Transform = new TranslateTransform3D(2, 0, 0)});

        System.Windows.Media.Media3D.Material mat = MaterialHelper.CreateMaterial(new SolidColorBrush(Color.FromRgb(255, 255, 255)));

        foreach (System.Windows.Media.Media3D.GeometryModel3D geometryModel in myModel.Children)
        {
            geometryModel.Material = mat;
            geometryModel.BackMaterial = mat;
        }

        visualModel.Content = myModel;
    }

    private double angle = 0;
}

使用这些参数,旋转非常顺利,但是您必须在应用程序上测试/调整它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62881837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档