Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

Is it possible to have transparents using the 2D renderer in URP?

Discussion in 'Universal Render Pipeline' started by japhib, Jan 15, 2020.

  1. japhib

    japhib

    Joined:
    May 26, 2017
    Posts:
    65
    Hi! I started using the URP for the 2D lights package and it's been really enjoyable so far. However, I'm unable to make a screen-space distortion effect (which utilizes a custom shader on a hopefully transparent object) with the 2D renderer that's necessary to use if you want the 2D lights & shadows. I want to know if it's possible, which it really seems like it should be, but I can't figure it out.

    My main question is, how do I mark an object as transparent for the 2D renderer in URP?

    I'm able to get the screen-space distortion effect with a Forward Renderer instead by following this tutorial. With a Forward Renderer you can make a new layer and then specify that layer to render after the others inside the "Render Features" settings, which doesn't appear to exist with a 2D renderer. But the problem with using a Forward Renderer is that 2D lights & shadows don't work anymore.

    As a different workaround for getting the same effect while using the 2D renderer, I've also tried to have the shader run directly on the camera by using this script attached to the camera, but it doesn't seem to do anything:

    Code (CSharp):
    1. public class ScreenWideEffect : MonoBehaviour
    2. {
    3.     public Material material;
    4.  
    5.     public void OnRenderImage(RenderTexture source, RenderTexture destination)
    6.     {
    7.         Graphics.Blit(source, destination, material);
    8.     }
    9. }
    Here's the shader graph I'm using, if it helps:
    1 - the shader for an object I wanted to make transparent and put in the scene on top of other stuff
    2 - slightly modified to take in the _CameraOpaqueTexture, so it can distort that and blit to screen

    I also tried setting up both a 2d renderer and a forward renderer but I can't seem to get the 2d renderer to render my should-be-transparent object as a big grey square. Which makes sense because I couldn't find a setting that looked like it should tell the renderer that object is different and should be rendered afterwards.

    So my questions are, in order of importance,
    A) Is it possible to make a transparent object with the 2d renderer?
    B) Is there some other way to get a screen space distortion effect while using a 2d renderer?

    C) Alternately, is it possible to get 2D lights & shadows without using the 2d renderer?
    D) What am I doing wrong with my OnRenderImage() script above, or with my multiple-renderer setup?
     
  2. yuanxing_cai

    yuanxing_cai

    Unity Technologies

    Joined:
    Sep 26, 2014
    Posts:
    335
    A) No. What you need for these effects is the opaque texture feature available in the Forward Renderer. 2D Renderer doesn't support this currently, but we have plans to implement it.

    B) Use multiple cameras. The first camera renders the scene to a render texture, then in the second camera sample the render texture and do the distortion. Our new 2D demo project uses this technique for water reflections: https://assetstore.unity.com/packag...-projects/lost-crypt-2d-sample-project-158673

    C) No.

    D) OnRenderImage() is not supported by any SRP.
     
  3. japhib

    japhib

    Joined:
    May 26, 2017
    Posts:
    65
  4. pastaluego

    pastaluego

    Joined:
    Mar 30, 2017
    Posts:
    126
    Sorry to hijack I'm just confused by the answer to A). It's not possible to make a transparent object with the 2D renderer? Aren't all sprites by default on the transparent render queue, or am I misunderstanding the question?
     
  5. japhib

    japhib

    Joined:
    May 26, 2017
    Posts:
    65
    If I understand correctly, you're right that all sprites are on the transparent render queue by default.

    What my question should've been is something like "is it possible to make a sprite that will render after the other sprites" to which the answer is no.
     
    pastaluego likes this.
  6. pastaluego

    pastaluego

    Joined:
    Mar 30, 2017
    Posts:
    126
    Ah yeah. I was hoping with SRP we'd be able to basically script it so we'd basically be able to make our own version of _CameraOpaqueTexture at a custom moment in the pipeline. Essentially like grabpass in the built-in render pipeline except not as devastating performancewise.
     
  7. Alejandro-Martinez-Chacin

    Alejandro-Martinez-Chacin

    Joined:
    Oct 15, 2013
    Posts:
    111
    Hello!
    Any news by any chance on this?
    Currently on 2019.3.5f1 and marvelled at how well 2D Renderer works with composite shadow casters and all even in low-ish end devices.

    However, was interested in this very same feature, wondering if it is coming soon or already implemented in further versions.

    Would rather go with A. and be done with it (if it is to be implemented anytime soon) or go the Scriptable Renderer Features (if it is to be implemented also) and have control on 'when' to create the opaque (or equivalent) texture for the sprites.

    B. would work, however it requires a bit of a dual camera setup to make the post processing effects work on a final rendered texture (the mix of a first camera RT base fullscreen sprite, the distortion effects overlaid, let the pipeline work post effects like bloom and vignette and others). Certainly not that complicated and on the easy side to do but would be the last option.

    Thanks a lot in advance.
     
  8. yuanxing_cai

    yuanxing_cai

    Unity Technologies

    Joined:
    Sep 26, 2014
    Posts:
    335
    We still haven't implemented the opaque texture feature for the 2D Renderer, but we are very close to starting.
     
  9. Sloppy-shorts

    Sloppy-shorts

    Joined:
    Apr 10, 2015
    Posts:
    43
    Hello, if OnRenderImage() is not supported, what is alternative in URP 2DRenderer?
     
  10. yuanxing_cai

    yuanxing_cai

    Unity Technologies

    Joined:
    Sep 26, 2014
    Posts:
    335
    The "opaque texture" feature is now implemented as it's being tested, though the final name of this feature might be something else.
     
  11. dr4

    dr4

    Joined:
    Jan 14, 2015
    Posts:
    74
    I'm extremely interested about this, that last message really made my heart beat (I have been googling regularly to see if this will be implemented for a very long time now), I just checked and I assume that it didn't make it through yet? Anyone knows anything about this? @Chris_Chu ?
     
  12. japhib

    japhib

    Joined:
    May 26, 2017
    Posts:
    65
    Nothing about it in the 2020.1 or 2020.2 release notes.
     
  13. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    222
    Its in 2021.1. There is a new variable called _CameraSortingLayersTexture. This can be enabled by going to the 2D Renderer Data. You can get it in the latest beta.

    I would definitely appreciate hearing any feedback.
     
  14. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    28,390
    Why is it called _CameraSortingLayersTexture if it's just the opaque render target? It probably should get called whatever it's largest use-case might be, or by what it actually is... that name doesn't really leap out to developers wanting an opaque texture if skimming any (eventual) docs.
     
  15. pastaluego

    pastaluego

    Joined:
    Mar 30, 2017
    Posts:
    126
    "There are no plans to support _CameraOpaqueTexture. We have instead created a new shader variable called _CameraSortingLayersTexture which will contain layers rendered from back to front, up until a specified layer. The upcoming release of URP 11 should have it." -Chris_Chu from another thread.
     
    hippocoder likes this.
  16. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    28,390
    Yeah I get that, but it is also compatible with just the one layer, so it's basically describing potential usage instead of what it actually is as an object.

    Not an issue for me, was just feeding back.
     
  17. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    222
    _CameraSortingLayerTextures was added to allow more flexibility with the 2D rendering workflow, by allowing users to render from the back-most layer to a selected layer and saving that into a texture.

    This was needed because sprites are by default all transparent and overwriting the _CameraOpaqueTexture after rendering transparency, would be confusing to users, unless they already know what _CameraOpaqueTexture does.

    Additionally, overwriting the _CameraOpaqueTexture could cause potential problems in the future if we plan to support mixed 2D/3D rendering.
     
    hippocoder likes this.
  18. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    28,390
    Thanks for the explanation, I guess it's no problem if it's documented.
     
  19. pastaluego

    pastaluego

    Joined:
    Mar 30, 2017
    Posts:
    126
    Would this mean it would be possible to emulate the current GrabPass functionality for transparents by using _CameraSortingLayerTexture?

    As in would it be possible to specify multiple sorting layers during the rendering process and saving each into a texture? Or is it limited to only 1 specific sorting layer using only that 1 built-in texture variable?
     
  20. dr4

    dr4

    Joined:
    Jan 14, 2015
    Posts:
    74
    thanks for the info @Chris_Chu , super happy to see Unity giving this some love, unfortunately I don't see to be able to make it work, This is literally the only thing that shows up on google when searching for this new variable so I'm sure that a little step by step guide will benefit the whole community for years to come as it is likely to stay up there.

    I have a test project that is working with CameraOpaque and Forward renderer, when I replace the CameraOpaque variable for the one you mentioned and change my forward renderer for the 2D renderer I can see the effect working in the preview window of shader graph (just like I could see the effect of _CameraOpaqueTexture) but in game I get the same result that _CameraOpaqueTexture were giving me with the 2D renderer (working fine with the other renderers), only the colour changes (just a semi-transparent black sprite), any chance of getting a little git sample where this node is up and running? This is how I have this set up:

    in my shader graph (which seems to display the correct effect), I just put this variable in the node as a texture where I use to have the camera opaque one (tried with both, and 's' and without an 's' at the end, noticed that you mentioned both):

    upload_2021-2-7_22-28-10.png

    then in the renderer I turn it on, pointing to a default layer:

    upload_2021-2-7_22-29-43.png

    my background is opaque (in the default layer):

    upload_2021-2-7_22-30-59.png

    the sprite with the _CameraSortingLayerTextures material is transparent (it is also in the default layer):

    upload_2021-2-7_22-31-50.png

    tried experimenting with different values but nothing seems to bring it back to live, am I missing something?

    thanks for your time!
     
    Last edited: Feb 7, 2021
  21. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    222
    You choose a range of layers starting from the back and they are rendered into a built-in texture variable. The functionality is similar to how _CameraOpaqueTexture works for 3D.
     
  22. dr4

    dr4

    Joined:
    Jan 14, 2015
    Posts:
    74
    spent another hour today trying to get this to work, I'm either missing something or _CameraSortingLayersTexture doesn't do anything. Set up a glass effect with Camera Opaque is super easy, but _CameraSortingLayersTexture just makes the sprite black
     
  23. Hugo-Bille

    Hugo-Bille

    Joined:
    Jul 6, 2012
    Posts:
    6
    I think I'm having the same problem in 2021.1.0b8. My shader tries to use _CameraSortingLayersTexture as a global texture variable and all it gets is a default grey square, no matter how I set it up in the Renderer 2D Data.

    I'm also wondering if there are plans to allow setting up multiple blit passes like these? I'm hoping to convert a big 2D project from Built-in to URP as the performance gains are truly massive, but in a lot of places we use 2, sometimes 3 grab passes and there seems to be no way to replicate that in URP.
     
  24. Carbone_13

    Carbone_13

    Joined:
    Sep 4, 2017
    Posts:
    2
    Hi, any news on this ? I can't make it work either; it gives me a grey texture

    _CameraSortingLayersTexture is indeed recognized but don't seem to work :(
     
  25. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    222
    Sorry but, no. I've had a number of bugs to sort out that took priority. I will try and get to this as soon as I can.
     
  26. KristoE

    KristoE

    Joined:
    Oct 13, 2012
    Posts:
    90
    I was strugging with it for a while as well and then came across another thread where I found solution for me.
    Adding a single light object into the scene made my shader work for me.
    Also make sure that the exposed parameter is unchecked for the _CameraSortingLayerTexture.
    And the parameter I used is as mentioned above _CameraSortingLayerTexture.
     
  27. Carbone_13

    Carbone_13

    Joined:
    Sep 4, 2017
    Posts:
    2
    I tried the solution mentionned by KristoE and the texture now "work" but look very buggy (see attachments).
    Maybe I am just not using it correctly

    test5.gif

    Screenshot_3.jpg
     
    dr4 likes this.
  28. dr4

    dr4

    Joined:
    Jan 14, 2015
    Posts:
    74
    Last edited: Apr 20, 2021
  29. Warthong

    Warthong

    Joined:
    Jan 12, 2014
    Posts:
    3
    Looks like there is a typo in the tooltip for this feature. You need to use _CameraSortingLayerTexture (like mentioned above) instead of _CameraSortingLayersTexture as mentioned in the tool tip. Drop the "s" in your shader and you should be good.
    upload_2021-4-25_21-11-41.png

    This worked for me.
    upload_2021-4-25_21-13-42.png
     
    japhib likes this.
  30. KristoE

    KristoE

    Joined:
    Oct 13, 2012
    Posts:
    90
    The issue you are having seems to be with trying to include the screen rendering image into the image itself.
    Try setting the sprite with the shader to the highest drawing layer, this should remove the glitching. If for your use case you need it behind, then I guess you need some clever masking to make that work.
     
  31. Graigy1337

    Graigy1337

    Joined:
    Oct 26, 2019
    Posts:
    4
    This is working incredibly well. I have set up a heat distortion, blur and underwater shader all completely working in the 2D pipeline. It's so great bring able to just place a heathaze sprite Infront of all the stuff I need to effect and it just works...

    My only problem is you can't have multiple _CameraSortingLayerTexture sorting layers.

    Is it possible to create a list of _CameraSortingLayerTexture and be able to set each to different layers and then reference them individually in the shader graph? This would really open up the opportunities for a vast number of effects.

    Thanks Chris for your hard work.
     
  32. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    222
    This seems like a good idea. Do you have an example of something you'd want to do if you had this?
     
    RedHillbilly likes this.
  33. luka-wisniewski

    luka-wisniewski

    Joined:
    Jun 22, 2013
    Posts:
    3
    I am trying many ways but I can not create a sprite object that distorts my objects behind it using URP 2D Render Data and Scene Color node in shader graph. Does anyone know how to get this effect? Any examples or tutorials?

    upload_2021-9-16_15-25-27.png
    result

    upload_2021-9-16_15-25-50.png

    upload_2021-9-16_15-26-4.png

    upload_2021-9-16_15-26-14.png
     
  34. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    971
  35. luka-wisniewski

    luka-wisniewski

    Joined:
    Jun 22, 2013
    Posts:
    3
    Thank you @Lo-renzo for the great examples. Unfortunately, I have not found an answer on how to deal with the distortion effect in the particle system using URP & 2 Render Data. Could someone please help me and give me some guidance:)?
    upload_2021-9-16_22-27-46.png
     
  36. dikar771

    dikar771

    Joined:
    Oct 30, 2015
    Posts:
    13
    Hi, Chris!
    Any news about this feature?
     
  37. RedHillbilly

    RedHillbilly

    Joined:
    Mar 24, 2014
    Posts:
    33
    Works really well.
    As mentioned above, steps to make it work:
    • use _CameraSortingLayerTexture tag, remove the "Exposed" checkmark.
    • In the 2D renderer data, set the "Foremost Sorting Layer" to the last layer you want to be renderer with distortion.
    • Set the "Sorting Layer" of the sprite renderer which contains your distortion material to be above the last layer of the "foremost sorting layer".
    Cheers
     
    samanabo and KamilJu like this.
unityunity