Search Unity

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.
     
    AlejMC likes this.
  3. japhib

    japhib

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

    pastaluego

    Joined:
    Mar 30, 2017
    Posts:
    196
    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:
    196
    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. AlejMC

    AlejMC

    Joined:
    Oct 15, 2013
    Posts:
    149
    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.
     
    AlejMC likes this.
  9. botove

    botove

    Joined:
    Apr 10, 2015
    Posts:
    52
    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:
    108
    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:
    257
    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

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    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:
    196
    "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

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    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:
    257
    _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

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

    pastaluego

    Joined:
    Mar 30, 2017
    Posts:
    196
    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:
    108
    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:
    257
    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:
    108
    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:
    257
    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:
    108
    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
     
    Nit_Ram and japhib like 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:
    9
    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.
     
    HofiOne likes this.
  32. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    This seems like a good idea. Do you have an example of something you'd want to do if you had this?
     
    HofiOne and RedHillbilly like 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:
    1,511
  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:
    19
    Hi, Chris!
    Any news about this feature?
     
  37. RedHillbilly

    RedHillbilly

    Joined:
    Mar 24, 2014
    Posts:
    39
    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
     
  38. Satelleye

    Satelleye

    Joined:
    Dec 26, 2020
    Posts:
    1
    Thanks @RedHillbilly for the tips, however, I can't seem to get mine working despite the setups. My sprite just shows a black material that sometimes flickers different colours depending on the camera zoom instead of a distortion material. I'm not sure if I am doing something wrong in my setup.

    Could someone help me identify the errors if I have made any?

    2D Renderer Data setup:
    2D Renderer Data Evi.png

    URP Pipeline Asset setup:
    URP Pipeline Asset Evi.png

    Shader setup:
    Shader Graph Evi.png
    Result:
    GameObject Mat Evi.png
     
  39. leohilbert

    leohilbert

    Joined:
    Nov 28, 2015
    Posts:
    21
    Hi, sorry to revive this thread, but adding support for multiple CameraSortingLayerTextures would be a great addition to the feature imo.
    In our case we have multiple effects like blur we want to apply to parts of the game world and this would allow us to stack them on top of each other. Otherwise we would have to make sure the effects don't overlap or use an additional camera (which renders into a texture). Is this something you are considering?
     
    Nit_Ram and RedHillbilly like this.
  40. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    It's not on our roadmap, but I'll bring it up for discussion.
     
    RedHillbilly and leohilbert like this.
  41. Rob_Koda

    Rob_Koda

    Joined:
    May 2, 2017
    Posts:
    7
    Hi!

    Just here to say that having multiple CameraSortingLayerTextures would be awesome indeed!

    In my current project, I want to blur the background depending on conditions, and I also want a heat distortion effect. As of now, I have to make a blur version of my background texture and fade between them. Is the legacy render pipeline, it was just a shader and was much more convenient!

    I hope to see this in the release notes in the future :)
     
    Nit_Ram, dr4 and leohilbert like this.
  42. Nit_Ram

    Nit_Ram

    Joined:
    May 8, 2016
    Posts:
    27
    I also think that this feature would be much more useful if it had the option for multiple CameraSortingLayerTextures.
     
  43. Kshesho

    Kshesho

    Joined:
    Nov 2, 2018
    Posts:
    5
    Could anyone lay out exactly how to get transparent sprite effects working using unity 2021.3.1.0f1 or later, and using the Renderer 2D? I don't understand exactly how the shader, renderer, and camera should be set up. I just want to have 2D lights and effects (such as heat distortion) working at the same time.

    I can't find info about this anywhere else, so your time is appreciated.
     
  44. oscurekid

    oscurekid

    Joined:
    Mar 6, 2019
    Posts:
    3
    Hi, i've been trying to achieve distortion effects while using URP 2D renderer, my project uses 2D lights, trying to apply a 2D distortion effect results on a gray square. This is the shader i'm using upload_2022-12-29_10-28-8.png

    As you can see this is a simple shader, 'been following all the steps on this youtube tutorial
    This tutorial uses the forward render pipeline and i'm trying to achieve this using the 2D render pipeline as i said before.

    I have the 2D render pipeline settings like this
    upload_2022-12-29_10-33-47.png

    upload_2022-12-29_10-34-15.png

    I would like to know if is there a way to achieve distortion effects, or directly, making the scene color node work
    using the 2D render pipeline, I've been MONTHS looking for a solution but didn't found anything and there is no solution in the docs, or any forum with a simple or complete solution or explanation on how to get distortion FX on a project with 2D Lights

    upload_2022-12-29_10-41-24.png

    @Chris_Chu , @RedHillbilly , @yuanxing_cai

    anyone can make a detailed explanation on this topic? Never needed so much help before, Thanks for the attention ^^
     
  45. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,511
    @oscurekid There are a couple problems.

    1. Forward Renderer Data vs 2D Renderer Data. Look at your Renderer List on the Universal Render Pipeline Asset. It lists the New 2D Renderer Data. Simply put, you do NOT want Forward Renderer Data. You can delete it. You're adding your Renderer Feature to the Forward Renderer Data, not the "New 2D Renderer Data" asset. Even if you added your Renderer Feature to the correct asset, it still wouldn't work and is also unnecessary.

    2. You're using Scene Color in your Shader Graph. You do NOT want that. The 2D Renderer does NOT create a _CameraOpaqueTexture, which is what the Scene Color pulls from. The 2D Renderer doesn't use opaques, just transparents, so that's why the special _CameraSortingLayerTexture was developed. It takes a snapshot of the colors on your screen at a particular Sorting Layer of the transparent render pass.

    ------

    How to fix so it works in shader and is populated (with a non-gray texture!) by the 2D Renderer.

    (A) Fix your Shader Graph. Add a new Property on the Blackboard called CameraSortingLayerTexture and make sure its reference is _CameraSortingLayerTexture exactly. Then UNCHECK Exposed. This tells Unity to pull this property from a global texture, which is exactly what we want. Then sample your CameraSortingLayerTexture, replacing SceneColor. Plug in your distorted UVs into the UVs of the Sample Texture2D node.

    (B) Fix the creation of _CameraSortingLayerTexture. On the 2D Renderer Data (for you, "New 2D Renderer Data"), go to the Camera Sorting Layer Texture section and choose a Sorting Layer. This sorting layer is when the snapshot of the current screen colors is taken. Create a Sorting Layer that's AFTER the colors you want to distort. The SpriteRenderer with your distortion material needs to have a Sorting Layer AFTER that snapshot or else you'll get the last frame's colors and it will be very weird.

    So your scene will render in this order:
    [some colors from background sprites]
    [take snapshot of those colors @ Sorting Layer 2]
    [distort that snapshot of colors @ Sorting Layer 3]
    [anything else that renders after Sorting Layer 3]

    For a working demo, check out the CameraSortingLayerTexture scene and folder here: https://github.com/Unity-Technologies/2d-renderer-samples/tree/21.1
     
    noofbahabri and oscurekid like this.
  46. oscurekid

    oscurekid

    Joined:
    Mar 6, 2019
    Posts:
    3
    Hi @Lo-renzo First of all, thanks for the reply.
    The problem is: My 2D Render Data doesn't have that property upload_2023-1-4_10-21-4.png

    Already implemented your (A) But i cannot continue from there, can you point me in the right direction? Many thanks i truly appreciate the response ^^

    Edit: Downloaded the example you pinned, copied the files into my project and change my renderer asset for yours, but is not working yet,
    I would like to talk with you if possible, maybe i'm missing something. here's my discord tag:
    Elven#0755
     
    Last edited: Jan 4, 2023
  47. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,511
    Aha, well that answers it. You must upgrade to a newer version of Unity. You need at least version 2021.1, but at this point go to 2021.3 LTS.
     
  48. oscurekid

    oscurekid

    Joined:
    Mar 6, 2019
    Posts:
    3
    @Lo-renzo Many thanks! I was lost but thanks to you managed to have it working rn ^^
     
  49. rubendariohh

    rubendariohh

    Joined:
    Oct 14, 2017
    Posts:
    32
    Hi @Chris_Chu . Is there a way to implement this silhouette effect behind objects with a 2D Renderer Data?

    I tried following the same steps as this guide but using the CameraSortingLayersTexture you mentioned instead of the Opaque Layer Mask Filtering of the Forward Renderer, but is not working.

    I Just want to know if there is any way to achieve this same silhouette effect behind objects using the 2D Renderer. Thank you!

    https://docs.unity3d.com/Packages/c...ures/how-to-custom-effect-render-objects.html
     
  50. Chris_Chu

    Chris_Chu

    Unity Technologies

    Joined:
    Apr 19, 2018
    Posts:
    257
    This doesn't work as depth testing isn't supported. The best way to get this working would probably be to write your own shader. But if you don't mind a sort of hacky way of doing this you can use sprite mask.

    To do this you need two sprites one is your main sprite, the other your silhoutte sprite. The silhoutte should be behind the main sprite (in sprite order) covering it. For your main sprite set the mask interaction to "Visible Outside Mask". You will need to also add a sprite mask using the main sprite, with a custom range starting beginning in front of your main sprite, and the ending after where your occulders are. For each of your occluders sprite, set their mask interaction to "Visible Outside Mask", then create a sprite mask using your occluding sprite and set the custom range to include main sprite.

    Attached is an example of this in action.
     

    Attached Files:

    Last edited: Feb 6, 2023