Search Unity

Deferred lightning impossible on Orthographic Camera?

Discussion in 'Editor & General Support' started by Sirithang, Jan 3, 2012.

  1. Sirithang

    Sirithang

    Joined:
    Nov 29, 2010
    Posts:
    28
    I just stumble upon an interesting fact that seems to be noted nowhere in the documentation.

    I have a point light inside a Lantern that cast shadow on the ground. When I switch the camera (or the scene preview) to orthographic, the shadow disapear!

    When I go check the setting of my light, there is a little warning that have appear on my light that say that in forward rendering, only directional light can cast shadow. But I just change the projection, everything is still set on deferred!

    Is this an intended behaviour or a bug? Because orthographic is just a different projection amtrix, so it shouldn't mess up neither with shadow map nor deferred rendering, no?
     
  2. garyhaus

    garyhaus

    Joined:
    Dec 16, 2006
    Posts:
    601
    hmmm.. check the rendering path on your orthographic camera... Is it set to Deferred?
     
    yanuu likes this.
  3. Sirithang

    Sirithang

    Joined:
    Nov 29, 2010
    Posts:
    28
    Yes of course, I've checked in my Player Setting and ON my camera, each are set to Deferred.

    And I've the shadow drawn when in perspective (and only deferred have shadow for point light) and when I switch to orthographic the Shadow disappear. Both in game view (so throught a camera set on ortho) and on the Scene Preview (so by right clicking on the little cube and unchecking "Perspective").

    So it would seems its a limitation : orthographic camera cannot be on a deferred path, but I find this strange since it's just a switch of projection matrix...
     
  4. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    Note to anyone having the same problem I did: Directional Lights should work in Orthographic. If they're not, you probably baked a lightmap that you need to clear.
     
    Last edited: Dec 6, 2013
  5. garyhaus

    garyhaus

    Joined:
    Dec 16, 2006
    Posts:
    601
    Yeah I tested it with a scene here... Seems to work just fine ...
     
  6. profanicus

    profanicus

    Joined:
    Nov 23, 2009
    Posts:
    295
    I've run into the exact same issue in 3.5 beta, did you ever find out what was wrong Sirithang?

    Toggling the camera between Perspective and Orthographic disables shadows on points lights and gives a "only directional lights can have shadows in forward rendering" warning. This happens in a fresh project, with an empty scene except for a camera and a point light.
     
  7. Sirithang

    Sirithang

    Joined:
    Nov 29, 2010
    Posts:
    28
    I've checked with the 3.5 beta and same issus as with 3.4 and 3.4.2f*...

    It work only with directional light, they give shadow, but point/spot don't give shadow with an ortho camera. Would it be an engine restriction? as shader can't transform from a perspective shadowmap to an ortho camera point of view? Why does it work with directional light then? Because the shadow map being already in orthographic the conversion is possible?

    Would lvoe to hear an official response on that, is there a mean to submit this to the Unity team, or should I send a mail?
     
  8. profanicus

    profanicus

    Joined:
    Nov 23, 2009
    Posts:
    295
    I have seen a few threads mentioning this now, but never an official response confirming whether or not it is working as intended.. I submitted a bug about it anyway, can't hurt. :)
     
  9. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    It was confirmed that realtime shadows don't work on orthographic cameras by Aras back on 3.0 or 3.1 already and was also mentioned to be by design.
     
  10. profanicus

    profanicus

    Joined:
    Nov 23, 2009
    Posts:
    295
    Ah ok, thanks for clearing that up.
     
  11. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,242
    Ouch, this is so bad. Anybody found a solution to this bug (because this is a bug, right?)?
     
  12. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,242
    Ok, found the fullforwardshadows options for Shaders :)
     
  13. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Yes, it's an unfortunate performance choice we had to do - deferred does not work with orthographic cameras. The reason was that we did not want to slow down perspective+deferred case just to make it possible to use orthographic projection.
     
  14. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,242
    Thanks for the explanation Aras :)
     
  15. bome99

    bome99

    Joined:
    Sep 14, 2009
    Posts:
    119
    OLD "Is there a work around?"

    EDIT ahh sorry found this it works :D

    Shader "0Diffuse1" {
    Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Base (RGB)", 2D) = "white" {}
    }
    SubShader {
    Tags { "RenderType"="Opaque" }
    LOD 200
    CGPROGRAM
    #pragma surface surf Lambert fullforwardshadows
    sampler2D _MainTex;
    fixed4 _Color;
    struct Input {float2 uv_MainTex;};
    void surf (Input IN, inout SurfaceOutput o) {
    fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    o.Albedo = c.rgb;
    o.Alpha = c.a;
    }
    ENDCG
    }
    Fallback "VertexLit"
    }
     
    Last edited: Jun 4, 2012
  16. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    a perspective one with a very small view angle. Its not perfectly orthogonal that way but depending on your explicit need might be enough.

    If it isn't, then you are out of luck unluckily
     
  17. Pete-Gartside

    Pete-Gartside

    Joined:
    Nov 28, 2012
    Posts:
    5
    I needed the data from the deferred render pipeline but wanted an orthographic view so i putting an orthographic projection matrix in to a perspective camera projection matrix to trick the pipeline and it works :)
     
  18. Satoris

    Satoris

    Joined:
    Mar 18, 2013
    Posts:
    2

    Hey Pete I'm looking for the same thing, would you mind sharing the script you made?
     
  19. GFX47

    GFX47

    Joined:
    Dec 7, 2010
    Posts:
    124
    I'm also very interested by this solution!
    I'm trying to have both dynamic shadows and pixel perfect rendering.
     
  20. GFX47

    GFX47

    Joined:
    Dec 7, 2010
    Posts:
    124

    Does it also applies to a perspective camera using an orthographic projection matrix?

    Here is an test project where I use a little script to apply an orthographic projection matrix to a perspective camera with deferred lighting enabled.
    The scene contains a point light that should produce dynamic shadows.

    https://dl.dropboxusercontent.com/u/24322340/Unity3D/orthoprojmatrix_vs_deferredlighting.unitypackage

    Here is the script I use:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class OrthoProjMatrix : MonoBehaviour
    6. {
    7.     [SerializeField] float orthographicSize = 10f;
    8.  
    9.     void Update()
    10.     {
    11.         float aspect = (float) Screen.width / (float) Screen.height;
    12.         camera.projectionMatrix = Matrix4x4.Ortho(-orthographicSize * aspect, orthographicSize * aspect, -orthographicSize, orthographicSize, camera.nearClipPlane, camera.farClipPlane);
    13.     }
    14. }
    15.  
    The lighting and shadows both appear perfectly when the script is not running


    :cool:

    but don't when it is.


    :(

    But here is what is even more wierd: when I increase the near clipping plane or move the camera forward, a halo appears when near the point light


    :confused:

    and shadows when near the background plane.


    :eek:

    Does it make any sense to you guys?
    Reminder: I'm trying to get both dynamic shadows and pixel perfect rendering.
     
  21. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    I would think so. Projected Z buffer distance is just very different between orthographic and perspective projections. The deferred lighting shaders assume it's perspective, and then it all (light attenuation, shadows) go boom if it's not.
     
  22. GFX47

    GFX47

    Joined:
    Dec 7, 2010
    Posts:
    124
    OK, thanks for your answer.
    Do you think that it would be achievable by modifying the shaders? (is it possible anyway?)
     
  23. TiberiuMunteanu

    TiberiuMunteanu

    Joined:
    May 9, 2015
    Posts:
    18
    What if you didn't slow dow the perspective scenario, but made a separate shader that assumes an ortographic matrix which we could set in place manually or smth? Can you point me in the right direction on how to achieve this?
    Thank you!