在 WPF 中旋转 3D 立方体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12655033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rotate 3D cube in WPF
提问by Vladimir Stazhilov
I make a 3D cube in WPF with the XAML code like this:
我在 WPF 中使用 XAML 代码制作了一个 3D 立方体,如下所示:
<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>
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:
然后是我的窗口的构造函数,我想围绕轴 OX、OY、OZ 应用旋转,我认为应该这样做:
RotateTransform3D myRotateTransform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 2, 0), 1));
meshMain.Transform=myRotateTransform;
// etc...
It seems I don't apply transform to proper node of XAML, what it the right way to effect transform in my case?
似乎我没有将转换应用于 XAML 的正确节点,在我的情况下,它是实现转换的正确方法吗?
回答by Mark Hall
You need to give your ModelVisual3Da name, MeshGeometry3Ddoes not have a TransformProperty where as the Model does. You also need to have access to your AxisAngleRotation3Dobject in order to set the Angleproperty.
您需要为ModelVisual3D命名,MeshGeometry3D没有像 Model 那样的Transform属性。您还需要有权访问您的AxisAngleRotation3D对象才能设置该Angle属性。
<ModelVisual3D x:Name="MyModel">
....
Editadded more code for CodeBehind method
编辑为 CodeBehind 方法添加了更多代码
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 ;
}
}
Though in this case I think you would be better off implementing your Transform in the Xaml.
尽管在这种情况下,我认为您最好在 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>
Also with this example you can change your AxisAngleRotation3D in the CodeBehind by setting its Angle Property:
同样在此示例中,您可以通过设置其 Angle 属性来更改 CodeBehind 中的 AxisAngleRotation3D:
rotate.Angle +=1;

