旋转3D魔方在WPF [英] Rotate 3D cube in WPF

查看:222
本文介绍了旋转3D魔方在WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做一个3D立方体与WPF XAML中code是这样的:

I make a 3D cube in WPF with the XAML code like this:

<Viewport3D Name="viewport3D1">
            <Viewport3D.Camera>
                <PerspectiveCamera x:Name="camMain" Position="6 5 4" LookDirection="-6 -5 -4">
                </PerspectiveCamera>
            </Viewport3D.Camera>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <DirectionalLight x:Name="dirLightMain" Direction="-1,-1,-1">
                    </DirectionalLight>
                </ModelVisual3D.Content>
            </ModelVisual3D>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <GeometryModel3D>
                        <GeometryModel3D.Geometry>
                            <MeshGeometry3D x:Name="meshMain"
                                Positions="0 0 0  1 0 0  0 1 0  1 1 0  0 0 1  1 0 1  0 1 1  1 1 1"
                                TriangleIndices="2 3 1  2 1 0  7 1 3  7 5 1  6 5 7  6 4 5  6 2 0  2 0 4  2 7 3  2 6 7  0 1 5  0 5 4">
                            </MeshGeometry3D>
                        </GeometryModel3D.Geometry>
                        <GeometryModel3D.Material>
                            <DiffuseMaterial x:Name="matDiffuseMain">
                                <DiffuseMaterial.Brush>
                                    <SolidColorBrush Color="Red"/>
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </GeometryModel3D.Material>
                    </GeometryModel3D>
                </ModelVisual3D.Content>
            </ModelVisual3D>
        </Viewport3D>

然后就是我的窗口的构造,我想申请绕轴OX,OY,OZ我认为应该是这样做的轮换:

Then is constructor of my window, I want to apply rotations around axis OX, OY, OZ which I think is supposed to be done like this:

RotateTransform3D myRotateTransform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 2, 0), 1));
        meshMain.Transform=myRotateTransform;
        // etc...

我似乎并不适用变换XAML的正确节点,它的正确的方式来实现我的情况变换?

It seems I don't apply transform to proper node of XAML, what it the right way to effect transform in my case?

推荐答案

您需要给你的<一个href="http://msdn.microsoft.com/en-us/library/system.windows.media.media3d.meshgeometry3d.aspx">ModelVisual3D名称,<一href="http://msdn.microsoft.com/en-us/library/system.windows.media.media3d.meshgeometry3d.aspx">MeshGeometry3D不具有<一个href="http://msdn.microsoft.com/en-us/library/system.windows.media.media3d.meshgeometry3d.aspx">Transform物业那里的模型一样。你还需要有才能设置角度属性访问到你的 AxisAngleRotation3D 对象。

You need to give your ModelVisual3D a name, MeshGeometry3D does not have a Transform Property where as the Model does. You also need to have access to your AxisAngleRotation3D object in order to set the Angle property.

<ModelVisual3D x:Name="MyModel">
....

修改添加了更多的$ C $下codeBehind方法

Edit added more code for CodeBehind method

public partial class MainWindow : Window
{
    AxisAngleRotation3D ax3d;
    public MainWindow()
    {
        InitializeComponent();

        ax3d = new AxisAngleRotation3D(new Vector3D(0, 2, 0), 1);
        RotateTransform3D myRotateTransform = new RotateTransform3D(ax3d);
        MyModel.Transform = myRotateTransform;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        ax3d.Angle += 1 ;
    }
}

尽管在这种情况下,我想你会过得更好实现你在XAML变换。

Though in this case I think you would be better off implementing your Transform in the Xaml.

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Viewport3D Name="viewport3D1">
            <Viewport3D.Camera>
                <PerspectiveCamera x:Name="camMain" Position="6 5 4" LookDirection="-6 -5 -4">
                </PerspectiveCamera>
            </Viewport3D.Camera>
            <ModelVisual3D>
                <ModelVisual3D.Content>
                    <DirectionalLight x:Name="dirLightMain" Direction="-1,-1,-1">
                    </DirectionalLight>
                </ModelVisual3D.Content>
            </ModelVisual3D>
            <ModelVisual3D x:Name="MyModel">
                <ModelVisual3D.Content>
                    <GeometryModel3D>
                        <GeometryModel3D.Geometry>
                            <MeshGeometry3D x:Name="meshMain" 
                                Positions="0 0 0  1 0 0  0 1 0  1 1 0  0 0 1  1 0 1  0 1 1  1 1 1" 
                                TriangleIndices="2 3 1  2 1 0  7 1 3  7 5 1  6 5 7  6 4 5  6 2 0  2 0 4  2 7 3  2 6 7  0 1 5  0 5 4">
                            </MeshGeometry3D>
                        </GeometryModel3D.Geometry>
                        <GeometryModel3D.Material>
                            <DiffuseMaterial x:Name="matDiffuseMain">
                                <DiffuseMaterial.Brush>
                                    <SolidColorBrush Color="Red"/>
                                </DiffuseMaterial.Brush>
                            </DiffuseMaterial>
                        </GeometryModel3D.Material>
                    </GeometryModel3D>
                </ModelVisual3D.Content>
                <ModelVisual3D.Transform>
                    <RotateTransform3D>
                        <RotateTransform3D.Rotation>
                            <AxisAngleRotation3D x:Name="rotate" Axis="0 2 0"/>
                        </RotateTransform3D.Rotation>
                    </RotateTransform3D>
                </ModelVisual3D.Transform>
            </ModelVisual3D>
        </Viewport3D>
        <Slider Height="23" HorizontalAlignment="Left" 
                Margin="12,12,0,0" Name="slider1"
                VerticalAlignment="Top" Width="187" 
                Maximum="360"
                Value="{Binding ElementName=rotate, Path=Angle}" />

    </Grid>
</Window>

另外这个例子中,你可以通过设置更改codeBehind您AxisAngleRotation3D其角度属性:

Also with this example you can change your AxisAngleRotation3D in the CodeBehind by setting its Angle Property:

rotate.Angle +=1;

这篇关于旋转3D魔方在WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