Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3.  
  4. [ExecuteInEditMode()]
  5. public class TerrainGenerator : MonoBehaviour
  6. {
  7. public float m_maxHeight = 10f;
  8. public int m_quality = 100;
  9. public float m_size = 10.0f;
  10. public float m_phase = 0f;
  11. public float m_scale = 0.25f;
  12. public Color m_topColor = new Color(0.2f, 1f, 0.1f);
  13. public Color m_midColor = new Color(0f, 0.5f, 0f);
  14. public Color m_bottomColor = new Color(0.7f, 0.3f, 0.1f);
  15. public float m_midColorPosition = 0.5f;
  16. private Texture2D m_texture = null;
  17. public Texture2D m_heightmap = null;
  18.  
  19. public bool m_neverUpdated = true;
  20. public bool m_update = false;
  21. public bool m_dirty = false;
  22.  
  23. private MeshFilter m_meshFilter = null;
  24. private MeshRenderer m_meshRenderer = null;
  25.  
  26. private int m_oQuality = 0;
  27. private float m_oSize = 0f;
  28. private float m_oPhase = 0f;
  29. private float m_oScale = 0f;
  30. private float m_oMaxHeight = 0f;
  31. private Color m_oTopColor = Color.black;
  32. private Color m_oMidColor = Color.black;
  33. private Color m_oBottomColor = Color.black;
  34. private float m_oMidColorPosition = 0f;
  35.  
  36. private void Start()
  37. {
  38. m_meshFilter = GetComponent<MeshFilter>();
  39. m_meshRenderer = GetComponent<MeshRenderer>();
  40.  
  41. UpdateVariables();
  42.  
  43. if (Application.isPlaying)
  44. {
  45. m_update = false;
  46. }
  47. else
  48. {
  49. m_update = true;
  50. m_dirty = false;
  51.  
  52. if (m_neverUpdated)
  53. {
  54. m_dirty = true;
  55. m_neverUpdated = false;
  56. }
  57. }
  58. }
  59.  
  60. private void Update()
  61. {
  62. if (m_update)
  63. {
  64. if (m_oQuality != m_quality)
  65. m_dirty = true;
  66. if (m_oScale != m_scale)
  67. m_dirty = true;
  68. if (m_oSize != m_size)
  69. m_dirty = true;
  70. if (m_oPhase != m_phase)
  71. m_dirty = true;
  72. if (m_oMaxHeight != m_maxHeight)
  73. m_dirty = true;
  74. if (m_oTopColor != m_topColor)
  75. m_dirty = true;
  76. if (m_oMidColor != m_midColor)
  77. m_dirty = true;
  78. if (m_oBottomColor != m_bottomColor)
  79. m_dirty = true;
  80. if (m_oMidColorPosition != m_midColorPosition)
  81. m_dirty = true;
  82.  
  83. if (m_dirty)
  84. {
  85. if (m_heightmap != null)
  86. {
  87. GenerateGrid();
  88. GenerateTexture();
  89. m_dirty = false;
  90. UpdateVariables();
  91. }
  92. }
  93. }
  94. }
  95.  
  96. private void UpdateVariables()
  97. {
  98. m_oQuality = m_quality;
  99. m_oSize = m_size;
  100. m_oPhase = m_phase;
  101. m_oScale = m_scale;
  102. m_oMaxHeight = m_maxHeight;
  103. m_oTopColor = m_topColor;
  104. m_oMidColor = m_midColor;
  105. m_oBottomColor = m_bottomColor;
  106. m_oMidColorPosition = m_midColorPosition;
  107. }
  108.  
  109. private void GenerateTexture()
  110. {
  111. Gradient g = new Gradient();
  112. GradientColorKey[] gck = new GradientColorKey[3];
  113. GradientAlphaKey[] gak = new GradientAlphaKey[1];
  114. gak[0].alpha = 1f;
  115. gak[0].time = 0f;
  116. gck[0].color = m_topColor;
  117. gck[0].time = 0f;
  118. gck[1].color = m_midColor;
  119. gck[1].time = m_midColorPosition;
  120. gck[2].color = m_bottomColor;
  121. gck[2].time = 1f;
  122. g.SetKeys(gck, gak);
  123. int h = 255;
  124. m_texture = new Texture2D(
  125. 1,
  126. h,
  127. TextureFormat.RGB24,
  128. false);
  129. for (int i = 0; i < h; ++i)
  130. {
  131. m_texture.SetPixel(
  132. 0,
  133. i,
  134. g.Evaluate(1f-(i/(float)h)));
  135. }
  136. m_texture.Apply();
  137. m_meshRenderer.sharedMaterial.mainTexture = m_texture;
  138. }
  139.  
  140. private void GenerateGrid()
  141. {
  142. Mesh m = new Mesh();
  143.  
  144. float s = m_size/(m_quality - 1);
  145.  
  146. int c = 0;
  147. Vector3[] verts = new Vector3[m_quality * m_quality];
  148. Vector2[] uvs = new Vector2[verts.Length];
  149. for (int i = 0; i < m_quality; ++i)
  150. {
  151. float y = i/(float) m_quality;
  152. for (int j = 0; j < m_quality; ++j)
  153. {
  154. float x = j/(float) m_quality;
  155. Color col = m_heightmap.GetPixel(
  156. (int) Mathf.Floor(x*m_heightmap.width),
  157. (int) Mathf.Floor(y*m_heightmap.height));
  158. Vector3 p = new Vector3(
  159. j*s,
  160. col.r*m_maxHeight,
  161. i * s);
  162. verts[c] = p;
  163. uvs[c] = new Vector2(
  164. 0.0f,
  165. p.y / m_maxHeight);
  166. ++c;
  167. }
  168. }
  169.  
  170. c = 0;
  171. int[] triangles = new int[m_quality*m_quality*6];
  172. for (int i = 0; i < m_quality - 1; ++i)
  173. {
  174. for (int j = 0; j < m_quality - 1; ++j)
  175. {
  176. triangles[c] = i*m_quality + j;
  177. triangles[c + 1] = (i + 1)*m_quality + j;
  178. triangles[c + 2] = i * m_quality + j + 1;
  179.  
  180. triangles[c + 3] = triangles[c + 1];
  181. triangles[c + 4] = (i + 1)*m_quality + j + 1;
  182. triangles[c + 5] = triangles[c + 2];
  183. c += 6;
  184. }
  185. }
  186.  
  187. m.vertices = verts;
  188. m.uv = uvs;
  189. m.triangles = triangles;
  190. m.RecalculateNormals();
  191. m.RecalculateBounds();
  192. m_meshFilter.mesh = m;
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement