当前位置:网站首页>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. 参考
边栏推荐
- 微信论坛交流小程序系统毕业设计毕设(7)中期检查报告
- Inftnews | web5 vs Web3: the future is a process, not a destination
- U盘拷贝东西时,报错卷错误,请运行chkdsk
- 定位到最底部[通俗易懂]
- Anta DTC | Anta transformation, building a growth flywheel that is not only FILA
- Digital collections accelerated out of the circle, and marsnft helped diversify the culture and tourism economy!
- 小程序多种开发方式对比-跨端?低代码?原生?还是云开发?
- The wonderful relationship between message queue and express cabinet
- Microbial Health Network, How to restore Microbial Communities
- CXF call reports an error. Could not find conduct initiator for address:
猜你喜欢

海内外技术人们“看”音视频技术的未来

Microbial health network, how to restore microbial communities

What is fake sharing after filling the previous hole?

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

It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen

ArcGIS: field assignment_ The attribute table field calculator assigns values to fields based on conditions

十四、数据库的导出和导入的两种方法

数据库每日一题---第22天:最后一次登录
![Cause analysis and solution of too laggy page of [test interview questions]](/img/33/2c2256fd98b908ddaf5573f644ad7f.png)
Cause analysis and solution of too laggy page of [test interview questions]

Understand the session, cookie and token at one time, and the interview questions are all finalized
随机推荐
Circumvention Technology: Registry
Line test - graphic reasoning - 1 - Chinese character class
Clean C disk
Exploratory data analysis of heartbeat signal
STL标准模板库(Standard Template Library)一周学习总结
U盘拷贝东西时,报错卷错误,请运行chkdsk
Grid
Why is network i/o blocked?
Installing vmtools is gray
ArcGIS: field assignment_ The attribute table field calculator assigns values to fields based on conditions
成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚
网络安全-安装CentOS
Network security - information query of operating system
Understand the session, cookie and token at one time, and the interview questions are all finalized
Wechat forum exchange applet system graduation design completion (8) graduation design thesis template
648. 单词替换
JS triangle
网络安全-sqlmap与DVWA爆破
微信论坛交流小程序系统毕业设计毕设(8)毕业设计论文模板
oc 可变參数传递