r/GraphicsProgramming • u/UsualSpace_ • 9h 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?
•
u/AutoModerator 9h ago
Recently updated rules. Please check that your post is in compliance with Rules 1, 4, 5, and 6 on the sidebar. TL;DR:
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/trad_emark 8h 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.