将旋转/平移附加到按钮meshgeometry3d wpf c#

时间:2016-06-09 20:41:25

标签: c# wpf xaml button rotation

我对wpf和c#还很陌生,我正在做一个关于旋转,平移网格的项目。

我在XAML中创建了网格,一切看起来都很好。当我尝试编程旋转/翻译按钮时,我遇到了问题。因为,大多数代码都在XAML中,我猜测旋转也应该在XAML中,但是我找不到将这种旋转附加到按钮的方法。我刚刚找到了一些例子,展示了如何旋转一个不是我想要的按钮。 因此,我想知道是否有办法在XAML中执行此操作,或者我需要在* .cs文件中传输网格的所有位置和索引。

这是我的XAML代码:

<Window x:Class="WpfApplication16.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication16"
    mc:Ignorable="d"
    Title="MainWindow" Height="979" Width="1319">
<Grid Margin="0,0,102,6">
    <Viewport3D Margin="0,0,600,-100" d:IsLocked="True" >
        <ModelVisual3D>
            <ModelVisual3D.Content>
                <Model3DGroup>
                    <GeometryModel3D>
                        <GeometryModel3D.Geometry>

                            <!-- Left: Corner Normal is (-1, 1, 2) -->
                            <MeshGeometry3D Positions="2,1,1
                                            1.7,1.7,1
                                            1,2,1
                                            0.3,1.7,1
                                            0 1 1
                                            0.3 0.3 1
                                            1 0 1
                                            1.7 0.3 1
                                            1.5 1 1
                                            1.35 1.35 1
                                            1 1.5 1

                                            0.65 1.35 1
                                            0.5 1 1
                                            0.65 0.65 1
                                            1 0.5 1
                                            1.35 0.65 1
                                            1.5 1 0.7
                                            1.35 1.35 0.7
                                            1 1.5 0.7
                                            0.65 1.35 0.7
                                            0.5 1 0.7
                                            0.65 0.65 0.7
                                            1 0.5 0.7
                                            1.35 0.65 0.7"

                                        TriangleIndices="0 1 9, 0 9 8,
                                                         1 2 10,
                                                         1 10 9, 2 3 11,
                                                         2 11 10, 3 4 12,
                                                         3 12 11, 4 5 13,
                                                         4 13 12, 5 6 14,
                                                         5 14 13, 6 7 15,
                                                         6 15 14, 7 0 8,
                                                         7 8 15, 8 9 15,
                                                         9 14 15, 9 10 14,
                                                         10 13 14, 10 11 13,
                                                         11 12 13

                                                         0 1 17, 0 17 16,
                                                         1 2 18, 1 18 17,
                                                         2 3 19, 2 19 18,
                                                         3 4 20, 3 20 19,
                                                         4 5 21, 4 21 20,
                                                         5 6 22, 5 22 21,
                                                         6 7 23, 6 23 22,
                                                         7 0 16, 7 16 23,
                                                         16 17 24, 17 18 24,
                                                         18 19 24, 19 20 24,
                                                         20 21 24, 21 22 24,
                                                         22 23 24, 23 16 24

                                                         16 17 23, 17 22 23,
                                                         17 18 22, 18 21 22,
                                                         18 19 21, 19 20 21" />
                        </GeometryModel3D.Geometry>

                        <GeometryModel3D.Material>
                            <DiffuseMaterial Brush="Red" />
                        </GeometryModel3D.Material>

                        <GeometryModel3D.Transform>
                            <RotateTransform3D>
                                <RotateTransform3D.Rotation>
                                    <AxisAngleRotation3D x:Name="rotate" Axis="1 0 0" Angle="10"/>
                                </RotateTransform3D.Rotation>
                            </RotateTransform3D>
                        </GeometryModel3D.Transform>
                    </GeometryModel3D>

                    <GeometryModel3D>
                        <GeometryModel3D.Geometry>
                            <!-- Right: Corner Normal is (-1, 2, 1) -->
                            <MeshGeometry3D Positions=" 1.5 1 0.7
                                                    1.35 1.35 0.7
                                                    1 1.5 0.7
                                                    0.65 1.35 0.7
                                                    0.5 1 0.7
                                                    0.65 0.65 0.7
                                                    1 0.5 0.7
                                                    1.35 0.65 0.7
                                                    1 1 0.5"

                                        TriangleIndices="0 1 8
                                                            1 2 8
                                                            2 3 8
                                                            3 4 8
                                                            4 5 8
                                                            5 6 8
                                                            6 7 8
                                                            7 0 8" />
                        </GeometryModel3D.Geometry>

                        <GeometryModel3D.Material>
                            <DiffuseMaterial Brush="Blue" />
                        </GeometryModel3D.Material>

                        <GeometryModel3D.Transform>
                            <RotateTransform3D>
                                <RotateTransform3D.Rotation>
                                    <AxisAngleRotation3D x:Name="rotate1" Axis="1 0 0" Angle="10"/>
                                </RotateTransform3D.Rotation>
                            </RotateTransform3D>
                        </GeometryModel3D.Transform>
                    </GeometryModel3D>

                    <!-- Light source -->
                    <DirectionalLight Color="White" Direction="-5,-5,-5" />

                </Model3DGroup>
            </ModelVisual3D.Content>
        </ModelVisual3D>


        <!-- Camera -->
        <Viewport3D.Camera>
            <OrthographicCamera Position="0 0 10"
                           LookDirection="0 0 -1"
                           UpDirection="0 1 0"
                           Width="6" />
        </Viewport3D.Camera>

    </Viewport3D>
    <Button x:Name="button" Content="Rotation de 10 degrées en X" HorizontalAlignment="Right" Height="81" Margin="0,68.5,0,0" VerticalAlignment="Top" Width="316"/>
    <Button x:Name="button1" Content="Rotation 5 degrées en Y" HorizontalAlignment="Right" Height="81" Margin="0,223.667,0,0" VerticalAlignment="Top" Width="316"/>
    <Button x:Name="button2" Content="Translation X+" Margin="0,353,0,0" Height="84" VerticalAlignment="Top" HorizontalAlignment="Right" Width="307"/>
    <Button x:Name="button3" Content="Translation Y+" HorizontalAlignment="Right" Height="81" Margin="0,503.5,0,0" VerticalAlignment="Top" Width="316"/>
    <Button x:Name="button4" Content="Translation Z+" HorizontalAlignment="Right" Height="81" Margin="0,0,0,217" VerticalAlignment="Bottom" Width="316" d:LayoutOverrides="VerticalAlignment"/>

