#Implementing normal map in custom lighting shader **(NOT SHADERGRAPH)**

1 messages · Page 1 of 1 (latest)

solar fulcrum
#

Trying to implement normal map in my custom lighting pipeline I use for my game's shaders. I'm NOT using shader graph (for multiple reasons that are completely irrelevant to this post) so keep that in mind.

I'm using triplanar mapping for this specific shader, and I use this to triplanar map the normal map:

  float3 wall_normal = triplanar_normal(IN.positionWS,IN.normalWS,_BumpMap,1.0,_RockTexture_ST.xy, IN);

triplanar_normal is just a reimplementation of the shader graph node using the code from the documentation https://docs.unity3d.com/Packages/com.unity.shadergraph@6.9/manual/Triplanar-Node.html

float3 triplanar_normal(float3 position, float3 normal, sampler2D t, float blend, float2 tile, Varyings IN)
{
    float3 node_uv = position * float3(tile.xxx);
    float3 node_blend = max(pow(abs(normal), blend), 0);
    float3 node_x = UnpackNormalmapRGorAG(tex2D(t, node_uv.zy));
    float3 node_y = UnpackNormalmapRGorAG(tex2D(t, node_uv.xz));
    float3 node_z = UnpackNormalmapRGorAG(tex2D(t, node_uv.xy));
    node_x = float3(node_x.xy + normal.zy, abs(node_x.z) * normal.x);
    node_y = float3(node_y.xy + normal.xz, abs(node_y.z) * normal.y);
    node_z = float3(node_z.xy + normal.xy, abs(node_z.z) * normal.z);
    float4 Out = float4(normalize(node_x.zyx * node_blend.x + node_y.xzy * node_blend.y + node_z.xyz * node_blend.z), 1);
    float3x3 node_transform = float3x3(IN.tangentWS, IN.bitangentWS, IN.normalWS);
    Out.rgb = TransformWorldToTangent(Out.rgb, node_transform);
    return Out;
}

In my vertex shader, I calulate the world space tangent, bitangent, and normal like this:

half3 wNormal = normal_inputs.normalWS;
half3 wTangent = normal_inputs.tangentWS;
half3 wBitangent = normal_inputs.bitangentWS;
OUT.normalWS = wNormal;
OUT.tangentWS = wTangent;
OUT.bitangentWS = wBitangent;

I then mix the normals like this:

lerp(IN.normalWS, wall_normal, mask)

The results from this look incorrect (this is the result from a simple NDotL calculation w/ the scene's directional light) and I'm not entirely sure why.

stiff idol
#

The first thing that I'd doubt is that lerp you have. Maybe try using the normal as is for starters and see if it solves the issue.

#

Also, it's not quite clear at what/where from/at what angle we're looking in the screenshot. A screenshot with more context or a video would be helpful.

frail laurel
#

The wall_normal is in tangent space, so it can’t be mixed with IN.normalWS.

stiff idol
stiff idol
#

My bad

meager knot
#

To clarify, ShaderGraph only includes that world->tangent conversion because the default output space is Tangent so it's easy to connect the usual normal map samples straight to the master stack. Behind the scenes it converts back to world for shading calcs.

The Triplanar node & Graph Settings actually allow you to switch the output space to avoid those unnecessary conversions if tangent space isn't required. Though the snippets in the docs don't show this.