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

Rendering object that is partially obscured by another

Discussion in 'General Graphics' started by Vladislav-Videnov, Jun 5, 2019.

  1. Vladislav-Videnov

    Vladislav-Videnov

    Joined:
    Nov 19, 2015
    Posts:
    18
    Hello guys.

    I work on a VR object and i am making object handles for moving, rotating and scaling.

    Currenty i am using two cameras and i am rendering the object handles always on TOP for easier use (if object is scaled way too big and obscures the handles, for example)

    I want to make the object handle partially transparent when is obscured by the object ( look at example image)

    Is there any easy way to do this? as the project time i have left is not so much i cant spent too much time writing shaders ? Is there any shaders free to use to achieve this effect or any other approach that is achievable within few hours ?

    thanks !
     

    Attached Files:

  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    You would have to write custom shaders for this. Easiest option is to use a two pass shader.

    First pass would render normally in the scene using ZTest LEqual instead of ZTest Always or using a second camera.
    Second pass would use ZTest Greater and a manually reduced alpha.
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Code (CSharp):
    1. Shader "Unlit/Partially Obscured Gizmo" {
    2.     Properties {
    3.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    4.         _ObscuredAlpha ("Obscured Alpha", Range (0,1)) = 0.05
    5.     }
    6.  
    7.     SubShader {
    8.         Tags {"Queue"="Transparent+999" "IgnoreProjector"="True" "RenderType"="Transparent"}
    9.         LOD 100
    10.  
    11.         ZWrite Off
    12.         Cull Off
    13.         Blend SrcAlpha OneMinusSrcAlpha
    14.  
    15.         CGINCLUDE
    16.         #pragma vertex vert
    17.         #pragma fragment frag
    18.         #pragma target 2.0
    19.  
    20.         #include "UnityCG.cginc"
    21.  
    22.         struct appdata_t {
    23.             float4 vertex : POSITION;
    24.             float2 texcoord : TEXCOORD0;
    25.             UNITY_VERTEX_INPUT_INSTANCE_ID
    26.         };
    27.  
    28.         struct v2f {
    29.             float4 pos : SV_POSITION;
    30.             float2 uv : TEXCOORD0;
    31.         };
    32.  
    33.         sampler2D _MainTex;
    34.         float4 _MainTex_ST;
    35.  
    36.         v2f vert (appdata_t v)
    37.         {
    38.             v2f o;
    39.             UNITY_SETUP_INSTANCE_ID(v);
    40.  
    41.             o.pos = UnityObjectToClipPos(v.vertex);
    42.             o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    43.            
    44.             return o;
    45.         }
    46.         ENDCG
    47.  
    48.         // Draw the unoccluded gizmo
    49.         Pass {
    50.             // ZTest LEqual // This is the default
    51.  
    52.             CGPROGRAM
    53.             fixed4 frag (v2f i) : SV_Target
    54.             {
    55.                 fixed4 col = tex2D(_MainTex, i.uv);
    56.                 return col;
    57.             }
    58.             ENDCG
    59.         }
    60.  
    61.         // Draw the occluded gizmo
    62.         Pass {
    63.             ZTest Greater
    64.  
    65.             CGPROGRAM
    66.             fixed _ObscuredAlpha;
    67.  
    68.             fixed4 frag (v2f i) : SV_Target
    69.             {
    70.                 fixed4 col = tex2D(_MainTex, i.uv);
    71.                 col.a *= _ObscuredAlpha;
    72.                 return col;
    73.             }
    74.             ENDCG
    75.         }
    76.     }
    77. }