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.

Feature Request Will Camera Depth Normal Textures be added fto urp?

Discussion in 'Universal Render Pipeline' started by RyanButlerWork, Jul 2, 2020.

  1. RyanButlerWork

    RyanButlerWork

    Joined:
    Aug 11, 2017
    Posts:
    1
    I'm curious if the built in pipeline's Camera Normals Texture feature will be added later to the universal render pipeline. I'm mostly using it for a Sobel filter but I know its also useful for an number of other post processing effects.

    I found an solution for a renderer feature that adds one here https://alexanderameye.github.io/outlineshader ,
    But Going through the trouble of setting up a global texture using a shader in hidden/internal seems like an awkward solution to a problem that should probably have built in solution considering how useful Camera normal textures seem to be.

    Also if any one had a more elegant solution for retrieving the normals of a pixel on camera I'd really appreciate it
     
    dccoo likes this.
  2. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    424
    Still, there are so many missing features in URP.
     
    Kaidash likes this.
  3. Elvar_Orn

    Elvar_Orn

    Unity Technologies

    Joined:
    Dec 9, 2019
    Posts:
    150
    Normals got added with SSAO to 10.x.

    If you have a renderer feature you can call
    ConfigureInput(ScriptableRenderPassInput.Normal);


    And it will generate a prepass providing you with the texture. And then in your shader have this include:
    #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareNormalsTexture.hlsl"


    that will give you access to
    SampleSceneNormals(float2 uv)
    LoadSceneNormals(uint2 uv)
     
  4. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    424
    Good news! Thank you!
     
  5. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    5,790
    forward only uh, does it cost 2x drawcall like in built in?
     
  6. Fuduin

    Fuduin

    Joined:
    Feb 7, 2020
    Posts:
    7
    @Elvar_Orn sorry for the dumb question, but how to use the normal texture in custom shader without renderer feature?

    In documentation says
    I looked at sources of urp 10.x but unfortunately, I'm not so good to understand how to make it works similar to _CameraDepthTexture.

    UOD: I tried to add pass like in Lit.shader but it doesn't change anything.
     
  7. battou

    battou

    Joined:
    Jan 25, 2011
    Posts:
    194
    How do you use it without scripts? I have custom shader, I added "DepthNormals" pass from Lit to my custom shader. Then in another shader I sample from _CameraNormalsTexture using SampleSceneNormals, but I allways get empty data, no sceen normals (red color). Also when I use Lit shader on objects I still cant get anithing from SampleSceneNormals. Am I should enable it somewhere?
     
  8. battou

    battou

    Joined:
    Jan 25, 2011
    Posts:
    194
    So. IF I add SSAO then it works. How do I enable render to _CameraNormalsTexture without adding SSAO to renderer?
     
    cgDoofus and tomekkie2 like this.
  9. fleity

    fleity

    Joined:
    Oct 13, 2015
    Posts:
    234
    I am very happy that I found this thread. I needed access to the CameraNormalsTexture for the normal direction masking of some amplify decal projectors. I duplicated the SSAO renderer feature and stripped everything away except for the ConfigureInput line which results in a renderer feature that triggers rendering to the CameraNormalsTexture and absolutely nothing else (and does not add more draw calls or sth similar if SSAO is used).

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.Rendering;
    4. using UnityEngine.Rendering.Universal;
    5.  
    6.  
    7. [DisallowMultipleRendererFeature]
    8. [Tooltip("The Scene Normals pass enables rendering to the CameraNormalsTexture if no other pass does it already.")]
    9. internal class SceneNormals : ScriptableRendererFeature
    10. {
    11.     private SceneNormalsPass m_SceneNormalsPass = null;
    12.  
    13.     public override void Create()
    14.     {
    15.         if (m_SceneNormalsPass == null)
    16.         {
    17.             m_SceneNormalsPass = new SceneNormalsPass();
    18.         }
    19.     }
    20.  
    21.     public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    22.     {
    23.         m_SceneNormalsPass.Setup();
    24.         renderer.EnqueuePass(m_SceneNormalsPass);
    25.     }
    26.  
    27.  
    28.     // The Scene Normals Pass
    29.     private class SceneNormalsPass : ScriptableRenderPass
    30.     {
    31.         public void Setup()
    32.         {
    33.             ConfigureInput(ScriptableRenderPassInput.Normal); // all of this to just call this one line
    34.             return;
    35.         }
    36.  
    37.         public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) { }
    38.     }
    39. }
    40.  
     
  10. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,373
    Thanks!

    One issue I'm having is that when I use this, terrain has issues with the depth buffer. This is how it should look like (water shader accesses depth buffer to render foam)

    upload_2022-5-8_15-3-15.png

    But with that feature added, the foam is gone. Would you have any idea why this happens? (only happens for terrain)
     
  11. fleity

    fleity

    Joined:
    Oct 13, 2015
    Posts:
    234
    @alexanderameye

    So I tried figuring that out but I think I really only have like a semi 80% ish good answer unfortunately.
    I made that script based on the urp version in unity 2021.2 and I believe the depth normals texture is not really considered production ready outside of the ssao effect and maybe decals (which in that version were not final either).

    I rewrote the following part 3 times now because everytime I thought "this is the reason" I realized it was something else.
    Good news it does work. Bad news it's a bit annoying.

    As I noticed that the Stylized Water Asset does not support 2021 yet I hence checked this in 2020.3.34f1 in the "Water Demo" scene. First of all _CameraDepthTexture seems to be renamed to _CameraDepthAttachment in 2021.2, fine.
    But switchting between Depth and DepthNormals as source for SSAO either renders a DepthPrePass or has no specific draw calls for depth normals at all... at first... until I enabled shadows on the Directional Light once and set the Floor Material to URP/Lit (instead of Unlit), since then the DepthNormalsPrePass gets executed correctly and foam and other depth effects show up.
    Screenshot 2022-05-09 172412.jpg Screenshot 2022-05-09 172441.jpg
    The shadow part is probably irrelevant what isn't is that Unlit shaders don't have a depthNormals pass and since no shader has one the draw calls don't show up at all. So if you want to use the depthNormals Texture all shaders that would write to it have to support it as well, there is no fallback to depthOnly.
    Screenshot 2022-05-09 172537.jpg Screenshot 2022-05-09 172551.jpg

    This part in implementation details led me somewhat in the right direction: https://docs.unity3d.com/Packages/c...t-processing-ssao.html#implementation-details

    Random side note: DepthNormals texture is not getting MSAA in 2021.2 if you don't have Depth Priming enabled but that is by default on in lower versions it seems.

    By way huge fan of the asset, very nice work.
     
  12. funkyCoty

    funkyCoty

    Joined:
    May 22, 2018
    Posts:
    613
    These people renaming things in urp need to be dealt with. It's so exhausting tracking stuff like this down!
     
    Euri and hippocoder like this.
  13. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    657
    So what does Sample URP Buffer node do in ShaderGraph do we have to do anything for that to work ? Theres no docmentation on it at all...
     
  14. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    657

    How do we use this in shader graph so we can sample scene normals ? Been trying to work that out for ages...
     
  15. fleity

    fleity

    Joined:
    Oct 13, 2015
    Posts:
    234
    You add the renderer feature and then in your shader graph you need to sample the globally declared _CameraNormalsTexture (the name and reference needs to match this exactly).
    Edit: Oh an be aware that the _CameraNormalsTexture contains worldspace normals, you might need to transform those into object / tangent space depending on what you want to do.
    upload_2022-12-9_10-29-7.png

    upload_2022-12-9_10-31-50.png
     
  16. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    657
    Omg thank you so much, spent all day reading the documentation and couldn't figure out how to do any of this.

    I must be missing something because everyone else seems to know what they are doing except me lol
     
  17. fleity

    fleity

    Joined:
    Oct 13, 2015
    Posts:
    234
    ehhh I know the feeling very well, hits me every day too. I guess thats normal if you work on something you haven't done exactly like this before.

    The camera normals texture stuff behaves very similarly like you would access this in the built in pipeline, having some knowledge from when I mainly used that helps. Although I always expect SRP to do everything differently.
     
  18. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    657
    I never really touched built in sadly.

    But why shader graph has a scene depth node but no scene normal node, and in order to enable scene normals we have to write C#. This stuff should just be in there for us so we don't have to write boiler plate code just to use a texture. And I would've never figured out the C# code I would have had to write to enable depth normals either. It was lucky I found this thread.
     
  19. fleity

    fleity

    Joined:
    Oct 13, 2015
    Posts:
    234
    I agree in so far that generating the camera normals texture should be a toggle in the urp asset, same as depth texture.
    They probably wanted to omit a normals texture from urp in the beginning (because creating it somewhat expensive because you render every object again to a separate buffer). Afaik it appeared first with the SSAO effect which absolutely needed it.

    Edit: Uhh just found your post about the undocumented sample buffer node. If it works that obviously is an option as well (but still needs to renderer feature to populate the buffer first)
     
    Last edited: Dec 9, 2022