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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Using Shaders With Canvas Set to "Screen Space - Overlay"

Discussion in 'Editor & General Support' started by the_terrible, May 13, 2020.

  1. the_terrible

    the_terrible

    Joined:
    Nov 4, 2015
    Posts:
    40
    Recently I've been trying to add shaders to my UI elements to snazz things up a bit. However, it seems that anytime I add a material with a shader attached to it to a UI object, that UI object disappears. Normally, my UI objects don't have any materials assigned to them. After doing a little bit of research I've seen some people saying that shaders don't work when your UI canvas is set to "Screen Space - Overlay". Is this true?

    When I set the UI canvas to "Screen Space - Camera" or "World Space" my UI breaks. Have others come across this issue? How do I add shaders to my currently existing UI?
     
  2. DiegoDePalacio

    DiegoDePalacio

    Unity Technologies

    Joined:
    Oct 28, 2009
    Posts:
    506
    Hi @the_terrible

    Basically, you can't use any shader for your UI elements, mostly because the shader logic can assume that your element is on the 'Screen Space' and for that reason, no conversion between 'Object Space' - 'World Space' - 'Camera Space' are needed.

    My recommendation is to search for shaders specifically created to be used in the UI.

    For example, you can check this forum thread about this: https://forum.unity.com/threads/shader-graph-ui-image-shader.628111/


    I hope it helps!
     
  3. the_terrible

    the_terrible

    Joined:
    Nov 4, 2015
    Posts:
    40
    @DiegoDePalacio Both solutions presented in that forum thread use "Screen Space - Camera" and not "Screen Space - Overlay". Am I correct in my understanding that it is literally impossible to use any shaders at all with "Screen Space - Overlay" and that only certain shaders will be functional with "Screen Space - Camera"?

    Bonus question: how do I convert the Render Mode of all my currently functioning UI from Overlay to Camera? It would seem to me that Builtin Layer 5 "UI" is drawn behind Builtin Layer 0 "Default" and that this ordering cannot be changed.

    [EDIT]: I was playing around with the sorting layers and was able to create one for the UI. Haven't really messed around with sorting layers before. Would still like confirmation on whether or not "Screen Space - Overlay" is technically capable of supporting shaders at all, though.
     
    Last edited: May 20, 2020
  4. krides

    krides

    Joined:
    Apr 7, 2011
    Posts:
    38
    I want to bump this thread and also mention that shaders absolutely used to work with Screen Space - Overlay, but after some Unity update, these two options appeared in the Shader Graph creation menu:

    upload_2020-8-15_15-35-35.png

    Until I opened the Shader Graph of an old shader, it worked perfectly fine in my project. However, as soon as I opened it in the Shader Graph tool, the object which had this shader's material assigned to, immediately disappeared. No amount of reverting in source control fixed it.

    Upon further examination, I noticed that my custom shader got changed to this new "Experimental" type of Master:

    upload_2020-8-15_15-37-24.png

    I can guarantee you that this used to be Unlit Master before I updated Unity. The fact that it changed to this broke the shader.

    So, my question is: how do I make this work the way it used to work? How am I supposed to change my shader in a way that will make it appear in the Overlay canvas again?

    Using Screen Space - Camera is not an option since I spawn the canvas and camera independently from each other, multiple times, and kind of all over the place. The amount of work that changing how this works represents is entirely unreasonable for something this stupid and small.
     
  5. tealm

    tealm

    Joined:
    Feb 4, 2014
    Posts:
    108
    I am in the exact same situation @krides, if I create a new Unlit shadergraph and a material and use that on a Image in my Canvas set to Screen space - Overlay, objects will appear ONTOP of my image depending on Z position of the Image. Sprite Unlit Graph behaves in the same way.

    @DiegoDePalacio how can we now create a "UI" shader in shadergraph that will ignore Z positioning and always be renderered ontop as expected when placed on a UI component inside Canvas, Screen space - Overlay?
     
  6. sdb7

    sdb7

    Joined:
    Apr 8, 2013
    Posts:
    23
    As of now i have same problem (ZTest problem where the UI is not drawn when entering playmode but visible on scene) and i would like as much as possible using screen space overlay for my UI, I solve it using amplify shader editor. I am sorry if this is not a solution for most people but this problem plagueing me for 2 days and shader graph give me a hard time with this particular problem.
     
  7. GlenMcKnight

    GlenMcKnight

    Joined:
    Feb 5, 2020
    Posts:
    1
    I am just joining in saying I really wish there was some clarity on why it would work in worldspace but not in overlay.
    Not that I like this idea, but do I need to render it to another camera, and then use the camera stacking aspect?
     
  8. DanjelRicci

    DanjelRicci

    Joined:
    Mar 8, 2010
    Posts:
    305
    I don't think my Shader Graph sprites are working at all with UI, either with Screen Space Overlay or Screen Space Camera. Sprites are invisible while using Sprite Unlit mode, but somehow they work with Sprite Lit.
     
    Last edited: Mar 3, 2021
  9. ALherbier

    ALherbier

    Joined:
    Sep 1, 2020
    Posts:
    8
    I would like to comment on this topic and give my own experience:
    In my case, I can use ShaderGraph within the Canvas in Screen Space - Overlay.
    (Although I keep reading everywhere that this is not possible).

    You can check in my pre-alpha game here : https://dasprofgames.itch.io/botcombat
    (there are red and blue enegy bars with animated water effect obtained using PBR shader)
    My versions of Unity and setup are the following:
    Linux (Ubuntu 18.04) > Unity 2019.4 (OpenGL 4.5) > Universal Render Pipeline > Canvas Screen Space - Overlay > and Additional Shader Channels are set to TexCoord1 and Tangent.

    However adding Normal to Additional Shader Channels [see Canvas options] is a killer.
    In my case, adding a TextMeshPro component in my Canvas was the origin of the "bug".
    Indeed the use of TextMeshPro will automatically turn ON the Additional Shader Channels Normal option in game mode (not in scene view).
    SEE this post : https://issuetracker.unity3d.com/is...nel-on-a-canvas-resets-when-entering-playmode

    Replacing TextMeshPro by normal Text and removing the Normal option to Additional Shader Channels was enough to recover my ShaderGraph in Canvas Screen Space - Overlay.

    Hope this can help.
     
    Hzzzzzppl likes this.
  10. Bodov_Olexandr

    Bodov_Olexandr

    Joined:
    Jul 4, 2016
    Posts:
    1
    ALherbier, thanx a lot! You made my day ) We strugled with this issue for a whole day. Tryed manualy rewrite the shader, swaping our canvases to camera space and fixing problems that came along with that...
    And now every thing is work fine.
     
    ALherbier likes this.