Normal on modified Standard shader not working

Hello,

I’ve modified Standard shader so that it can render two textures combined by a mask. Its albedo is all fine but the normal won’t come out.

  1. Unity’s standard shader with normal
  2. Unity’s standard shader and my shader without normal
  3. Unity’s standard shader and my shader with normal

I’ve attached a unitypackage with scene, shaders, and cgincs. I suspect the “#ifdef _NORMALMAP” at “UnityStandardInput.cginc” is false or something. Why is that? How can I modify my shader to make the normal appears?

2795593–202762–StandardTwoTextures.unitypackage (346 KB)

Bump :frowning:

Seen you didn’t enable the _NORMALMAP keyword. CustomEditor “StandardShaderGUI” do the job. You need to create your custom editor.

1 Like

Oh I see.

So I skipped creating custom editor and edited the cginc directly to enable the normals by modifying these files:

// Test_UnityStandardCore.cginc
half3 PerPixelWorldNormal(float4 i_tex, half4 tangentToWorld[3]) {
    half3 tangent = tangentToWorld[0].xyz;
    half3 binormal = tangentToWorld[1].xyz;
    half3 normal = tangentToWorld[2].xyz;

    half3 normalTangent = NormalInTangentSpace(i_tex);
    half3 normalWorld = NormalizePerPixelNormal(tangent * normalTangent.x + binormal * normalTangent.y + normal * normalTangent.z); 

    return normalWorld;   
}
// Test_UnityStandardInput.cginc
//#ifdef _NORMALMAP
half3 NormalInTangentSpace(float4 texcoords) {
    float4 mask = tex2D(_MainTex, texcoords.xy);
    half3 normalTangent = UnpackScaleNormal(tex2D(_BumpMap, texcoords.xy), _BumpScale) * mask.r + 
            UnpackScaleNormal(tex2D(_Normal2, texcoords.xy), _BumpScale) * (1 - mask.r);

    return normalTangent;
}
//#endif

But still the normals won’t come out at 0 to 1 normal value (_BumpScale). If I fill the normal value with values over 1, some black artifact appears.

You should multiple mask.r with _BumpScale and not the unpacked normal.

Hmm, it doesn’t work. Weirdly it’s the same when I use only the first normal.

normalTangent = UnpackScaleNormal(tex2D(_BumpMap, texcoords.xy), _BumpScale);

I try. It seem working.

    float4 mask = tex2D(_MainTex, texcoords.xy);
    half3 normalTangent = UnpackScaleNormal(tex2D(_BumpMap, texcoords.xy), _BumpScale * mask.r)+
                            UnpackScaleNormal(tex2D(_Normal2, texcoords.xy), _BumpScale * (1 - mask.r));

:hushed:

Was my PerPixelWorldNormal wrong? What did I miss?

Here the shader file.

2798237–203039–Test_UnityStandardInput.cginc.zip (1.71 KB)

Ow man it (magically) works by creating the editor!

Thank you @tsangwailam

1 Like