1
\$\begingroup\$

I am programming a Real Time Strategy game, and I was wondering how I could retexture one part of a terrain quad to change a texture in the position where I click at. For example, if I clicked in the center of the terrain, I could change the standard grass texture into a dirt texture.

I was considering, perhaps, using blend maps, but those would not work, as they map out the terrain pre-render, as is done by a heightmap.

Here is a video of the RTS game at the moment

I intend to use these so that I can retexture the ground when there are trees (like in the image below): enter image description here

or if there are farms (minus the wheat, just the tilled land):

enter image description here

or, last, but not least, resources:

enter image description here

Are there any shader techniques that I could try to get an effect similar to these? If so, are there any resources that I could try that would provide me enough information? It does not have to be in C++/OpenGL. I am in greater need of an explanation of how I could do this correctly and efficiently, in order to give the RTS map more flavor.

I am willing to provide any code that might be needed of me.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Let's go from the top:

  1. You need 2 textures - grass and dirt.
  2. Terrain geometry needs some UV mapping for both textures (explicit or procedural, whatever).
  3. You also need a mask to denote where each texture goes (assume simple black-n-white)
  4. For each fragment, sample from mask and draw either of two textures (or a blend)

How this can go:

  1. Could be in a single pass and single shader with both textures and mask lookups.
  2. Could be in two separate passes and shaders. Think decals.
  3. Could be a single megatexture (prepared beforehand or dynamically).

Now come details:

  1. Mask could be a simple greyscale to allow for smooth blending, but it is generally looking washed-out. You might want to use some dithering or a blend texture or blend based on original textures height info.
  2. Rendering whole terrain whilst checking with the mask might be inefficient. You might want to render "plain grass" areas with a simpler shader without mask lookup. and reserve the more complex mask sampling shader for the areas where this is needed.
  3. Storing mask texture in high-enough resolution for the whole terrain might be wasteful. Perhaps you could store the mask values in terrain geometry (per-vertice).
  4. Could be texture-less solution if you know how to generate the pattern you need programatically in shader.
\$\endgroup\$
2
  • \$\begingroup\$ So basically, in the fragment shader, I could, hypothetically, do a thing where if a boolean is true at a certain texture coordinate or a certain group of triangles then a different image can be rendered there, and if not, the standard one? Am I understanding correctly, or no? \$\endgroup\$ Jul 9, 2022 at 7:38
  • \$\begingroup\$ Or, rather, get the vertices of the area where the model is being rendered? \$\endgroup\$ Jul 9, 2022 at 7:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .