当前位置:网站首页>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
边栏推荐
猜你喜欢

LeeCode -- 6. Z 字形变换

Talk about the design and implementation logic of payment process

Matlab-SEIR传染病模型预测
![MATLAB signal processing [Q & A essays · 2]](/img/be/0baa92767c3abbda9b0bff47cb3a75.png)
MATLAB signal processing [Q & A essays · 2]

RE1 attack and defense world reverse

Wechat forum exchange applet system graduation design (3) background function

Adults have only one main job, but they have to pay a price. I was persuaded to step back by personnel, and I cried all night

USB(十五)2022-04-14

GEE(四):计算两个变量(影像)之间的相关性并绘制散点图

Matlab 信号处理【问答随笔·2】
随机推荐
云原生数据仓库AnalyticDB MySQL版用户手册
Two kinds of curves in embedded audio development
Opencv scalar passes in three parameters, which can only be displayed in black, white and gray. Solve the problem
成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚
PHP uses Alibaba cloud storage
ROS2专题(03):ROS1和ROS2的区别【02】
Tree background data storage (using webmethod) [easy to understand]
ROS2专题(03):ROS1和ROS2的区别【01】
微信论坛交流小程序系统毕业设计毕设(5)任务书
Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
Redhat下安装fedora
统计电影票房排名前10的电影并存入还有一个文件
Install Fedora under RedHat
微信论坛交流小程序系统毕业设计毕设(2)小程序功能
Wechat forum exchange applet system graduation design completion (7) Interim inspection report
Technology at home and abroad people "see" the future of audio and video technology
经纬度PLT文件格式说明
USB(十四)2022-04-12
智慧社區和智慧城市之間有什麼异同
十三、系统优化