当前位置:网站首页>Unity3D学习笔记4——创建Mesh高级接口
Unity3D学习笔记4——创建Mesh高级接口
2022-07-07 21:50:00 【charlee44】
1. 概述
在文章Unity3D学习笔记2——绘制一个带纹理的面中使用代码的方式创建了一个Mesh,不过这套接口在Unity中被称为简单接口。与其相对应的,Unity还提供了一套高级API来创建Mesh。
2. 详论
根据Unity文档的论述,使用高级接口能够得到更高的性能,能够跳过一些验证检查。但是这并不是最关键的,简单接口有个最大的缺点是顶点个数超过65535个时就有问题(至少在2019.4.3f1版本还是这样)。
话不多说,直接上代码:
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";
//顶点数据
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;
//顶点索引文件
mesh.SetIndexBufferParams(indexCount, IndexFormat.UInt32);
mesh.SetIndexBufferData(triangles, 0, 0, indexCount);
//子Mesh描述
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()
{
}
}
最后可以直接得到与Unity3D学习笔记2——绘制一个带纹理的面一样的效果。如果有一些图形基础,就会很容易理解这段代码。都是申请一个buffer,定义顶点的描述信息,这里是按照x,y,z,nx,ny,nz,u,v的顺序,一个顶点一个顶点进行排列。接着是定义一个顶点索引buffer;不同的是增加了一个对于子mesh的描述。在Unity里,一个Mesh可以包含多个子Mesh,每个子Mesh都能对应MeshRenderer中的多个材质中的一个。
3. 其他
- 根据官方文档论述,这套高API性能更高。但个人使用感觉不是很明显。跳过验证的设置也可能带来一些其他问题,我一般用默认设置。
- 另一个优点是,可以避免简单接口中顶点个数超过65535时Mesh绘制不正确的问题。理论上,绘制的批次越少越好,这就要求尽可能合批次绘制,同样顶点个数的物体分多个mesh绘制,性能比不上使用一个大的Mesh一次绘制。
- 官方文档还提到了有其他接口可以通过C# Jobs和Burst创建Mesh,C# Jobs与多线程相关,难道意味着可以在多线程下创建Mesh了?有待进一步研究。
4. 参考
边栏推荐
- Quelles sont les similitudes et les différences entre les communautés intelligentes et les villes intelligentes?
- Brush question 3
- PCL . VTK files and Mutual conversion of PCD
- 网络安全-beef
- 高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
- Use JfreeChart to generate curves, histograms, pie charts, and distribution charts and display them to JSP-1
- opencv scalar传入三个参数只能显示黑白灰问题解决
- ArcGIS: field assignment_ The attribute table field calculator assigns values to fields based on conditions
- Microbial Health Network, How to restore Microbial Communities
- Wechat forum exchange applet system graduation design (5) assignment
猜你喜欢

Gbu1510-asemi power supply special 15A rectifier bridge gbu1510

Online interview, how to better express yourself? In this way, the passing rate will be increased by 50%~

Line test - graphic reasoning - 1 - Chinese character class

Database daily question --- day 22: last login

JMeter-接口自动化测试读取用例,执行并结果回写

ArcGIS:矢量要素相同字段属性融合的两种方法

微信论坛交流小程序系统毕业设计毕设(4)开题报告

ArcGIS:字段赋值_属性表字段计算器(Field Calculator)依据条件为字段赋值

肠道里的微生物和皮肤上的一样吗?

Digital collections accelerated out of the circle, and marsnft helped diversify the culture and tourism economy!
随机推荐
Kubernetes' simplified data storage storageclass (creation, deletion and initial use)
Digital collections accelerated out of the circle, and marsnft helped diversify the culture and tourism economy!
What is ADC sampling rate (Hz) and how to calculate it
Are the microorganisms in the intestines the same as those on the skin?
Wechat forum exchange applet system graduation design completion (1) development outline
Txt file virus
Brush question 3
十三、系统优化
Wechat forum exchange applet system graduation design (5) assignment
Database daily question --- day 22: last login
The wonderful relationship between message queue and express cabinet
Online interview, how to better express yourself? In this way, the passing rate will be increased by 50%~
Network security - phishing
每日一题——PAT乙级1002题
Gbu1510-asemi power supply special 15A rectifier bridge gbu1510
Talk about DART's null safety feature
2021-01-12
[network] Introduction to C language
微信论坛交流小程序系统毕业设计毕设(4)开题报告
Wechat forum exchange applet system graduation design completion (8) graduation design thesis template