- Home /
Procedurally Connected Mesh/3d Model Tiles?
On a grid-based game, how would I procedurally determine adjacent objects (North, South,East, West) in an efficient manner?
I already have everything implemented from grid, to grid object placement, to actual object placement. Id like to implement this automatic system rather than having the player ie. select a corner for the building or sidewalk model
Below is a picture to better describe what Im trying to explain:
I have several 3d model variations of multiple objects from cliffs to paths/fences to buildings. Im aiming to allow players to place an object in runtime where if another of the same object is placed next to it, then depending on the direction a different mesh piece would replace the other.
Think of it as the Cliff system while making a map in starcraft 2 or warcraft 3.
(Place an object, in this case a cliff, then place another next to it and both will change where the left cliff would have no face on its right side and the right cliff would have nothing on its left face.)
Im trying to do this because allowing objects to simply overlap with one model rather than having multiple variations of the model with different faces not rendered is more ideal. Another example with exactly what I want:
Answer by Cherno · Jul 10, 2015 at 05:46 PM
You have to implement your own tile identification logic. Each tile needs something that identifies it's basic type, such as "hill", "river", "road" and so on. This can be done in several ways, for example a simple int where 0 is clear, 1 is ground, 2 is road, 3 is hill and so on. Or a string that directly describes the type. Or a enum value. Something like that.
Then, when building the mesh, you have to go through each tile, and for each tile, check it's neighboring tiles for their type as well. Based on that information, your code can decide which mesh part to use for the current tile. For example, if the tile at x,y is a hill, and the tile at x+1,y (left neighbor) is ground/clear, then you need a cliff for the tile at x,y. It gets more complicated the more neighbors you have to take into account, of course.
Ah okay perfect. I made a rough draft of my own version last week doing exactly what you explained. It works properly and as intended but now a question of what is an efficient way? Ive known people use something like raycasts (which is fine if I place one object at a time and call the method) but this may be an issue if I do a mass placement?
The best way would be to locally check that tile rather than the whole tile map which is what you explained, so now would I use Raycasting (can be costly), forloops (what Im currently using) or do you have another suggestion?
Thanks for the quick and awesome reply by the way!
Raycasting is highly inefficient for this. Take a look a $$anonymous$$inecraft-in-Unity tutorials and how they create the terrain mesh. You declare a two- or three-dimensional array where each element corresponds to one cell of your world. I assume you already do this since you mentioned using loops, which is exactly how you should do it.
Ah alright, then I suppose the system I have is already as efficient as it is.
Thanks for the advice again!