Search Unity

Unity 5 Shadows Do Not Render in Game View with Custom Projection Matrix

Discussion in 'Editor & General Support' started by Ben-BearFish, Mar 4, 2015.

  1. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    Has anyone had the issue with Unity 5 shadows not rendering when upgrading from Unity 4 to Unity 5 when using a custom projection matrix as mentioned here:

    http://docs.unity3d.com/ScriptReference/Camera-projectionMatrix.html

    In Unity 4, as long as you had the rendering in Forward rendering and had certain quality settings the shadows would show up, but now upgrading to Unity 5, the shadows only show in the editor scene view, but not the game view with the same settings. Is there additional setup or something I need to change?
     
    XCO likes this.
  2. Alabatross

    Alabatross

    Joined:
    Dec 15, 2012
    Posts:
    223
    Do they work in the editor?

    Our shadows have stopped working in builds since upgrading to 5 and switching to deferred, but they run fine in the editor. But I don't believe we use a custom matrix
     
    Last edited: Mar 10, 2015
  3. AhrenM

    AhrenM

    Joined:
    Aug 30, 2014
    Posts:
    74
    I've yet to see shadows on terrain with the default shader. :(
     
  4. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    @Alabatross , the shadows appear in the Editor's scene view, but not in the Editor's Game View when not in play mode. In play mode, nothing shows up as well in game view. This is in forward lighting mode.

    In Unity 4 they still kind of worked, but in 5 they don't appear at all.

    Is there a new or better way to access shadows, or make our own shadows with shaders in Unity 5? Unity 4 made it kind of a pain.
     
  5. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    @KEngelstoft , could you ask someone on the team if this is a bug, or if this is an intended functionality in Unity 5, and there are specific steps we need to take to get shadows to work correctly with custom projection mtrices, as it does in Unity 4? I was going to file a bug report, but I didn't want to do that unless somebody could confirm this is a bug or not. Thank you.
     
    Alkann likes this.
  6. VoxelLoop-Curtis

    VoxelLoop-Curtis

    Joined:
    Dec 17, 2012
    Posts:
    5
    Just to make sure I'm also having this same issue,
    I see the amazing new lighting just fine in the editor, but there are no shadows in the build. (Aside from some shadows on trees that appear to clip through the terrain)
    Is that the same issue as yours?

    (Image below shows my issue, editor on the left, build on the right)
     
  7. Alabatross

    Alabatross

    Joined:
    Dec 15, 2012
    Posts:
    223
    Voxel, are you on U5? We're having the same issue
     
  8. VoxelLoop-Curtis

    VoxelLoop-Curtis

    Joined:
    Dec 17, 2012
    Posts:
    5
    Yes, I am.
    Glad to know I'm not the only one having this issue.

    I thought it might be related to the DX3D version, but forcing DX3D9 or DX3D11 makes no difference sadly.
     
  9. Alabatross

    Alabatross

    Joined:
    Dec 15, 2012
    Posts:
    223
    Good to know I'm not alone either, our shadows do exactly what yours do and even show through other objects
     
  10. VoxelLoop-Curtis

    VoxelLoop-Curtis

    Joined:
    Dec 17, 2012
    Posts:
    5
    It's odd, it seems like the terrain isn't receiving shadows.
    The trees in the build have the correct shadows, and project shadows correctly onto other trees.

    I guess it's possibly a Unity bug. What OS are you running? Not that it should make any difference but I'm on Windows 8.1 Pro, exporting my builds for Windows. (Both x86 and x64)
     
  11. Alkann

    Alkann

    Joined:
    Dec 17, 2014
    Posts:
    12
    I also have this problem on a custom projection matrix, I don't have shadows on Unity 5 (had them fine on Unity 4) but if I increase enough the near clip plane they appear but not correctly, they go through objects emmiting the shadows and the shadows direction changes when I rotate the camera (also in my case I recompute the projection matrix according to the camera rotation).
     
  12. Alabatross

    Alabatross

    Joined:
    Dec 15, 2012
    Posts:
    223
    I don't think it's the terrain specifically that's broken, our shadows are glitched and showing through buildings in a very similar way

    I'm on Win 7
     
  13. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    So, no updates from anyone at Unity?

    @Aras , do you think you can take a look at this issue in Unity 5, or perhaps ask someone about the problem?
     
    Last edited: Mar 16, 2015
  14. KEngelstoft

    KEngelstoft

    Unity Technologies

    Joined:
    Aug 13, 2013
    Posts:
    1,366
    You are on to something Alkann, try setting near and far used during shadow map rendering to the same value as the camera's near and far.
     
  15. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    Might I ask how to access the near and far during shadow map rendering? Do we need to do this through a shader? I'm kind of unsure how to access shadow maps in Unity 5's new shader setup.
     
    Alkann likes this.
  16. Alkann

    Alkann

    Joined:
    Dec 17, 2014
    Posts:
    12
    I'm sorry but I don't really understand what I should do, if it's about shaders I don't know where to start i never tried yet to mess with them.
    All I do is modifying the camera projection matrix in a LateUpdate, no render to texture (Unity free version).

    I tried to find something else in the manual and api refering to a near and far plane but without success, only cameras seem to have those so I was thinking maybe you meant to set my near plane using the api rather than the editor.
    Just in case I did it (set to 15f in my scene) before and after the matrix modification but no changes.
    I'll make a video of what happens normally, what it does when I activate my custom matrix then adjust the near plane to show what happens with shadows. I just need the time to encode and upload, I'll maybe edit this post to share the link.

    UPDATE:
    I warn you my spoken english is REALLY bad.
    Shadows direction is wrong, not depending of the directional light orientation, depends of the camera orientation. Also appearing above objects, as if depth was wrong somewhere, not able to know if an object is supposed to be in front or behind of the shadow emitter.
    At the end of the video I don't know what I want to say, it's just to show quiclky how GL computes shadows step by step with the projection matrix, maybe someone more used to work with this will see something interesting.


    The video will be available once youtube encoding done.
     
    Last edited: Mar 17, 2015
    Ben-BearFish likes this.
  17. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    I setup a .unitypackage for Unity 5 with my scene here. It can be downloaded and run. You can see that no shadows appear whatsoever in Forward rendering with the custom projection matrix. To move the skewed camera left/right/up/down, just click and hold the left mouse button and drag the mouse around.

    Also, it's worth noting this is how I currently feel:
     
    Last edited: Mar 18, 2015
  18. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    For anyone that is absolutely stuck with this issue and needs it to work now, the Sunshine plugin with the right settings will get the shadows working again in Unity 5. It's not a cheap plugin, but it at least does what Unity 5 should be doing.

    If you have questions on how to setup Sunshine to work with the custom projection matrix, I spoke with the Sunshine developer and he generously walked me through how to do it. Now that's what I call great customer service. You can PM me if you want the steps to getting Sunshine setup correctly with the custom projection matrix.
     
    Last edited: Mar 18, 2015
  19. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    @Alkann I'd suggest you submit your problem to Issue Tracker and try to up-vote it to get more attention.
     
  20. Alkann

    Alkann

    Joined:
    Dec 17, 2014
    Posts:
    12
  21. Alabatross

    Alabatross

    Joined:
    Dec 15, 2012
    Posts:
    223
    Still no luck here?

    I started having the same shadow issue as shown Voxels post (work in editor and all, break in build) in RC2, when it would randomly break in some builds but be fine in others (with completely unrelated changes in between). But since RC3 they've broken completely in all builds.
     
  22. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    @Alabatross , no luck indeed. The only solution is to figure out how to create your own shadow maps and not use Unity's. The Sunshine plugin seems to work that way, so if you can figure it out on your own it should work. I just am unsure of how to currently do that in Unity 5.
     
  23. jococo

    jococo

    Joined:
    Dec 15, 2012
    Posts:
    232
    I'm having the exact same issue. It worked well in 4. Is there a way besides posting here to say to Unity yes I am having the same issue?
     
  24. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    @jococo , you can file a bug report, or try to find the email contact for one of the graphics engineers, but I'm not sure about how to do the latter. There's also a Wishlist forum somewhere.
     
  25. Alkann

    Alkann

    Joined:
    Dec 17, 2014
    Posts:
    12
    Hello, I think I got a hint about why I must touch my near plane to have shadows appear.
    If you have more knowledge than me about matrix projections maybe will you have an idea about what to do to correct this problem.

    I found a little problem under Unity 4 with Point Lights, sometimes the objects lightened by it are not, as if they were no more in the frustum because the light is not applied along a line parralel to a side of the screen (right, left, top or bottom, depends of the situation).
    Then I touched everything to see if it could change, once again increasing the near plane value can resolve the problem and let everything litten correctly.

    I think the frustum computed for lights is not computed the same way it is for our objects. Both use surely our matrix projection thought.

    Look here to see top left bottom and right planes I'm talking about.
    http://www.songho.ca/opengl/gl_projectionmatrix.html


    I may be wrong but here is my hypothesis:
    I understood what could happens when I saw the non litten part following a plan parrallel to my frustum right plane.
    If you look at the values in a normal projection matrix you can see we can determine form the matrix if a point is between the right and left (or bot and top) planes or not just knowing the near plane values : 2n/(r-l)

    The problem is about the frustum in my case. My projection matrix is computed in a way where my left and right plans are not parralel, neither are the top and bottom planes, also my matrix changes (update each frame) depending of my camera orientation. Doing this I also do not have my near and far plane as real planes if the formula used to know if an object is inside the frustum or not consist to follow the sides:
    Let's take a normal frustum, we try to figure the near plane at a distance of 10 from the camera, with 20° inclinaison everywhere it would make a distance of ~10.64 along on a plane on the sides so it's correct to assume anything along any side at a distance < 10.64 is behind the near plane so not in the frustum.
    Now imagine my situation where top bot left and right are not parralel. We won't be able to obtain a plane for sure if we take the points on the lines obtained when we cross top left, top right, bot left, bot right, at a distance of 10.64.

    so we can have 2 cases to know if an object is in the frustum (near plane case)
    Case A :
    We have the near plane with a normal in the camera direction and at a distance of 10 from the camera. We look if an object is behind in front of the plane to know if he is in the frustum.
    Case B :
    Using the 4 lines (line equations) obtained when crossing top/left, top/right, bot/left, bot/right we get the position of points at a distance of 10.64 from the camera along those lines (1 point by line so 4 points). We only need 3 points to know the near plane.

    The result observed is as if objects are computed in the fustum using the case A and if lights must be applied on them or not using the case B.
    Sans titre-1.jpg
    Imagine the 2 differents near planes obtained, where they cross is the line shown on the screen where we don't have lights anymore because they would be computed in or out of the frustum using a different case than objects.
    Sans titre-2.jpg
    If it's the case for the lights it can also be it for the shadows.

    If we could force computation of every light shadows and object ignoring the frustum (just for debug usage! not final build heeeey...) we could confirm or not if it's a frustum problem.

    Edit : When saying parralel planes of course they aren't by default in perpective mode but you know what I mean, when you cut sides with a plane having a normal along the camera direction the lines obtained are parralel for the pairs bot/top and left/right.
     
    Last edited: Apr 8, 2015
  26. Alkann

    Alkann

    Joined:
    Dec 17, 2014
    Posts:
    12
  27. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    @Alkann, thanks for the heads up. I'd suggest anyone with this issue or a similar issue with shadows or custom projection matrices to vote on this issue to bring it to the Unity dev's attention. @jococo , @VoxelLoop-Curtis , @Chrarc , and @Alabatross I'd recommend you vote on that issue.
     
  28. Metnias

    Metnias

    Joined:
    Sep 1, 2014
    Posts:
    1
    (Sorry, English is not my native language)
    This is the first time I've used Unity bug reporter, and I'm a bit confused.
    Is it the same issue with http://fogbugz.unity3d.com/default.asp?686520_7lhbntgd3vi4d3el and what @Alkann posted?
    Case number is same w/ issue ID, but I just wanted to know, since there was no email from Unity after the first one.

    + Attached ZIP is the same project I sent through the bug reporter. It shows something like this.
     

    Attached Files:

  29. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    @Metnias , yeah that's the same issue as Alkann.
     
  30. andriepu

    andriepu

    Joined:
    Apr 27, 2014
    Posts:
    1
    I changed my Camera's Clipping Planes, and the shadow are now showing. Do you guys, ever tried to do this?
     
  31. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    @andriepu , your shadows are showing, but are they correct?

    Could you take a screenshot of what your scene with shadows looks like and what your camera settings are?
     
  32. jimmikaelkael

    jimmikaelkael

    Joined:
    Apr 27, 2015
    Posts:
    796
    I'm having the same problem as @VoxelLoop-Curtis. Strangely it does not happen on a freshly created project with just a terrain and trees (from Terrain asset). As soon as I add a character from the Standard assets for example shadows are broken, then I remove this character from the scene and shadows get back to working state.

    I'm using Unity 5.

    Any feedback from Unity team about this ? Or anyone have a solution ?
     
  33. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    We haven't heard anything from Unity so far. The best we can do right now is to up vote the issue on the Issuetracker link @Alkann posted above.
     
  34. jimmikaelkael

    jimmikaelkael

    Joined:
    Apr 27, 2015
    Posts:
    796
    Thank you for reply @Ben BearFish
    On my side I do not use a custom projection matrix, but the bug is exactly the same.
    I'm voting for this issue.
     
  35. jimmikaelkael

    jimmikaelkael

    Joined:
    Apr 27, 2015
    Posts:
    796
    Just to let you know:
    I installed Unity 5.0.1p2 yesterday, cleared the global GI cache and my GI baked data and now the problem is solved.
     
    Orion likes this.
  36. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    Do you mean you clicked on the arrow on the Build button under the Lighting->Scene tab and selected Clear Baked Data?
     
  37. jimmikaelkael

    jimmikaelkael

    Joined:
    Apr 27, 2015
    Posts:
    796
    Yeah exactly, and after in Edit->Preferences->GI Cache I've pressed the "Clean cache" button too.
     
    Ben-BearFish likes this.
  38. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    Hmm...that still didn't help with my issue, but it's good to hear it helped with yours.
     
  39. jimmikaelkael

    jimmikaelkael

    Joined:
    Apr 27, 2015
    Posts:
    796
  40. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
  41. lisa.staehli

    lisa.staehli

    Joined:
    May 20, 2015
    Posts:
    1
    I've got the same issue here: after porting my Unity4-project into Unity 5, I couldn't get shadows to work (in Editor AND Game View). If I upload all the assets into a new Unity5-project, shadows are working... So, I guess there must have been happening something while upgrading from U4 to U5, right?

    Is there any news about that issue? Some Workaround to fix this?
     
  42. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    @lisa.staehli , you can vote on the bug here. Besides that I used the Sunshine plugin, which is somewhat expensive, but it only works in Forward Rendering and with one directional light.
     
  43. Dave-Taylor

    Dave-Taylor

    Joined:
    Jul 7, 2012
    Posts:
    21
    When you set the Camera.projectionMatrix, it forces the camera into Forward Rendering mode. I hit this same issue while using Aras' mirror code. You can't see this by looking at Camera.renderingPath, which is what's displayed in the inspector, but you can see the change in script by looking at Camera.actualRenderingPath. It changes from Deferred to Forward as soon as you change Camera.projectionMatrix.
     
  44. sbsmith

    sbsmith

    Joined:
    Feb 7, 2013
    Posts:
    126
    I am having a similar issue with a custom oblique matrix that I need to create a portal-like effect. If my camera is aligned so that the frustum is symmetrical, the shadows work fine. However, as soon as I move the camera, the matrix becomes oblique and the shadows disappear. I'm using Unity 5.3.2 and as per Dave Taylor's suggestion, I checked actualRenderingPath after setting the matrix and it still says Deferred.

    I have also noticed that many of the Standard Asset camera effects (such as DoF) break with an oblique matrix.

    When I found this thread and saw that it started a year ago, I was hoping to find an answer. I'm also following the issue on this thread: http://forum.unity3d.com/threads/sh...ection-matrix-still-broken-in-5-4-0b1.372623/

    If this solves the problem then I will post back here.
     
    Last edited: Mar 11, 2016
  45. sbsmith

    sbsmith

    Joined:
    Feb 7, 2013
    Posts:
    126
    There is hope it seems.

    There was a post in this issue thread by Unity that said users of 5.4 beta could download a package to attach to their cameras to fix screen space shadows.

    I installed 5.4.0b9 (which is still quite buggy when importing projects from 5.3.2) and attached the replacement screen space shadow shader to my cameras. When I moved my camera, the matrix became oblique and... the shadows did not disappear. :)

    There still appears to be some issues with shadow clipping, but it seems like they are on the right track. Though, the new Depth of Field is still broken for oblique projection matrices.
     
    Last edited: Aug 15, 2016
  46. LaserTrent

    LaserTrent

    Joined:
    Apr 26, 2016
    Posts:
    1
  47. BigJoe7022

    BigJoe7022

    Joined:
    Sep 26, 2017
    Posts:
    1
    I was having fits with my shadows showing in the editor view but not the game view.

    SOLUTION:
    I had a directional light rotated to 90 degrees on the X axis. This value apparently breaks shadow casting. A value of 89.99 in X rotation gives me the direct overhead shadows I needed.

    Shadow Solution.png