TriangleNet下载如下:
TriangleNet
效果如下:
代码参考如下:
using System.Collections.Generic;
using UnityEngine;
using TriangleNet.Geometry;
public class TestTriangleNet : MonoBehaviour
{
[SerializeField]
Material material;
void Start()
{
Vector3[] vs = new Vector3[625];
for (int i = 0; i < vs.Length; i++)
{
vs[i] = new Vector3(i % 25, 0, i / 25);
}
Polygon poly = new();
foreach (var v in vs)
{
poly.Add(new Vertex(v.x, v.z));
}
//设置外轮廓
Vertex[] vertexOut = new Vertex[] { new(1, 10), new(3, 16), new(10, 19), new(16, 16), new(19, 10), new(16, 3), new(10, 1), new(3, 8) };
poly.Add(new Contour(vertexOut), false);
//设置洞洞1
Vertex[] vertexIn01 = new Vertex[] { new(2, 10), new(4, 12), new(6, 10), new(4, 8) };
poly.Add(new Contour(vertexIn01), true);
//设置洞洞2
Vertex[] vertexIn02 = new Vertex[] { new(12, 6), new(14, 8), new(16, 6), new(14, 4) };
poly.Add(new Contour(vertexIn02), true);
var options = new TriangleNet.Meshing.ConstraintOptions
{
SegmentSplitting = 1,
ConformingDelaunay = false,
Convex = false
};
//生成Triangle网格
var triangleMesh = ExtensionMethods.Triangulate(poly, options);
List<Vector3> listVert = new();
foreach (Vertex v in triangleMesh.Vertices)
{
listVert.Add(new Vector3(v.X, 0, v.Y));
}
List<int> listTriangle = new();
foreach (var tm in triangleMesh.Triangles)
{
listTriangle.Add(tm.GetVertexID(1));
listTriangle.Add(tm.GetVertexID(0));
listTriangle.Add(tm.GetVertexID(2));
}
Mesh mesh = new()
{
vertices = listVert.ToArray(),
triangles = listTriangle.ToArray()
};
mesh.RecalculateNormals();
MeshFilter filter = GetComponent<MeshFilter>();
if (!filter) filter = gameObject.AddComponent<MeshFilter>();
filter.sharedMesh = mesh;
MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
if(!meshRenderer)meshRenderer = gameObject.AddComponent<MeshRenderer>();
meshRenderer.material = material;
}
}
如果不想中间挖洞,使用下面的代码即可:
using System.Collections.Generic;
using UnityEngine;
using TriangleNet.Geometry;
using static UnityEditor.Progress;
public class TestTriangleNet : MonoBehaviour
{
[SerializeField]
Material material;
void Start()
{
Vector3[] vs = new Vector3[625];
for (int i = 0; i < vs.Length; i++)
{
vs[i] = new Vector3(i % 25, 0, i / 25);
}
Polygon poly = new();
foreach (var v in vs)
{
poly.Add(new Vertex(v.x, v.z));
}
var triangleMesh = poly.Triangulate();
List<Vector3> listVert = new();
foreach (Vertex v in triangleMesh.Vertices)
{
listVert.Add(new Vector3(v.X, 0, v.Y));
}
List<int> listTriangle = new();
foreach (var tm in triangleMesh.Triangles)
{
listTriangle.Add(tm.GetVertexID(1));
listTriangle.Add(tm.GetVertexID(0));
listTriangle.Add(tm.GetVertexID(2));
}
Mesh mesh = new()
{
vertices = listVert.ToArray(),
triangles = listTriangle.ToArray()
};
mesh.RecalculateNormals();
MeshFilter filter = GetComponent<MeshFilter>();
if (!filter) filter = gameObject.AddComponent<MeshFilter>();
filter.sharedMesh = mesh;
MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
if(!meshRenderer)meshRenderer = gameObject.AddComponent<MeshRenderer>();
meshRenderer.material = material;
}
}