Advertisement
Guest User

Untitled

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