Unity URP Shader(HLSL)踩坑日记(一)

最近开始转TA,刚开始学习,资料比较杂乱,其中遇到的问题和一些计算方式,记录一下,后续会一直完善补充。

1.urp中基础不受光shader

Shader "Example/URPUnlitShaderColor"
{
    Properties
    {
        [MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
        [MainTexture] _BaseMap("Base Map", 2D) = "white"{}
    }

    SubShader
    {
        // SubShader Tags 定义何时以及在何种条件下执行某个 SubShader 代码块或某个通道。
        Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }

        Pass
        {
            // 声明Pass名称,方便调用与识别
            Name "ForwardUnlit"
            // HLSL 代码块。Unity SRP 使用 HLSL 语言。
            HLSLPROGRAM
            // 此行定义顶点着色器的名称。
            #pragma vertex vert
            // 此行定义片元着色器的名称。
            #pragma fragment frag

            // Core.hlsl 文件包含常用的 HLSL 宏和函数的定义,还包含对其他 HLSL 文件(例如Common.hlsl、SpaceTransforms.hlsl 等)的 #include 引用。
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            // 结构定义将定义它包含哪些变量。此示例使用 Attributes 结构作为顶点着色器中的输入结构。
            struct Attributes
            {
                // positionOS 变量包含对象空间中的顶点
                float4 positionOS   : POSITION;
                // uv 变量包含给定顶点的纹理上的
                float2 uv           : TEXCOORD0;
                // 声明包含每个顶点的法线矢量的
                half3 normal        : NORMAL;
            };

            struct Varyings
            {
                // 此结构中的位置必须具有 SV_POSITION 语义。
                float4 positionHCS  : SV_POSITION;
                float2 uv           : TEXCOORD0;
                half3 normal        : NORMAL;
                
            };
            // 材质单独声明,使用DX11风格的新采样方法
            // 此宏将 _BaseMap 声明为 Texture2D 对象。
            TEXTURE2D(_BaseMap);
            // This macro declares the sampler for the _BaseMap texture.
            SAMPLER(sampler_BaseMap);

            // 要使 Unity 着色器 SRP Batcher 兼容,请在名为 UnityPerMaterial 的单个 CBUFFER 代码块中声明与材质相关的所有属性。
            // 有关 SRP Batcher 的更多信息 https://docs.unity3d.com/Manual/SRPBatcher.html
            CBUFFER_START(UnityPerMaterial)
                // 以下行声明了 _BaseColor 变量,以便可以在片元着色器中使用它。
                half4 _BaseColor;
                // 以下行声明 _BaseMap_ST 变量,以便可以在片元着色器中使用 _BaseMap 变量。为了使平铺和偏移有效,有必要使用 _ST 后缀。
                float4 _BaseMap_ST;
            CBUFFER_END

            // 顶点着色器定义具有在 Varyings 结构中定义的属性。vert 函数的类型必须与它返回的类型(结构)匹配。
            Varyings vert(Attributes IN)
            {
                // // 使用 Varyings 结构声明输出对象 (OUT)。
                Varyings OUT;
                //方法一
                // TransformObjectToHClip 函数将顶点位置从对象空间变换到齐次裁剪空间。
                OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
                //方法二
                //GetVertexPositionInputs方法根据使用情况自动生成各个坐标系下的定点信息。
                //#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl"
                // VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
                // OUT.positionHCS = vertexInput.positionCS;
                
                // TRANSFORM_TEX 宏执行平铺和偏移
                OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap);
                // 使用 TransformObjectToWorldNormal 函数将法线从对象空间变换到世界空间。此函数来自 Core.hlsl 中引用的SpaceTransforms.hlsl 文件。
                OUT.normal = TransformObjectToWorldNormal(IN.normal);
                return OUT;
            }

            half4 frag(Varyings IN) : SV_Target
            {
                // SAMPLE_TEXTURE2D 宏使用给定的采样器对纹理进行
                half4 color = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv);
                color*=_BaseColor;
                return color;
            }
            ENDHLSL
        }
    }
}

注意此时Properties中的属性,如果要开启SRP合批,需要放到CBUFFER代码块中。

2.从代码块中可以看出渲染的流程:

应用阶段准备的数据---->

顶点着色处理数据(返回值为处理后的数据)---->------>片元着色器接收上一阶段的数据,return片元渲染结果

3.如果不使用CBUFFER中声明,在Properties中声明属性之后,引入库

#include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"

这里会把属性的变量包装,但属性名字需要和类库中的一致,库中默认属性:

 

 此时就不需要自己在CBUFFER中声明属性了,对于Texture也不需要额外处理了,如:

 4.添加一个Toggle判断

 注意Toggle(_AdditionalLights)里面的字段

 声明shader_feature

 

 通过#if xxx  #endif判断

5.Blinn-Phong光照模型

Shader "MyShader/BarkShader1"
{
   Properties
    {
      
        [MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
        [MainTexture] _BaseMap("Base Map", 2D) = "white"{}
        _SpecColor("Specular", Color) = (1.0, 1.0, 1.0, 1.0)
        _Smoothness("Gloss", Range(8.0, 256)) = 20
    }

    SubShader
    {
        // URP的shader要在Tags中注明渲染管线是UniversalPipeline
      Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }


        Pass
        {
            // 声明Pass名称,方便调用与识别
            Name "ForwardBlinPhong"

            HLSLPROGRAM

            // 声明顶点/片段着色器对应的函数
            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
            //引用后,自动SRP合批,且不需要再声明TEXTURE2D(_BaseMap); SAMPLER(sampler_BaseMap);及CBUFFER_START
            #include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
            struct Attributes
            {
                float4 positionOS : POSITION;
                float3 normalOS   : NORMAL;
                float2 uv         : TEXCOORD0;
            };

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                float3 positionWS : POSITION_WS;
                float2 uv         : TEXCOORD0;
                float3 normalWS    : NORMAL_WS;
            };
           
            // 顶点着色器
            Varyings vert(Attributes input)
            {
                // GetVertexPositionInputs方法根据使用情况自动生成各个坐标系下的定点信息
                const VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
                    
                Varyings output;
                output.uv = TRANSFORM_TEX(input.uv, _BaseMap);
                output.positionCS = vertexInput.positionCS;
                output.positionWS = vertexInput.positionWS;
                output.normalWS = TransformObjectToWorldNormal(input.normalOS);
                return output;
            }

            // 片段着色器
            half4 frag(Varyings input) : SV_Target
            {
                float4 output;  
                  
                real3 positionWS = input.positionWS;
               
                real3 normalWS = normalize(input.normalWS);
                Light mainLight = GetMainLight(); // 主光源
                real3 lightColor = mainLight.color; // 主光源颜色
                 
                real3 lightDir = normalize(mainLight.direction); // 主光源方向
                real3 albedo = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv) * _BaseColor;
                real3 viewDirectionWS = SafeNormalize(GetCameraPositionWS() - positionWS); // safe防止分母为0
                real3 h = SafeNormalize(viewDirectionWS + lightDir);
                real3 specular = pow(saturate(dot(h, input.normalWS)), _Smoothness) * lightColor * saturate(_SpecColor); // 高光
                real3 ambient = SampleSH(normalWS) * albedo; // 环境光
                   
                real3 diffuse = saturate(dot(lightDir,normalWS)) * lightColor * albedo; // 漫反射
                
                output = real4(ambient + diffuse + specular, 1.0);
                return output;
                }
            ENDHLSL
        }
       // 一般在Buit-In管线里,我们只需要最后FallBack返回到系统的Diffuse Shader,管线就会去里面找到他处理阴影的Pass。但是在URP中,一个Shader中的所有Pass需要有一致的CBuffer,否则便会打破SRP Batcher,影响效率。
       // 而系统默认SimpleLit的Shader中的CBuffer内容和我的写的并不一致,所以我们需要把它阴影处理的Pass复制一份,并且删掉其中引用的SimpleLitInput.hlsl(相关CBuffer的声明在这里面)
          Pass
        {
            Name "ShadowCaster"
            Tags{"LightMode" = "ShadowCaster"}

            ZWrite On
            ZTest LEqual
            ColorMask 0
            Cull[_Cull]

            HLSLPROGRAM
            #pragma exclude_renderers gles gles3 glcore
            #pragma target 4.5

            // -------------------------------------
            // Material Keywords
            #pragma shader_feature_local_fragment _ALPHATEST_ON
            #pragma shader_feature_local_fragment _GLOSSINESS_FROM_BASE_ALPHA

            //--------------------------------------
            // GPU Instancing
            #pragma multi_compile_instancing
            #pragma multi_compile _ DOTS_INSTANCING_ON

            #pragma vertex ShadowPassVertex
            #pragma fragment ShadowPassFragment

            #include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/Shaders/ShadowCasterPass.hlsl"
            ENDHLSL
        }
    }
  
}

