Custom Lighting model with real-time shadows?

Hi there!

I have been searching the forums for ages, and even though there have been people with similar issues, I haven’t been able to achieve the particular effect I am looking for.

The thing that I want to create, is a point light that has no falloff and no “NdotL”. And by “no NdotL” I mean even if the light hits a surface from an angle, the surface should be equally lit. Unless the angle between the surface normal and the light direction is over 90°, in which case I want the surface to turn completely unlit. By tweaking the custom lighting model example for surface shaders in the Unity Documentation, I have actually been able to come up with pretty much the desired effect:

 Shader "surfaceNoFalloff" {
        Properties {
            _MainTex ("Texture", 2D) = "white" {}
        }
        SubShader {
        Tags { "RenderType" = "Opaque"}
        CGPROGRAM
          #pragma surface surf SimpleLambert
            
          half4 LightingSimpleLambert_PrePass (SurfaceOutput s, half3 lightDir, half atten){

           half NdotL = dot (s.Normal, lightDir);
                  NdotL = 100*max(0.0,NdotL);
                  NdotL = saturate(NdotL);
                  half4 c;
                  c.rgb = s.Albedo * _LightColor0.rgb * (NdotL* 1);
                  c.a = s.Alpha;

           return c;
         }
        struct Input {
            float2 uv_MainTex;
        };
       
        sampler2D _MainTex;
       
        void surf (Input IN, inout SurfaceOutput o) {
            o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
        }
        ENDCG
        }
        Fallback "VertexLit"
    }

The problem is though, that I can’t get realtime-shadows working with my point light, as soon as I use my custom lighting model. I think it might have something to do with my lighting model being in the forward rendering path by default, but I am not sure? Plus, I don’t know how or if it is even possible to write such a specific lighting model in the deffered lighting pass.

Do any of you know how to add real time shadows to my script, or know any other way by which I would be able to achieve the desired effect? Any help would be much appreciated :slight_smile:

Tough one! You do realize that both the normal point-light falloff and the shadowing are included together in the one atten variable passed to your foo_PrePass function?

If yes, you’ll realize that you’ll have to “dig deeper”. As in, “roll your own point lights” and replace a vast portion of the existing Unity-provided shader code running outside your little custom lighting function.

If not, well now you are :wink:

Since you’re in Deferred, there MAY be a way with some hacky #defines located at the right spot to prevent the “existing Unity-provided shader code running outside your little custom lighting function” from sampling the screen-space shadow map. This means atten only contains falloff (and perhaps lightmap/whatever provided shadowing).

Then you could ignore atten and hence falloff and manually sample into the screen-space shadow map (simply with the fullscreen-quad UVs if you can figure out how to get them) to still realize real-time shadows.

So if I faced the problem you’re facing right now, this would be the avenue I’d first explore.

But since I don’t, I’m not going to code all that :smile:

This may not even be possible at all in standard Deferred… it should be doable in my custom-coded Deferred setup where lighting is a post process (like Unity 5 does, I hear), not a geometry pre-pass (like Unity 4 does), but that doesn’t help you at all right now I guess.

There may be some blindingly obvious solution I’m totally overlooking though, so let’s see what others here have to say…