Advertisement
Guest User

Untitled

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