4

URP打开半透明阴影(一百二十七)

 2 years ago
source link: https://www.xuanyusong.com/archives/4868
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

URP打开半透明阴影(一百二十七)

在URP下默认只有不透明才会有阴影,其实可以使用alpha test给shadowmap上画阴影, 物体本身则使用alpha blend这样也更加合理。

首先拷贝一下默认的Lit.shader,在Properties中添加一个Toggle变量来控制是否开启半透明阴影。

    Properties
        //....
        [Toggle]_TShadow("TShadow", Float) = 0
    //....
    CustomEditor "MyLitShader"

在写一个shader面板,继承默认的BaseShaderGUI

    private MaterialProperty tShadow;
    public override void FindProperties(MaterialProperty[] properties)
        base.FindProperties(properties);
        litProperties = new LitGUI.LitProperties(properties);
        tShadow = BaseShaderGUI.FindProperty("_TShadow", properties, false);

根据Toggle决定是否启动半透明阴影

    public override void DrawSurfaceInputs(Material material)
        base.DrawSurfaceInputs(material);
        LitGUI.Inputs(litProperties, materialEditor, material);
        DrawEmissionProperties(material, true);
        DrawTileOffset(materialEditor, baseMapProp);
        //绘制半透明阴影
        SurfaceType surfaceType = (SurfaceType)material.GetFloat("_Surface");
        if(surfaceType == SurfaceType.Transparent)
            materialEditor.ShaderProperty(tShadow, "半透明阴影");
            material.SetShaderPassEnabled("ShadowCaster", (material.GetFloat("_TShadow") == 1.0));
            material.SetShaderPassEnabled("ShadowCaster", true);

然后在面板中启动半透明阴影,如下图所示,启动半透明阴影,然后是Alpha Clipping,通过观察URP源码发现,如果不想影响到半透明渲染,只给阴影生效,也可以自己拓展一个Threshold变量在ShadowCaster中使用alpha Test,这里就关闭面板上的alpha clipping。

最终半透明物体可以产生阴影了。

URP打开半透明阴影(一百二十七) - 雨松MOMO程序研究院 - 2

作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK