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. Dismiss Notice

Question Adding pass to a specific camera [URP]

Discussion in 'Universal Render Pipeline' started by AwesomeAlexx, Feb 5, 2021.

  1. AwesomeAlexx

    AwesomeAlexx

    Joined:
    Jan 30, 2016
    Posts:
    18
    Hi,

    I'm working on Unity 2020.2.2f1 and I'm trying to add pass for a specific camera (because I need each cam to have their own distortion map).

    I'm facing weird behaviour, I tried many things and here's the result :



    As you can see the blitcmd isn't a part of the camera's passes, and more it's excecuted before the camera I'd like to use that cmd.

    Here's my code (attached to Camera (1) this one with depth = 1):
    Code (CSharp):
    1.  
    2.         public Texture2D blendMap;
    3.         public RenderTexture tempRT;
    4.         public Shader blendShader;
    5.  
    6.         Material blendMat;
    7.  
    8.         private void Awake()
    9.         {
    10.             blendMat = new Material(blendShader);
    11.             blendMat.SetTexture("_BlendTex", blendMap);
    12.         }
    13.  
    14.         private void OnEnable()
    15.         {
    16.             RenderPipelineManager.beginCameraRendering += PreRenderOperations;
    17.             RenderPipelineManager.endCameraRendering += PostRenderOperations;
    18.         }
    19.  
    20.         private void OnDisable()
    21.         {
    22.             RenderPipelineManager.beginCameraRendering -= PreRenderOperations;
    23.             RenderPipelineManager.endCameraRendering -= PostRenderOperations;
    24.         }
    25.  
    26.         void PreRenderOperations(ScriptableRenderContext context, Camera camera)
    27.         {
    28.             camera.targetTexture = tempRT;
    29.         }
    30.  
    31.         CommandBuffer cmd;
    32.         void PostRenderOperations(ScriptableRenderContext context, Camera camera)
    33.         {
    34.             camera.targetTexture = null;
    35.             //cmd = CommandBufferPool.Get(nameof(UnityEngine.Rendering.Universal.Internal.FinalBlitPass));
    36.             cmd = new CommandBuffer();
    37.             cmd.name = "Blit cmd";
    38.             Debug.Log("hi here cam " + gameObject.name + " I got a depth of " + camera.depth);
    39.             cmd.Blit(tempRT, camera.targetTexture, blendMat);
    40.             context.ExecuteCommandBuffer(cmd);
    41.             CommandBufferPool.Release(cmd);
    42.             cmd.Clear();
    43.         }

    And this is the shader :
    Code (CSharp):
    1. {
    2.         Properties
    3.         {
    4.             _MainTex("Main", 2D) = "white" {}
    5.             _BlendTex("Blend", 2D) = "white" {}
    6.         }
    7.         SubShader
    8.         {
    9.             // No culling or depth
    10.             Cull Off ZWrite Off ZTest Always
    11.  
    12.             Pass
    13.             {
    14.                 CGPROGRAM
    15.                 #pragma vertex vert
    16.                 #pragma fragment frag
    17.  
    18.                 #include "UnityCG.cginc"
    19.  
    20.                 struct appdata
    21.                 {
    22.                     float4 vertex : POSITION;
    23.                     float2 uv : TEXCOORD0;
    24.                 };
    25.  
    26.                 struct v2f
    27.                 {
    28.                     float2 uv : TEXCOORD0;
    29.                     float4 vertex : SV_POSITION;
    30.                 };
    31.  
    32.                 v2f vert (appdata v)
    33.                 {
    34.                     v2f o;
    35.                     o.vertex = UnityObjectToClipPos(v.vertex);
    36.                     o.uv = v.uv;
    37.                     return o;
    38.                 }
    39.  
    40.                 sampler2D _MainTex;
    41.                 sampler2D _BlendTex;
    42.  
    43.                 fixed4 frag (v2f i) : SV_Target
    44.                 {
    45.                     fixed4 blend = tex2D(_BlendTex, i.uv);
    46.                     fixed4 main = tex2D(_MainTex, i.uv);
    47.  
    48.                     if (blend.r == 0 && blend.g == 0 && blend.b == 0)
    49.                         discard;
    50.  
    51.                     main *= blend;
    52.  
    53.                     return main;
    54.                 }
    55.                 ENDCG
    56.             }
    57.         }
    58.     }
     
  2. AwesomeAlexx

    AwesomeAlexx

    Joined:
    Jan 30, 2016
    Posts:
    18
    Hi,
    Nobody have any clue on that ?
    Am I the only trying to distord image aftereverything ?

    I have made a specific pass for all camera, now my issue is a 'finalblit' is made after my pass.
    Using URP, I tried to change that 'finalblit' pass but each time on recompile it was reverted saying something as changed inside URP... that's exactly what I want :)

    Does anyone know what to do ? (creating my own pipeline isn't an option, I'm not clever enought :) )