Use XAML in expression blend to create a 3D application

Source: Internet
Author: User
For more information, see <creating 3D content with WPF>.
Source File Download Http://www.wangpangzi.net/uploads/200703/3dlab.zip
The purpose of this article is to study the 3D feature of Windows Presentation Foundation (WPF) in the blend environment. First, we import a created 3D model Program , Rotate it with an animation. Then, we will study how to use it dynamically. Code Generate a model. Finally, we map the 2D user interface to the 3D model, and the user can interact with it.
Note: Source code It is designed for each stage, so you can start learning from the center or use it for phase backup. Each exercise also contains a complete source code after completion. You can view the final result directly through them.

Exercise 1 use static 3D Models(4 tasks in total)
In this exercise, you will import a created 3D model, use the viewport3d object to display it, and then use an animation to rotate it. Finally, you can use the mouse to rotate the model.

Task 1 Imports a 3D model.
although we can use XAML to directly create a 3D model, we recommend that you use professional tools to create a model for a slightly complex model, then export it to The XAML format for our use. In this exercise, we will use a 3D model created in this way, that is, a XAML file. And import it to blend.
1. Open Microsoft Expression blend.
2. Run File> new project... Create a new program.
3. In the new project window that appears, select standard application, enter show3dmodel in name, select your save folder, and click OK to create a new program.
4. In the source \ exercise1 sub-file, you will see a file named model. XAML, which contains the 3D model you will use. Open this file in notepad and you will see a lot of code, which is difficult and boring to input manually. The tool that generates this model file greatly simplifies the work, and automatically encapsulates the code in viewport3d and puts it in viewbox. You can put the entire file into xamlpad to preview the model.
5. paste all content in model. XAML to the grid label in the window. XAML file in the application.
6. Compile the program by pressing F5. The running result is as follows:

Task 2 changes the camera position.
In this task, we will learn how to change the camera position.
1. Open the window1.xaml file and find the viewport3d. Camera tag in viewport3d. It includes a perspectivecamera, that is, the perspective camera. Its updirection is currently 0, 1, 0. Change it to 0.2, and 0, and then press F5 to run the program to see the effect changes.
2. Change the camera position to 0.25, 0, 2.2339, and then run the program to observe the camera position changes.
3. Change position to 3, 0, 2.2339, and lookdirection to-3, 0,-2.2. Then run the program to observe the change.
4. Good. Please try a few data combinations at will now. Please note that if you try too much data, it is easy for the camera to go to a distant place, so there will be nothing to see.
5. After completing the experiment, use the code to return it to the initial default position for subsequent tasks.

<Perspectivecamera X: Name = "frontor9" farplanedistance = "10" lookdirection = "0, 0,-1"
Updirection = "2.29339, 0" nearplanedistance = "1" position ="
Fieldofview = "39.5978" type = "codeph" text = "/codeph"/>

Task 3 adds an animation to the model.
1. Open the window1.xaml file and find the viewport3d. Camera tag in viewport3d. After this tag, add the following code:

<Viewport3d. triggers>
<Eventtrigger routedevent = "viewport3d. Loaded">
<Eventtrigger. Actions>
<Beginstoryboard>
<Storyboard>
<Rotation3danimation storyboard. targetname = "wpflogoor12"
Storyboard. targetproperty = "(model3dgroup. Transform). (transform3dgroup. Children) [2]. (rotatetransform3d. Rotation )"
Duration = "0: 5" autoreverse = "true"
Repeatbehavior = "forever" accelerationratio = "0.4" decelerationratio = "0.4">
<Rotation3danimation. From>
<Axisanglerotation3d axis = "2, 10, 3" angle = "-45"/>
</Rotation3danimation. From>
<Rotation3danimation. To>
<Axisanglerotation3d axis = "2, 10, 3" angle = "45"/>
</Rotation3danimation. To>
</Rotation3danimation>

</Storyboard>
</Beginstoryboard>
</Eventtrigger. Actions>
</Eventtrigger>
</Viewport3d. triggers>

This Code uses the rotation3danimation label to add an animation to the model. The storyboard. targetname and storyboard. targetproperty attributes indicate the specific part of the model where an animation occurs. (Wpflogoor12 is a label in the model, representing the overall logo Model)
2. Run the program. The logo model will automatically rotate repeatedly.

Task 4 interactive rotation model.
In the last task of this exercise, we will allow the mouse to rotate the model. You will use 3D tools for the Windows Presentation Foundation. This is a set of tools that can help us use XAML to create 3D applications in WPF. You can download them from http://www.codeplex.com/3dtools. Of course, we do not need to download it now. The 3dtools. dll library file we want to use is in the source \ exercise1 folder of the instance.
1. Add the 3dtools. dll library file to the project. (On the project panel, right-click references and choose add reference ..., Find the 3dtools. dll file)
2. Open the window1.xaml file and add an XML namespace declaration to use 3D tools in this XAML file:

<Window X: class = "show3dmodel. window1"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: Tools = "CLR-namespace: _ 3 dtools; Assembly = 3 dtools"
Title = "show3dmodel" Height = "300" width = "300"
>

3. add the <tools: trackballdecorator> label so that the <viewport3d> </viewport3d> label is included in the entire tag <tools: trackballdecorator>... </tools: trackballdecorator>.
Trackballdecorator allows you to use the left mouse button to rotate the included model and right-click the model to zoom in.
4. Compile and run the program, and now it can rotate the model, zoom in, and interact with the model.

This exercise is complete
Http://www.wangpangzi.net/article.asp? Id = 87

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.