在上一篇的文章里我们知道如何构造一个简单的三维场景,这次的课程我将和大家一起来研究如何用代码,完成对建立好了的三维场景的观察。

首先看一下DEMO的界面:

使用WPF实现3D场景[二]

可以看到8个方向的按钮,它们将提供观察角度的变化和三维场景的旋转这样的功能。

观察位置变化:

实现原理:改变场景内照相机的绝对位置等属性

实现代码:

定义照相机

使用WPF实现3D场景[二]<Viewport3DName="myViewport"Margin="0,0,0,0">
使用WPF实现3D场景[二]
<Viewport3D.Camera>
使用WPF实现3D场景[二]
<PerspectiveCamerax:Name="myViewportCamera"FarPlaneDistance="5000"NearPlaneDistance="0.25"FieldOfView="90"Position="1800,0,0"LookDirection="-1,0,0"UpDirection="0,1,0"></PerspectiveCamera>
使用WPF实现3D场景[二]
</Viewport3D.Camera>

定义照相机(观察角度)的变化事件:

使用WPF实现3D场景[二]voidrightButton_Click(objectsender,RoutedEventArgse)
使用WPF实现3D场景[二]使用WPF实现3D场景[二]
...{
使用WPF实现3D场景[二]
this.myViewportCamera.Position=newSystem.Windows.Media.Media3D.Point3D(this.myViewportCamera.Position.X,this.myViewportCamera.Position.Y,this.myViewportCamera.Position.Z+100);
使用WPF实现3D场景[二]}

使用WPF实现3D场景[二]
使用WPF实现3D场景[二]
voidleftButton_Click(objectsender,RoutedEventArgse)
使用WPF实现3D场景[二]使用WPF实现3D场景[二]
...{
使用WPF实现3D场景[二]
this.myViewportCamera.Position=newSystem.Windows.Media.Media3D.Point3D(this.myViewportCamera.Position.X,this.myViewportCamera.Position.Y,this.myViewportCamera.Position.Z-100);
使用WPF实现3D场景[二]}

使用WPF实现3D场景[二]
使用WPF实现3D场景[二]
voidbackButton_Click(objectsender,RoutedEventArgse)
使用WPF实现3D场景[二]使用WPF实现3D场景[二]
...{
使用WPF实现3D场景[二]
this.myViewportCamera.Position=newSystem.Windows.Media.Media3D.Point3D(this.myViewportCamera.Position.X+100,this.myViewportCamera.Position.Y,this.myViewportCamera.Position.Z);
使用WPF实现3D场景[二]}

使用WPF实现3D场景[二]
使用WPF实现3D场景[二]
voidfrontButton_Click(objectsender,RoutedEventArgse)
使用WPF实现3D场景[二]使用WPF实现3D场景[二]
...{
使用WPF实现3D场景[二]
this.myViewportCamera.Position=newSystem.Windows.Media.Media3D.Point3D(this.myViewportCamera.Position.X-100,this.myViewportCamera.Position.Y,this.myViewportCamera.Position.Z);
使用WPF实现3D场景[二]}

三维场景角度变化:

实现原理:改变三维场景内定义的轴的角度

实现代码:

定义操作轴:

使用WPF实现3D场景[二]<ModelVisual3D.Transform>
使用WPF实现3D场景[二]
<Transform3DGroup>
使用WPF实现3D场景[二]
<MatrixTransform3D/>
使用WPF实现3D场景[二]
<RotateTransform3D>
使用WPF实现3D场景[二]
<RotateTransform3D.Rotation>
使用WPF实现3D场景[二]
<AxisAngleRotation3DAngle="0"Axis="0,10,0"x:Name="myAngleRotationChair"/>
使用WPF实现3D场景[二]
</RotateTransform3D.Rotation>
使用WPF实现3D场景[二]
</RotateTransform3D>
使用WPF实现3D场景[二]
<RotateTransform3D>
使用WPF实现3D场景[二]
<RotateTransform3D.Rotation>
使用WPF实现3D场景[二]
<AxisAngleRotation3DAngle="0"Axis="0,0,10"x:Name="myAngleRotationChair_1"/>
使用WPF实现3D场景[二]
</RotateTransform3D.Rotation>
使用WPF实现3D场景[二]
</RotateTransform3D>
使用WPF实现3D场景[二]
</Transform3DGroup>
使用WPF实现3D场景[二]
</ModelVisual3D.Transform>
使用WPF实现3D场景[二]
</ModelVisual3D>

定义轴旋转代码:

使用WPF实现3D场景[二]voiddown_Click(objectsender,RoutedEventArgse)
使用WPF实现3D场景[二]使用WPF实现3D场景[二]
...{
使用WPF实现3D场景[二]
this.myAngleRotationChair_1.Angle-=10;
使用WPF实现3D场景[二]}

使用WPF实现3D场景[二]
使用WPF实现3D场景[二]
voidup_Click(objectsender,RoutedEventArgse)
使用WPF实现3D场景[二]使用WPF实现3D场景[二]
...{
使用WPF实现3D场景[二]
this.myAngleRotationChair_1.Angle+=10;
使用WPF实现3D场景[二]}

使用WPF实现3D场景[二]
使用WPF实现3D场景[二]
voidleft_Click(objectsender,RoutedEventArgse)
使用WPF实现3D场景[二]使用WPF实现3D场景[二]
...{
使用WPF实现3D场景[二]
this.myAngleRotationChair.Angle-=10;
使用WPF实现3D场景[二]}

使用WPF实现3D场景[二]
使用WPF实现3D场景[二]
voidright_Click(objectsender,RoutedEventArgse)
使用WPF实现3D场景[二]使用WPF实现3D场景[二]
...{
使用WPF实现3D场景[二]
this.myAngleRotationChair.Angle+=10;
使用WPF实现3D场景[二]}

好的~如果您对更多的三维场景变成想有所了解,请关注第三讲。

如果您想下载源代码或收听语音教程,请访问:微软webcast

再次感谢您的关注,谢谢!

相关文章: