Mesh Basics

1. Mesh concept and composition

If you want to display a 3D model in Unity, you must know the shape of the model, which is actually mesh data. After having the mesh data, send the mesh data to the Mesh Renderer component for rendering through the Mesh Filter component . The Mesh Filter component does not necessarily exist for every gameObject object, but the Mesh Rnder component must exist. We can also customize the data of the mesh, and then hang a Mesh Renderer component to render the effect we want.

Mesh concept: Mesh is the mesh of the model. Lines are formed by points, triangles are formed by lines, and surfaces are formed by triangles. Then, the surface of the mesh is expressed by materials, such as shadows and textures. In addition to the mesh of ui, Unity needs MeshFilter to determine the shape of the mesh, and renders the mesh through Material and MeshRenderer.

Mesh composition:

1. Vertex coordinates (vertex) : Vertices are the most basic part of the grid, which can be obtained and assigned through mesh.vertices.
mesh.vertices is an array of Vector3, and each Vector3 is the relative coordinate (local position) of this vertex and this game object.
There is no limit to the number of vertices. Several same vertices can be combined into several different shapes and different numbers of triangles.

2. Normal : A normal is a line perpendicular to a surface, which has a direction but no size. It can be obtained and assigned through mesh.normals. The normal array stores the normal of each vertex of the mesh, and its size corresponds to the vertex coordinates. normal[i] corresponds to the normal of the vertex[i]. The face whose normal is facing outward is the front face, and the opposite is the back face. Generally speaking, the face can only be seen from the front, and cannot be seen from the back. It can be calculated by mesh.RecalculateNormals() .

3. Texture coordinates (uv) : If you want to display a 3D mode in Unity, you must know the shape of the mode, which is actually mesh data. After having the mesh data, the mesh data is sent to the Mesh Renderer component for rendering through the Mesh Filter component. The Mesh Filter component does not necessarily exist for every gameObject object, but the Mesh Rnder component must exist. Then we can customize the mesh data, and then hang a Mesh Renderer component to render the effect we want.
The uv value is used in many ways, such as sampling the material texture, or using some of its characteristics (range 0-1; the uv value of each pixel in the same uv area is different) to perform some calculations in the GPU. mesh.uv as a Vector2 array must have the same length as mesh.vertices, and the order of each uv corresponds to the order of vertices , that is, uv[0] represents the uv value of vertices[0]. The order in which the triangles are formed is irrelevant. After each vertex is assigned a uv value, in the fragment shading stage, the fragment shader will automatically interpolate the uv value of each pixel covered by the triangle according to the uv value of the vertex. The uv interpolation is performed in units of triangles, that is, the uv value of each pixel in the triangle ABC is irrelevant to the uv value of each vertex of other surrounding triangles.

4. Triangle sequence (triangle) : triangles is an int array, which is a list of triangles containing the index of the vertex array.
The size of the triangle array is a multiple of 3, and each three ints represent which three vertices and in what order (orientation) form a triangle.
mesh.triangles determines the shape and orientation of the triangles in the mesh. The rendering order of the triangles is left-handed with the triangle's front normals. The three points of the triangle face toward us clockwise and away from us counterclockwise.

5.MeshFilter (grid filter): MeshFilter contains a Public member Mesh. The data of the 3D model (uv, triangle, vertex, etc.) is stored in the Mesh

2. Quadrilateral Mesh source code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class QuadMeshTest:MeshTest
{
    //2 * size
    public int size = 20;

  	//顶点
    protected override Vector3[] Vertices
    {
        get
        {
            Vector3[] vertices = new Vector3[4];
            vertices[0] = new Vector3(-1, 1, 0) * size;
            vertices[1] = new Vector3(-1, -1, 0) * size;
            vertices[2] = new Vector3(1, -1, 0) * size;
            vertices[3] = new Vector3(1, 1, 0) * size;
            return vertices;
        }
    }

    //三角形序列
    protected override int[] Triangles 
    { 
        get 
        { 
            // clockwise facing 
            int[] triangles = new int[6] 
            { 
                1, 0, 3, 3, 2, 1 
            }; 
            
            // // counterclockwise facing away 
            // int[ ] triangles = new int[6] 
            // { 
            // 1, 2, 3, 3, 0, 1, 
            // }; 
            // 
            // // double-sided grid 
            // int[] triangles = new int[12 ] 
            // { 
            // 1, 2, 3, 3, 0, 1, 1, 0, 3, 3, 2, 1 // } 
            ; 
            
            return triangles; 
        } 
    }

    protected override string MeshName
    {
        get
        {
            return "QuadMesh";
        }
    }

  	//纹理坐标
    protected override Vector2[] Uvs
    {
        get
        {
            Vector2[] uvs = new Vector2[4];
            uvs[0] = new Vector2(0, 1);
            uvs[1] = new Vector2(0, 0);
            uvs[2] = new Vector2(1, 0);
            uvs[3] = new Vector2(1, 1);
            
            return uvs;
        }
    }

  	//法线
    protected override Vector3[] Normals
    {
        get
        {
            mesh.RecalculateNormals();
            return mesh.normals;

        }
    }
}

Guess you like

Origin blog.csdn.net/m0_57771536/article/details/127964559