【ShaderLab踩坑记录】【待解决 / 求助】给AlphaTest添加阴影导致的奇奇怪怪的Bug

庄懂的技术美术入门课(美术向)-直播录屏-第12课

偶然看到了庄懂老师的公开课,下载了工程,跟着做了一个第12节课的DOTA2角色shader,结果遇到了一个动用我全身上下的脑子(bushi)也想不清的问题…

该部分需要的文件我打包放到了度盘,希望热心大佬可以尝试攻略一番QWQ
Package网盘链接欢迎大佬捯饬,分享码9958快救救我八

问题描述

因为做了Clip,想要被Clip部分的阴影显示正确,需要做如下操作:

  1. 添加正确的FallBack:
		FallBack "Legacy Shaders/Transparent/Cutout/VertexLit
  1. 添加参数:
		[HideInInspector] _Cutoff ("Alpha cutoff", Range(0, 1)) = 0.5
        [HideInInspector] _Color ("Main Color", Color) = (1.0, 1.0, 1.0, 1.0)

当初也在《UnityShader入门精要》中学到过,也手操过,没有什么不对劲

但是这次出现了下面的情况:

  1. 按下鼠标按键(左中右)拖动屏幕,阴影会消失;若离得够近,还可以看到一些像噪点一样的细碎暗色面片在移动
  2. 松开鼠标按键,几秒后阴影会再次出现,但是出现的阴影依然是没有计算Clip部分的阴影
  3. 使用该shader的物体,在按下鼠标按键(左中右)拖动屏幕,会发现能看到物体后面的物体(虽然只是轮廓),就像变透明了一样
  4. 在代码中注释“_Cutoff” 属性的话,上面的3条现象随着也消失了

具体见视频:
【Help】Something wrong when I add shadow to an AlphaTest shader


原谅我放了YouTube…
CSDN到底是什么时候不能放视频的啊…
由于不想把奇奇怪怪的东西放在b站,只好放YouTube了…

Debug尝试

  1. 输出shadow,确定问题出在shadow上

  2. 修改了Tag,无明显变化

Tags {
    
      "Queue"="AlphaTest" 
		"IgnoreProjector"="True" 
		"RenderType"="TransparentCutout"
		}
  1. 注释掉 _Color 属性,无明显变化
  2. 注释掉 _Cutoff 属性,阴影回归正常;但依然无法正确显示Clip部分的阴影
  3. 修改 _Cutoff 的值到0.1,无明显变化

代码比较

我的代码(阴影错误)

