unity 在透明物体上投射阴影

unity版本:2019.4.16

创建一个shader,代码如下:

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"
}

将shader添加到材质球,将材质球给到被透明的物体,即地板plane

将相机ClearFlags设置为SolidColor模式即可

最终效果如下:

URP版本为:

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版本:

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"
}
 

猜你喜欢

转载自blog.csdn.net/Abel02/article/details/112286475