• 深入URP之Shader篇2: 目录结构和Unlit Shader分析[上]


    Unity和URP版本

    我使用的Unity版本为2020.3.33f1,对应的URP和SRP Core版本为10.8.1。阅读URP源码建议把package从Library/PackageCache中拷贝到Packages目录,也就是自定义package的方式,然后推荐使用VS code打开工程,这样可以很方便的跳转代码阅读。

    URP Shader目录结构

    首先,我们看一下URP源码的目录结构,看一下Shader代码的位置:
    在这里插入图片描述

    Shader代码位于Shader目录以及ShaderLibrary目录中,先总体看一下都有哪些内容。

    在这里插入图片描述
    在这里插入图片描述

    从hlsl文件的名称中我们就可以发现很多URP的渲染管线功能了,比如渲染阴影贴图使用的ShadowCasterPass.hlsl,Z-PrePass使用的DepthOnlyPass.hlsl,以及众多我们在材质Inspector中可以选择的URP Shader:

    在这里插入图片描述

    从本篇开始,我们会逐一去学习分析这些Shader。

    SRP Core包

    另外,URP的Shader还会引用SRP Core这个包中的ShaderLibrary:

    在这里插入图片描述

    这个SRP Core提供了URP/HDRP共享的一些功能的实现,如果需要自定义一个SRP,使用这个库也可以大大简化工作。其ShaderLibrary中包含了很多基础和核心Shader函数以及宏定义。

    Unlit Shader

    本篇分析第一个shader: Unlit Shader,即URP Shader目录中的Unlit.shader。这个Shader虽然是最简单的无光照Shader,但是其结构以及用法体现了URP Shader甚至URP渲染管线的体系架构。由于这是我们分析的第一个Shader,会有很多公共的基础内容,因此篇幅较长,因此分为上下两篇。本篇中我们分析unlit shader的属性部分,重点是ShaderGUI对属性的处理。

    Properties

    先看看属性部分,Unlit Shader的属性不多,除了贴图、颜色、Alpha Cutout之外,就是混合模式相关参数,以及渲染队列偏移值。

    Properties
        {
            [MainTexture] _BaseMap("Texture", 2D) = "white" {}
            [MainColor]   _BaseColor("Color", Color) = (1, 1, 1, 1)
            _Cutoff("AlphaCutout", Range(0.0, 1.0)) = 0.5
    
            // BlendMode
            [HideInInspector] _Surface("__surface", Float) = 0.0
            [HideInInspector] _Blend("__blend", Float) = 0.0
            [HideInInspector] _AlphaClip("__clip", Float) = 0.0
            [HideInInspector] _SrcBlend("Src", Float) = 1.0
            [HideInInspector] _DstBlend("Dst", Float) = 0.0
            [HideInInspector] _ZWrite("ZWrite", Float) = 1.0
            [HideInInspector] _Cull("__cull", Float) = 2.0
    
            // Editmode props
            [HideInInspector] _QueueOffset("Queue offset", Float) = 0.0
    
            // ObsoletePropertes
            [HideInInspector] _MainTex("BaseMap", 2D) = "white" {}
            [HideInInspector] _Color("Base Color", Color) = (0.5, 0.5, 0.5, 1)
            [HideInInspector] _SampleGI("SampleGI", float) = 0.0 // needed from bakedlit
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • URP统一使用 _BaseMap 作为主贴图,_BaseColor作为主颜色。相应的,老的名称被归于ObsoletePropertes部分,在这儿还保留着是为了兼容老的材质用法。
    • [HideInInspector]表示不在Inspector中展示,这往往因为该属性是内部处理的,或者是使用了自定义的ShaderGUI展示。在这儿两种原因都有。
    • unlit shader使用的ShaderGUI代码为shader最下方定义的:CustomEditor "UnityEditor.Rendering.Universal.ShaderGUI.UnlitShader",但其实主要的工作是在基类 BaseShaderGUI中完成。需要注意的是,这个BaseShaderGUI是处理很多不同Shader的公共属性的,因此其中有些属性unlit shader并没有。

    unlit shader的相关属性的处理

    下面研究unlit shader中的属性在BaseShaderGUI中如何处理。

    _AlphaClip和 _Cutoff

    _AlphaClip是个开关,表示是否启用Alpha clipping,在_AlphaClip开启时,才可以编辑_Cutoff值,显示在Inspector上就是这样:

    在这里插入图片描述

    SetupMaterialBlendMode函数中,可以看到_AlphaClip的开关决定了是否启用_ALPHATEST_ON关键字,这个关键字是一个Shader Feature关键字,在shader代码中会根据是否启用这个关键字决定是否执行alpha clip操作:

                bool alphaClip = false;
                if(material.HasProperty("_AlphaClip"))
                    alphaClip = material.GetFloat("_AlphaClip") >= 0.5;
    
                if (alphaClip)
                {
                    material.EnableKeyword("_ALPHATEST_ON");
                }
                else
                {
                    material.DisableKeyword("_ALPHATEST_ON");
                }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    _Surface, _QueueOffset 以及 Blend

    _Surface属性决定材质是透明材质还是不透明材质,如果是透明材质,就会启用Blend相关属性的编辑:

    DoPopup(Styles.surfaceType, surfaceTypeProp, Enum.GetNames(typeof(SurfaceType)));
    if ((SurfaceType)material.GetFloat("_Surface") == SurfaceType.Transparent)
        DoPopup(Styles.blendingMode, blendModeProp, Enum.GetNames(typeof(BlendMode)));
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    • 透明材质和不透明材质会设置不同的RenderQueue,对于不透明材质设置如下:
                    if (surfaceType == SurfaceType.Opaque)
                    {
                        if (alphaClip)
                        {
                            material.renderQueue = (int) RenderQueue.AlphaTest;
                            material.SetOverrideTag("RenderType", "TransparentCutout");
                        }
                        else
                        {
                            material.renderQueue = (int) RenderQueue.Geometry;
                            material.SetOverrideTag("RenderType", "Opaque");
                        }
    
                        material.renderQueue += material.HasProperty("_QueueOffset") ? (int) material.GetFloat("_QueueOffset") : 0;
                        material.SetInt("_SrcBlend", (int) UnityEngine.Rendering.BlendMode.One);
                        material.SetInt("_DstBlend", (int) UnityEngine.Rendering.BlendMode.Zero);
                        material.SetInt("_ZWrite", 1);
                        material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                        material.SetShaderPassEnabled("ShadowCaster", true);
                    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    不透明根据是否启用AlphaClip,设置renderQueue为AlphaTestGeometry,并且会设置相应的RenderTypeTag值。_QueueOffset用于在当前队列位置上进行偏移,这样可以单独指定这个材质的渲染顺序,利用这点我们可以让同一材质的物体排列在一起渲染有利于SRP Batcher合批。这个用法效果和直接在sub shader的QueueTags中使用+/-来偏移值是一样的,但是提供了UI的编辑。
    另外不透明材质会默认设置_SrcBlend为One,_DstBlend为Zero,以及默认开启_ZWrite。同时会禁用关键字_ALPHAPREMULTIPLY_ON,以及启用ShadowCaster这个pass来渲染阴影贴图。

    • 注意以上这些操作都是编辑器操作,相应的材质会被修改设置,然后序列化保存,游戏运行时反序列化材质获取这些设置。
    • 下面看一下透明材质的处理:
                        BlendMode blendMode = (BlendMode) material.GetFloat("_Blend");
    
                        // Specific Transparent Mode Settings
                        switch (blendMode)
                        {
                            case BlendMode.Alpha:
                                material.SetInt("_SrcBlend", (int) UnityEngine.Rendering.BlendMode.SrcAlpha);
                                material.SetInt("_DstBlend", (int) UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                                break;
                            case BlendMode.Premultiply:
                                material.SetInt("_SrcBlend", (int) UnityEngine.Rendering.BlendMode.One);
                                material.SetInt("_DstBlend", (int) UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                                material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
                                break;
                            case BlendMode.Additive:
                                material.SetInt("_SrcBlend", (int) UnityEngine.Rendering.BlendMode.SrcAlpha);
                                material.SetInt("_DstBlend", (int) UnityEngine.Rendering.BlendMode.One);
                                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                                break;
                            case BlendMode.Multiply:
                                material.SetInt("_SrcBlend", (int) UnityEngine.Rendering.BlendMode.DstColor);
                                material.SetInt("_DstBlend", (int) UnityEngine.Rendering.BlendMode.Zero);
                                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                                material.EnableKeyword("_ALPHAMODULATE_ON");
                                break;
                        }
    
                        // General Transparent Material Settings
                        material.SetOverrideTag("RenderType", "Transparent");
                        material.SetInt("_ZWrite", 0);
                        material.renderQueue = (int)RenderQueue.Transparent;
                        material.renderQueue += material.HasProperty("_QueueOffset") ? (int) material.GetFloat("_QueueOffset") : 0;
                        material.SetShaderPassEnabled("ShadowCaster", false);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    同样是根据不同的BlendMode设置材质的属性值,并开启或禁用相关关键字,设置材质的Tag,以及设置renderQueue,并且关闭ShadowCasterpass。

    本篇小结

    • UPR的内置shader会使用ShaderGUI对材质进行设置,ShaderGUI会设置材质的属性以及关键字等信息。
    • unlit shader材质的渲染队列是由Surface是否透明和是否开启AlphaClip共同决定的,并且使用一个queue offset可以微调材质在队列中的位置。
    • unlit shader包含了透明和不透明,以及alpha clip材质,这几种可能性通过一个shader包含了,而差别仅仅是不同的属性值和少量的关键字。这即减少了不同shader的数量,也有利于SRP Bathcer合批。
  • 相关阅读:
    文件的导出
    Java基础 对象创建流程
    西瓜播放器xgplayer设置自动播放踩坑
    vue + canvas 实现涂鸦面板
    pinia安装使用
    pytorch的使用:卷积神经网络模块
    MySQL基础入门教程(InsCode AI 创作助手)
    Java版分布式微服务云开发架构 Spring Cloud+Spring Boot+Mybatis 电子招标采购系统源码
    debian无法使用reboot 等系统命令解决
    【操作教程】TSINGSEE青犀视频平台如何将旧数据库导入到新数据库?
  • 原文地址:https://blog.csdn.net/n5/article/details/128152523