Unity version: 2019.4.16
Create a shader with the following code:
Shader "ChuckLee/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"
}
Add the shader to the shader, and give the shader to the transparent object, that is, the floor plane
Set the camera ClearFlags to SolidColor mode
The final effect is as follows:
The URP version is:
Shader "ChuckLee/ARShadowURP" {
Properties {
_ShadowColor("Shadow Color", Color) = (0.1, 0.1, 0.1, 0.53)
}
SubShader {
Tags {"Queue"="Transparent" "RenderType"="Transparent"}
Pass {
Stencil {
Ref 1
Comp always
Pass replace
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
};
struct v2f {
float4 pos : SV_POSITION;
float4 shadowCoord : TEXCOORD0;
};
float4 _ShadowColor;
v2f vert (appdata v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.shadowCoord = ComputeShadowCoord(o.pos);
return o;
}
float4 frag (v2f i) : SV_Target {
float atten = SampleShadowmap(i.shadowCoord);
return _ShadowColor * atten;
}
ENDCG
}
}
FallBack "Diffuse"
}
HDRP version:
Shader "ChuckLee/ARShadowHDRP" {
Properties {
_ShadowColor("Shadow Color", Color) = (0.1, 0.1, 0.1, 0.53)
}
SubShader {
Tags {"Queue"="Transparent" "RenderType"="Transparent"}
Pass {
Stencil {
Ref 1
Comp always
Pass replace
}
HLSLPROGRAM
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/Lighting.hlsl"
struct appdata {
float4 vertex : POSITION;
};
struct v2f {
float4 pos : SV_POSITION;
float4 shadowCoord : TEXCOORD0;
};
float4 _ShadowColor;
v2f vert (appdata v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.shadowCoord = ComputeShadowCoord(o.pos);
return o;
}
float4 frag (v2f i) : SV_Target {
float atten = HDShadowValue(i.shadowCoord);
return _ShadowColor * atten;
}
ENDHLSL
}
}
FallBack "Diffuse"
}