Search Unity

Possible to retrieve reference to vertices of Terrain?

Discussion in 'Scripting' started by rw3iss, Dec 29, 2012.

  1. rw3iss

    rw3iss

    Joined:
    Dec 17, 2012
    Posts:
    5
    Heyas,
    My goal is to take a mountain I sculpted with the terrain editor, and create another mesh (actually a layer) that sits on top, to simulate a sort of avalanche. I'd like to know if it's possible to obtain the location of the vertices of the mountain/terrain, so that I can generate the corresponding 'overlay mesh'. If it's not, is there a different way to do this?

    Thanks,
    Ryan
     
  2. RazorCut

    RazorCut

    Joined:
    May 7, 2009
    Posts:
    393
    You can get the heights as floats from Terrain.terrainData. To convert those to Vector3 values you have to use those height values with some maths, depending on the resolution, size, and location of your terrain.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You can't get the terrain vertices as such, since the terrain meshes are dynamic and depend on the camera's viewpoint.

    Or you could just use TerrainData.GetInterpolatedHeight.

    --Eric
     
  4. RazorCut

    RazorCut

    Joined:
    May 7, 2009
    Posts:
    393
    I assumed he meant at design time, not runtime.

    Perhaps I'm totally on crack though and it's not possible at design time either. If so, then you could do a discrete raycast approach to build your secondary mesh, i.e. loop through the X,Y dimensions of your terrain at whatever course step value you want and raycast down to find the Vector3.
     
    Last edited: Dec 29, 2012
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The answer is still TerrainData.GetInterpolatedHeight. ;) If you have a terrain then you have TerrainData, runtime or not. Actually, Terrain.SampleHeight works too and may be easier to use (since it takes a world-space Vector3).

    --Eric
     
  6. RazorCut

    RazorCut

    Joined:
    May 7, 2009
    Posts:
    393
    Awesome!
     
  7. rw3iss

    rw3iss

    Joined:
    Dec 17, 2012
    Posts:
    5
    Thanks guys..
    I change my technique by not relying on terrain data. Instead, I grab a RaycastHit from camera to the terrain, and then interpolate that towards other points along the RaycastHit.normal.

    I did try terrainData.GetIterpolatedHeight() and it did work as expected.

    Ryan