当前位置:网站首页>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. 参考
边栏推荐
- Network security - Eternal Blue
- When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK
- QT graphicsview graphical view usage summary with flow chart development case prototype
- USB (十七)2022-04-15
- 14、 Two methods of database export and import
- Wechat forum exchange applet system graduation design (3) background function
- 嵌入式音频开发中的两种曲线
- 【刷题记录】3. 无重复字符的最长子串
- About idea cannot find or load the main class
- Network security - joint query injection
猜你喜欢

Brush question 3

iNFTnews | Web5 vs Web3:未来是一个过程,而不是目的地

Innovation today | five key elements for enterprises to promote innovation

Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020

Wechat forum exchange applet system graduation design completion (1) development outline

聊聊 Dart 的空安全 (null safety) 特性

小程序多种开发方式对比-跨端?低代码?原生?还是云开发?

十三、系统优化

ArcGIS: two methods of attribute fusion of the same field of vector elements

Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
随机推荐
Gbu1510-asemi power supply special 15A rectifier bridge gbu1510
2022 words for yourself
kubernetes的简单化数据存储StorageClass(建立和删除以及初步使用)
Statistical method for anomaly detection
数字藏品加速出圈,MarsNFT助力多元化文旅经济!
Introduction to redis and jedis and redis things
Advantages and disadvantages of rest ful API
Unity dynamically merges mesh textures
Wechat forum exchange applet system graduation design completion (1) development outline
Bit operation
One question per day - pat grade B 1002 questions
嵌入式音频开发中的两种曲线
Comparison of various development methods of applets - cross end? Low code? Native? Or cloud development?
位运算(Bit Operation)
Some parameters of Haikang IPC
Two kinds of curves in embedded audio development
网络安全-CSRF
About idea cannot find or load the main class
Software test classification
The wonderful relationship between message queue and express cabinet