原文:
使用WPF实现3D场景[二]
在上一篇的文章里我们知道如何构造一个简单的三维场景,这次的课程我将和大家一起来研究如何用代码,完成对建立好了的三维场景的观察。
首先看一下DEMO的界面:
可以看到8个方向的按钮,它们将提供观察角度的变化和三维场景的旋转这样的功能。
观察位置变化:
实现原理:改变场景内照相机的绝对位置等属性
实现代码:
定义照相机
<
Viewport3D
Name
="myViewport"
Margin
="0,0,0,0"
>
< Viewport3D .Camera >
< PerspectiveCamera x:Name ="myViewportCamera" FarPlaneDistance ="5000" NearPlaneDistance ="0.25" FieldOfView ="90" Position ="1800,0,0" LookDirection ="-1,0,0" UpDirection ="0,1,0" ></ PerspectiveCamera >
</ Viewport3D.Camera >
< Viewport3D .Camera >
< PerspectiveCamera x:Name ="myViewportCamera" FarPlaneDistance ="5000" NearPlaneDistance ="0.25" FieldOfView ="90" Position ="1800,0,0" LookDirection ="-1,0,0" UpDirection ="0,1,0" ></ PerspectiveCamera >
</ Viewport3D.Camera >
定义照相机(观察角度)的变化事件:
void
rightButton_Click(
object
sender, RoutedEventArgs e)
{
this.myViewportCamera.Position = new System.Windows.Media.Media3D.Point3D(this.myViewportCamera.Position.X, this.myViewportCamera.Position.Y , this.myViewportCamera.Position.Z +100);
}
void leftButton_Click( object sender, RoutedEventArgs e)
{
this.myViewportCamera.Position = new System.Windows.Media.Media3D.Point3D(this.myViewportCamera.Position.X , this.myViewportCamera.Position.Y, this.myViewportCamera.Position.Z- 100);
}
void backButton_Click( object sender, RoutedEventArgs e)
{
this.myViewportCamera.Position = new System.Windows.Media.Media3D.Point3D(this.myViewportCamera.Position.X + 100, this.myViewportCamera.Position.Y, this.myViewportCamera.Position.Z );
}
void frontButton_Click( object sender, RoutedEventArgs e)
{
this.myViewportCamera.Position = new System.Windows.Media.Media3D.Point3D(this.myViewportCamera.Position.X - 100, this.myViewportCamera.Position.Y , this.myViewportCamera.Position.Z );
}
{
this.myViewportCamera.Position = new System.Windows.Media.Media3D.Point3D(this.myViewportCamera.Position.X, this.myViewportCamera.Position.Y , this.myViewportCamera.Position.Z +100);
}
void leftButton_Click( object sender, RoutedEventArgs e)
{
this.myViewportCamera.Position = new System.Windows.Media.Media3D.Point3D(this.myViewportCamera.Position.X , this.myViewportCamera.Position.Y, this.myViewportCamera.Position.Z- 100);
}
void backButton_Click( object sender, RoutedEventArgs e)
{
this.myViewportCamera.Position = new System.Windows.Media.Media3D.Point3D(this.myViewportCamera.Position.X + 100, this.myViewportCamera.Position.Y, this.myViewportCamera.Position.Z );
}
void frontButton_Click( object sender, RoutedEventArgs e)
{
this.myViewportCamera.Position = new System.Windows.Media.Media3D.Point3D(this.myViewportCamera.Position.X - 100, this.myViewportCamera.Position.Y , this.myViewportCamera.Position.Z );
}
三维场景角度变化:
实现原理:改变三维场景内定义的轴的角度
实现代码:
定义操作轴:
<
ModelVisual3D
.Transform
>
< Transform3DGroup >
< MatrixTransform3D />
< RotateTransform3D >
< RotateTransform3D .Rotation >
< AxisAngleRotation3D Angle ="0" Axis ="0,10,0" x:Name ="myAngleRotationChair" />
</ RotateTransform3D.Rotation >
</ RotateTransform3D >
< RotateTransform3D >
< RotateTransform3D .Rotation >
< AxisAngleRotation3D Angle ="0" Axis ="0,0,10" x:Name ="myAngleRotationChair_1" />
</ RotateTransform3D.Rotation >
</ RotateTransform3D >
</ Transform3DGroup >
</ ModelVisual3D.Transform >
</ ModelVisual3D >
< Transform3DGroup >
< MatrixTransform3D />
< RotateTransform3D >
< RotateTransform3D .Rotation >
< AxisAngleRotation3D Angle ="0" Axis ="0,10,0" x:Name ="myAngleRotationChair" />
</ RotateTransform3D.Rotation >
</ RotateTransform3D >
< RotateTransform3D >
< RotateTransform3D .Rotation >
< AxisAngleRotation3D Angle ="0" Axis ="0,0,10" x:Name ="myAngleRotationChair_1" />
</ RotateTransform3D.Rotation >
</ RotateTransform3D >
</ Transform3DGroup >
</ ModelVisual3D.Transform >
</ ModelVisual3D >
定义轴旋转代码:
void
down_Click(
object
sender, RoutedEventArgs e)
{
this.myAngleRotationChair_1.Angle -= 10;
}
void up_Click( object sender, RoutedEventArgs e)
{
this.myAngleRotationChair_1.Angle += 10;
}
void left_Click( object sender, RoutedEventArgs e)
{
this.myAngleRotationChair.Angle -= 10;
}
void right_Click( object sender, RoutedEventArgs e)
{
this.myAngleRotationChair.Angle += 10;
}
{
this.myAngleRotationChair_1.Angle -= 10;
}
void up_Click( object sender, RoutedEventArgs e)
{
this.myAngleRotationChair_1.Angle += 10;
}
void left_Click( object sender, RoutedEventArgs e)
{
this.myAngleRotationChair.Angle -= 10;
}
void right_Click( object sender, RoutedEventArgs e)
{
this.myAngleRotationChair.Angle += 10;
}
好的~如果您对更多的三维场景变成想有所了解,请关注第三讲。
如果您想下载源代码或收听语音教程,请访问:微软webcast
再次感谢您的关注,谢谢!