Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Chunk : ITickable
  6. {
  7. public readonly int ChunkWidth = 20;
  8. public readonly int ChunkHeight = 75;
  9. public int CloudValue;
  10.  
  11. private Cube[,,] _Cubes;
  12. private int PosX;
  13. private int Posz;
  14.  
  15. public Chunk(int px, int pz)
  16. {
  17. PosX = px;
  18. Posz = pz;
  19. }
  20.  
  21. protected bool HasGenerated = false;
  22. public float GetHeight(float px, float pz, float py)
  23. {
  24. px += (PosX * ChunkWidth);
  25. pz += (Posz * ChunkWidth);
  26.  
  27. float p1 = Mathf.PerlinNoise(px / GameManager.Sdx, pz / GameManager.Sdz) * GameManager.Smul;
  28. p1 *= (GameManager.Smy * py);
  29.  
  30. return p1;
  31. }
  32.  
  33. public void Start()
  34. {
  35. _Cubes = new Cube[ChunkWidth, ChunkHeight, ChunkWidth];
  36.  
  37. for (int x = 0; x < ChunkWidth; x++)
  38. {
  39. for (int y = 0; y < ChunkHeight; y++)
  40. {
  41. for (int z = 0; z < ChunkWidth; z++)
  42. {
  43. float perlin = GetHeight(x, z, y);
  44. System.Random m_rnd = new System.Random();
  45. CloudValue = m_rnd.Next(1, 10);
  46. if (perlin > GameManager.Scutoff)
  47. {
  48. _Cubes[x, y, z] = Cube.Air;
  49. }
  50. else if (perlin > GameManager.Scutoff / 2)
  51. {
  52. _Cubes[x, y, z] = Cube.Grass;
  53. }
  54. else if (perlin > GameManager.Scutoff / 3)
  55. {
  56. _Cubes[x, y, z] = Cube.Water;
  57. }
  58. else
  59. {
  60. _Cubes[x, y, z] = Cube.Dirt;
  61. }
  62. if (y == 100 & CloudValue > 5)
  63. {
  64. _Cubes[x / 3, y, z / 5] = Cube.Cloud;
  65. }
  66. }
  67. }
  68. }
  69. HasGenerated = true;
  70. }
  71.  
  72. public void Tick()
  73. {
  74.  
  75. }
  76.  
  77. protected bool HasDrawn = false;
  78. private MeshData data;
  79. protected bool DrawnLock = false;
  80. public void Update()
  81. {
  82. if (!HasDrawn && HasGenerated && !DrawnLock)
  83. {
  84. DrawnLock = true;
  85. data = new MeshData();
  86. for (int x = 0; x < ChunkWidth; x++)
  87. {
  88. for (int y = 0; y < ChunkHeight; y++)
  89. {
  90. for (int z = 0; z < ChunkWidth; z++)
  91. {
  92. data.Merge(_Cubes[x, y, z].Draw(this, _Cubes, x, y, z));
  93. }
  94. }
  95. }
  96. DrawnLock = false;
  97. HasDrawn = true;
  98. }
  99. }
  100. protected bool HasRendered = false;
  101. private GameObject go;
  102. public virtual void OnUnityUpdate()
  103. {
  104. if (HasGenerated && !HasRendered && HasDrawn)
  105. {
  106. HasRendered = true;
  107. Mesh mesh = data.ToMesh();
  108. if (go == null)
  109. {
  110. go = new GameObject();
  111. }
  112.  
  113. Transform t = go.transform;
  114.  
  115. if (t.gameObject.GetComponent<MeshFilter>() == null)
  116. {
  117. t.gameObject.AddComponent<MeshFilter>();
  118. t.gameObject.AddComponent<MeshRenderer>();
  119. t.gameObject.AddComponent<MeshCollider>();
  120. t.gameObject.GetComponent<MeshRenderer>().material = Resources.Load<Material>("ChunkMaterial");
  121. t.transform.position = new Vector3(PosX * ChunkWidth / 2, 0, Posz * ChunkWidth / 2);
  122. t.gameObject.layer = LayerMask.NameToLayer("Ground");
  123. Texture2D tmp = new Texture2D(0, 0);
  124. tmp.LoadImage(System.IO.File.ReadAllBytes("atlas.png"));
  125. tmp.filterMode = FilterMode.Point;
  126. t.gameObject.GetComponent<MeshRenderer>().material.mainTexture = tmp;
  127. }
  128.  
  129. t.transform.GetComponent<MeshFilter>().sharedMesh = mesh;
  130. t.transform.GetComponent<MeshCollider>().sharedMesh = mesh;
  131. }
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement