Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class meshGenerator : MonoBehaviour{
- Mesh mesh;
- Material m;
- MeshFilter mf;
- MeshRenderer mr;
- Voxel v;
- Vector3[] vertices;
- int[] triangles;
- void Start() {
- mf = GetComponent<MeshFilter>();
- mr = GetComponent<MeshRenderer>();
- mesh = new Mesh();
- v = new Voxel(new Vector3Int(0, 0, 0));
- int indexOffset = 0;
- vertices = v.vertices.ToArray();
- triangles = v.returnTriangles(indexOffset);
- mesh.vertices = vertices;
- mesh.triangles = triangles;
- mf.mesh = mesh;
- }
- public class Voxel {
- Vector3Int position;
- public static Vector3Int[] corners = new Vector3Int[8]{
- new Vector3Int(1, 1, 0), // 0
- new Vector3Int(1, 0, 0), // 1
- new Vector3Int(0, 0, 0), // 2
- new Vector3Int(0, 1, 0), // 3
- new Vector3Int(0, 1, 1), // 4
- new Vector3Int(1, 1, 1), // 5
- new Vector3Int(1, 0, 1), // 6
- new Vector3Int(0, 0, 1) // 7
- };
- Dictionary<string, bool> visibleFaces = new Dictionary<string, bool>{
- { "top", true },
- { "north", true },
- { "east", true },
- { "south", true },
- { "west", true },
- { "bottom", true }
- };
- public List<Vector3> vertices = new List<Vector3>();
- List<int> triangles = new List<int>();
- public Voxel(Vector3Int pos) {
- position = pos;
- // checkNeighbours();
- pushFaces();
- pushTriangles();
- }
- /* void checkNeighbours() {
- }*/
- void pushVertices(Vector3Int A, Vector3Int B, Vector3Int C, Vector3Int D) {
- vertices.Add(A + position);
- vertices.Add(B + position);
- vertices.Add(C + position);
- vertices.Add(D + position);
- }
- int[] faceTriangles(int o) {
- return new int[] {
- o,
- o + 1,
- o + 2,
- o,
- o + 2,
- o + 3,
- };
- }
- void pushFaces() {
- if (visibleFaces["south"]) {
- pushVertices(corners[0], corners[1], corners[2], corners[3]);
- }
- if (visibleFaces["top"]) {
- pushVertices(corners[0], corners[3], corners[4], corners[5]);
- }
- if (visibleFaces["east"]) {
- pushVertices(corners[0], corners[5], corners[6], corners[1]);
- }
- if (visibleFaces["north"]) {
- pushVertices(corners[7], corners[6], corners[5], corners[4]);
- }
- if (visibleFaces["west"]) {
- pushVertices(corners[7], corners[4], corners[3], corners[2]);
- }
- if (visibleFaces["bottom"]) {
- pushVertices(corners[7], corners[2], corners[1], corners[6]);
- }
- }
- void pushTriangles() {
- int end = 0;
- foreach (bool v in visibleFaces.Values) if(v) end+=4;
- for (int i = 0; i < end; i+=4) {
- triangles.AddRange(faceTriangles(i));
- }
- }
- public int[] returnTriangles(int offset = 0) {
- int[] ts = triangles.ToArray();
- for (int i = 0; i < ts.Length; i++) ts[i] += offset;
- return ts;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement