Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class ChunkRenderer : MonoBehaviour {
- public Texture basicTexture;
- public int[,,] chunk;
- int height = 6;
- int width = 2;
- int depth = 3;
- LineRenderer lineRenderer;
- MeshCollider meshCollider;
- MeshFilter meshFilter;
- MeshRenderer meshRenderer;
- Mesh mesh;
- // Use this for initialization
- void Start () {
- chunk = new int[width,height,depth];
- // Set Block Type //
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- for (int z = 0; z < depth; z++)
- {
- chunk[x,y,z] = 1;
- }
- }
- }
- // Add Mesh Pieces //
- meshCollider = gameObject.AddComponent<MeshCollider>();
- meshFilter = gameObject.AddComponent<MeshFilter>();
- meshRenderer = gameObject.AddComponent<MeshRenderer>();
- ChunkRender();
- }
- // Update is called once per frame
- void Update () {
- //ChunkRender(chunk);
- }
- public void ChunkRender()
- {
- List<Vector3> vertices = new List<Vector3>();
- List<Vector2> uvs = new List<Vector2>();
- List<int> triangles = new List<int>();
- int vertexIndex = 0;
- int top = 0;
- int north = 0;
- int east = 0;
- int south = 0;
- int west = 0;
- int bottom = 0;
- meshFilter.mesh.Clear();
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- for (int z = 0; z < depth; z++)
- {
- Block block = new Block();
- block.blockType = chunk[x,y,z];
- if (x == width - 1) {
- east = 0;
- } else {
- east = chunk[x + 1, y, z];
- }
- if (x == 0) {
- west = 0;
- } else {
- west = chunk[x - 1, y, z];
- }
- if (y == height - 1) {
- top = 0;
- } else {
- top = chunk[x, y + 1, z];
- }
- if (y == 0) {
- bottom = 0;
- } else {
- bottom = chunk[x, y - 1, z];
- }
- if (z == depth - 1) {
- north = 0;
- } else {
- north = chunk[x, y, z + 1];
- }
- if (z == 0) {
- south = 0;
- } else {
- south = chunk[x, y, z - 1];
- }
- // Check which faces to render //
- // Grassy Block - Finished //
- if (block.blockType == 1 && top == 0)
- {
- vertexIndex = vertices.Count;
- vertices.Add(new Vector3(x, y + 1, z));
- vertices.Add(new Vector3(x, y + 1, z + 1));
- vertices.Add(new Vector3(x + 1, y + 1, z + 1));
- vertices.Add(new Vector3(x + 1, y + 1, z));
- // first triangle for the block top
- triangles.Add(vertexIndex);
- triangles.Add(vertexIndex + 1);
- triangles.Add(vertexIndex + 2);
- // second triangle for the block top
- triangles.Add(vertexIndex + 2);
- triangles.Add(vertexIndex + 3);
- triangles.Add(vertexIndex);
- // add UV
- uvs.Add(new Vector2 (0.125f, 0.0f));
- uvs.Add(new Vector2 (0.25f, 0.0f));
- uvs.Add(new Vector2 (0.25f, 0.125f));
- uvs.Add(new Vector2 (0.125f, 0.125f));
- }
- // Bottom is Done //
- if (block.blockType == 1 && bottom == 0)
- {
- vertexIndex = vertices.Count;
- vertices.Add(new Vector3(x, y, z));
- vertices.Add(new Vector3(x + 1, y, z));
- vertices.Add(new Vector3(x + 1, y, z + 1));
- vertices.Add(new Vector3(x, y , z + 1));
- triangles.Add(vertexIndex);
- triangles.Add(vertexIndex + 1);
- triangles.Add(vertexIndex + 2);
- triangles.Add(vertexIndex + 2);
- triangles.Add(vertexIndex + 3);
- triangles.Add(vertexIndex);
- // add UV
- uvs.Add(new Vector2 (0.25f, 0.0f));
- uvs.Add(new Vector2 (0.375f, 0.0f));
- uvs.Add(new Vector2 (0.375f, 0.125f));
- uvs.Add(new Vector2 (0.25f, 0.125f));
- }
- // North Block faces Away //
- if (block.blockType == 1 && north == 0)
- {
- vertexIndex = vertices.Count;
- vertices.Add(new Vector3(x, y, z + 1));
- vertices.Add(new Vector3(x + 1, y, z + 1));
- vertices.Add(new Vector3(x + 1, y + 1, z + 1));
- vertices.Add(new Vector3(x, y + 1 , z + 1));
- triangles.Add(vertexIndex);
- triangles.Add(vertexIndex + 1);
- triangles.Add(vertexIndex + 2);
- triangles.Add(vertexIndex + 2);
- triangles.Add(vertexIndex + 3);
- triangles.Add(vertexIndex);
- // add UV
- uvs.Add(new Vector2 (0.75f, 0.0f));
- uvs.Add(new Vector2 (0.625f, 0.0f));
- uvs.Add(new Vector2 (0.625f, 0.125f));
- uvs.Add(new Vector2 (0.75f, 0.125f));
- }
- // South block faces Player
- if (block.blockType == 1 && south == 0)
- {
- vertexIndex = vertices.Count;
- vertices.Add(new Vector3(x, y, z));
- vertices.Add(new Vector3(x, y + 1, z));
- vertices.Add(new Vector3(x + 1, y + 1, z));
- vertices.Add(new Vector3(x + 1, y , z));
- triangles.Add(vertexIndex);
- triangles.Add(vertexIndex + 1);
- triangles.Add(vertexIndex + 2);
- triangles.Add(vertexIndex + 2);
- triangles.Add(vertexIndex + 3);
- triangles.Add(vertexIndex);
- // add UV
- uvs.Add(new Vector2 (0.0f, 0.0f));
- uvs.Add(new Vector2 (0.125f, 0.0f));
- uvs.Add(new Vector2 (0.125f, 0.125f));
- uvs.Add(new Vector2 (0.0f, 0.125f));
- }
- if (block.blockType == 1 && east == 0)
- {
- vertexIndex = vertices.Count;
- vertices.Add(new Vector3(x + 1, y, z));
- vertices.Add(new Vector3(x + 1, y + 1, z));
- vertices.Add(new Vector3(x + 1, y + 1, z + 1));
- vertices.Add(new Vector3(x + 1, y , z + 1));
- triangles.Add(vertexIndex);
- triangles.Add(vertexIndex + 1);
- triangles.Add(vertexIndex + 2);
- triangles.Add(vertexIndex + 2);
- triangles.Add(vertexIndex + 3);
- triangles.Add(vertexIndex);
- // add UV
- uvs.Add(new Vector2 (0.25f, 0.0f));
- uvs.Add(new Vector2 (0.375f, 0.0f));
- uvs.Add(new Vector2 (0.375f, 0.125f));
- uvs.Add(new Vector2 (0.25f, 0.125f));
- }
- if (block.blockType == 1 && west == 0)
- {
- vertexIndex = vertices.Count;
- vertices.Add(new Vector3(x, y, z + 1));
- vertices.Add(new Vector3(x, y + 1, z + 1));
- vertices.Add(new Vector3(x, y + 1, z));
- vertices.Add(new Vector3(x, y , z));
- triangles.Add(vertexIndex);
- triangles.Add(vertexIndex + 1);
- triangles.Add(vertexIndex + 2);
- triangles.Add(vertexIndex + 2);
- triangles.Add(vertexIndex + 3);
- triangles.Add(vertexIndex);
- // add UV
- uvs.Add(new Vector2 (0.375f, 0.0f));
- uvs.Add(new Vector2 (0.50f, 0.0f));
- uvs.Add(new Vector2 (0.50f, 0.125f));
- uvs.Add(new Vector2 (0.375f, 0.125f));
- }
- }
- }
- }
- // End Loop //
- mesh = meshFilter.mesh;
- mesh.vertices = vertices.ToArray();
- mesh.triangles = triangles.ToArray();
- mesh.uv = uvs.ToArray();
- mesh.RecalculateNormals();
- meshCollider.sharedMesh = null;
- meshCollider.sharedMesh = mesh;
- gameObject.renderer.material.mainTexture = basicTexture;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement