Getting correct normals for texturing in triplanar shader

I wanted a quick procedural generated terrain. I generated it as a mesh and tried using a triplanar shader to texture it. It worked OK most of the time but I got really nasty smearing across primitives which where almost orthogonal. Eventually I worked out it was because of the interpolation in the vertex normals (which are used to control the blending for the texturing). Because the primitives are large this causes problems with the triplanar shader. The image below shows what I mean. note the stretching at the foot of the cliff and some of the vertical edges.

My fix is to syntesise the normals for the texturing in the fragment shader using the ddx and ddy functions. The image below shows the result/

I got the code for calculating the normal from this post by David Williams:

 float3 surfaceNormal = normalize(cross(ddx(IN.worldPos.xyz), ddy(IN.worldPos.xyz)));

Seems to solve my problem anyway.

1 Like

for info also,
Normal Mapping for a Triplanar Shader

1 Like

It’s a good solution for low poly geometry. You would need barycentrics, a higher resolution mesh, or some kind of texture based solution (base normal maps or just edge highlights to fake barycentrics) to gain back some of the smooth transitions around corners while still avoiding the inherent issue of interpolated normals on low poly models causing the stretching originally seen.

My article is more on using tangent space normal maps than solving problems with the base mesh normals.