Unity3d modifying the mesh data of a model

Source: Internet
Author: User

"Dog planing Learning Net"after the model is imported into unity, you can use Unity's Inspector panel to pan, rotate, and scale the model on an axis (1).

Figure 1

Inspector Panel provides the function is WYSIWYG, immediately after adjustment can see the effect, quite convenient. But these functions do not fully meet the requirements of game development, for example, in order to reduce the workload of the art of some games on the scene of the model standards made some rules, so that these modular small model can be in accordance with a certain pattern of splicing into a rich and varied large models. The stitching process tends to adjust the local coordinates of the model in order to eventually form a "seamless" large model, which is often done during game run time (or when the map is initialized). The engine is therefore required to provide an interface to modify the model mesh. Fortunately, Unity provides such an interface.

Mesh and Meshfilterneed to modify the model grid data, the first step is to find a way to get the mesh of the model, here we need to explain in the mesh filter,unity document as follows:
The
mesh Filter takes a mesh from your assets and passes it to the mesh Renderer for rendering on the SCR Een.

Figure 2

For example, the mesh of the gun robot in the hand is stored in a mesh named Gun_model, which belongs to the meshfilter of Gun_model. Knowing where the grid data is stored, the next thing you need to do is get the data and modify it. Here is an example of how to modify the grid data for a model using a simple cliff model.

Simple Examplefirst of all, what does the original model look like? Figure 3now we need to modify the grid data of the model, and pick the height of the highest point of the model one times higher, 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:
: int p = 0;
One : int flag =-1;
: float maxheight = 0.0F;
: while (p < vertices. Length) {
: if(Vertices[p].z > MaxHeight) {
: maxheight = vertices[p].z;
: flag = p;
: }
: p++;
: }
: vertices[flag] + = new Vector3 (0, 0, maxheight);
£ º
: mesh.vertices = vertices;
At : mesh. Recalculatenormals ();
: }
: }

Note: In this example, the z-axis of the model's local coordinate system corresponds to Unity's y-axis, so the above code modifies the z-axis. The results of the code run are as follows:

Figure 4

isn't it simple! Unity's powerful interfaces and more detailed documentation are indeed a boon for developers, plus C # programming is really cool for my hard-pressed C + + programmers! It is also worth noting that the script corresponding to the above code must be drag to the owner of the Meshfilter in Gameobject to make the code effective. If 5 must drag the script to Cliffs01 to take effect, because in cliffs_1cm gameobject only Cliffs01 has Meshfilter (6)

Figure 5


20120805231657857.png (17.84 KB, download number: 0)

Download attachments to albums

Yesterday 11:25 upload

Figure 6

Disclaimer: This document is from the "Dog Planing Learning Network" community, is the Netizen self-published Unity3d study articles, if there is any content violated your relevant interests, please communicate with the official, we will deal with the real-time.

Unity3d modifying the mesh data of a model

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.