Unity中Mesh使用MeshTopology.Lines模式绘制线条及MeshTopology.Quads模式绘制网格参考

Unity中MeshFilter中的Mesh默认情况下使用MeshTopology.Trigangles类型绘制网格,就是通常的绘制三角形网格,实际上Mesh有五种绘制模式,对应MeshTopology的枚举,分别是

|---------------------------------------------------------------------------------------------------------|-----------|
| Triangles | 网格由三角形构成。 |
| Quads | 网格由四边形构成。 |
| Lines | 网格由线条构成。 |
| LineStrip | 网格是线条带。 |
| Points | 网格由点构成。 |

这里以Lines和Quads为例说明Trigangles方式之外的绘制方式。

参考代码如下:

cs 复制代码
using System.Collections.Generic;
using UnityEngine;

public class LineMeshTest : MonoBehaviour
{
	[SerializeField]
	Material material;

	void Start()
	{
		GameObject circle = new GameObject("Circle");
		circle.transform.SetParent(transform);
		circle.transform.localPosition = new Vector3(-5, 0, 0);
		MeshFilter filterCircle = circle.AddComponent<MeshFilter>();
		Mesh meshCircle = GetCircleMesh(2);
		filterCircle.mesh = meshCircle;
		MeshRenderer renderCircle = circle.AddComponent<MeshRenderer>();
		renderCircle.material = material;

		GameObject quad = new GameObject("Quad");
		quad.transform.SetParent(transform);
		quad.transform.localPosition = new Vector3(5, 0, 0);
		MeshFilter filterQuad = quad.AddComponent<MeshFilter>();
		Mesh meshQuad = GetQuadMesh(5);
		filterQuad.mesh = meshQuad;
		MeshRenderer rendeQuad = quad.AddComponent<MeshRenderer>();
		rendeQuad.material = material;
	}

	Mesh GetCircleMesh(float radius)
	{
		List<Vector3> vertexList = new();
		List<int> indexList = new List<int>();
		float radDelta = Mathf.PI / 36;
		for (float i = 0; i < 72f; i++)
		{
			float rad = radDelta * i;
			Vector3 vert = new(radius * Mathf.Cos(rad), radius * Mathf.Sin(rad), 0);
			vertexList.Add(vert);
		}

		indexList.Add(0);
		for (int i = 1; i < 72; i++)
		{
			indexList.Add(i);
			indexList.Add(i);
		}
		indexList.Add(0);

		Mesh mesh = new();
		mesh.SetVertices(vertexList);
		mesh.SetIndices(indexList.ToArray(), MeshTopology.Lines, 0);
		return mesh;
	}

	Mesh GetQuadMesh(float size)
	{
		float s = size * 0.5f;
		Vector3[] vertices = new[] { new Vector3(s, s, 0), new Vector3(s, -s, 0), new Vector3(-s, -s, 0), new Vector3(-s, s, 0) };
		int[] indices = new[] { 0, 1, 2, 3 };

		Mesh mesh = new Mesh();
		mesh.SetVertices(vertices);
		mesh.SetIndices(indices, MeshTopology.Quads, 0);

		return mesh;
	}
}

这个例子里面使用Mesh绘制了一个圆形线条,注意这两行语句:

cs 复制代码
mesh.SetIndices(indexList.ToArray(), MeshTopology.Lines, 0);
cs 复制代码
mesh.SetIndices(indices, MeshTopology.Quads, 0);
相关推荐
小李也疯狂1 天前
Unity 中的立方体贴图(Cubemaps)
unity·游戏引擎·贴图·cubemap
牛掰是怎么形成的1 天前
Unity材质贴图引用陷阱:包体暴涨真相
unity·材质·贴图
呆呆敲代码的小Y1 天前
【Unity工具篇】| 超实用工具LuBan,快速上手使用
游戏·unity·游戏引擎·unity插件·luban·免费游戏·游戏配置表
EQ-雪梨蛋花汤1 天前
【Unity优化】Unity多场景加载优化与资源释放完整指南:解决Additive加载卡顿、预热、卸载与内存释放问题
unity·游戏引擎
我的offer在哪里1 天前
用 Unity 从 0 做一个「可以玩的」游戏,需要哪些步骤和流程
游戏·unity·游戏引擎
泡泡茶壶ᐇ1 天前
Unity游戏开发入门指南:从零开始理解游戏引擎核心概念
unity·游戏引擎
YigAin1 天前
Unity中的Lock,到底在锁什么,什么时候该用?
unity
Var_al1 天前
抖小Unity WebGL分包命令行工具实践指南
unity·游戏引擎·webgl
天人合一peng1 天前
unity 通过代码修改button及其名字字体的属性
unity·游戏引擎
GLDbalala1 天前
Unity基于自定义管线实现经典经验光照模型
unity·游戏引擎