Thursday, September 5, 2013

[UDK/UE3 Material] Normals From Height

This post is about generating normals from height maps on runtime in the material editor.
UDK comes with a material function that can do this job but it's rather poor: the result is pretty noisy, might suffer from small artifacts and shifts the texture by half a texel. The main reason for this is that it only offsets the height texture in one direction on both, x and y axis by a certain amount - then calculates normals from change in height.


For a better result you should offset the height texture in both directions on both axes. It's also recommended to do these offsets in texel increments.

Of course there are more advanced algorithms available to generate normals but these are expensive and therefore not advisable for real-time usage. As an example, this is the code from ATI's NormalMapGenerator/Rendermonkey implemented in a custom node (8 samples, Sobel kernel):


UDK Material Function (55 instructions)

Recommended Material (55 instructions)
ATI/Rendermonkey/Sobel (70 instructions)

For further comparison, here's the equivalent external normalmap

TC_Normalmap (48 instructions)

  TC_NormalmapUncompressed (51 instructions)

Monday, September 2, 2013

[UDK] Iterative Parallax Mapping

Iterative Parallax Mapping is a good way to improve the simple, old fashioned Parallax/BumpOffset Mapping which often suffers from texture swimming and only barely noticable effects. Yet it's way cheaper than complex techniques like parallax occlusion mapping or relief mapping.


To get started with the material editor I'd suggest to rebuild the functionality of the BumpOffset node first.

With iterative parallax mapping the texcoords of this simple parallax effect are used as an input for another parallax pass. In addition the height value is being multiplied by the normal maps blue channel which prevents from texture swimming at steep slopes. Having multiple iterations also means that the total height ratio is equal to [height ratio per pass * iteration count].
This is the material setup for 2 iterations:

As an alternative you can chain up multiple BumpOffset nodes. This won't include a multiply by normal.z but is faster to set up as well as cheaper on instructions.

For a more flexible setup (varying iteration counts) you can put all of this into a custom node:


Iterative parallax mapping can produce some visually pleasing results. Of course it's not as good POM as it won't handle self occlusion. But even though it's just a shift of texture coordinates, some of the fakery depth can be preserved even at an oblique angle of view. For textures like bricks or cobble stone it might be also a good idea to implement some fakery self shadowing for the material.


The performance impact of the material is 5 shader instructions per additional iteration (or 3 instructions per iteration if you stick with multiple BumpOffset nodes). For most materials there is no need to go higher than 4 iterations.

Normal Mapping Normal + Parallax Normal + Iterative Plx
(1 Iteration)
Normal + Iterative Plx
(2 Iterations)
Normal + Iterative Plx
(3 Iterations)
Normal + Iterative Plx
(4 Iterations)
Normal + Iterative Plx
(8 Iterations)
Instructions 48 51 54 59 64 69 89