Advertisement
Guest User

Untitled

a guest
Oct 6th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CursorGrid : MonoBehaviour {
  5.  
  6. public Terrain terrain;
  7. private TerrainData terrainData;
  8. private Vector3 terrainSize;
  9. private int heightmapWidth;
  10. private int heightmapHeight;
  11. private float[,] heightmapData;
  12. public Transform Object1;
  13. public Transform Object2;
  14.  
  15. void GetTerrainData()
  16. {
  17. if ( !terrain )
  18. {
  19. terrain = Terrain.activeTerrain;
  20. }
  21.  
  22. terrainData = terrain.terrainData;
  23.  
  24. terrainSize = terrain.terrainData.size;
  25.  
  26. heightmapWidth = terrain.terrainData.heightmapWidth;
  27. heightmapHeight = terrain.terrainData.heightmapHeight;
  28.  
  29. heightmapData = terrainData.GetHeights( 0, 0, heightmapWidth, heightmapHeight );
  30. }
  31.  
  32. private Vector3 rayHitPoint;
  33. private Vector3 heightmapPos;
  34.  
  35. // Use this for initialization
  36. void Start () {
  37. GetTerrainData();
  38. ConstructMesh();
  39. Debug.Log("Initiated.");
  40. }
  41.  
  42. // Update is called once per frame
  43. void Update () {
  44. // raycast to the terrain
  45. RaycastToTerrain();
  46.  
  47. // find the heightmap position of that hit
  48. GetHeightmapPosition();
  49.  
  50. // Calculate Grid
  51. CalculateGrid();
  52.  
  53. // Update Mesh
  54. UpdateMesh();
  55. }
  56.  
  57. void RaycastToTerrain()
  58. {
  59. RaycastHit hit;
  60. Ray rayPos = Camera.main.ScreenPointToRay( Input.mousePosition );
  61.  
  62. if ( Physics.Raycast( rayPos, out hit, Mathf.Infinity ) ) // also consider a layermask to just the terrain layer
  63. {
  64. Debug.DrawLine( Camera.main.transform.position, hit.point, Color.red );
  65. rayHitPoint = hit.point;
  66. }
  67.  
  68. if(Input.GetKeyDown(KeyCode.Mouse0)){
  69. Debug.Log ("Instantiating Windmill.");
  70. Instantiate(Object1, hit.point, Quaternion.Euler (0,120,0));
  71. }
  72.  
  73. if(Input.GetKeyDown(KeyCode.Mouse1)){
  74. Debug.Log ("Instantiating Farm.");
  75. Instantiate(Object2, hit.point, Quaternion.identity);
  76. }
  77. }
  78.  
  79.  
  80. void GetHeightmapPosition()
  81. {
  82. // find the heightmap position of that hit
  83. heightmapPos.x = ( rayHitPoint.x / terrainSize.x ) * ((float) heightmapWidth );
  84. heightmapPos.z = ( rayHitPoint.z / terrainSize.z ) * ((float) heightmapHeight );
  85.  
  86. // convert to integer
  87. heightmapPos.x = Mathf.Round( heightmapPos.x );
  88. heightmapPos.z = Mathf.Round( heightmapPos.z );
  89.  
  90. // clamp to heightmap dimensions to avoid errors
  91. heightmapPos.x = Mathf.Clamp( heightmapPos.x, 0, heightmapWidth - 1 );
  92. heightmapPos.z = Mathf.Clamp( heightmapPos.z, 0, heightmapHeight - 1 );
  93. }
  94.  
  95.  
  96. // --------------------------------------------------------------------------- Calculate Grid
  97.  
  98. private Vector3[,] mapGrid = new Vector3[ 9, 9 ];
  99.  
  100. public float indicatorSize = 1.0f;
  101. public float indicatorOffsetY = 5.0f;
  102.  
  103.  
  104. void CalculateGrid()
  105. {
  106. for ( int z = -4; z < 5; z ++ )
  107. {
  108. for ( int x = -4; x < 5; x ++ )
  109. {
  110. Vector3 calcVector;
  111.  
  112. calcVector.x = heightmapPos.x + ( x * indicatorSize );
  113. calcVector.x /= ((float) heightmapWidth );
  114. calcVector.x *= terrainSize.x;
  115.  
  116. float calcPosX = heightmapPos.x + ( x * indicatorSize );
  117. calcPosX = Mathf.Clamp( calcPosX, 0, heightmapWidth - 1 );
  118.  
  119. float calcPosZ = heightmapPos.z + ( z * indicatorSize );
  120. calcPosZ = Mathf.Clamp( calcPosZ, 0, heightmapHeight - 1 );
  121.  
  122. calcVector.y = heightmapData[ (int)calcPosZ, (int)calcPosX ] * terrainSize.y; // heightmapData is Y,X ; not X,Y (reversed)
  123. calcVector.y += indicatorOffsetY; // raise slightly above terrain
  124.  
  125. calcVector.z = heightmapPos.z + ( z * indicatorSize );
  126. calcVector.z /= ((float) heightmapHeight );
  127. calcVector.z *= terrainSize.z;
  128.  
  129. mapGrid[ x + 4, z + 4 ] = calcVector;
  130. }
  131. }
  132. }
  133.  
  134.  
  135. // --------------------------------------------------------------------------- INDICATOR MESH
  136.  
  137. private Mesh mesh;
  138.  
  139. private Vector3[] verts;
  140. private Vector2[] uvs;
  141. private int[] tris;
  142.  
  143.  
  144. void ConstructMesh()
  145. {
  146. if ( !mesh )
  147. {
  148. mesh = new Mesh();
  149. MeshFilter f = GetComponent("MeshFilter") as MeshFilter;
  150. f.mesh = mesh;
  151. mesh.name = gameObject.name + "Mesh";
  152. }
  153.  
  154. mesh.Clear();
  155.  
  156. verts = new Vector3[9 * 9];
  157. uvs = new Vector2[9 * 9];
  158. tris = new int[ 8 * 2 * 8 * 3];
  159.  
  160. float uvStep = 1.0f / 8.0f;
  161.  
  162. int index = 0;
  163. int triIndex = 0;
  164.  
  165. for ( int z = 0; z < 9; z ++ )
  166. {
  167. for ( int x = 0; x < 9; x ++ )
  168. {
  169. verts[ index ] = new Vector3( x, 0, z );
  170. uvs[ index ] = new Vector2( ((float)x) * uvStep, ((float)z) * uvStep );
  171.  
  172. if ( x < 8 && z < 8 )
  173. {
  174. tris[ triIndex + 0 ] = index + 0;
  175. tris[ triIndex + 1 ] = index + 9;
  176. tris[ triIndex + 2 ] = index + 1;
  177.  
  178. tris[ triIndex + 3 ] = index + 1;
  179. tris[ triIndex + 4 ] = index + 9;
  180. tris[ triIndex + 5 ] = index + 10;
  181.  
  182. triIndex += 6;
  183. }
  184.  
  185. index ++;
  186. }
  187. }
  188.  
  189.  
  190. // - Build Mesh -
  191. mesh.vertices = verts;
  192. mesh.uv = uvs;
  193. mesh.triangles = tris;
  194.  
  195. mesh.RecalculateBounds();
  196. mesh.RecalculateNormals();
  197. }
  198.  
  199.  
  200. void UpdateMesh()
  201. {
  202. int index = 0;
  203.  
  204. for ( int z = 0; z < 9; z ++ )
  205. {
  206. for ( int x = 0; x < 9; x ++ )
  207. {
  208. verts[ index ] = mapGrid[ x, z ];
  209.  
  210. index ++;
  211.  
  212. }
  213. }
  214.  
  215. // assign to mesh
  216. mesh.vertices = verts;
  217.  
  218. mesh.RecalculateBounds();
  219. mesh.RecalculateNormals();
  220. }
  221.  
  222. void OnMouseUp ()
  223.  
  224. {
  225. /* GameObject newObject;
  226.  
  227. newObject = (GameObject) Instantiate(Object1, hit.point, Quaternion.identity);
  228.  
  229. Vector3 currentPosition = newObject.transform.position;
  230.  
  231. currentPosition.y = currentPosition.y + 5.0f;
  232.  
  233. newObject.transform.position = currentPosition;*/
  234. }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement