使用WPF實現3D場景[二]
阿新 • • 發佈:2018-10-22
his order sof 分享圖片 align css log sid nim 原文:使用WPF實現3D場景[二]
????????< 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> 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?);
????????}
????????????<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>
????????{
????????????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;
????????}
在上一篇的文章裏我們知道如何構造一個簡單的三維場景,這次的課程我將和大家一起來研究如何用代碼,完成對建立好了的三維場景的觀察。
首先看一下DEMO的界面:
?
?
可以看到8個方向的按鈕,它們將提供觀察角度的變化和三維場景的旋轉這樣的功能。
觀察位置變化:
實現原理:改變場景內照相機的絕對位置等屬性
實現代碼:
定義照相機
<Viewport3D?Name="myViewport"?Margin="0,0,0,0">????????<
??????????<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(????????{
????????????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場景[二]