Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.49 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using UnityEngine;
  6.  
  7. [RequireComponent(typeof(MeshRenderer))]
  8. [RequireComponent(typeof(MeshFilter))]
  9. [RequireComponent(typeof(MeshCollider))]
  10. public class Chunk : MonoBehaviour
  11. {
  12.     public Material TerrainMat;
  13.  
  14.     public List<Vector3> Vertices = new List<Vector3>();
  15.     public List<int> Indices = new List<int>();
  16.     public List<Vector2> UVs = new List<Vector2>();
  17.  
  18.     public Voxel[,,] Data;
  19.  
  20.     MeshFilter mf;
  21.     MeshRenderer mr;
  22.     MeshCollider mc;
  23.  
  24.     bool shouldRebuild = false;
  25.     Mesh mesh;
  26.  
  27.     public int X = 0;
  28.     public int Y = 0;
  29.     public int Z = 0;
  30.     public bool WasCreated = false;
  31.  
  32.     // Start is called before the first frame update
  33.     void Start()
  34.     {
  35.         mf = GetComponent<MeshFilter>();
  36.         mr = GetComponent<MeshRenderer>();
  37.         mc = GetComponent<MeshCollider>();
  38.  
  39.        
  40.         //ThreadPool.QueueUserWorkItem(Create);
  41.         //Create();
  42.  
  43.         //X = (int)this.transform.position.x;
  44.        
  45.     }
  46.  
  47.     public void S()
  48.     {
  49.         Data = new Voxel[VoxelGlobals.ChunkSizeX, VoxelGlobals.ChunkSizeY, VoxelGlobals.ChunkSizeZ];
  50.         mesh = new Mesh();
  51.         //ThreadPool.QueueUserWorkItem(new WaitCallback(Create));
  52.         //Task.Factory.CreationOptions = TaskCreationOptions.LongRunning;
  53.         //Task.Factory.StartNew(Create);
  54.         Thread t = new Thread(() =>
  55.         {
  56.             Create();
  57.         });
  58.         t.IsBackground = true;
  59.         //t.Priority = ThreadPriority.Normal;
  60.         t.Start();
  61.     }
  62.  
  63.     // Update is called once per frame
  64.     void Update()
  65.     {
  66.         if(shouldRebuild)
  67.         {
  68.             shouldRebuild = false;
  69.  
  70.             SetMesh();
  71.         }
  72.     }
  73.  
  74.     public void Create()
  75.     {
  76.        
  77.         //Thread t = new Thread(() =>
  78.         //{
  79.  
  80.             //var watch = System.Diagnostics.Stopwatch.StartNew();
  81.            
  82.             Fill(1);
  83.             Extract();
  84.             //watch.Stop();
  85.             //var elapsedMs = watch.ElapsedMilliseconds;
  86.             //Debug.Log("Chunk created in (ms): " + elapsedMs);
  87.             shouldRebuild = true;
  88.             WasCreated = true;
  89.         //});
  90.         //t.Start();
  91.     }
  92.  
  93.     public void Fill(uint id)
  94.     {
  95.         for (int x = 0; x < VoxelGlobals.ChunkSizeX; x++)
  96.         {
  97.             for (int y = 0; y < VoxelGlobals.ChunkSizeY; y++)
  98.             {
  99.                 for (int z = 0; z < VoxelGlobals.ChunkSizeZ; z++)
  100.                 {
  101.                     /*Data[x, y, z] = new Voxel(id);
  102.                     Data[x, y, z].SetAllSides(new Vector2(8, 0));
  103.                     Data[x, y, z].Top = new Vector2(9, 0);
  104.                     Data[x, y, z].Bottom = new Vector2(10, 0);*/
  105.  
  106.                     Data[x,y,z] = WorldGenerator.Generate(X, Y, Z, x, y, z);
  107.                 }
  108.             }
  109.         }
  110.     }
  111.  
  112.     public void Extract()
  113.     {
  114.         Vertices.Clear();
  115.         Indices.Clear();
  116.         UVs.Clear();
  117.  
  118.         int indexCount = 0;
  119.         for (int x = 0; x < VoxelGlobals.ChunkSizeX; x++)
  120.         {
  121.             for (int y = 0; y < VoxelGlobals.ChunkSizeY; y++)
  122.             {
  123.                 for (int z = 0; z < VoxelGlobals.ChunkSizeZ; z++)
  124.                 {
  125.                     if (Data[x, y, z].ID != 0)
  126.                     {
  127.                         if ((y + 1 < VoxelGlobals.ChunkSizeY && Data[x, y + 1, z].ID == 0) || y + 1 >= VoxelGlobals.ChunkSizeY)
  128.                         {
  129.                             CreateTopPlane(x, y, z, indexCount);
  130.                             indexCount += 4;
  131.                         }
  132.                         if ((y - 1 >= 0 && Data[x, y - 1, z].ID == 0) || y - 1 < 0)
  133.                         {
  134.                             CreateBottomPlane(x, y, z, indexCount);
  135.                             indexCount += 4;
  136.                         }
  137.                         if ((x + 1 < VoxelGlobals.ChunkSizeX && Data[x + 1, y, z].ID == 0) || x + 1 >= VoxelGlobals.ChunkSizeX)
  138.                         {
  139.                             CreateRightPlane(x, y, z, indexCount);
  140.                             indexCount += 4;
  141.                         }
  142.                         if ((x - 1 >= 0 && Data[x - 1, y, z].ID == 0) || x - 1 < 0)
  143.                         {
  144.                             CreateLeftPlane(x, y, z, indexCount);
  145.                             indexCount += 4;
  146.                         }
  147.                         if ((z + 1 < VoxelGlobals.ChunkSizeZ && Data[x, y, z + 1].ID == 0) || z + 1 >= VoxelGlobals.ChunkSizeZ)
  148.                         {
  149.                             CreateFrontPlane(x, y, z, indexCount);
  150.                             indexCount += 4;
  151.                         }
  152.                         if ((z - 1 >= 0 && Data[x, y, z - 1].ID == 0) || z - 1 < 0)
  153.                         {
  154.                             CreateBackPlane(x, y, z, indexCount);
  155.                             indexCount += 4;
  156.                         }
  157.                     }
  158.                 }
  159.             }
  160.         }
  161.  
  162.        
  163.     }
  164.  
  165.     public void SetMesh()
  166.     {
  167.         mesh.Clear();
  168.         mesh.SetVertices(Vertices);
  169.         mesh.SetIndices(Indices.ToArray(), MeshTopology.Triangles, 0, true);
  170.         mesh.SetUVs(0, UVs);
  171.         mesh.Optimize();
  172.         mesh.RecalculateNormals();
  173.         mesh.RecalculateBounds();
  174.  
  175.         mf.mesh = mesh;
  176.         mr.material = TerrainMat;
  177.         mc.sharedMesh = mesh;
  178.     }
  179.     void CreateTopPlane(int x, int y, int z, int indexCount)
  180.     {
  181.         Vertices.Add(new Vector3(x, y + VoxelGlobals.VoxelSize, z + VoxelGlobals.VoxelSize));
  182.         Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y + VoxelGlobals.VoxelSize, z + VoxelGlobals.VoxelSize));
  183.         Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y + VoxelGlobals.VoxelSize, z));
  184.         Vertices.Add(new Vector3(x, y + VoxelGlobals.VoxelSize, z));
  185.  
  186.         Indices.Add(indexCount);
  187.         Indices.Add(indexCount + 1);
  188.         Indices.Add(indexCount + 2);
  189.  
  190.         Indices.Add(indexCount);
  191.         Indices.Add(indexCount + 2);
  192.         Indices.Add(indexCount + 3);
  193.  
  194.         UVs.Add(new Vector2(Data[x, y, z].Top.x * VoxelGlobals.SpriteUVSize,
  195.             1.0f - (Data[x, y, z].Top.y * VoxelGlobals.SpriteUVSize)));
  196.         UVs.Add(new Vector2(Data[x, y, z].Top.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
  197.             1.0f - (Data[x, y, z].Top.y * VoxelGlobals.SpriteUVSize)));
  198.         UVs.Add(new Vector2(Data[x, y, z].Top.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
  199.             1.0f - (Data[x, y, z].Top.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
  200.         UVs.Add(new Vector2(Data[x, y, z].Top.x * VoxelGlobals.SpriteUVSize,
  201.             1.0f - (Data[x, y, z].Top.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
  202.     }
  203.  
  204.     void CreateBottomPlane(int x, int y, int z, int indexCount)
  205.     {
  206.         Vertices.Add(new Vector3(x, y, z + VoxelGlobals.VoxelSize));
  207.         Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y, z + VoxelGlobals.VoxelSize));
  208.         Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y, z));
  209.         Vertices.Add(new Vector3(x, y, z));
  210.  
  211.         Indices.Add(indexCount + 2);
  212.         Indices.Add(indexCount + 1);
  213.         Indices.Add(indexCount + 0);
  214.  
  215.         Indices.Add(indexCount + 3);
  216.         Indices.Add(indexCount + 2);
  217.         Indices.Add(indexCount + 0);
  218.  
  219.         UVs.Add(new Vector2(Data[x, y, z].Bottom.x * VoxelGlobals.SpriteUVSize,
  220.             1.0f - (Data[x, y, z].Bottom.y * VoxelGlobals.SpriteUVSize)));
  221.         UVs.Add(new Vector2(Data[x, y, z].Bottom.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
  222.             1.0f - (Data[x, y, z].Bottom.y * VoxelGlobals.SpriteUVSize)));
  223.         UVs.Add(new Vector2(Data[x, y, z].Bottom.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
  224.             1.0f - (Data[x, y, z].Bottom.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
  225.         UVs.Add(new Vector2(Data[x, y, z].Bottom.x * VoxelGlobals.SpriteUVSize,
  226.             1.0f - (Data[x, y, z].Bottom.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
  227.     }
  228.  
  229.     void CreateLeftPlane(int x, int y, int z, int indexCount)
  230.     {
  231.         Vertices.Add(new Vector3(x, y, z));
  232.         Vertices.Add(new Vector3(x, y + VoxelGlobals.VoxelSize, z));
  233.         Vertices.Add(new Vector3(x, y + VoxelGlobals.VoxelSize, z + VoxelGlobals.VoxelSize));
  234.         Vertices.Add(new Vector3(x, y, z + VoxelGlobals.VoxelSize));
  235.  
  236.         Indices.Add(indexCount + 2);
  237.         Indices.Add(indexCount + 1);
  238.         Indices.Add(indexCount + 0);
  239.  
  240.         Indices.Add(indexCount + 3);
  241.         Indices.Add(indexCount + 2);
  242.         Indices.Add(indexCount + 0);
  243.  
  244.         UVs.Add(new Vector2(Data[x, y, z].Left.x * VoxelGlobals.SpriteUVSize,
  245.             1.0f - (Data[x, y, z].Left.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
  246.         UVs.Add(new Vector2(Data[x, y, z].Left.x * VoxelGlobals.SpriteUVSize,
  247.             1.0f - (Data[x, y, z].Left.y * VoxelGlobals.SpriteUVSize)));
  248.         UVs.Add(new Vector2(Data[x, y, z].Left.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
  249.             1.0f - (Data[x, y, z].Left.y * VoxelGlobals.SpriteUVSize)));
  250.         UVs.Add(new Vector2(Data[x, y, z].Left.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
  251.             1.0f - (Data[x, y, z].Left.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));  
  252.     }
  253.  
  254.     void CreateRightPlane(int x, int y, int z, int indexCount)
  255.     {
  256.         Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y, z));
  257.         Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y + VoxelGlobals.VoxelSize, z));
  258.         Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y + VoxelGlobals.VoxelSize, z + VoxelGlobals.VoxelSize));
  259.         Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y, z + VoxelGlobals.VoxelSize));
  260.  
  261.         Indices.Add(indexCount + 0);
  262.         Indices.Add(indexCount + 1);
  263.         Indices.Add(indexCount + 2);
  264.  
  265.         Indices.Add(indexCount + 0);
  266.         Indices.Add(indexCount + 2);
  267.         Indices.Add(indexCount + 3);
  268.  
  269.         UVs.Add(new Vector2(Data[x, y, z].Right.x * VoxelGlobals.SpriteUVSize,
  270.             1.0f - (Data[x, y, z].Right.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
  271.         UVs.Add(new Vector2(Data[x, y, z].Right.x * VoxelGlobals.SpriteUVSize,
  272.             1.0f - (Data[x, y, z].Right.y * VoxelGlobals.SpriteUVSize)));
  273.         UVs.Add(new Vector2(Data[x, y, z].Right.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
  274.             1.0f - (Data[x, y, z].Right.y * VoxelGlobals.SpriteUVSize)));
  275.         UVs.Add(new Vector2(Data[x, y, z].Right.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
  276.             1.0f - (Data[x, y, z].Right.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
  277.     }
  278.  
  279.     void CreateFrontPlane(int x, int y, int z, int indexCount)
  280.     {
  281.         Vertices.Add(new Vector3(x, y, z + VoxelGlobals.VoxelSize));
  282.         Vertices.Add(new Vector3(x, y + VoxelGlobals.VoxelSize, z + VoxelGlobals.VoxelSize));
  283.         Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y + VoxelGlobals.VoxelSize, z + VoxelGlobals.VoxelSize));
  284.         Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y, z + VoxelGlobals.VoxelSize));
  285.  
  286.         Indices.Add(indexCount + 2);
  287.         Indices.Add(indexCount + 1);
  288.         Indices.Add(indexCount + 0);
  289.  
  290.         Indices.Add(indexCount + 3);
  291.         Indices.Add(indexCount + 2);
  292.         Indices.Add(indexCount + 0);
  293.  
  294.         UVs.Add(new Vector2(Data[x, y, z].Front.x * VoxelGlobals.SpriteUVSize,
  295.             1.0f - (Data[x, y, z].Front.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
  296.         UVs.Add(new Vector2(Data[x, y, z].Front.x * VoxelGlobals.SpriteUVSize,
  297.             1.0f - (Data[x, y, z].Front.y * VoxelGlobals.SpriteUVSize)));
  298.         UVs.Add(new Vector2(Data[x, y, z].Front.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
  299.             1.0f - (Data[x, y, z].Front.y * VoxelGlobals.SpriteUVSize)));
  300.         UVs.Add(new Vector2(Data[x, y, z].Front.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
  301.             1.0f - (Data[x, y, z].Front.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
  302.     }
  303.  
  304.     void CreateBackPlane(int x, int y, int z, int indexCount)
  305.     {
  306.         Vertices.Add(new Vector3(x, y, z));
  307.         Vertices.Add(new Vector3(x, y + VoxelGlobals.VoxelSize, z));
  308.         Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y + VoxelGlobals.VoxelSize, z));
  309.         Vertices.Add(new Vector3(x + VoxelGlobals.VoxelSize, y, z));
  310.  
  311.         Indices.Add(indexCount + 0);
  312.         Indices.Add(indexCount + 1);
  313.         Indices.Add(indexCount + 2);
  314.  
  315.         Indices.Add(indexCount + 0);
  316.         Indices.Add(indexCount + 2);
  317.         Indices.Add(indexCount + 3);
  318.  
  319.        
  320.        
  321.        
  322.         UVs.Add(new Vector2(Data[x, y, z].Back.x * VoxelGlobals.SpriteUVSize,
  323.             1.0f - (Data[x, y, z].Back.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
  324.         UVs.Add(new Vector2(Data[x, y, z].Back.x * VoxelGlobals.SpriteUVSize,
  325.             1.0f - (Data[x, y, z].Back.y * VoxelGlobals.SpriteUVSize)));
  326.         UVs.Add(new Vector2(Data[x, y, z].Back.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
  327.             1.0f - (Data[x, y, z].Back.y * VoxelGlobals.SpriteUVSize)));
  328.         UVs.Add(new Vector2(Data[x, y, z].Back.x * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize,
  329.             1.0f - (Data[x, y, z].Back.y * VoxelGlobals.SpriteUVSize + VoxelGlobals.SpriteUVSize)));
  330.     }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement