Search Unity

Shader drawing over everything

Discussion in 'Shaders' started by jazzbach, Jan 20, 2021.

  1. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    41
    Hello !

    I'm working on a VR application that calls some async methods and, while awaiting for it, I'd like to have a "loading" effect that overdraws every object in the scene. For example, if I have some cubes and a skybox on the scene, I'd like this "loading" effect to overdraw everything (always draw in front of these cubes and skybox).

    I was trying by changing the Render Queue number but id didn't work.

    Do you know if there's a way to do it ? Thanks beforehand
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
  3. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    41
    Hi Invertex. Thanks a lot for your response. I'll try it and I'll report back my results !
     
  4. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    41
    Hi Invertex. I'm checking the documentation you provided but I noticed that it refers specifically to sprites. Does "Render Sorting Order" work as well with non sprite objects (3D objects) ?
     
  5. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Yes, there is a render order value that can only be access from code on regular renderers as well.
    But regardless the important part here is using a shader that doesn't do ZTest. Here's an example:
    Code (CSharp):
    1. Shader "Unlit/RenderOnTop"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Transparent" "Queue"="Overlay+50" }
    10.         LOD 100
    11.  
    12.         Pass
    13.         {
    14.             ZTest Off
    15.             ZWrite Off
    16.             Blend One OneMinusSrcAlpha
    17.  
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.  
    22.             #include "UnityCG.cginc"
    23.  
    24.             struct appdata
    25.             {
    26.                 float4 vertex : POSITION;
    27.                 float2 uv : TEXCOORD0;
    28.             };
    29.  
    30.             struct v2f
    31.             {
    32.                 float2 uv : TEXCOORD0;
    33.                 float4 vertex : SV_POSITION;
    34.             };
    35.  
    36.             sampler2D _MainTex;
    37.             float4 _MainTex_ST;
    38.  
    39.             v2f vert (appdata v)
    40.             {
    41.                 v2f o;
    42.                 o.vertex = UnityObjectToClipPos(v.vertex);
    43.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    44.                 return o;
    45.             }
    46.  
    47.             fixed4 frag (v2f i) : SV_Target { return tex2D(_MainTex, i.uv); }
    48.             ENDCG
    49.         }
    50.     }
    51. }
     
    Last edited: Jan 21, 2021
  6. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    41
    Ohh, that's perfect. Thanks a lot. I'll implement it tonight and report back my findings
     
  7. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    41
    Ohh, that's perfect. Thanks a lot. I'll implement it tonight and report back my findings

     
  8. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    I see you quoted it before I added the Blend line, so just a heads up in case, don't want to forget that
    Blend One OneMinusSrcAlpha
    line if you want transparency since you mentioned it's an image you're displaying.
    (And if you're using an actual Text component, that will require a slightly different shader)
     
  9. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    41
    Thanks for the feedback again.

    This is how the shader looks at the end. Is very simple but it achieves the goal I needed :) :

    Code (CSharp):
    1. Shader "JazzBACH/OverdrawShader"{
    2.  
    3.     Properties{
    4.         _Tex("Texture", 2D) = "white" {}
    5.     }
    6.    
    7.     SubShader{
    8.        
    9.         Ztest Always
    10.         Blend SrcAlpha OneMinusSrcAlpha
    11.        
    12.         pass{
    13.             CGPROGRAM
    14.             #pragma vertex vert
    15.             #pragma fragment frag
    16.             #include "UnityCG.cginc"
    17.  
    18.             struct input{
    19.                 float4 vertex : POSITION;
    20.                 float4 texcoord : TEXCOORD0;
    21.             };
    22.  
    23.             struct output{
    24.                 float4 vertex : SV_POSITION;
    25.                 float4 texcoord : TEXCOORD0;
    26.             };
    27.  
    28.             uniform sampler2D _Tex;
    29.  
    30.             // ------------------------------------------------------OWN METHODS
    31.            
    32.             fixed4 _GetTexture(output o){
    33.                 return tex2D(_Tex, o.texcoord);
    34.             }
    35.  
    36.             // ---------------------------------------------------SHADER METHODS
    37.            
    38.             output vert(input i){
    39.                 output o;
    40.                 o.vertex = UnityObjectToClipPos(i.vertex);
    41.                 o.texcoord = i.texcoord;
    42.  
    43.                 return o;
    44.             }
    45.  
    46.             fixed4 frag(output o) : SV_TARGET{
    47.                 //return _GetTexture(o);
    48.                 return _GetTexture(o);
    49.             }
    50.             ENDCG
    51.         }
    52.     }
    53. }
    54.