Imposing a grid on 3D terrain

I’m fairly new to 3D programming and am looking into using Panda3D for my first “major” game project, which will be an RPG using grid-based movement on 3D terrain. In order for this to work I’m going to need a method to impose a grid onto a terrain mesh and highlight individual squares according to various conditions and am unsure of how to proceed with that. Could anyone give me an idea as to what techniques would be best for this and possibly point me to the relevant bits of documentation?

I’ve fiddled with this on two very unfinished projects. I’m at work so I can’t write up details, here is some old code I did for a city simulator: bazaar.launchpad.net/~croxis/+ju … ronment.py In short I had two different textures for the grid lines and the tiles. The grid was a black square outline with thransparency. Is was scaled down and repeated for the grid. Thecell colors was just a simple image where each pixle was a grid cell.

Note: there are probably better ways to go about doing this with shaders.

I wasn’t able to get your game to run. Do you have any screenshots or video of it in action so that I can see if your techniques would work for what I have in mind?

Here are some screenshots:

Using textures is good, but another method that might be interesting to experiment with is to instanceTo the terrain mesh to another node (so that it gets updates), enable wireframe rendering on it, and use a depth offset (or a Z offset) to make it always render on top. Perhaps you could combine this with small texture with setBorderColor and texture transforms to make a square highlight.

However, terrain meshes are built out of triangles, so there’ll unfortunately be a cross-line. It would not be difficult to make a script that renders your terrain mesh out of line segments, however.

That said, croxis’ suggestion is probably a lot easier - just use a square texture and tile it over the terrain, with possibly a second square texture on top (with border color instead of repeat mode) with an appropriate texture transform for the highlighted cell.

If you are using a shader for your terrain, you might be able to adjust your shader instead to avoid having to use textures in the first place.

The thing is, I want the grid to map strictly to X/Z coordinates regardless of the contours of the terrain. The terrain polygons aren’t going to correspond to the grid squares (I intend for the terrain visible to the player to be substantially more detailed than the grid used for gameplay calculations) and I want each “tile” to be a perfect square on the X/Z plane so that it would look like a perfect grid when viewed from directly above, but have the squared vary in size in actual 3D space.

I’ll see if there’s a way to do this through shaders. Theoretically, I just need to check the X/Z coordinates of each pixel on the terrain mesh and apply highlights accordingly, but I’m not certain how difficult that would be in practice since I’ve never worked with shaders before.

I think the texture approach would work in your case. UV coordinates of GeoMipTerrain are generated using top-down projection, so a tiled square texture would show up as a perfect grid from above.

You can set the texture scale to any degree to control how big your grid cells are - you are not bound by the polygon size.

Or am I misunderstanding what you’re trying to do?

No, that sounds about right, although I’m still going to have to figure out how to selectively highlight and un-highlight individual squares. Still, I think I’ve got enough of a general idea on what I have to do to move forward.