Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

need help with shader to render through GUI.

Discussion in 'Shaders' started by CoochieInspector, Jan 26, 2019.

  1. CoochieInspector

    CoochieInspector

    Joined:
    Jan 5, 2018
    Posts:
    4
    hi everyone,


    I'm trying to figure out how to make my shader not only render through walls and every asset in the scene but to also render through GUI/canvas and anything beyond that. I was only told that i had to "increase its value" somehow and the rest was on me. i figured it had something to do with the ZTest intensity if i'm right. I'm not a 100% expert on this stuff other than the most basic knowledge. is there something i need to add or modify on the shader script? Below is my shader script.



    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    4.  
    5. Shader "Unlit/ScreenTexture"
    6. {
    7.     Properties
    8.     {
    9.         _MainTex ("Main Texture", 2D) = "white" {}
    10.         _NoiseTex("Noise Texture", 2D) = "white" {}
    11.         SrcMode ("SrcMode", int) = 1
    12.         DstMode ("DstMode", int) = 1
    13.         }
    14.     SubShader
    15.     {
    16.         Tags{ "Queue" = "Transparent+55789279" "IgnoreProjector"="True" "RenderType" = "Transparent"}
    17.         Blend [SrcMode] [DstMode]
    18.         Cull Off
    19.         Lighting On
    20.         ZWrite off
    21.         ZTest always
    22.         // Grab the screen behind the object into _MainTex
    23.  
    24.  
    25.         Pass
    26.         {
    27.             CGPROGRAM
    28.             #pragma vertex vert
    29.             #pragma fragment frag
    30.             #pragma multi_compile_fog
    31.            
    32.             #include "UnityCG.cginc"
    33.  
    34.             struct appdata
    35.             {
    36.                 float4 pos : POSITION;
    37.                 float4 screenPos : TEXCOORD1;
    38.                 float2 uv : TEXCOORD0;
    39.                 float4 normal : NORMAL;
    40.             };
    41.  
    42.             struct v2f
    43.             {
    44.                 float4 screenPos : TEXCOORD1;
    45.                 float4 pos : SV_POSITION;
    46.                 float2 uv : TEXCOORD0;
    47.             };
    48.            
    49.             v2f vert (appdata v)
    50.             {
    51.                 v2f o;
    52.                 o.uv = v.uv;
    53.                 o.pos = UnityObjectToClipPos(v.pos);
    54.                 o.screenPos = ComputeScreenPos(o.pos);
    55.                 return o;
    56.             }
    57.             sampler2D _MainTex;
    58.             sampler2D _MainTex_ST;
    59.             sampler2D _NoiseTex;
    60.             fixed4 frag (v2f i) : SV_Target
    61.             {
    62.  
    63.             float3 noiseInput = tex2D(_NoiseTex, float2(_Time.x, _Time.x)).rgb;
    64.             float2 redOffset = float2(sin(_Time.x * 0), 0);
    65.             float2 greenOffset = float2(0, sin(_Time.x * -0));
    66.             float2 blueOffset = float2(sin(_Time.x * -0), 0);
    67.             float2 uvScreen = i.screenPos / i.screenPos.w;
    68.             float red = tex2D(_MainTex, uvScreen + redOffset * step(0, noiseInput.r)).r;
    69.             float green = tex2D(_MainTex, uvScreen + greenOffset * step(0, noiseInput.g)).g;
    70.             float blue = tex2D(_MainTex, uvScreen + blueOffset * step(0, noiseInput.b)).b;
    71.  
    72.             return fixed4(red, green, blue, 1);
    73.             }
    74.             ENDCG
    75.         }
    76.     }
    77. }
    78.  
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    ZTest is used for per pixel sorting against opaque surfaces. Opaque objects usually render to the z buffer / depth buffer (using ZWrite On) to record the closest surface per pixel. If an object is closer than the previously rendered object for that pixel, it is drawn and overwrites the depth value, otherwise it's skipped. Transparent objects tend to test against the depth buffer, but not write to it using ZWrite Off and ZTest LEqual (which is the default if not specified). ZTest Always means ignore all that and just render over everything rendered before. But that won't help if something renders after it.

    The Queue helps determine the order of rendering, higher queues render after lower queues. The usual valid range is between 0 and 5000, anything above that is gratuitous (and also the behaviour is undefined, so it may not render at all).

    However the Queue can only do so much. If you're using multiple cameras, or if you're using Unity's canvas system with Render Mode set to Screen Space - Overlay, then none of that matters.

    If you have the UI rendering in a second camera, then it'll always get rendered after anything in the first camera; the queue only defines the rendering order within a single camera. However you could render this particular object in that second camera instead of the first, or you could use Stencils to mark areas to mask out the UI with.

    If you're using Screen Space - Overlay then Unity also forcibly renders the UI last, and clears the stencil buffer. The only option here is to have your object be a UI element on the canvas, it's impossible to render it last. You can't even apply post processing effects to the UI in this case. The only solution is to not use Screen Space - Overlay.
     
    Rockat33r likes this.