Unity3D: Modify the mesh data of a model, and unity3d model mesh

Source: Internet
Author: User

Unity3D: Modify the mesh data of a model, and unity3d model mesh

[Dog learning network] after a model is imported into unity, you can use the Inspector panel of unity to pan, rotate, and scale the model on a certain coordinate axis (1 ).

Figure 1

The Inspector Panel provides what you see is what you get. After adjustment, you can see the effect immediately, which is quite convenient. However, these functions cannot fully meet the game development requirements. For example, to reduce the workload of artists, some games have made some provisions on model standards in scenarios, these modular small models can be spliced into various large models according to certain rules. During the splicing process, the local coordinates of the model must be adjusted to form a "seamless" large model. This process is often performed during game operation (or during map initialization ), therefore, the engine must provide an interface for modifying the model mesh. Fortunately, unity provides such an interface.

Mesh and MeshFilter need to modify the mesh data of the model. The first step is to obtain the Mesh of the model. Here we need Mesh Filter. The explanation in the unity document is as follows:
The Mesh Filter takes a mesh from your assets and passes it to the Mesh Renderer for rendering on the screen.

Figure 2

For example, the Mesh data of the gun in the Robot hand is stored in the Mesh named gun_model, and this Mesh belongs to the MeshFilter of Gun_model. Knowing the location where the grid data is stored, what needs to be done next is to obtain and modify the data. The following uses a simple cliff model as an example to describe how to modify the grid data of a model.

Simple Example first, let's look at what the original model looks like? Figure 3 now we need to modify the mesh data of the model and double the height coordinate of the model's highest point. The Code is as follows: 1: using UnityEngine; 2: using System. Collections;
3:
4: [RequireComponent (typeof (MeshFilter)]
5: public class example: MonoBehaviour {
6: void Update (){
7: Mesh mesh = GetComponent (). mesh;
8: Vector3 [] vertices = mesh. vertices;
9:
10: int p = 0;
11: int flag =-1;
12: float maxheight = 0.0F;
13: while (p <vertices. Length ){
14: if (vertices [p]. z> maxheight ){
15: maxheight = vertices [p]. z;
16: flag = p;
17 :}
18: p ++;
19 :}
20: vertices [flag] + = new Vector3 (0, 0, maxheight );
21:
22: mesh. vertices = vertices;
23: mesh. RecalculateNormals ();
24 :}
25 :}

Note: The Z axis of the local Coordinate System of the model used in this example is equivalent to the Y axis of unity. Therefore, the Z axis is modified in the preceding code. The code execution result is as follows:

Figure 4

Is it easy! The powerful interfaces and more detailed documents of unity are indeed a good news for developers. In addition, using C # programming is really good for the unqualified C ++ programmers! Note that the script corresponding to the above Code must be drag to the owner of MeshFilter in the GameObject to make the Code take effect. For example, 5 the script must be dragged to Cliffs01 to take effect, because only Cliffs01 has MeshFilter (6) in the cliffs_ 1cm GameObject)

Figure 5


20120805231657857. png (17.84 KB, downloads: 0)

Download the attachment and save it to the album.

Uploaded at yesterday

Figure 6

Disclaimer: This document is from the "Dog Learning Network" community. It is an Unity3D learning article published by netizens. If any content infringes on your rights and interests, please contact the official website, it will be processed in real time.

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.