Unity, the Terrain and me, Part 2

After having the basics for this, see part 1, I created a plane and subdivided it a few times in Blender to have enough vertices in the mesh. I probably could use ProBuilder and do the subdivide there, but I try it first this way.

I imported the mesh as fbx into Unity3D and scaled it and moved it over the terrain. Now I want that every vertics of the mesh has the same y-axis height than the terrain at its x/z position.

Since I want to change vertices in the mesh, I have to set it to Read/Write Enabled in its importing settings on the model file.

First thing is to get the mesh data. But, and here I stucked last time I was trying this, the imported plane mesh asset only has a MeshFilter. What is a MeshFilter ?

„The Mesh Filter takes a mesh from your assets and passes it to the Mesh Renderer for rendering on the screen.“ (Unity3D – Documentation)

Since it is an instance, I am still not sure if I can change it at all. This is a basic flaw in my understanding of Unity3D. Please be patient with me here. 😀

But after reading the documentation, I noticed that last time I was trying this, I did it in editor mode. And there you are advised to use the sharedMesh field of the MeshFilter. And using that, you actually change the asset itself ! Unity3D clearly states, that you should not use sharedMesh for changing the mesh verticies.

If you use the mesh field of MeshFilter durching Run-Time, it creates a duplicate and every time you use mesh, this duplicate is returned. Also sharedMesh then points to this duplicate. So I will do that then. Here I only use one mesh, but for the planed used this is important, since there I will change a lot of instances of the same mesh.

And actually, this worked on the spot :

  MeshFilter meshFilter;
    Mesh mesh;

    meshFilter = terrainCoveringMesh.GetComponent<MeshFilter>();
    mesh = meshFilter.mesh;

    Vector3[] verts = mesh.vertices;

    Debug.Log("Found " + verts.Length + " Vertices for Tile " + terrainCoveringMesh.name);


    for (int i = 0; i < verts.Length; i++)
    {

        Vector3 vertPos = terrainCoveringMesh.transform.TransformPoint(verts[i]);

        Vector3 pos = vertPos;
        pos.y = Terrain.activeTerrain.SampleHeight(vertPos);

        vertPos.y = pos.y;

        Vector3 backToLocal = terrainCoveringMesh.transform.InverseTransformPoint(vertPos);

        verts[i] = backToLocal;

    }

    mesh.vertices = verts;

    meshFilter.sharedMesh = mesh;

    meshFilter.sharedMesh.RecalculateBounds();
    meshFilter.sharedMesh.RecalculateNormals();
I made the covering mesh smaller so you can see the underlaying structure.

Important here is that you convert the vertices position from local space to world space and then back to localspace, using the transform of the mesh you want to lay over the terrain.

So this is done. Now how to get this working in Editor Mode is another question and the final problem.

Unity, the Terrain and me, Part 1

This is a very naive approach to the wonderfull world of Unity3D Terrain System.

I played around with it for a long time, did some cool things with it, like on-the-fly bomb craters via script and stuff.

It actually is time now to do this a bit more focused, since I am planing to use it in my game Tanks with Legs. For doing so, I started a laboratory session with Unity3D 2019.

First thing I want and need to do : Take a Mesh and put it on the terrain surface like a coat. I need this for my procedural level generator streetmap generator to make the streets or paths align to the height of the terrain.

But I ran into some problems while doing so and noticed I have to fresh up on a few things. I will do this very slowly and document it here.

Put a grid on the terrain

First thing I want to do is to put a grid of sphers on the terrain, on the same height as their world position on the terrain.

I setup a new GameObject and put a script on it which I named TerrainGridder.

First thing I need to know is which terrain to use, which is done by a public Terrain field so it can be assigned in the inspector.

Now I need to get the pivot world location of the terrain and then the dimensions of the terrain. Since the dimensions in the transform of a terrain is (1/1/1), this is not really usefull.

On a normal GameObject you would use the bounds of the Renderer Component, but since a terrain has no renderer (at least not by default or as component), you can use the size field of the terrainData field of the terrain. This field holds a vector with the dimensions of the terrain in worldScale.

With the method getHeight of the terrainData object of the terrain I can get easily the height of the terrain at a (x/z) world position.

So after using this code :

   for (int y = 0; y < gridSize; y++)
    {
        float ypos = (terrainDimensionsInWorldScale.y / gridSize) * y;

        for (int x = 0; x < gridSize; x++)
        {
            GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);

            float xpos = (terrainDimensionsInWorldScale.x / gridSize) * x;


            sphere.transform.position = new Vector3(xpos, terrainToGrid.terrainData.GetHeight((int)xpos, (int)ypos), ypos);
        }
    }

I have this grid on the terrain :

Grid on Terrain made with my little TerrainGridder script.

But if I change the height of the spheres using the retrieved y height with the terrainData method, it is nor working as excepted :

So what am I doing wrong here ? Let me investigate.

So first of all, it seems that I should use terrain.SampleHeight and not terrain.terrainData.GetHeight.

So with this code :

    for (int y = 0; y < gridSize; y++)
    {
        float ypos = (terrainDimensionsInWorldScale.z / gridSize) * y;

        for (int x = 0; x < gridSize; x++)
        {
            GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);

            float xpos = (terrainDimensionsInWorldScale.x / gridSize) * x;

            sphere.transform.position = new Vector3(xpos, terrainToGrid.SampleHeight(new Vector3(xpos,0,ypos)), ypos);
        }
    }

I get the result I wanted as first step.

The grid of spheres is now perfectly aligned on the y-axis according to the terrain height. With this I can start part two of the TerrainGridder : Taking a mesh and changing the height of its vertices to the height of the terrain.