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

[Unity 5] Custom light model and self shadows

Discussion in 'Shaders' started by Phantomx, Mar 10, 2015.

  1. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    Hey,
    I'm trying to create a BRDF in unity 5, but I running into issues.
    So in unity 4 I used that lighting model:
    Code (CSharp):
    1.  
    2.         half4 LightingLambertBRDF (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten) {
    3.  
    4.             half NdotL = max (0, dot (s.Normal, normalize(lightDir) ));
    5.             half NdotV = max (0, dot (s.Normal, normalize(viewDir) ));
    6.            
    7.             fixed3 BRDF = tex2D(_BRDF, float2(NdotL,NdotV) );
    8.  
    9.             half4 c;
    10.             c.rgb = (s.Albedo * _LightColor0.rgb * BRDF) * atten;
    11.             c.a = s.Alpha;
    12.             return c;
    13.         }
    14.  
    it still works in unity 5 but self shadows are crazy jaggy.




    So I tried using the new way they do it in Lighting.cginc
    here's the code:
    Code (CSharp):
    1. inline fixed4 UnityLambertBRDFLight (SurfaceOutput s, half3 viewDir, UnityLight light)
    2.         {
    3.             fixed NdotL = max (0, dot (s.Normal, light.dir));
    4.             fixed NdotV = max (0, dot (s.Normal, viewDir));
    5.            
    6.             fixed3 BRDF = tex2D(_BRDF,float2(NdotL,NdotV));
    7.            
    8.             fixed4 c;
    9.             c.rgb = s.Albedo * light.color * BRDF;
    10.             c.a = s.Alpha;
    11.             return c;
    12.         }
    13.  
    14.         inline fixed4 LightingLambertBRDF(SurfaceOutput s, half3 viewDir,UnityGI gi)
    15.         {
    16.             fixed4 c;
    17.             c = UnityLambertBRDFLight (s, viewDir, gi.light);
    18.  
    19.             #if defined(DIRLIGHTMAP_SEPARATE)
    20.                 #ifdef LIGHTMAP_ON
    21.                     c += UnityLambertBRDFLight (s,viewDir, gi.light2);
    22.                 #endif
    23.                 #ifdef DYNAMICLIGHTMAP_ON
    24.                     c += UnityLambertBRDFLight (s,viewDir, gi.light3);
    25.                 #endif
    26.             #endif
    27.  
    28.             #ifdef UNITY_LIGHT_FUNCTION_APPLY_INDIRECT
    29.                 c.rgb += s.Albedo * gi.indirect.diffuse;
    30.             #endif
    31.  
    32.             return c;
    33.         }
    34.  
    35.         inline void LightingLambertBRDF_GI (
    36.             SurfaceOutput s,
    37.             UnityGIInput data,
    38.             inout UnityGI gi)
    39.         {
    40.             gi = UnityGlobalIllumination (data, 1.0, 0.0, s.Normal, false);
    41.         }

    But I still get the same result with self shadows. Shadows coming from other objects are all good.

    Thanks!
     
  2. e_Glyde

    e_Glyde

    Joined:
    Apr 20, 2013
    Posts:
    42
    Can you post a screenshot of this in Unity 4?
     
  3. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    Humm good point, it does look almost the same in unity 4. After all it's just unity's shadows that are crappy after all but you can't notice it in the default lighting model because the shadows are the same color as the "Ramp".
     
  4. e_Glyde

    e_Glyde

    Joined:
    Apr 20, 2013
    Posts:
    42
    I've faced similar problems as well. But why are the shadows that nasty brown colour? Is it because you don't use ambient light(the sky)?
     
  5. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    I am using the ambient, I guess that's just the mix between the purple and the ambient that gives this result.
     
  6. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    Have you tried to lower the strength and increase the resolution of the soft shadows ?
     
  7. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    yes, it's already at very high and I lowered the camera range and shadow distance to 10m, that's pretty much the best quality you can get I guess.
     
  8. e_Glyde

    e_Glyde

    Joined:
    Apr 20, 2013
    Posts:
    42
    Can I see your brdf texture?
     
  9. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    Sure, It's only for testing but it looks like that: