Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- using UnityEngine;
- [RequireComponent(typeof(MeshRenderer))]
- [RequireComponent(typeof(MeshFilter))]
- [RequireComponent(typeof(MeshCollider))]
- public class Chunk : MonoBehaviour
- {
- public Material TerrainMat;
- public List<Vector3> Vertices = new List<Vector3>();
- public List<int> Indices = new List<int>();
- public List<Vector2> UVs = new List<Vector2>();
- public Voxel[,,] Data;
- MeshFilter mf;
- MeshRenderer mr;
- MeshCollider mc;
- bool shouldRebuild = false;
- Mesh mesh;
- public int X = 0;
- public int Y = 0;
- public int Z = 0;
- public bool WasCreated = false;
- // Start is called before the first frame update
- void Start()
- {
- mf = GetComponent<MeshFilter>();
- mr = GetComponent<MeshRenderer>();
- mc = GetComponent<MeshCollider>();
- //ThreadPool.QueueUserWorkItem(Create);
- //Create();
- //X = (int)this.transform.position.x;
- }
- public void S()
- {
- Data = new Voxel[VoxelGlobals.ChunkSizeX, VoxelGlobals.ChunkSizeY, VoxelGlobals.ChunkSizeZ];
- mesh = new Mesh();
- //ThreadPool.QueueUserWorkItem(new WaitCallback(Create));
- //Task.Factory.CreationOptions = TaskCreationOptions.LongRunning;
- //Task.Factory.StartNew(Create);
- Thread t = new Thread(() =>
- {
- Create();
- });
- t.IsBackground = true;
- //t.Priority = ThreadPriority.Normal;
- t.Start();
- }
- // Update is called once per frame
- void Update()
- {
- if(shouldRebuild)
- {
- shouldRebuild = false;
- SetMesh();
- }
- }
- public void Create()
- {
- //Thread t = new Thread(() =>
- //{
- //var watch = System.Diagnostics.Stopwatch.StartNew();
- Fill(1);
- Extract();
- //watch.Stop();
- //var elapsedMs = watch.ElapsedMilliseconds;
- //Debug.Log("Chunk created in (ms): " + elapsedMs);
- shouldRebuild = true;
- WasCreated = true;
- //});
- //t.Start();
- }
- public void Fill(uint id)
- {
- for (int x = 0; x < VoxelGlobals.ChunkSizeX; x++)
- {
- for (int y = 0; y < VoxelGlobals.ChunkSizeY; y++)
- {
- for (int z = 0; z < VoxelGlobals.ChunkSizeZ; z++)
- {
- /*Data[x, y, z] = new Voxel(id);
- Data[x, y, z].SetAllSides(new Vector2(8, 0));
- Data[x, y, z].Top = new Vector2(9, 0);
- Data[x, y, z].Bottom = new Vector2(10, 0);*/
- Data[x,y,z] = WorldGenerator.Generate(X, Y, Z, x, y, z);
- }
- }
- }
- }
- public void Extract()
- {
- Vertices.Clear();
- Indices.Clear();
- UVs.Clear();
- int indexCount = 0;
- for (int x = 0; x < VoxelGlobals.ChunkSizeX; x++)
- {
- for (int y = 0; y < VoxelGlobals.ChunkSizeY; y++)
- {
- for (int z = 0; z < VoxelGlobals.ChunkSizeZ; z++)
- {
- if (Data[x, y, z].ID != 0)
- {
- if ((y + 1 < VoxelGlobals.ChunkSizeY && Data[x, y + 1, z].ID == 0) || y + 1 >= VoxelGlobals.ChunkSizeY)
- {
- CreateTopPlane(x, y, z, indexCount);
- indexCount += 4;
- }
- if ((y - 1 >= 0 && Data[x, y - 1, z].ID == 0) || y - 1 < 0)
- {
- CreateBottomPlane(x, y, z, indexCount);
- indexCount += 4;
- }
- if ((x + 1 < VoxelGlobals.ChunkSizeX && Data[x + 1, y, z].ID == 0) || x + 1 >= VoxelGlobals.ChunkSizeX)
- {
- CreateRightPlane(x, y, z, indexCount);
- indexCount += 4;
- }
- if ((x - 1 >= 0 && Data[x - 1, y, z].ID == 0) || x - 1 < 0)
- {
- CreateLeftPlane(x, y, z, indexCount);
- indexCount += 4;
- }
- if ((z + 1 < VoxelGlobals.ChunkSizeZ && Data[x, y, z + 1].ID == 0) || z + 1 >= VoxelGlobals.ChunkSizeZ)
- {
- CreateFrontPlane(x, y, z, indexCount);
- indexCount += 4;
- }
- if ((z - 1 >= 0 && Data[x, y, z - 1].ID == 0) || z - 1 < 0)
- {
- CreateBackPlane(x, y, z, indexCount);
- indexCount += 4;
- }
- }
- }
- }
- }
- }
- public void SetMesh()
- {
- mesh.Clear();
- mesh.SetVertices(Vertices);
- mesh.SetIndices(Indices.ToArray(), MeshTopology.Triangles, 0, true);
- mesh.SetUVs(0, UVs);
- mesh.Optimize();
- mesh.RecalculateNormals();
- mesh.RecalculateBounds();
- mf.mesh = mesh;
- mr.material = TerrainMat;
- mc.sharedMesh = mesh;
- }
- void CreateTopPlane(int x, int y, int z, int indexCount)
- {
- Vertices.Add(new Vector3(x, y + VoxelGlobals.VoxelSize, z + VoxelGlobals.VoxelSize));
- Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y + VoxelGlobals.VoxelSize, z + VoxelGlobals.VoxelSize));
- Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y + VoxelGlobals.VoxelSize, z));
- Vertices.Add(new Vector3(x, y + VoxelGlobals.VoxelSize, z));
- Indices.Add(indexCount);
- Indices.Add(indexCount + 1);
- Indices.Add(indexCount + 2);
- Indices.Add(indexCount);
- Indices.Add(indexCount + 2);
- Indices.Add(indexCount + 3);
- UVs.Add(new Vector2(Data[x, y, z].Top.x * VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Top.y * VoxelGlobals.SpriteUVSize)));
- UVs.Add(new Vector2(Data[x, y, z].Top.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Top.y * VoxelGlobals.SpriteUVSize)));
- UVs.Add(new Vector2(Data[x, y, z].Top.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Top.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
- UVs.Add(new Vector2(Data[x, y, z].Top.x * VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Top.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
- }
- void CreateBottomPlane(int x, int y, int z, int indexCount)
- {
- Vertices.Add(new Vector3(x, y, z + VoxelGlobals.VoxelSize));
- Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y, z + VoxelGlobals.VoxelSize));
- Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y, z));
- Vertices.Add(new Vector3(x, y, z));
- Indices.Add(indexCount + 2);
- Indices.Add(indexCount + 1);
- Indices.Add(indexCount + 0);
- Indices.Add(indexCount + 3);
- Indices.Add(indexCount + 2);
- Indices.Add(indexCount + 0);
- UVs.Add(new Vector2(Data[x, y, z].Bottom.x * VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Bottom.y * VoxelGlobals.SpriteUVSize)));
- UVs.Add(new Vector2(Data[x, y, z].Bottom.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Bottom.y * VoxelGlobals.SpriteUVSize)));
- UVs.Add(new Vector2(Data[x, y, z].Bottom.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Bottom.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
- UVs.Add(new Vector2(Data[x, y, z].Bottom.x * VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Bottom.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
- }
- void CreateLeftPlane(int x, int y, int z, int indexCount)
- {
- Vertices.Add(new Vector3(x, y, z));
- Vertices.Add(new Vector3(x, y + VoxelGlobals.VoxelSize, z));
- Vertices.Add(new Vector3(x, y + VoxelGlobals.VoxelSize, z + VoxelGlobals.VoxelSize));
- Vertices.Add(new Vector3(x, y, z + VoxelGlobals.VoxelSize));
- Indices.Add(indexCount + 2);
- Indices.Add(indexCount + 1);
- Indices.Add(indexCount + 0);
- Indices.Add(indexCount + 3);
- Indices.Add(indexCount + 2);
- Indices.Add(indexCount + 0);
- UVs.Add(new Vector2(Data[x, y, z].Left.x * VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Left.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
- UVs.Add(new Vector2(Data[x, y, z].Left.x * VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Left.y * VoxelGlobals.SpriteUVSize)));
- UVs.Add(new Vector2(Data[x, y, z].Left.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Left.y * VoxelGlobals.SpriteUVSize)));
- UVs.Add(new Vector2(Data[x, y, z].Left.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Left.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
- }
- void CreateRightPlane(int x, int y, int z, int indexCount)
- {
- Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y, z));
- Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y + VoxelGlobals.VoxelSize, z));
- Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y + VoxelGlobals.VoxelSize, z + VoxelGlobals.VoxelSize));
- Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y, z + VoxelGlobals.VoxelSize));
- Indices.Add(indexCount + 0);
- Indices.Add(indexCount + 1);
- Indices.Add(indexCount + 2);
- Indices.Add(indexCount + 0);
- Indices.Add(indexCount + 2);
- Indices.Add(indexCount + 3);
- UVs.Add(new Vector2(Data[x, y, z].Right.x * VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Right.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
- UVs.Add(new Vector2(Data[x, y, z].Right.x * VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Right.y * VoxelGlobals.SpriteUVSize)));
- UVs.Add(new Vector2(Data[x, y, z].Right.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Right.y * VoxelGlobals.SpriteUVSize)));
- UVs.Add(new Vector2(Data[x, y, z].Right.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Right.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
- }
- void CreateFrontPlane(int x, int y, int z, int indexCount)
- {
- Vertices.Add(new Vector3(x, y, z + VoxelGlobals.VoxelSize));
- Vertices.Add(new Vector3(x, y + VoxelGlobals.VoxelSize, z + VoxelGlobals.VoxelSize));
- Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y + VoxelGlobals.VoxelSize, z + VoxelGlobals.VoxelSize));
- Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y, z + VoxelGlobals.VoxelSize));
- Indices.Add(indexCount + 2);
- Indices.Add(indexCount + 1);
- Indices.Add(indexCount + 0);
- Indices.Add(indexCount + 3);
- Indices.Add(indexCount + 2);
- Indices.Add(indexCount + 0);
- UVs.Add(new Vector2(Data[x, y, z].Front.x * VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Front.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
- UVs.Add(new Vector2(Data[x, y, z].Front.x * VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Front.y * VoxelGlobals.SpriteUVSize)));
- UVs.Add(new Vector2(Data[x, y, z].Front.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Front.y * VoxelGlobals.SpriteUVSize)));
- UVs.Add(new Vector2(Data[x, y, z].Front.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Front.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
- }
- void CreateBackPlane(int x, int y, int z, int indexCount)
- {
- Vertices.Add(new Vector3(x, y, z));
- Vertices.Add(new Vector3(x, y + VoxelGlobals.VoxelSize, z));
- Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y + VoxelGlobals.VoxelSize, z));
- Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y, z));
- Indices.Add(indexCount + 0);
- Indices.Add(indexCount + 1);
- Indices.Add(indexCount + 2);
- Indices.Add(indexCount + 0);
- Indices.Add(indexCount + 2);
- Indices.Add(indexCount + 3);
- UVs.Add(new Vector2(Data[x, y, z].Back.x * VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Back.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
- UVs.Add(new Vector2(Data[x, y, z].Back.x * VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Back.y * VoxelGlobals.SpriteUVSize)));
- UVs.Add(new Vector2(Data[x, y, z].Back.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Back.y * VoxelGlobals.SpriteUVSize)));
- UVs.Add(new Vector2(Data[x, y, z].Back.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
- 1.0f - (Data[x, y, z].Back.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement