Search Unity

normal looks correct in direct light but reversed in the shadow on a terrain in 2018.4

Discussion in 'Shaders' started by laurentlavigne, Aug 6, 2020.

  1. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,327
    upload_2020-8-5_16-58-38.png
    you can see that in the realtime shadow the bumps look like dimples.
    How can I fix that?
    Note: it's on a terrain

    Code (CSharp):
    1. Shader "Ground Shader With Bliss - No Vertex Color" {
    2.  
    3.     Properties
    4.     {
    5.         _Color ("Main Color", Color) = (1,1,1,1)
    6.         _MainTex ("Base (RGB)", 2D) = "white" {}
    7.         _BlissTexVeryBad ("Very Bad Bliss", 2D) = "red" {}
    8.         _BlissTexNeutral ("Neutral Bliss", 2D) = "white" {}
    9.         _BlissTexVeryGood ("Very Good Bliss", 2D) = "green" {}
    10.         _OutlineColor ("Outline Color", Color) = (0,0,0,1)
    11.         _Outline ("Outline width", float) = .005
    12.         _BlissBadNrm ("Bad Bliss Normal", 2D) = "red" {}
    13.         _BlissNeutralNrm ("Neutral Bliss Normal", 2D) = "white" {}
    14.         _BlissGoodNrm ("Good Bliss Normal", 2D) = "green" {}
    15.  
    16.     }
    17.    
    18.      // for desktop ------------
    19.     SubShader
    20.     {
    21.         Tags { "RenderType"="Opaque" }
    22.         LOD 900
    23.        
    24.         CGPROGRAM  
    25.         #pragma surface surf Lambert halfasview vertex:vert addshadow fullforwardshadows
    26.         #pragma target 3.0
    27.  
    28.         struct Input
    29.         {
    30.             half2 uv_BlissTexVeryBad;
    31.             float3 worldPos;
    32.         };
    33.        
    34.         void vert (inout appdata_full v, out Input o) {
    35.             UNITY_INITIALIZE_OUTPUT(Input,o);
    36.           }  
    37.      
    38.           sampler2D _BlissMap;
    39.         sampler2D _BlissTexNeutral;
    40.         sampler2D _BlissTexVeryBad;
    41.         sampler2D _BlissTexVeryGood;
    42.         fixed _BlissMapSize;
    43.            sampler2D _BlissBadNrm, _BlissNeutralNrm, _BlissGoodNrm ;
    44.              
    45.         float3 blend_udn(float4 n1, float4 n2)
    46.         {
    47.             float3 c = float3(2, 1, 0);
    48.             float3 r;
    49.             r = n2*c.yyz + n1.xyz;
    50.             r =  r*c.xxx -  c.xxy;
    51.             return normalize(r);
    52.         }
    53.              
    54.         void surf (Input IN, inout SurfaceOutput o)
    55.         {  
    56.             // bliss
    57.             fixed2 w = half2(_BlissMapSize*.5 + IN.worldPos.x, _BlissMapSize*.5 + IN.worldPos.z);
    58.             fixed2 pos = half2(w[0]/_BlissMapSize, w[1]/_BlissMapSize);          
    59.             // take bliss value, add texture based on it
    60.             fixed bliss = tex2D(_BlissMap,pos).a;
    61.             half4 c1 = tex2D(_BlissTexVeryBad, IN.uv_BlissTexVeryBad);              
    62.             fixed a1 = max(1 - bliss*2, 0);
    63.             // NEGATIVE BLISS EFFECT
    64.             //          comment both for just blended     - 1    
    65.             fixed threshold = 1-c1.a;
    66.             a1 = max(step(threshold, a1), min(a1/threshold, 1)); // threshold, blended mk 2- 4
    67.             half4 c = c1*a1;                      
    68.             c1 = tex2D(_BlissTexVeryGood, IN.uv_BlissTexVeryBad);          
    69.             fixed a5 = max(bliss*2-1,0);
    70.             // POSITIVE BLISS EFFECT
    71.             //          comment both for just blended  - 1        
    72.             threshold = 1-c1.a;
    73.             a5 = max(step(threshold, a5), min(a5/threshold, 1)); // threshold, blended mk 2- 4
    74.             c += c1*a5;
    75.             c1=tex2D(_BlissTexNeutral, IN.uv_BlissTexVeryBad);
    76.             fixed a3 = max(1-(a1+a5),0);          
    77.             c += c1*a3;
    78.             //total
    79.             o.Albedo = c.rgb*1;
    80.             o.Emission += c.a *c.rgb;
    81.             half4 cn;
    82.             //cn = tex2D (_BlissBadNrm, IN.uv_BlissTexVeryBad) * a1;
    83.             //cn += tex2D (_BlissGoodNrm, IN.uv_BlissTexVeryBad) * a5;
    84.             //cn += tex2D (_BlissNeutralNrm, IN.uv_BlissTexVeryBad) * a3;
    85.             cn = tex2D (_BlissGoodNrm, IN.uv_BlissTexVeryBad);
    86.             o.Normal = UnpackNormal (cn);
    87.         }
    88.    
    89.         ENDCG
    90.           UsePass "Toon/Basic Outline/OUTLINE"
    91.     }
     
  2. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,327
    this inversion only happens if the terrain is baked, if i clear it it will suddenly work, this inversion also happens if i set the progressive baker to directional