</Grid>
</Window>

以下是我在* .cs文件中尝试的内容:

namespace WpfApplication16
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
{
    AxisAngleRotation3D rotx;

    public MainWindow()
    {
        InitializeComponent();

        rotx = new AxisAngleRotation3D(new Vector3D(1, 0, 0), 10);

    }

    MeshGeometry3D myGeometryModel = new MeshGeometry3D();

    private void button_click(object sender, RoutedEventArgs e)
    {

        //GeometryModel3D.Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), angle += 10));

        rotx.Angle += 10;
        rotate.Angle += 10;
        rotate1.Angle += 10;

        RotateTransform3D myRotateTransform3D = new RotateTransform3D();
        AxisAngleRotation3D myAXisAngleRotation3d = new AxisAngleRotation3D();
        myAXisAngleRotation3d.Axis = new Vector3D(1, 0, 0);
        myAXisAngleRotation3d.Angle = 10;
        myRotateTransform3D.Rotation = myAXisAngleRotation3d;

        Transform3DGroup myTransform3Dgroup = new Transform3DGroup();
        myTransform3Dgroup.Children.Add(myRotateTransform3D);

        myGeometryModel.Transform = myTransform3Dgroup;


    }
}
}

这是第一个按钮的尝试,我想围绕X轴旋转10度。并且它无法正常工作。

结果是在最后一行说错误。 Transform is not a method group。其他按钮将用于围绕Y轴旋转。另外3个用于在3轴方向x,y,z上平移1个单位。

同样,我可以使用我在XAML中声明的内容,或者我需要在* .cs文件中输入这些信息吗?

这可能是一个非常简单的答案,但就像我说的那样,我是新手。所以提前谢谢。

1 个答案:

答案 0 :(得分:0)

将变换的xaml更改为:

<GeometryModel3D.Transform>
    <Transform3DGroup>
        <RotateTransform3D>
            <RotateTransform3D.Rotation>
                <AxisAngleRotation3D x:Name="rotate" Axis="1 0 0" Angle="10"/>
            </RotateTransform3D.Rotation>
        </RotateTransform3D>
        <RotateTransform3D>
            <RotateTransform3D.Rotation>
                <AxisAngleRotation3D x:Name="rotatey" Axis="0 1 0" Angle="0"/>
            </RotateTransform3D.Rotation>
        </RotateTransform3D>
        <TranslateTransform3D x:Name="translate" OffsetX="0" OffsetY="0" OffsetZ="0" />
    </Transform3DGroup>
</GeometryModel3D.Transform>

同样对于蓝色网格:

<GeometryModel3D.Transform>
    <Transform3DGroup>
        <RotateTransform3D>
            <RotateTransform3D.Rotation>
                <AxisAngleRotation3D x:Name="rotate1" Axis="1 0 0" Angle="10"/>
            </RotateTransform3D.Rotation>
        </RotateTransform3D>
        <RotateTransform3D>
            <RotateTransform3D.Rotation>
                <AxisAngleRotation3D x:Name="rotatey1" Axis="0 1 0" Angle="0"/>
            </RotateTransform3D.Rotation>
        </RotateTransform3D>
        <TranslateTransform3D x:Name="translate1" OffsetX="0" OffsetY="0" OffsetZ="0" />
    </Transform3DGroup>
</GeometryModel3D.Transform>

然后按下按钮点击这个:

private void button_click(object sender, RoutedEventArgs e)
{
    rotate.Angle += 10;
    rotate1.Angle += 10;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
    rotatey.Angle += 5;
    rotatey1.Angle += 5;
}
private void button2_Click(object sender, RoutedEventArgs e)
{
    translate.OffsetX += 1;
    translate1.OffsetX += 1;
}
private void button3_Click(object sender, RoutedEventArgs e)
{
    translate.OffsetY += 1;
    translate1.OffsetY += 1;
}
private void button4_Click(object sender, RoutedEventArgs e)
{
    translate.OffsetZ += 1;
    translate1.OffsetZ += 1;
}

我希望有所帮助。