Wednesday, June 6, 2012

Distance Based Texture Blending (UE3/UDK)

Repetitive patterns of tileable textures can be a real pain, espacially when working on terrains. Common ways to fight this effect is the use of detail textures or a multiplication of the texture by itself at a different scale. However these methods have some drawbacks and won't give you completely satisfying results in some situations.

This led me to another approach: blending the diffuse map with another texture at a certain distance. That can be (for example) the same texture with a different (=less) tiling, an overall landscape texture or a multiple tiled version of the same texture just without noticeable repetitiveness.


For a material editor implementation you start off with calculating the distance between the World Position and Camera World Position. This is usually a quite large value but by dividing it by a large number (I suggest values between 4096-32768) you can bring it close to the 0-1 range. Afterwards you plug that on into a power node to control the transition falloff. A minimum value of 2 is highly recommended to prevent the near diffuse texture to get blurry. All you have to do now is clamping the result between 0 and 1 to use it as the alpha input of a Lerp node.

In older UE3 builds (like in UT3) there won't be neither a WorldPosition node nor a Distance node. But you can recreate those node functions by setting up your material like this:


Here you can see typical repetitive patterns of a tileable texture on a large landscape:

For the distance blending I did create a 16 times tiled version of the same
texture in Photoshop and removed recognizable repeatings:

 Finally, a screenshot with just the Lerp nodes alpha input: