I've thought about this a bit already but I was wondering if it was possible if you're for example placing buildings to flatten a mesh they're being placed on to make them fit? The most obvious method would probably be raycasts arranged in a box around the building you're placing then doing a for loop check for vertices or something like that. I'm wondering though if there's any kind of straightforward way available for Godot?
Flatten a mesh according to raycasts above it?
In code terms it would probably be easiest to simply scale the model/meshinstance however for that to work predictably the models would all have to have their origins/pivots also placed very precisely/deliberately when modeling them.
Assuming the building flattens the terrain mesh to y == 0, as far as I can think of atm, it would help if you already had a function for generating the terrain. Are you procedurally generating terrain or loading in a mesh? Either way, if the terrain pieces are in smaller chunks then finding the vertices to set to zero shouldn't be too expensive. On the other hand, if your terrain is just a big plane/grid of vertices that you're offsetting with a vertex shader it might be even easier.
- Edited
Erich_L I could do either in the future I guess but the mesh currently is already pre-made not procedurally generated, it's just something I'm curious about because it's that age old problem when it comes to games that have any kind of placement where you have to think about how to deal with the uneven terrain. Do you make the terrain match up with the building once it's placed? Or do you simply intersect the building into terrain? Most devs I've noticed tend to stick with the latter because that's the easier option which is fair enough.
Editing a static mesh in code is a real pain and not very efficient in gdscript. I'd look at each terrain vertex the building is getting placed over, average the height and place the building at that y value. Next make sure the building mesh has some "under belly" like concrete foundation or brick which can stick in and then through the ground. That would probably the easiest route.
- Edited
I don't think you need to cast rays here.
I assume you have a box to place on a height map in a 3D scene.
Don't know if this is best practice, but I think you need control over both meshes, the box and the one generated from the height map. Get the bottom surface area of the box to place, then determine the place to put the box and level all the height values, plus a certain rim around them, to the same y value. Don't forget to set values in the source as well, e.g. in the texture, which is probably the most complicated part if Godot doesn't have a functionality for it.
This will create the bottom plate so to say. But you'll have steep (up to 90°) banks in areas where there is now a large step. So set a maximum angle (maybe 70°) to avoid too heavy artefacts when rendering textures over it, or have textures meshes of walls or other earth- or construction works to put there. That'll be content creation.
There's another thing to have in mind: not all terrain rendering has meshes, specially large terrain generates them on the fly each time the player looks at it (oh numerical stability sighs). Maybe think over how to change the box, and not the terrain, add modularized patio and walls/embankments if necessary around it, together with textures. There must be an interface between such models and the terrain renderer to place these things gap-free into the landscape.
I guess that's how Slartibartfast would do it :-)
- Edited
Pixophir That's not a bad idea at all, I suppose I could update the models to make them intersect with uneven terrain cleaner than changing the mesh at all like you suggest. I remember in Fallout 4 you had those concrete foundations for example which did the job even though they were a bit daft and you were clearly going through the world with them but as far as the gamer is concerned it's all clean geometry.
Some redesigning of my buildings might be in order in that case.