当前位置:网站首页>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. 参考
边栏推荐
- JMeter-接口自动化测试读取用例,执行并结果回写
- 开发那些事儿:Go加C.free释放内存,编译报错是什么原因?
- Use JfreeChart to generate curves, histograms, pie charts, and distribution charts and display them to JSP-1
- 成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚
- Cases of agile innovation and transformation of consumer goods enterprises
- Brush question 5
- Innovation today | five key elements for enterprises to promote innovation
- 30讲 线性代数 第五讲 特征值与特征向量
- USB(十四)2022-04-12
- PCL . VTK files and Mutual conversion of PCD
猜你喜欢
![[record of question brushing] 3 Longest substring without duplicate characters](/img/44/1cd8128d93c9c273e0f4718d84936e.png)
[record of question brushing] 3 Longest substring without duplicate characters

Digital collections accelerated out of the circle, and marsnft helped diversify the culture and tourism economy!

PMP项目管理考试过关口诀-1

Binary tree

When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK

高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏

How to operate DTC community?

Unity and webgl love each other

Use JfreeChart to generate curves, histograms, pie charts, and distribution charts and display them to jsp-2

JMeter-接口自动化测试读取用例,执行并结果回写
随机推荐
Wechat forum exchange applet system graduation design (3) background function
消息队列与快递柜之间妙不可言的关系
[network] Introduction to C language
网络安全-安装CentOS
微生物健康網,如何恢複微生物群落
微信论坛交流小程序系统毕业设计毕设(5)任务书
When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK
Network security - joint query injection
【微服务|SCG】gateway整合sentinel
Knowledge drop - PCB manufacturing process flow
今日创见|企业促进创新的5大关键要素
位运算(Bit Operation)
Talk about DART's null safety feature
Develop those things: go plus c.free to free memory, and what are the reasons for compilation errors?
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
开发那些事儿:Go加C.free释放内存,编译报错是什么原因?
Introduction to redis and jedis and redis things
Brush question 5
iNFTnews | NFT技术的广泛应用及其存在的问题
解决:信息中插入avi格式的视频时,提示“unsupported video format”