1. 程式人生 > >使用WPF實現3D場景[二]

使用WPF實現3D場景[二]

his order sof 分享圖片 align css log sid nim

原文:使用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>

定義照相機(觀察角度)的變化事件:

技術分享圖片????????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?);
技術分享圖片????????}

?

三維場景角度變化:

實現原理:改變三維場景內定義的軸的角度

實現代碼:

定義操作軸:

技術分享圖片??????????<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>

定義軸旋轉代碼:

技術分享圖片?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;
技術分享圖片????????}

?

好的~如果您對更多的三維場景變成想有所了解,請關註第三講。

如果您想下載源代碼或收聽語音教程,請訪問:微軟webcast

?

再次感謝您的關註,謝謝!

?

使用WPF實現3D場景[二]