In the previous article, we learned how to construct a simple 3D scenario. In this course, I will work with you to learn how to use code to complete the observation of the established 3D scenario.
First, let's take a look at the demo interface:
You can see the buttons in eight directions, which provide the ability to observe the angle change and rotate the 3D scene.
Observe the location changes:
Implementation principle: Changing the camera's absolute position and other attributes in the scenario
Implementation Code:
Define a camera
<Viewport3d name = "myviewport" margin = "0, 0, 0"
<Viewport3d. Camera>
<Perspectivecamera X: Name = "myviewportcamera" farplanedistance = "5000" nearplanedistance = "0.25" fieldofview = "90" position = "1800,0, 0" lookdirection =, 0 "updirection =" 0, 1 "> </perspectivecamera>
</Viewport3d. Camera>
Define a camera (viewing angle) change event:
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 plus 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. *-100, this. myviewportcamera. position. y, this. myviewportcamera. position. z );
}
3D scene angle changes:
Implementation principle: change the angle of the axis defined in a 3D scenario
Implementation Code:
Define the operation axis:
<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>
Define the axis rotation code:
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;
}
Okay ~ If you want to know more about 3D scenarios, please pay attention to the third section.
If you want to download the source code or listen to the voice tutorial, visit: Microsoft webcast
Thank you again for your attention. Thank you!