Search Unity

Oclussion Gameobject

Discussion in 'AR/VR (XR) Discussion' started by RJCastro, Sep 10, 2018.

  1. RJCastro


    Jul 29, 2018
    Good morning everyone.

    I am making an AR application with Hololens and I have created a rectangular gameobject (cube) of little thickness and I need to assign a material that is transparent and that also conceals what I have below it.

    Project things on a wall and this wall has shelves, and I would like if I look from above the shelf, do not see what I have below, but that if I look at it in front, if you see it. So where I theoretically have the shelf, I will place this material.

    I'm using Hololens with vuforia (Updated to date) and Unity 2018.2.1f1

    Does anyone think of how to create this material?
  2. StickyHoneybuns


    Jan 16, 2018
    I have no experience with Vuforia or Hololens development. However, I'm thinking you need more than just a special material. I think what you need to do is change the transparency of the object during runtime based on the players position in comparison with the cube. You may be able to do this with a shader as well but I know very little about shaders. This doesn't sound AR/VR specific so I would try on another forum and you might have better luck. Try the shader forums here:
  3. BrandonFogerty


    Unity Technologies

    Jan 29, 2016
    Hi @RJCastro!

    You should consider using the built-in occlusion shader which is called "VR/SpatialMapping/Occlusion".
    To use it, create a new material and assign the VR->SpatialMapping->Occlusion shader to it. Then place the material on a game object. The shader works by writing to the depth buffer while disabling writes the color buffer to increase performance. It is used by the spatial mapping components used with HoloLens but it isn't specific to the HoloLens.

    The following is the source for the shader code if you would like to make modifications.
    Code (CSharp):
    1. Shader "VR/SpatialMapping/Occlusion"
    2. {
    3.     SubShader
    4.     {
    5.         // Render the Occlusion shader before all
    6.         // opaque geometry to prime the depth buffer.
    7.         Tags { "Queue"="Geometry-1" }
    9.         ZWrite On
    10.         ZTest LEqual
    11.         ColorMask 0
    13.         Pass
    14.         {
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    19.             #include "UnityCG.cginc"
    21.             struct appdata
    22.             {
    23.                 float4 vertex : POSITION;
    24.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    25.             };
    27.             struct v2f
    28.             {
    29.                 float4 position : SV_POSITION;
    30.                 UNITY_VERTEX_OUTPUT_STEREO
    31.             };
    33.             v2f vert (appdata input)
    34.             {
    35.                 v2f output;
    36.                 UNITY_SETUP_INSTANCE_ID(input);
    37.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
    39.                 output.position = UnityObjectToClipPos(input.vertex);
    40.                 return output;
    41.             }
    43.             fixed4 frag (v2f input) : SV_Target
    44.             {
    45.                 return fixed4(0.0, 0.0, 0.0, 0.0);
    46.             }
    47.             ENDCG
    48.         }
    49.     }
    50. }
    linojon and StickyHoneybuns like this.