Search Unity

Is surface shader lighting bugged for sprite backsides? Normals are correctly reversed

Discussion in '2D' started by Cerzi, Jul 6, 2018.

  1. Cerzi

    Cerzi

    Joined:
    Dec 28, 2014
    Posts:
    12
    edit: had a brainwrong on this one, this surface shader i was stripping down wasn't reversing the normalmaps in the first place.

    I'll hijack my own thread to ask, then: Is there a simple way to do reversed normals in a surface shader?


    Using localscale.x = -1, I've flipped a sprite with a simple surface shader material, and directly beneath a spotlight there appears to be an issue (reversed sprite is on the left):


    To look into this, I set the o.Emission of the surface shader to equal the normal, so that we can clearly see the normalmap and ensure it's behaving correctly:


    Does anyone know what might be going on here? Both sprites should be lit from the top, but the reversed sprite is being lit as though its y normal was reversed.

    Here's the code of the shader, simply using a fresh surface shader with added bumpmap property which unpacks for the normals.

    Code (CSharp):
    1. Shader "Custom/normaltest" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _BumpMap("Normal map", 2d) = "white" {}
    6.     }
    7.     SubShader {
    8.         Tags { "RenderType"="Opaque" "Queue" = "Transparent" }
    9.         LOD 200
    10.         Cull Off
    11.         ZWrite Off
    12.         Blend SrcAlpha OneMinusSrcAlpha
    13.         ZTest Off
    14.  
    15.         CGPROGRAM
    16.         // Physically based Standard lighting model, and enable shadows on all light types
    17.         #pragma surface surf Standard fullforwardshadows
    18.  
    19.         // Use shader model 3.0 target, to get nicer looking lighting
    20.         #pragma target 3.0
    21.  
    22.         sampler2D _MainTex;
    23.         sampler2D _BumpMap;
    24.  
    25.         struct Input {
    26.             float2 uv_MainTex;
    27.         };
    28.  
    29.         half _Glossiness;
    30.         half _Metallic;
    31.         fixed4 _Color;
    32.  
    33.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    34.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    35.         // #pragma instancing_options assumeuniformscaling
    36.         UNITY_INSTANCING_BUFFER_START(Props)
    37.             // put more per-instance properties here
    38.         UNITY_INSTANCING_BUFFER_END(Props)
    39.  
    40.         void surf (Input IN, inout SurfaceOutputStandard o) {
    41.             // Albedo comes from a texture tinted by color
    42.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    43.             o.Albedo = c.rgb;
    44.             float4 BumpMap = tex2D(_BumpMap, IN.uv_MainTex);
    45.             float3 n = UnpackNormal(BumpMap);
    46.             o.Normal = n;
    47.             o.Emission = n;
    48.             o.Alpha = c.a;
    49.             clip(o.Alpha - 0.05);
    50.         }
    51.         ENDCG
    52.     }
    53.     FallBack "Diffuse"
    54. }
    55.  
    This might be driving me slightly mad, anyone have any ideas?
     
    Last edited: Jul 6, 2018