Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- [RequireComponent(typeof(MeshFilter))]
- [RequireComponent(typeof(MeshRenderer))]
- [RequireComponent(typeof(MeshCollider))]
- public class ChunkC : MonoBehaviour
- {
- public BlockBase[, ,] blocks = new BlockBase[chunkSize, chunkSize, chunkSize];
- public static int chunkSize = 16;
- public bool update = false;
- public bool rendered;
- MeshFilter filter;
- MeshCollider coll;
- public World world;
- public WorldPos pos;
- public Material standardMaterial;
- public Material leafMaterial;
- public Material[] materials = { standardMaterial, leafMaterial };
- void Start ()
- {
- filter = gameObject.GetComponent<MeshFilter> ();
- coll = gameObject.GetComponent<MeshCollider> ();
- }
- //Update is called once per frame
- void Update ()
- {
- if (update) {
- update = false;
- UpdateChunk ();
- }
- }
- public BlockBase GetBlock (int x, int y, int z)
- {
- if (InRange (x) && InRange (y) && InRange (z))
- return blocks [x, y, z];
- return world.GetBlock (pos.x + x, pos.y + y, pos.z + z);
- }
- public static bool InRange (int index)
- {
- if (index < 0 || index >= chunkSize)
- return false;
- return true;
- }
- public void SetBlock (int x, int y, int z, BlockBase block)
- {
- if (InRange (x) && InRange (y) && InRange (z)) {
- blocks [x, y, z] = block;
- } else {
- world.SetBlock (pos.x + x, pos.y + y, pos.z + z, block);
- }
- }
- public void SetBlocksUnmodified ()
- {
- foreach (BlockBase block in blocks) {
- block.changed = false;
- }
- }
- // Updates the chunk based on its contents
- void UpdateChunk ()
- {
- rendered = true;
- MeshData meshData = new MeshData ();
- for (int x = 0; x < chunkSize; x++) {
- for (int y = 0; y < chunkSize; y++) {
- for (int z = 0; z < chunkSize; z++) {
- meshData = blocks [x, y, z].Blockdata (this, x, y, z, meshData);
- }
- }
- }
- RenderMesh (meshData);
- }
- // Sends the calculated mesh information
- // to the mesh and collision components
- void RenderMesh (MeshData meshData)
- {
- filter.mesh.Clear ();
- filter.mesh.vertices = meshData.vertices.ToArray ();
- filter.mesh.triangles = meshData.triangles.ToArray ();
- filter.mesh.uv = meshData.uv.ToArray ();
- filter.mesh.RecalculateNormals ();
- coll.sharedMesh = null;
- Mesh mesh = new Mesh ();
- mesh.subMeshCount = 2;
- mesh.SetTriangles (meshData.colTriangles.ToArray (), 0);
- mesh.SetTriangles (meshData.subTriangles.ToArray (), 1);
- mesh.vertices = meshData.colVertices.ToArray ();
- mesh.triangles = meshData.colTriangles.ToArray ();
- mesh.RecalculateNormals ();
- coll.sharedMesh = mesh;
- }
- protected override MeshData AddTriangles (MeshData meshData)
- {
- int count = meshData.vertices.Count;
- meshData.AddSubTriangle (count - 4);
- meshData.AddSubTriangle (count - 3);
- meshData.AddSubTriangle (count - 2);
- meshData.AddSubTriangle (count - 4);
- meshData.AddSubTriangle (count - 2);
- meshData.AddSubTriangle (count - 1);
- return meshData;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement