当前位置:网站首页>Unity3d learning notes 4 - create mesh advanced interface
Unity3d learning notes 4 - create mesh advanced interface
2022-07-07 23:22:00 【charlee44】
List of articles
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
- 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 .
- 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 .
- 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
边栏推荐
猜你喜欢
随机推荐
Cloud native is devouring everything. How should developers deal with it?
Unity3D学习笔记4——创建Mesh高级接口
U盘拷贝东西时,报错卷错误,请运行chkdsk
UE4_UE5全景相机
十四、数据库的导出和导入的两种方法
Tree background data storage (using webmethod) [easy to understand]
LeeCode -- 6. Z 字形变换
Mysql索引优化实战一
在软件工程领域,搞科研的这十年!
leetcode-520. 检测大写字母-js
Adrnoid Development Series (XXV): create various types of dialog boxes using alertdialog
Gee (III): calculate the correlation coefficient between two bands and the corresponding p value
经纬度PLT文件格式说明
系统设计概述
2021ICPC上海 H.Life is a Game Kruskal重构树
Cloud native data warehouse analyticdb MySQL user manual
14、 Two methods of database export and import
微信论坛交流小程序系统毕业设计毕设(2)小程序功能
V20变频器手自动切换(就地远程切换)的具体方法示例
智慧社區和智慧城市之間有什麼异同