Unity 实现AR 过程中的阴影与浮现效果

版权声明:他人成果,为我所用。留下足迹,美名杰成。 https://blog.csdn.net/weixin_41814169/article/details/87968079

添加阴影

在 Unity 中我们通常给物体添加阴影来增加真实感。

但是我们直接这么做的话会在画面中也看到一个白色的平面。所以我们需要一个透明的平面,但能够接收阴影。为了达到这个目的,我们需要一个自定义的着色器。

Shader "FX/Matte Shadow" {
 
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}
 
SubShader {
 
    Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    LOD 200
    Blend Zero SrcColor
 
CGPROGRAM
 
#pragma surface surf ShadowOnly alphatest:_Cutoff
 
fixed4 _Color;
 
struct Input {
    float2 uv_MainTex;
};
 
inline fixed4 LightingShadowOnly (SurfaceOutput s, fixed3 lightDir, fixed atten)
{
    fixed4 c;
    c.rgb = s.Albedo*atten;
    c.a = s.Alpha;
 
    return c;
}
 
void surf (Input IN, inout SurfaceOutput o) 
{
 
    fixed4 c = _Color; 
    o.Albedo = c.rgb;
    o.Alpha = 1;
 
}
 
ENDCG
 
}
 
Fallback "Transparent/Cutout/VertexLit"
 
}

使用 Matte Shadow 制作材质球,再将它拖拽到我们的平面上,就得到了我们期望的效果。注意如果没看到阴影的话,很可能是 Scale 或者 Project Quality 设置的问题。

从识别对象中浮现

要实现这个效果,我们需要使用 [Depth Shader](http://pan.baidu.com/s/1fLnFC)。原理是我们将使用一个透明但看不到内部的盒子将低于物料的物体隐藏起来。

在透明盒子上加上 DepthMask.shader,将 SetRenderQueue.cs 加在需要影藏的物体上。最终我们获得视频中的效果。

猜你喜欢

转载自blog.csdn.net/weixin_41814169/article/details/87968079