Shader "DOTA2/Ogre"
{
    
    
    Properties
    {
    
    
        [Header(Texture)]
        _BaseTex ("Base Tex", 2D) = "white" {
    
    }
        _BumpMap ("Bump Map", 2D) = "white" {
    
    }
        _SRTE ("Spec Rim Tint SpecExp", 2D) = "white" {
    
    }
        _Metallic ("Metallic", 2D) = "black" {
    
    } // Default 0
        _Emission ("Emission", 2D) = "black" {
    
    } 
        _Fresnel ("Fresnel Warp", 2D) = "white" {
    
    }
        _DiffWrap ("Diffuse Warp", 2D) = "white" {
    
    }
        _EnvCube ("EnvCube", Cube) = "white" {
    
    }

        [Header(Color)]
        _LightCol ("Light Color", Color) = (1,1,1,1)
        _EnvCol ("Environment Color", Color) = (1,1,1,1)

        [Header(Adjust Value)]
        _SpecInt ("Specular Intensity", float) = 5
        _SpecExp ("Specular Exponent", float) = 5
        _EnvSpecInt ("Env Spec Int", float) = 1
        _EmtInt ("Emission Int", float) = 1

        // Necessary for correct AlphaTest shadow
        //[HideInInspector] _Cutoff ("Alpha cutoff", Range(0, 1)) = 0.5
        [HideInInspector] _Color ("Main Color", Color) = (1.0, 1.0, 1.0, 1.0)
    }
    SubShader
    {
    
    
        Tags {
    
     "RenderType"="Opaque" }

        Pass
        {
    
    
            Tags{
    
     "LightMode" = "ForwardBase"}
            Cull Off

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            #include "Lighting.cginc"
            #include "AutoLight.cginc"

            #pragma multi_compile_fwdbase
            #pragma target 3.0

            sampler2D _BaseTex;
            sampler2D _BumpMap;
            sampler2D _SRTE;
            sampler2D _Metallic;
            sampler2D _Emission;
            sampler2D _Fresnel;
            sampler2D _DiffWrap;
            samplerCUBE _EnvCube;
            
            fixed3 _LightCol;
            fixed3 _EnvCol;
            fixed _SpecInt;
            fixed _SpecExp;
            fixed _EnvSpecInt;
            fixed _EmtInt;

            fixed _Cutoff;

            struct a2v
            {
    
    
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float4 tangent : TANGENT;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
    
    
                float4 pos : SV_POSITION; // Must use 'pos' for Shadow Macros
                float2 uv : TEXCOORD0;
                float4 t2w0 : TEXCOORD1;
                float4 t2w1 : TEXCOORD2;
                float4 t2w2 : TEXCOORD3;
                LIGHTING_COORDS(4, 5)
            };
           

            v2f vert (a2v v)
            {
    
    
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                float3 posWS = UnityObjectToWorldDir(v.vertex);

                o.uv = v.uv;

                fixed3 normalWS = UnityObjectToWorldNormal(v.normal);
                fixed3 tangentWS = UnityObjectToWorldDir(v.tangent);
                fixed3 bitangentWS = cross(normalWS, tangentWS) * v.tangent.w;

                o.t2w0 = float4(tangentWS.x, bitangentWS.x, normalWS.x, posWS.x);
                o.t2w1 = float4(tangentWS.y, bitangentWS.y, normalWS.y, posWS.y);
                o.t2w2 = float4(tangentWS.z, bitangentWS.z, normalWS.z, posWS.z);

                TRANSFER_VERTEX_TO_FRAGMENT(o);

                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
    
    
                // Clip
                fixed4 baseCol = tex2D(_BaseTex, i.uv);
                clip(baseCol.a - _Cutoff);

                // Bump
                fixed3 bump = UnpackNormal(tex2D(_BumpMap, i.uv));
                fixed3 normalDirWS = normalize(mul(fixed3x3(i.t2w0.xyz, i.t2w1.xyz, i.t2w2.xyz), bump));

                // World-Space vectors
                float3 posWS = float3(i.t2w0.w, i.t2w1.w, i.t2w2.w);
                fixed3 lightDirWS = normalize(UnityWorldSpaceLightDir(posWS));
                fixed3 viewDirWS = normalize(UnityWorldSpaceViewDir(posWS));
                fixed3 viewReflDirWS = reflect(-viewDirWS, normalDirWS);

                // Texture Sampling
                fixed4 srte = tex2D(_SRTE, i.uv); // SpecMsk(SpecInt), RimMsk(RimMsk), TintByBaseMsk(TintMsk), SpecExp
                fixed metallic = tex2D(_Metallic, i.uv).r;
                fixed3 fresnelWarp = tex2D(_Fresnel, dot(viewDirWS, normalDirWS)); // FresnelWarpCol, FresnelWarpRim, FresnelWarpSpec
                fresnelWarp = lerp(fresnelWarp, 0.0, metallic); // *
                fixed emsMsk = tex2D(_Emission, i.uv).r;
                fixed3 envCol = texCUBElod(_EnvCube, float4(viewReflDirWS, lerp(8.0, 0.0, srte.a))).rgb; // *


                /// Lighting
                /// Base Color - ExpModel
                fixed3 baseDiff = lerp(baseCol, half3(0.0, 0.0, 0.0), metallic); // *
                fixed3 baseSpec = lerp(baseCol, half3(0.3, 0.3, 0.3), srte.b) * srte.r; // *
                /// Light
                fixed halfLambert = dot(normalDirWS, lightDirWS) * 0.5 + 0.5;
                fixed diffWarp = tex2D(_DiffWrap, fixed2(halfLambert, 0.2)).r; // Adjusts diffuse value
                fixed blinnPhong = pow(saturate(dot(normalDirWS, normalize(lightDirWS + viewDirWS))), srte.a * _SpecExp);
                blinnPhong *= saturate(dot(normalDirWS, lightDirWS));
                fixed3 lightDiff = _LightCol * baseDiff * diffWarp;
                fixed3 lightSpec = _LightCol * baseSpec * max(blinnPhong, fresnelWarp.b) * _SpecInt; // *
                /// Environment
                fixed reflInt = max(fresnelWarp.b, metallic) * srte.r; // *
                fixed3 envDiff = baseDiff * _EnvCol;
                fixed3 envSpec = envCol * baseSpec * reflInt * _EnvSpecInt;
                fixed3 rim = float3(1,1,1) * fresnelWarp.g * srte.g * saturate(normalDirWS.g);
                fixed3 emission = baseDiff * emsMsk * _EmtInt;
                /// Shadow
                fixed shadow = LIGHT_ATTENUATION(i);
                // return shadow;




                return float4((lightDiff + lightSpec) * shadow + envDiff + envSpec + rim + emission, 1);
            }
            ENDCG
        }
    }
    FallBack "Legacy Shaders/Transparent/Cutout/VertexLit"
}

庄懂老师的代码(阴影正确)

