Tanoda
MeshCreator.cs
Go to the documentation of this file.
1
5
6using System.Collections;
7using System.Collections.Generic;
8using UnityEngine;
10{
11
14[ExecuteInEditMode]
15public class MeshCreator : MonoBehaviour
16{
17 public void CreateMesh(List<Vector2> points)
18 {
19 // Create Vector2 vertices
20 Vector2[] vertices2D = points.ToArray();
21
22 // Use the triangulator to get indices for creating triangles
23 Triangulator tr = new Triangulator(vertices2D);
24 int[] indices = tr.Triangulate();
25
26 // Create the Vector3 vertices
27 Vector3[] vertices = new Vector3[vertices2D.Length];
28 for (int i = 0; i < vertices.Length; i++)
29 {
30 vertices[i] = new Vector3(vertices2D[i].x, vertices2D[i].y, 0);
31 }
32
33 // Create the mesh
34 Mesh msh = new Mesh();
35 msh.vertices = vertices;
36 msh.triangles = indices;
37 msh.RecalculateNormals();
38 msh.RecalculateBounds();
39
40 // Set up game object with mesh;
41 GetComponent<MeshFilter>().mesh = msh;
42 }
43}
44}
void CreateMesh(List< Vector2 > points)
Definition: MeshCreator.cs:17
Credit Erdener Gonenc - @PixelEnvision.