6.处理多光源

如果希望每个光源都能够进行逐像素的漫反射和高光反射计算,那么我们就需要在片元着色器中遍历每一个光源,并进行和上面一样的光照计算

GetAdditionalLightsCount()能够获取到影响这个片段的附加光源数量,但是如果数量超过了URP中设定的附加光照上限,就会返回附加光照上限的数量。

GetAdditionalLight(lightIndex, IN.positionWS);方法会按照index去找到对应的光源,并根据提供的片段世界坐标位置计算光照和阴影衰减,并存储在返回的Light结构体内。

int pixelLightCount = GetAdditionalLightsCount();
for (int lightIndex = 0; lightIndex < pixelLightCount; ++lightIndex)
{
    Light light = GetAdditionalLight(lightIndex, IN.positionWS);
    diffuse += LightingLambert(light.color, light.direction, IN.normalWS);
    specular += LightingSpecular(light.color, light.direction, normalize(IN.normalWS), normalize(IN.viewDirWS), _SpecularColor, _Smoothness);
}

7.带开关的多光源检测及高光

Shader "MyShader/BarkShader"
{
   Properties
    {
      
        [MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
        [MainTexture] _BaseMap("Base Map", 2D) = "white"{}
        _BumpMap("Normal Map", 2D) = "bump"{}
        _BumpScale("NormalScale",Float) = 1.0
        _SpecColor("Specular", Color) = (1.0, 1.0, 1.0, 1.0)
        _Smoothness("Gloss", Range(8.0, 256)) = 20
        //添加一个Toggle判断
        [Toggle(_AdditionalLights)] _AddLights ("AddLights", Float) = 0
        [Toggle(_SpecColorToggle)] _SpecColorTog ("SpecColorToggle",Float) = 0
    }

    SubShader
    {
        // URP的shader要在Tags中注明渲染管线是UniversalPipeline
      Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }


        Pass
        {
            // 声明Pass名称,方便调用与识别
            Name "ForwardBlinPhong"

            HLSLPROGRAM

            // 声明顶点/片段着色器对应的函数
            #pragma vertex vert
            #pragma fragment frag
            #pragma shader_feature _AdditionalLights
            #pragma shader_feature _SpecColorToggle
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
            //引用后,自动SRP合批,且不需要再声明TEXTURE2D(_BaseMap); SAMPLER(sampler_BaseMap);及CBUFFER_START
            #include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
            //
            // TEXTURE2D(_BaseMap);
            // SAMPLER(sampler_BaseMap);
            // CBUFFER_START(UnityPerMaterial)
            // float4 _BaseColor;
            // float4 _BaseMap_ST;
            // real _Smoothness;
            // float4 _SpecColor;
            // CBUFFER_END
     
            struct Attributes
            {
                float4 positionOS : POSITION;
                float3 normalOS   : NORMAL;
                float4 tangentOS  : TANGENT;
                float2 uv         : TEXCOORD0;
            };

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                float3 positionWS : POSITION_WS;
                float2 uv         : TEXCOORD0;
                float3 normalWS    : NORMAL_WS;
                float4 tangentWS  : TANGENT_WS;
            };
           
            // 顶点着色器
            Varyings vert(Attributes input)
            {
                    // GetVertexPositionInputs方法根据使用情况自动生成各个坐标系下的定点信息
                const VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
                const VertexNormalInputs   vertexNormalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
                    
                Varyings output;
                real sign = input.tangentOS.w * GetOddNegativeScale();
                output.uv = TRANSFORM_TEX(input.uv, _BaseMap);
                output.positionCS = vertexInput.positionCS;
                output.positionWS = vertexInput.positionWS;
                output.normalWS   = vertexNormalInput.normalWS;
                output.tangentWS = real4(vertexNormalInput.tangentWS, sign);
                return output;

                //方案2
                 // output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
                 // output.normalWS = TransformObjectToWorldNormal(input.normalOS);
            }

            // 片段着色器
            half4 frag(Varyings input) : SV_Target
            {
                float4 output;  
                  
                real3 positionWS = input.positionWS;
                real sgn = input.tangentWS.w;      // should be either +1 or -1
                real3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
                real3 normalTS=UnpackNormalScale(SAMPLE_TEXTURE2D(_BumpMap,sampler_BumpMap,input.uv),_BumpScale);
                real3 normalWS = mul(normalTS, real3x3(input.tangentWS.xyz, bitangent.xyz, input.normalWS.xyz)); 
                // real3 normalWS = normalize(input.normalWS);
                Light mainLight = GetMainLight(); // 主光源
                real3 lightColor = mainLight.color; // 主光源颜色
                 
                real3 lightDir = normalize(mainLight.direction); // 主光源方向
                real3 albedo = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv) * _BaseColor;
                   
                real3 ambient = SampleSH(normalWS) * albedo; // 环境光
                   
                real3 diffuse = saturate(dot(lightDir,normalWS)) * lightColor * albedo; // 漫反射

                output = real4(ambient+diffuse,1.0);
                
                #if _SpecColorToggle
                real3 viewDirectionWS = SafeNormalize(GetCameraPositionWS() - positionWS); // safe防止分母为0
                real3 h = SafeNormalize(viewDirectionWS + lightDir);
                real3 specular = pow(saturate(dot(h, input.normalWS)), _Smoothness) * lightColor * saturate(_SpecColor); // 高光
                output = real4(ambient + diffuse + specular, 1.0);
                #endif
               
                // // return real4(ambient + diffuse + specular, 1.0);
                # ifdef _AdditionalLights
                int lightCount = GetAdditionalLightsCount();
                for(int index = 0; index < lightCount; index++)
                {
                    Light light = GetAdditionalLight(index, positionWS);     
                    real3 lightColorx = light.color;
                    real3 lightDirx = normalize(light.direction);
                    albedo += SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv) * _BaseColor;
                    diffuse += saturate(dot(lightDirx,normalWS)) * lightColorx * albedo;
                  
                    #if _SpecColorToggle
                    real3 hx = SafeNormalize(viewDirectionWS + lightDirx);
                    specular += pow(saturate(dot(hx, input.normalWS)), _Smoothness) * lightColorx * saturate(_SpecColor);
                    #endif
                    
                }
                output = real4(ambient+diffuse,1.0);
                #if _SpecColorToggle
                output = real4(ambient + diffuse + specular, 1.0);
                #endif
               
                #endif
                return output;
                }
            ENDHLSL
        }
       // 一般在Buit-In管线里,我们只需要最后FallBack返回到系统的Diffuse Shader,管线就会去里面找到他处理阴影的Pass。但是在URP中,一个Shader中的所有Pass需要有一致的CBuffer,否则便会打破SRP Batcher,影响效率。
       // 而系统默认SimpleLit的Shader中的CBuffer内容和我的写的并不一致,所以我们需要把它阴影处理的Pass复制一份,并且删掉其中引用的SimpleLitInput.hlsl(相关CBuffer的声明在这里面)
          Pass
        {
            Name "ShadowCaster"
            Tags{"LightMode" = "ShadowCaster"}

            ZWrite On
            ZTest LEqual
            ColorMask 0
            Cull[_Cull]

            HLSLPROGRAM
            #pragma exclude_renderers gles gles3 glcore
            #pragma target 4.5

            // -------------------------------------
            // Material Keywords
            #pragma shader_feature_local_fragment _ALPHATEST_ON
            #pragma shader_feature_local_fragment _GLOSSINESS_FROM_BASE_ALPHA

            //--------------------------------------
            // GPU Instancing
            #pragma multi_compile_instancing
            #pragma multi_compile _ DOTS_INSTANCING_ON

            #pragma vertex ShadowPassVertex
            #pragma fragment ShadowPassFragment

            #include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/Shaders/ShadowCasterPass.hlsl"
            ENDHLSL
        }
    }
  
}

8.透明度裁切Alpha Clipping

声明属性Properties

_Cutoff("Cutoff",float)=0.5

片元着色器中计算

half4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv);
clip(baseMap.a-_Cutoff);

注意baseMap的类型,需要是xxx4,才能有变量a

9.法线贴图

如果引用urp 的shader库,名字需要一致

必要参数

顶点着色器

片元着色器

 (不计算时法线贴图时,直接real3 normalWS = normalize(input.normalWS);

猜你喜欢

转载自blog.csdn.net/Star_MengMeng/article/details/127669887
今日推荐