Search Unity

How to place the vertices of one mesh on the height of another mesh?

Discussion in 'Scripting' started by NVJOB, Jun 6, 2018.

  1. NVJOB

    NVJOB

    Joined:
    May 18, 2017
    Posts:
    21
    The solution was found independently.
     
    Last edited: Apr 21, 2019
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    There's a lot of ways to tackle this problem.

    Probably the easiest to do this would be to make Mesh 1 a mesh collider and raycast against it from each vertex position to sample the height.

    If Mesh 1 is a terrain it's even easier, Unity has a Terrain.SampleHeight method already.

    You could also bake a heightmap from Mesh 1 and apply it to Mesh 2 using a shader or compute shader.

    You could also do the mesh "raycasting" yourself and calculate the barycentric coordinates for each vertex in Mesh 2 on its complementary point in Mesh 1, then use those results to calculate the lerped height.
     
  3. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    If it's a terrain, simply use SampleHeight.
     
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Ah, ok. I'm assuming this is runtime procedural generation?

    I'd still be tempted to use a compute shader, especially since it only has to be run once.

    If not, however, I'd do what I suggested in my last point: calculate the barycentric coordinates for each vertex on Mesh 2 for its neighboring vertices on Mesh 1.