Friday, 8 June 2012

How to get shadows using ortographic camera


I was looking for a way to get shadows in my game. I want my hero to cast some kinda shadow from his light source.

I'm using an orthographic camera and the light I want to cast a shadow from is a point-light so it should be as easy as turning “Defferend lighting” on in rendering under build settings and violia.
But no, that didn't work, searching the net I found this post
http://forum.unity3d.com/threads/117884-Deferred-lightning-impossible-on-Orthographic-Camera

OK so we can't use “Defferend lighting” on an orthographic camera, but I found work around.

We can use FullForwardShadows.
http://answers.unity3d.com/questions/253531/fullforwardshadows-if-lights-more-than-1.html

My background uses bump to give it more life, so I rewrote the shader to also have bump.

Code:

Shader "BoShadowShader" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
}
SubShader {
    Tags { "RenderType"="Opaque" }
    LOD 400
    
    CGPROGRAM
    #pragma surface surf BlinnPhong fullforwardshadows
    sampler2D _MainTex;
    sampler2D _BumpMap;
fixed4 _Color;


    struct Input {float2 uv_MainTex; float2 uv_BumpMap;};
    
    void surf (Input IN, inout SurfaceOutput o) {
       fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
       o.Albedo = tex.rgb * _Color.rgb;
       o.Gloss = tex.a;
       o.Alpha = tex.a * _Color.a;
  o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
 
    }
    ENDCG
}
Fallback "VertexLit"
}




So now I have two lights on my hero, one that lights up the foreground, the blocks you walk on, and a light behind him that lights up the background and casts shadow.


I get this error.

But it doesn't seam to affect performance, please comment if you find a solution.

Thanks to valera on UnityAnswers for the shader and Aras on the Unity3d forum for clearing up the whole “Defferend ligting” issue.


I strongly encourage you to make “let's play”s of the game, there is no better way to show it of :D

I made one myself.
here some early ones.

And even one in Russian.

Have nice weekend!
Cheers
Bo

6 comments:

  1. Nice post!
    Didn't find a lot of articles about this subject. Might try it myself :)
    Thanks!

    ReplyDelete
  2. How/where exactly do you apply the shader? I also need to cast shadows using a point light with an orthographic camera and I think this could be what I need to get it working. Any help or clarification would be greatly appreciated!

    ReplyDelete
  3. hi Chad

    As far as I remember I use it on the background, the plane that needs to draw the shadow.

    The light is attached to the player.

    ReplyDelete
  4. Thanks a lot man, I'll give it shot.

    ReplyDelete
  5. Works pretty good. Thanks alot.

    ReplyDelete