Shader "AP01/L12/Dota2" {
    
    
    Properties {
    
    
        [Header(Texture)]
        _MainTex        ("RGB:颜色 A:透贴", 2d) = "white"{
    
    }
        _MaskTex        ("R:高光强度 G:边缘光强度 B:高光染色 A:高光次幂", 2d) = "black"{
    
    }
        _NormTex        ("RGB:法线贴图", 2d) = "bump"{
    
    }
        _MatelnessMask  ("金属度遮罩", 2d) = "black"{
    
    }
        _EmissionMask   ("自发光遮罩", 2d) = "black"{
    
    }
        _DiffWarpTex    ("颜色Warp图", 2d) = "gray"{
    
    }
        _FresWarpTex    ("菲涅尔Warp图", 2d) = "gray"{
    
    }
        _Cubemap        ("环境球", cube) = "_Skybox"{
    
    }
        [Header(DirDiff)]
        _LightCol       ("光颜色", color) = (1.0, 1.0, 1.0, 1.0)
        [Header(DirSpec)]
        _SpecPow        ("高光次幂", range(0.0, 99.0)) = 5
        _SpecInt        ("高光强度", range(0.0, 10.0)) = 5
        [Header(EnvDiff)]
        _EnvCol         ("环境光颜色", color) = (1.0, 1.0, 1.0, 1.0)
        [Header(EnvSpec)]
        _EnvSpecInt     ("环境镜面反射强度", range(0.0, 30.0)) = 0.5
        [Header(RimLight)]
        [HDR]_RimCol    ("轮廓光颜色", color) = (1.0, 1.0, 1.0, 1.0)
        [Header(Emission)]
        _EmitInt        ("自发光强度", range(0.0, 10.0)) = 1.0
        [HideInInspector]
        _Cutoff         ("Alpha cutoff", Range(0,1)) = 0.5
        [HideInInspector]
        _Color          ("Main Color", Color) = (1.0, 1.0, 1.0, 1.0)
    }
    SubShader {
    
    
        Tags {
    
    
            "RenderType"="Opaque"
        }
        Pass {
    
    
            Name "FORWARD"
            Tags {
    
    
                "LightMode"="ForwardBase"
            }
            Cull Off
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            #include "AutoLight.cginc"
            #include "Lighting.cginc"
            #pragma multi_compile_fwdbase_fullshadows
            #pragma target 3.0
            // 输入参数
            uniform sampler2D _MainTex;
            uniform sampler2D _MaskTex;
            uniform sampler2D _NormTex;
            uniform sampler2D _MatelnessMask;
            uniform sampler2D _EmissionMask;
            uniform sampler2D _DiffWarpTex;
            uniform sampler2D _FresWarpTex;
            uniform samplerCUBE _Cubemap;
            // DirDiff
            uniform half3 _LightCol;
            // DirSpec
            uniform half _SpecPow;
            uniform half _SpecInt;
            // EnvDiff
            uniform half3 _EnvCol;
            // EnvSpec
            uniform half _EnvSpecInt;
            // RimLight
            uniform half3 _RimCol;
            // Emission
            uniform half _EmitInt;
            // Other
            uniform half _Cutoff;
            // 输入结构
            struct VertexInput {
    
    
                float4 vertex   : POSITION;   // 顶点信息 Get✔
                float2 uv0      : TEXCOORD0;  // UV信息 Get✔
                float4 normal   : NORMAL;     // 法线信息 Get✔
                float4 tangent  : TANGENT;    // 切线信息 Get✔
            };
            // 输出结构
            struct VertexOutput {
    
    
                float4 pos    : SV_POSITION;  // 屏幕顶点位置
                float2 uv0      : TEXCOORD0;  // UV0
                float4 posWS    : TEXCOORD1;  // 世界空间顶点位置
                float3 nDirWS   : TEXCOORD2;  // 世界空间法线方向
                float3 tDirWS   : TEXCOORD3;  // 世界空间切线方向
                float3 bDirWS   : TEXCOORD4;  // 世界空间副切线方向
                LIGHTING_COORDS(5,6)          // 投影相关
            };
            // 输入结构>>>顶点Shader>>>输出结构
            VertexOutput vert (VertexInput v) {
    
    
                VertexOutput o = (VertexOutput)0;                   // 新建输出结构
                    o.pos = UnityObjectToClipPos( v.vertex );       // 顶点位置 OS>CS
                    o.uv0 = v.uv0;                                  // 传递UV
                    o.posWS = mul(unity_ObjectToWorld, v.vertex);   // 顶点位置 OS>WS
                    o.nDirWS = UnityObjectToWorldNormal(v.normal);  // 法线方向 OS>WS
                    o.tDirWS = normalize(mul(unity_ObjectToWorld, float4(v.tangent.xyz, 0.0)).xyz); // 切线方向 OS>WS
                    o.bDirWS = normalize(cross(o.nDirWS, o.tDirWS) * v.tangent.w);  // 副切线方向
                    TRANSFER_VERTEX_TO_FRAGMENT(o)                  // 投影相关
                return o;                                           // 返回输出结构
            }
            // 输出结构>>>像素
            float4 frag(VertexOutput i) : COLOR {
    
    
                // 向量准备
                half3 nDirTS = UnpackNormal(tex2D(_NormTex, i.uv0));
                half3x3 TBN = half3x3(i.tDirWS, i.bDirWS, i.nDirWS);
                half3 nDirWS = normalize(mul(nDirTS, TBN));
                half3 vDirWS = normalize(_WorldSpaceCameraPos.xyz - i.posWS);
                half3 vrDirWS = reflect(-vDirWS, nDirWS);
                half3 lDirWS = _WorldSpaceLightPos0.xyz;
                half3 lrDirWS = reflect(-lDirWS, nDirWS);
                // 中间量准备
                half ndotl = dot(nDirWS, lDirWS);
                half ndotv = dot(nDirWS, vDirWS);
                half vdotr = dot(vDirWS, lrDirWS);
                // 采样纹理
                half4 var_MainTex = tex2D(_MainTex, i.uv0);
                half4 var_MaskTex = tex2D(_MaskTex, i.uv0);
                half var_MatelnessMask = tex2D(_MatelnessMask, i.uv0).r;
                half var_EmissionMask = tex2D(_EmissionMask, i.uv0).r;
                half3 var_FresWarpTex = tex2D(_FresWarpTex, ndotv);
                half3 var_Cubemap = texCUBElod(_Cubemap, float4(vrDirWS, lerp(8.0, 0.0, var_MaskTex.a))).rgb;
                // 提取信息
                half3 baseCol = var_MainTex.rgb;
                half opacity = var_MainTex.a;
                half specInt = var_MaskTex.r;
                half rimInt = var_MaskTex.g;
                half specTint = var_MaskTex.b;
                half specPow = var_MaskTex.a;
                half matellic = var_MatelnessMask;
                half emitInt = var_EmissionMask;
                half3 envCube = var_Cubemap;
                half shadow = LIGHT_ATTENUATION(i);
                // 光照模型
                    // 漫反射颜色 镜面反射颜色
                    half3 diffCol = lerp(baseCol, half3(0.0, 0.0, 0.0), matellic);
                    half3 specCol = lerp(baseCol, half3(0.3, 0.3, 0.3), specTint) * specInt;
                    // 菲涅尔
                    half3 fresnel = lerp(var_FresWarpTex, 0.0, matellic);
                    half fresnelCol = fresnel.r;    // 无实际用途
                    half fresnelRim = fresnel.g;
                    half fresnelSpec = fresnel.b;
                    // 光源漫反射
                    half halfLambert = ndotl * 0.5 + 0.5;
                    half3 var_DiffWarpTex = tex2D(_DiffWarpTex, half2(halfLambert, 0.2));
                    half3 dirDiff = diffCol * var_DiffWarpTex * _LightCol;
                    // 光源镜面反射
                    half phong = pow(max(0.0, vdotr), specPow * _SpecPow);
                    half spec = phong * max(0.0, ndotl);
                    spec = max(spec, fresnelSpec);
                    spec = spec * _SpecInt;
                    half3 dirSpec = specCol * spec * _LightCol;
                    // 环境漫反射
                    half3 envDiff = diffCol * _EnvCol;
                    // 环境镜面反射
                    half reflectInt = max(fresnelSpec, matellic) * specInt;
                    half3 envSpec = specCol * reflectInt * envCube * _EnvSpecInt;
                    // 轮廓光
                    half3 rimLight = _RimCol * fresnelRim * rimInt * max(0.0, nDirWS.g);
                    // 自发光
                    half3 emission = diffCol * emitInt * _EmitInt;
                    // 混合
                    half3 finalRGB = (dirDiff + dirSpec) * shadow + envDiff + envSpec + rimLight + emission;
                // 透明剪切
                clip(opacity - _Cutoff);
                // 返回值
                return float4(finalRGB, 1.0);
            }
            ENDCG
        }
    }
    // 声明回退Shader
    FallBack "Legacy Shaders/Transparent/Cutout/VertexLit"
}

庄懂的技术美术入门课(美术向)-直播录屏-第12课

希望明白的大佬留言,救救孩子QWQ

该部分需要的文件我打包放到了度盘,希望热心大佬可以尝试攻略一番QWQ
Package网盘链接欢迎大佬捯饬,分享码9958快救救我八

猜你喜欢

转载自blog.csdn.net/weixin_44045614/article/details/116212114