Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Normal on modified Standard shader not working

Discussion in 'Shaders' started by Daybreaker32, Sep 21, 2016.

  1. Daybreaker32

    Daybreaker32

    Joined:
    Jun 11, 2014
    Posts:
    73
    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?
     

    Attached Files:

  2. Daybreaker32

    Daybreaker32

    Joined:
    Jun 11, 2014
    Posts:
    73
  3. tsangwailam

    tsangwailam

    Joined:
    Jul 30, 2013
    Posts:
    280
    Seen you didn't enable the _NORMALMAP keyword. CustomEditor "StandardShaderGUI" do the job. You need to create your custom editor.
     
    Daybreaker32 likes this.
  4. Daybreaker32

    Daybreaker32

    Joined:
    Jun 11, 2014
    Posts:
    73
    Oh I see.

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

    Code (CSharp):
    1.  
    2. // Test_UnityStandardCore.cginc
    3. half3 PerPixelWorldNormal(float4 i_tex, half4 tangentToWorld[3]) {
    4.     half3 tangent = tangentToWorld[0].xyz;
    5.     half3 binormal = tangentToWorld[1].xyz;
    6.     half3 normal = tangentToWorld[2].xyz;
    7.  
    8.     half3 normalTangent = NormalInTangentSpace(i_tex);
    9.     half3 normalWorld = NormalizePerPixelNormal(tangent * normalTangent.x + binormal * normalTangent.y + normal * normalTangent.z);
    10.  
    11.     return normalWorld;  
    12. }
    13.  
    Code (CSharp):
    1.  
    2. // Test_UnityStandardInput.cginc
    3. //#ifdef _NORMALMAP
    4. half3 NormalInTangentSpace(float4 texcoords) {
    5.     float4 mask = tex2D(_MainTex, texcoords.xy);
    6.     half3 normalTangent = UnpackScaleNormal(tex2D(_BumpMap, texcoords.xy), _BumpScale) * mask.r +
    7.             UnpackScaleNormal(tex2D(_Normal2, texcoords.xy), _BumpScale) * (1 - mask.r);
    8.  
    9.     return normalTangent;
    10. }
    11. //#endif
    12.  
    13.  
    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.

     
  5. tsangwailam

    tsangwailam

    Joined:
    Jul 30, 2013
    Posts:
    280
    You should multiple mask.r with _BumpScale and not the unpacked normal.
     
  6. Daybreaker32

    Daybreaker32

    Joined:
    Jun 11, 2014
    Posts:
    73
    Hmm, it doesn't work. Weirdly it's the same when I use only the first normal.

    Code (CSharp):
    1. normalTangent = UnpackScaleNormal(tex2D(_BumpMap, texcoords.xy), _BumpScale);
     
  7. tsangwailam

    tsangwailam

    Joined:
    Jul 30, 2013
    Posts:
    280
    I try. It seem working.

    Code (CSharp):
    1.     float4 mask = tex2D(_MainTex, texcoords.xy);
    2.     half3 normalTangent = UnpackScaleNormal(tex2D(_BumpMap, texcoords.xy), _BumpScale * mask.r)+
    3.                             UnpackScaleNormal(tex2D(_Normal2, texcoords.xy), _BumpScale * (1 - mask.r));
    4.  
     

    Attached Files:

  8. Daybreaker32

    Daybreaker32

    Joined:
    Jun 11, 2014
    Posts:
    73
    :eek:

    Was my PerPixelWorldNormal wrong? What did I miss?
     
  9. tsangwailam

    tsangwailam

    Joined:
    Jul 30, 2013
    Posts:
    280
    Here the shader file.
     

    Attached Files:

  10. Daybreaker32

    Daybreaker32

    Joined:
    Jun 11, 2014
    Posts:
    73
    Ow man it (magically) works by creating the editor!

    Thank you @tsangwailam
     
    tsangwailam likes this.