-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProceduralCube.cs
59 lines (44 loc) · 1.33 KB
/
ProceduralCube.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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[RequireComponent (typeof(MeshFilter), typeof(MeshRenderer))]
public class ProceduralCube : MonoBehaviour {
Mesh mesh;
List<Vector3> vertices;
List<int> triangles;
public float scale = 1f;
public int posX, posY, posZ;
float adjScale;
// Use this for initialization
void Awake () {
mesh = GetComponent<MeshFilter> ().mesh;
adjScale = scale * 0.5f;
}
void Start() {
MakeCube (adjScale, new Vector3((float)posX * scale, (float)posY * scale, (float)posZ * scale));
UpdateMesh ();
}
void MakeCube (float cubeScale, Vector3 cubePos){
vertices = new List<Vector3> ();
triangles = new List<int> ();
for (int i = 0; i < 6; i++) {
MakeFace (i, cubeScale, cubePos);
}
}
void MakeFace (int dir, float faceScale, Vector3 facePos){
vertices.AddRange (CubeMeshData.faceVertices (dir, faceScale, facePos));
int vCount = vertices.Count;
triangles.Add (vCount - 4);
triangles.Add (vCount - 4 + 1);
triangles.Add (vCount - 4 + 2);
triangles.Add (vCount - 4);
triangles.Add (vCount - 4 + 2);
triangles.Add (vCount - 4 + 3);
}
void UpdateMesh(){
mesh.Clear ();
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.RecalculateNormals ();
}
}