Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Terrain: Paint on Terrain between 2 or more Vector3?

Discussion in 'Scripting' started by Der_Kevin, Nov 11, 2016.

  1. Der_Kevin

    Der_Kevin

    Joined:
    Jan 2, 2013
    Posts:
    517
    hey! I am actually pretty new to Terrain modification so please excuse this question if it might be stupid.

    So what i wanna do now is, i have an array of Vector3s. like this:

    and now i "simply" wanna paint a line on the terrain from Element 0 to element 1 to element 2 ..to 3... and so on.

    if you are interested what i need this for. i wanna paint roads on a dynamic generated terrain. which looks like this:

    the green line is the A* debug path which goes from village to village. and all i wanna do now is paint it
     
    Last edited: Nov 11, 2016
  2. Der_Kevin

    Der_Kevin

    Joined:
    Jan 2, 2013
    Posts:
    517
    so. i will just answer myself - or better give a status update since its not done yet.
    what i got so far is this (or what i found on the interwebs so far...):

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class PaintRoad : MonoBehaviour {
    6.  
    7.     float[,,] element;
    8.     int mapX, mapY;
    9.     TerrainData terrainData;
    10.     Vector3 terrainPosition;
    11.     public Terrain myTerrain;
    12.     float[,,] map;
    13.     private Vector3 lastPos;
    14.  
    15.     void Awake()
    16.     {
    17.         map = new float[myTerrain.terrainData.alphamapWidth, myTerrain.terrainData.alphamapHeight, myTerrain.terrainData.alphamapLayers];
    18.  
    19.         element = new float[10, 10, myTerrain.terrainData.alphamapLayers];
    20.         terrainData = myTerrain.terrainData;
    21.         terrainPosition = myTerrain.transform.position;
    22.  
    23.         lastPos = transform.position;
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         UpdateMapOnTheTarget();
    29.     }
    30.  
    31.     void UpdateMapOnTheTarget()
    32.     {
    33.         //just update if you move
    34.         if(Vector3.Distance(transform.position, lastPos) > 1)
    35.         {
    36.             print("paint");
    37.             //convert world coords to terrain coords
    38.             mapX = (int)(((transform.position.x - terrainPosition.x) / terrainData.size.x) * terrainData.alphamapWidth);
    39.             mapY = (int)(((transform.position.z - terrainPosition.z) / terrainData.size.z) * terrainData.alphamapHeight);
    40.  
    41.             map[mapY, mapX, 0] = element[0, 0, 0] = 0;
    42.             map[mapY, mapX, 1] = element[0, 0, 1] = 1;
    43.  
    44.             myTerrain.terrainData.SetAlphamaps(mapX, mapY, element);
    45.  
    46.             lastPos = transform.position;
    47.         }
    48.     }
    49.  
    50. }
    51.  
    put this on a cube that is on/over your terrain and move this cube(in play mode).
    this will paint something black on the terrain where the cube is.

    now i only need to find a way to paint from a list of Vectors?
     
    Last edited: Nov 11, 2016