-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProceduralMesh.cs
60 lines (41 loc) · 1.21 KB
/
ProceduralMesh.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(MeshFilter))]
public class ProceduralMesh : MonoBehaviour {
public float quadSize = 1f;
public Vector3 quadOffset;
Mesh mesh;
Vector3[] vertices;
int[] triangles;
void Awake() {
mesh = GetComponent<MeshFilter> ().mesh;
}
// Use this for initialization
void Start () {
MakeMeshData (quadSize, quadOffset);
CreateMesh ();
}
void MakeMeshData (float size, Vector3 offset){
//set size of vertices and triangles arrays
vertices = new Vector3[4];
triangles = new int[6];
float vertexOffset = size / 2;
//populate arrays
vertices [0] = new Vector3 (-vertexOffset, 0, -vertexOffset) + offset;
vertices [1] = new Vector3 (-vertexOffset, 0, vertexOffset) + offset;
vertices [2] = new Vector3 (vertexOffset, 0, -vertexOffset) + offset;
vertices [3] = new Vector3 (vertexOffset, 0, vertexOffset) + offset;
triangles [0] = 0;
triangles [1] = 1;
triangles [2] = 2;
triangles [3] = 2;
triangles [4] = 1;
triangles [5] = 3;
}
void CreateMesh(){
mesh.Clear ();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals ();
}
}