2016-06-09 44 views
1

我对wpf和c#还很新,我正在做一个关于旋转,网格平移的项目。将旋转/平移到按钮meshgeometry3d 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文件中?

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

+0

在button_click中,rotate.Angle + = 10(用于红色)和rotate1.Angle + = 10(用于蓝色)就是需要旋转的所有东西。 myGeometryModel甚至不在窗口中。我有点困惑你的问题。 –

回答

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

我希望有所帮助。

+0

非常感谢!但我复制了xaml和cs文件,但按钮没有响应?没有什么东西在动,我尝试了一个新的绘图,但没有任何动作。你有什么想法可能是什么? – user3558419

+0

您是否将按钮连接到事件处理程序? <按钮...点击=“button1_Click”/> –

+0

这就是我忘了。我很抱歉,但是非常感谢你的时间。 – user3558419