Bottom area of a 3D model? Raycast?

Hi! I’m working on a system that is like any common RTS. You press a building and put it down where ever you want.

Too be able to level the ground (heightmap) where I put a building I have to know which points to edit (I want to level the area to avoid some parts going through the terrain).
So how would I go about to get the area (and corresponding points) of a 3D model/gameobject?

Should I just use Raycast over a large area around the 3D model, see which ones hit terrain and which hit the 3D model. Then use the ones that hit the 3D model as the points to edit in the heightmap?
This feels very inefficient though, especially when I don’t know which size each model is (so I don’t know for sure what the smallest possible area to search is). So if raycasts is the best way, how can I optimize it based on model-size?

Personally I wouldn’t use raycasting here as you already have the position data of the model and of the terrain. Basically I’d compare the verts in the model to the terrain mesh and get the ones that are closest to the model. An implementation might go something like this (please note this is very much untested):

    GameObject building = GameObject.Find("Building");
    GameObject terrain = GameObject.Find("Terrain");

    Mesh mesh = building.GetComponent<MeshFilter>().mesh;
    List<Vector3> verts = mesh.vertices.ToList();

    int minX = (int)verts.Min(v => v.x);
    int minZ = (int)verts.Min(v => v.z);

    int maxX = (int)verts.Max(v => v.x);
    int maxZ = (int)verts.Max(v => v.z);

    //Note: this assumes your terrain is 0,0 based and unscaled, add offsets as needed
    float[,] terrainHeights = terrain.GetComponent<Terrain>().terrainData.GetHeights(minX, minZ, maxX - minX, maxZ - minZ);
    List<Vector3> terrainVerts = new List<Vector3>();
    //Populates a list of terrain vertices from the hieghts list
    for(int i = minX; i < maxX; i++)
    {
        for(int j = minZ; j < maxZ; j++)
        {
            terrainVerts.Add(new Vector3(i, terrainHeights[i - minX, j - minZ], j));
        }
    }
    //Removes all terrain verts farther than 1 from model verts
    //Could be implemented with loops instead of LINQ
    //These are the verts you need to modify in your terrain
    terrainVerts.RemoveAll(v => verts.Exists(x => Vector3.Distance(v, x) > 1f));