Advertisement
Guest User

ChunkC.cs

a guest
Apr 13th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.98 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [RequireComponent(typeof(MeshFilter))]
  5. [RequireComponent(typeof(MeshRenderer))]
  6. [RequireComponent(typeof(MeshCollider))]
  7.  
  8. public class ChunkC : MonoBehaviour
  9. {
  10.  
  11.     public BlockBase[, ,] blocks = new BlockBase[chunkSize, chunkSize, chunkSize];
  12.  
  13.     public static int chunkSize = 16;
  14.     public bool update = false;
  15.     public bool rendered;
  16.  
  17.     MeshFilter filter;
  18.     MeshCollider coll;
  19.  
  20.     public World world;
  21.     public WorldPos pos;
  22.  
  23.     public Material standardMaterial;
  24.     public Material leafMaterial;
  25.     public Material[] materials = { standardMaterial, leafMaterial };
  26.  
  27.     void Start ()
  28.     {
  29.         filter = gameObject.GetComponent<MeshFilter> ();
  30.         coll = gameObject.GetComponent<MeshCollider> ();
  31.     }
  32.  
  33.     //Update is called once per frame
  34.     void Update ()
  35.     {
  36.         if (update) {
  37.             update = false;
  38.             UpdateChunk ();
  39.         }
  40.     }
  41.  
  42.     public BlockBase GetBlock (int x, int y, int z)
  43.     {
  44.         if (InRange (x) && InRange (y) && InRange (z))
  45.             return blocks [x, y, z];
  46.         return world.GetBlock (pos.x + x, pos.y + y, pos.z + z);
  47.     }
  48.  
  49.     public static bool InRange (int index)
  50.     {
  51.         if (index < 0 || index >= chunkSize)
  52.             return false;
  53.  
  54.         return true;
  55.     }
  56.  
  57.     public void SetBlock (int x, int y, int z, BlockBase block)
  58.     {
  59.         if (InRange (x) && InRange (y) && InRange (z)) {
  60.             blocks [x, y, z] = block;
  61.         } else {
  62.             world.SetBlock (pos.x + x, pos.y + y, pos.z + z, block);
  63.         }
  64.     }
  65.  
  66.     public void SetBlocksUnmodified ()
  67.     {
  68.         foreach (BlockBase block in blocks) {
  69.             block.changed = false;
  70.         }
  71.     }
  72.  
  73.     // Updates the chunk based on its contents
  74.     void UpdateChunk ()
  75.     {
  76.         rendered = true;
  77.         MeshData meshData = new MeshData ();
  78.  
  79.         for (int x = 0; x < chunkSize; x++) {
  80.             for (int y = 0; y < chunkSize; y++) {
  81.                 for (int z = 0; z < chunkSize; z++) {
  82.                     meshData = blocks [x, y, z].Blockdata (this, x, y, z, meshData);
  83.                 }
  84.             }
  85.         }
  86.  
  87.         RenderMesh (meshData);
  88.     }
  89.  
  90.     // Sends the calculated mesh information
  91.     // to the mesh and collision components
  92.     void RenderMesh (MeshData meshData)
  93.     {
  94.         filter.mesh.Clear ();
  95.         filter.mesh.vertices = meshData.vertices.ToArray ();
  96.         filter.mesh.triangles = meshData.triangles.ToArray ();
  97.  
  98.         filter.mesh.uv = meshData.uv.ToArray ();
  99.         filter.mesh.RecalculateNormals ();
  100.  
  101.         coll.sharedMesh = null;
  102.         Mesh mesh = new Mesh ();
  103.         mesh.subMeshCount = 2;
  104.         mesh.SetTriangles (meshData.colTriangles.ToArray (), 0);
  105.         mesh.SetTriangles (meshData.subTriangles.ToArray (), 1);
  106.  
  107.         mesh.vertices = meshData.colVertices.ToArray ();
  108.         mesh.triangles = meshData.colTriangles.ToArray ();
  109.         mesh.RecalculateNormals ();
  110.  
  111.         coll.sharedMesh = mesh;
  112.     }
  113.  
  114.  
  115.     protected override MeshData AddTriangles (MeshData meshData)
  116.     {
  117.         int count = meshData.vertices.Count;
  118.    
  119.         meshData.AddSubTriangle (count - 4);
  120.         meshData.AddSubTriangle (count - 3);
  121.         meshData.AddSubTriangle (count - 2);
  122.    
  123.         meshData.AddSubTriangle (count - 4);
  124.         meshData.AddSubTriangle (count - 2);
  125.         meshData.AddSubTriangle (count - 1);
  126.    
  127.         return meshData;
  128.     }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement