当前位置:网站首页>Unity3d learning notes 4 - create mesh advanced interface

Unity3d learning notes 4 - create mesh advanced interface

2022-07-07 23:22:00 charlee44

1. summary

In the article Unity3D Learning notes 2—— Draw a textured face Created a Mesh, But this interface is Unity Is called simple interface . It corresponds to ,Unity It also provides a set of advanced API To create Mesh.

2. Detailed discussion

according to Unity Discussion of documents , Higher performance can be achieved by using advanced interfaces , Be able to skip some validation checks . But this is not the most critical , The biggest disadvantage of simple interfaces is that the number of vertices exceeds 65535 There is a problem at an hour ( At least in the 2019.4.3f1 The version is still like this ).

Don't talk much , Go straight to the code :

using UnityEngine;
using UnityEngine.Rendering;

[ExecuteInEditMode]
public class Note4Main : MonoBehaviour
{
    
    public Material material;

    // Start is called before the first frame update
    void Start()
    {
    
        Mesh mesh = new Mesh();
        mesh.name = "quad";

        // Vertex data 
        VertexAttributeDescriptor[] vertexAttributeDescriptorList = new[]{
    
             new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3),
             new VertexAttributeDescriptor(VertexAttribute.Normal, VertexAttributeFormat.Float32, 3),
             new VertexAttributeDescriptor(VertexAttribute.TexCoord0, VertexAttributeFormat.Float32, 2)};

        const int vertexCount = 4;
        const int verticesAttributeBufferLength = vertexCount * (3 + 3 + 2);
        float[] verticesAttributeBuffer = new float[verticesAttributeBufferLength] {
    
            -5, -5, 0, 0, 0, -1,0, 0,
            -5, 5, 0, 0, 0, -1, 0, 1,
            5, -5, 0, 0, 0, -1, 1, 0,
            5, 5, 0, 0, 0, -1, 1, 1
        };

        mesh.SetVertexBufferParams(vertexCount, vertexAttributeDescriptorList);
        mesh.SetVertexBufferData(verticesAttributeBuffer, 0, 0, verticesAttributeBufferLength, 0);

        int[] triangles = new int[6] {
     0, 1, 2, 1, 3, 2 };
        int indexCount = triangles.Length;

        // Vertex index file 
        mesh.SetIndexBufferParams(indexCount, IndexFormat.UInt32);
        mesh.SetIndexBufferData(triangles, 0, 0, indexCount);

        // Son Mesh describe 
        mesh.subMeshCount = 1;
        SubMeshDescriptor subMeshDescriptor = new SubMeshDescriptor(0, indexCount);
        mesh.SetSubMesh(0, subMeshDescriptor);

        MeshFilter mf = gameObject.GetComponent<MeshFilter>();
        if (mf == null)
        {
    
            mf = gameObject.AddComponent<MeshFilter>();
        }
        mf.sharedMesh = mesh;

        MeshRenderer meshRenderer = gameObject.GetComponent<MeshRenderer>();
        if (meshRenderer == null)
        {
    
            meshRenderer = gameObject.AddComponent<MeshRenderer>();
        }
        meshRenderer.material = material;
    }

    // Update is called once per frame
    void Update()
    {
    
    }
}

Finally, you can get directly with Unity3D Learning notes 2—— Draw a textured face Same effect . If there is some graphic foundation , It will be easy to understand this code . They all apply for one buffer, Define the description information of the vertex , Here is according to x,y,z,nx,ny,nz,u,v The order of , Arrange one vertex at a time . Next, define a vertex index buffer; The difference is to add a pair mesh Description of . stay Unity in , One Mesh It can contain more than one child Mesh, Each child Mesh Can correspond to MeshRenderer One of several materials in .

3. other

  1. According to official documents , This set is high API Higher performance . But the feeling of personal use is not very obvious . The setting of skipping verification may also cause some other problems , I usually use the default settings .
  2. Another advantage is , It can avoid the number of vertices in a simple interface exceeding 65535 when Mesh Draw incorrect questions . Theoretically , The fewer batches you draw, the better , This requires that it should be drawn in accordance with the batch as much as possible , Objects with the same number of vertices are divided into multiple mesh draw , The performance is not as good as using a big Mesh Draw once .
  3. The official documents also mention that there are other interfaces that can be accessed C# Jobs and Burst establish Mesh,C# Jobs Related to multithreading , Does it mean that you can create under multithreading Mesh 了 ? It needs further study .

4. Reference resources

  1. Unity3D Learning notes 2—— Draw a textured face
  2. Unity Documentation - Mesh
原网站

版权声明
本文为[charlee44]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207072022149817.html