Search Unity

Make object and everything behind it invisible

Discussion in 'Shaders' started by MrHongkong, Jan 21, 2020.

  1. MrHongkong

    MrHongkong

    Joined:
    Oct 26, 2018
    Posts:
    4
    I want to make an object invivisble and that blocks vision for things behind it.
    The purpose for this is for AR.
    For example i want to place eyes that follow the user on a statue. When i am behind the statue i dont want the eyes to be visible through the statue.
    I am quite new to shaders and wonder how this can be done.
    Thank you for any advice or tips
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    You want a depth only shader.
    Code (csharp):
    1. Shader "Custom/Depth Only"
    2. {
    3.   SubShader {
    4.     Pass {
    5.       ColorMask 0
    6.       ZWrite On
    7.       Cull Off
    8.       CGPROGRAM
    9.       #pragma vertex vert
    10.       #pragma fragment frag
    11.       #include "UnityCG.cginc"
    12.  
    13.       float4 vert (float4 vertex : POSITION) : SV_POSITION { return UnityObjectToClipPos(vertex); }
    14.       void frag() {}
    15.       ENDCG
    16.     }
    17.   }
    18. }
     
  3. MrHongkong

    MrHongkong

    Joined:
    Oct 26, 2018
    Posts:
    4
    I hadnt had a chance to test it until to day. It worked somewhat.
    It doesnt block everything. It seems to work while in scene but not when i build it to my phone.

    In the picture there is a cube infront and behind the object with the shader.
     

    Attached Files:

  4. MrHongkong

    MrHongkong

    Joined:
    Oct 26, 2018
    Posts:
    4
    NeverMind I got it working.
    I changed the render queue and then it worked. (Pic) 1
    How it looks (pic2)
    Thank you very much for your help Bgolus.
     

    Attached Files:

  5. LiakhimAsenhold

    LiakhimAsenhold

    Joined:
    Nov 24, 2018
    Posts:
    1
    Hey, I hope you guys might help me, Im using this shader, but in the build, Where it should be transparent Im seing all black, any ideas? Thanks in advance