当前位置:网站首页>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
边栏推荐
猜你喜欢
Matlab-SEIR传染病模型预测
Wechat forum exchange applet system graduation design completion (6) opening defense ppt
JS get the key and value of the object
When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK
Unity3D学习笔记6——GPU实例化(1)
Installing spss25
微信论坛交流小程序系统毕业设计毕设(5)任务书
在软件工程领域,搞科研的这十年!
经纬度PLT文件格式说明
Wechat forum exchange applet system graduation design completion (4) opening report
随机推荐
系统架构设计师备考经验分享:论文出题方向
Vulnerability recurrence ----- 49. Apache airflow authentication bypass (cve-2020-17526)
成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚
Wechat forum exchange applet system graduation design completion (7) Interim inspection report
Advantages and disadvantages of rest ful API
Network security sqlmap and DVWA explosion
Network security -beef
POJ2392 SpaceElevator [DP]
网络安全-sqlmap与DVWA爆破
Wechat forum exchange applet system graduation design (5) assignment
Solution: prompt "unsupported video format" when inserting avi format video into the message
Unity3D学习笔记5——创建子Mesh
统计电影票房排名前10的电影并存入还有一个文件
Coreseek:第二步建索引及測试
U盘拷贝东西时,报错卷错误,请运行chkdsk
./ setup. Insufficient sh permission
Kubernetes' simplified data storage storageclass (creation, deletion and initial use)
1. Sum of two numbers
Adrnoid Development Series (XXV): create various types of dialog boxes using alertdialog
When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK