r/GraphicsProgramming • • 1d ago

Question Smoothly blending integer index based terrain material maps?

So imagine I am rendering terrain on an arbitrary chunk, that receives a height and normal map for said chunk. Now say I want to apply different materials to this chunk. Ideally I can store all of this info in one 4 channel texture. Height in r channel, 2 angles for the world space normal in the g and b channels, and then a spatial distribution of a 1D index dictating which materials exist where within the chunk. For example, if a region of the texture contains index 0, use dirt, if another region in the same texture contains index 1, use grass, and so on. I could have a ton of materials represented this way in just a single texture. The plan is that when I render the final terrain, the fragment shader will sample a texture array containing all the possible materials via the material index map stored in the terrain chunk texture.

The issue with this approach is that boundaries between materials will have hard edges. If I introduce a blending factor between edges, this could end up sampling material indices in between the 2 regions, which is not what we want to happen.

So my question is, is there a way to introduce smooth blending between these material indices, ideally restricted to a constant amount of extra memory if extra is necessary?

Alternatively, is there a better way to handle this?

3 Upvotes

5 comments sorted by

•

u/AutoModerator 1d ago

Recently updated rules. Please check that your post is in compliance with Rules 1, 4, 5, and 6 on the sidebar. TL;DR:

  • No showcases.
  • Disclose AI usage in post or project.
  • Present a topic to talk about, e.g. how something works.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

3

u/CptCap 15h ago edited 15h ago

Read the 4 neighboring material ids, fetch the 4 materials and blend between them (using whatever blending algorithm you like).

This is what we did on Ghost Recon: here is the presentation

Fetching 4 materials per pixel is expensive, (at least on last gen, it was more expensive that was reasonable for shipping), but there are ways of dealing with it. You can split your terrain mesh to only render the parts with 1, 2 or 4 materials blending using separate shaders, or you can stochastically evaluate only one material per pixel and let the denoiser/TAA do the blending. On GR we did the first one, far cry went with the second presentation. 

NB: both GR and far cry use a procedural VT to cache all this, but it doesn't change the algorithm. (Also, proc VT aren't really worth it anymore)

1

u/UsualSpace_ 10h ago

Hey, thank you! Seems simple enough and it's nice to see this in action too.

2

u/trad_emark 1d ago

i am curious how others do this. anyway, here is my approach:
i determine the terrain type/index in vertex fragment. (i have it stored in vertex data, not texture, but the principle is the same). after that i construct a matrix where each element is the strength of the type of terrain. so element 0,0 is dirt, element 0,1 is grass, etc, up to 16 types in single matrix. use value 1 for the index actual index, and fill 0 in all remaining elements.
after that matrix is interpolated, in fragment shader, i find the two/three types with highest value in the matrix. (i specifically use only two highest values, but it is possible to use 3 as well). the values must be normalized, and that yields weights for interpolating between the terrain types.
the blending width is determined by the distance of the vertices.
hope this idea helps. i would also be interested to hear other ideas too.

1

u/UsualSpace_ 10h ago

That is an interesting approach, I will look into it, thanks!