Advertisement
Guest User

Untitled

a guest
May 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.78 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class meshGenerator : MonoBehaviour{
  6.  
  7.     Mesh mesh;
  8.     Material m;
  9.     MeshFilter mf;
  10.     MeshRenderer mr;
  11.     Voxel v;
  12.     Vector3[] vertices;
  13.     int[] triangles;
  14.  
  15.     void Start() {
  16.         mf = GetComponent<MeshFilter>();
  17.         mr = GetComponent<MeshRenderer>();
  18.         mesh = new Mesh();
  19.         v = new Voxel(new Vector3Int(0, 0, 0));
  20.         int indexOffset = 0;
  21.         vertices = v.vertices.ToArray();
  22.         triangles = v.returnTriangles(indexOffset);
  23.         mesh.vertices = vertices;
  24.         mesh.triangles = triangles;
  25.         mf.mesh = mesh;
  26.     }
  27.  
  28.     public class Voxel {
  29.         Vector3Int position;
  30.         public static Vector3Int[] corners = new Vector3Int[8]{
  31.             new Vector3Int(1, 1, 0), // 0
  32.             new Vector3Int(1, 0, 0), // 1
  33.             new Vector3Int(0, 0, 0), // 2
  34.             new Vector3Int(0, 1, 0), // 3
  35.  
  36.             new Vector3Int(0, 1, 1), // 4
  37.             new Vector3Int(1, 1, 1), // 5
  38.             new Vector3Int(1, 0, 1), // 6
  39.             new Vector3Int(0, 0, 1)  // 7
  40.         };
  41.  
  42.         Dictionary<string, bool> visibleFaces = new Dictionary<string, bool>{
  43.             { "top",    true },
  44.             { "north",  true },
  45.             { "east",   true },
  46.             { "south",  true },
  47.             { "west",   true },
  48.             { "bottom", true }
  49.         };
  50.  
  51.         public List<Vector3> vertices = new List<Vector3>();
  52.         List<int> triangles = new List<int>();
  53.  
  54.         public Voxel(Vector3Int pos) {
  55.             position = pos;
  56.             //  checkNeighbours();
  57.             pushFaces();
  58.             pushTriangles();
  59.         }
  60.  
  61.         /*  void checkNeighbours() {
  62.  
  63.             }*/
  64.  
  65.         void pushVertices(Vector3Int A, Vector3Int B, Vector3Int C, Vector3Int D) {
  66.             vertices.Add(A + position);
  67.             vertices.Add(B + position);
  68.             vertices.Add(C + position);
  69.             vertices.Add(D + position);
  70.         }
  71.  
  72.         int[] faceTriangles(int o) {
  73.             return new int[] {
  74.                 o,
  75.                 o + 1,
  76.                 o + 2,
  77.                 o,
  78.                 o + 2,
  79.                 o + 3,
  80.             };
  81.         }
  82.  
  83.         void pushFaces() {
  84.             if (visibleFaces["south"]) {
  85.                 pushVertices(corners[0], corners[1], corners[2], corners[3]);
  86.             }
  87.             if (visibleFaces["top"]) {
  88.                 pushVertices(corners[0], corners[3], corners[4], corners[5]);
  89.             }
  90.             if (visibleFaces["east"]) {
  91.                 pushVertices(corners[0], corners[5], corners[6], corners[1]);
  92.             }
  93.             if (visibleFaces["north"]) {
  94.                 pushVertices(corners[7], corners[6], corners[5], corners[4]);
  95.             }
  96.             if (visibleFaces["west"]) {
  97.                 pushVertices(corners[7], corners[4], corners[3], corners[2]);
  98.             }
  99.             if (visibleFaces["bottom"]) {
  100.                 pushVertices(corners[7], corners[2], corners[1], corners[6]);
  101.             }
  102.         }
  103.  
  104.         void pushTriangles() {
  105.             int end = 0;
  106.             foreach (bool v in visibleFaces.Values) if(v) end+=4;
  107.             for (int i = 0; i < end; i+=4) {
  108.                 triangles.AddRange(faceTriangles(i));
  109.             }
  110.         }
  111.  
  112.         public int[] returnTriangles(int offset = 0) {
  113.             int[] ts = triangles.ToArray();
  114.             for (int i = 0; i < ts.Length; i++) ts[i] += offset;
  115.             return ts;
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement