1
votes

I'm building a Unity AR Foundation project, and need my objects to appear from below the floor level. I need to place a floor plane that will render shadows, but more importantly not render a surface material, and should prevent objects beneath it from rendering at all.

I have played with some Depth Mask Shaders but none of them have worked, and are just rendering as a big black hole.

Has anyone achieved this? It must be a common thing for Augmented Reality projects.

1

1 Answers

0
votes

I found two Shaders. One for making shadows on an invisible plane, and the other for making an invisible plane occlude whatever is underneath it.

The shadow Shader is here:

Shader "AR/ARShadow"
{
    Properties
    {
        _ShadowColor("Shadow Color", Color) = (0.1, 0.1, 0.1, 0.53)
    }
        SubShader
    {
        Tags{ "RenderType" = "Transparent" "Queue" = "Geometry+1" }
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
    {
        Tags{ "LightMode" = "ForwardBase" }

        CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase

#include "UnityCG.cginc"
#include "AutoLight.cginc" 

        struct appdata
    {
        float4 vertex : POSITION;
    };

    struct v2f
    {
        float4 pos : SV_POSITION;
        SHADOW_COORDS(2)
    };

    fixed4 _ShadowColor;

    v2f vert(appdata v)
    {
        v2f o;
        o.pos = UnityObjectToClipPos(v.vertex);
        TRANSFER_SHADOW(o);
        return o;
    }

    fixed4 frag(v2f i) : SV_Target
    {
        fixed atten = SHADOW_ATTENUATION(i);
    return fixed4(_ShadowColor.rgb,saturate(1 - atten)*_ShadowColor.a);
    }
        ENDCG
    }
    }
        FallBack "Diffuse"
}

and here's the Occlusion shader:

Shader "AR/Occlusion"
{
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        Tags { "Queue" = "Geometry-1" }
        ZWrite On
        ZTest LEqual
        ColorMask 0

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                return fixed4(0.0, 0.0, 0.0, 0.0);
            }
            ENDCG
        }
    }
}

All I need to do now is figure out how to merge the two.

:-)