FineNerds

World Struct

Jul 2nd, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4.  
  5. [RequireComponent(typeof(MeshFilter))]
  6. [RequireComponent(typeof(MeshRenderer))]
  7. [RequireComponent(typeof(MeshCollider))]
  8. public class WorldStructure : MonoBehaviour {
  9.     Voxel[,,] voxels;
  10.     MeshFilter filter;
  11.     MeshCollider coll;
  12.    
  13.     public int chunkSize = 16;
  14.     public bool update;
  15.     public bool rendered;
  16.     public bool showGen;
  17.  
  18.     void Start()
  19.     {
  20.         filter = gameObject.GetComponent<MeshFilter>();
  21.         coll = gameObject.GetComponent<MeshCollider>();
  22.         voxels = new Voxel[chunkSize, chunkSize, chunkSize];
  23.         try
  24.         {
  25.             for (int x = 0; x < chunkSize; x++)
  26.             {
  27.                 for (int y = 0; y < chunkSize; y++)
  28.                 {
  29.                     for (int z = 0; z < chunkSize; z++)
  30.                     {
  31.                         createVoxel(x, y, z, "air");
  32.                     }
  33.                 }
  34.             }
  35.             try
  36.             {
  37.                 if(showGen)
  38.                     StartCoroutine(constructVoxels());
  39.                 else
  40.                 {
  41.                     for (int x = 1; x < chunkSize - 1; x++)
  42.                     {
  43.                         for (int y = 2; y < chunkSize - 2; y++)
  44.                         {
  45.                             for (int z = 1; z < chunkSize - 1; z++)
  46.                             {
  47.                                 createVoxel(x, 1, z);
  48.                                 createVoxel(x, 2, z, "dirt");
  49.                                 createVoxel(x, 3, z, "grass");
  50.                             }
  51.                         }
  52.                     }
  53.                 }
  54.             }
  55.             catch (Exception e) { Debug.Log(e); }
  56.         }
  57.         catch (Exception e) { Debug.Log(e); }
  58.         UpdateChunk();
  59.     }
  60.  
  61.     IEnumerator constructVoxels()
  62.     {
  63.         for (int x = 1; x < chunkSize - 1; x++)
  64.         {
  65.                 for (int z = 1; z < chunkSize - 1; z++)
  66.                 {
  67.                     createVoxel(x, 1, z);
  68.                     createVoxel(x, 2, z, "dirt");
  69.                     createVoxel(x, 3, z, "grass");
  70.                     UpdateChunk();
  71.                     yield return new WaitForSeconds(0f);
  72.                 }
  73.         }
  74.     }
  75.    
  76.     void Update()
  77.     {
  78.         if(update)
  79.         {
  80.             update = false;
  81.             UpdateChunk();
  82.         }
  83.     }
  84.  
  85.     void UpdateChunk()
  86.     {
  87.         rendered = true;
  88.         MeshData meshData = new MeshData();
  89.         try
  90.         {
  91.             for (int x = 0; x < chunkSize; x++)
  92.             {
  93.                 for (int y = 0; y < chunkSize; y++)
  94.                 {
  95.                     for (int z = 0; z < chunkSize; z++)
  96.                     {
  97.                         meshData = voxels[x, y, z].VoxelData(this, x, y, z, meshData);
  98.                     }
  99.                 }
  100.             }
  101.         }
  102.         catch (Exception e) { Debug.Log(e); }
  103.         RenderMesh(meshData);
  104.     }
  105.  
  106.     void createVoxel(int x, int y, int z)
  107.     {
  108.         voxels[x, y, z] = new Voxel();
  109.     }
  110.  
  111.     void createVoxel(int x, int y, int z, string voxelType)
  112.     {
  113.         if (voxelType.ToLower() == "air")
  114.             voxels[x, y, z] = new Voxel_Air();
  115.         if (voxelType.ToLower() == "grass")
  116.             voxels[x, y, z] = new Voxel_Grass();
  117.         if (voxelType.ToLower() == "dirt")
  118.             voxels[x, y, z] = new Voxel_Dirt();
  119.     }
  120.  
  121.     void RenderMesh(MeshData meshData)
  122.     {
  123.         filter.mesh.Clear();
  124.         filter.mesh.vertices = meshData.verts.ToArray();
  125.         filter.mesh.triangles = meshData.tris.ToArray();
  126.         filter.mesh.uv = meshData.uvs.ToArray();
  127.         filter.mesh.RecalculateNormals();
  128.  
  129.         coll.sharedMesh = null;
  130.         Mesh mesh = new Mesh();
  131.         mesh.vertices = meshData.colVerts.ToArray();
  132.         mesh.triangles = meshData.colTris.ToArray();
  133.         mesh.RecalculateNormals();
  134.  
  135.         coll.sharedMesh = mesh;
  136.     }
  137.  
  138.     public Voxel getVoxel(int x, int y, int z)
  139.     {
  140.         return voxels[x, y, z];
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment