HLSL SimpleTexture.shader

作者:追风剑情 发布于:2024-2-28 10:37 分类:Shader

//URP Shader
Shader "Custom/URP/SimpleTexture"
{
    Properties
    {
        [MainTexture]
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        //指定使用的渲染管线为 UniversalPipeline
        //经测试必须指定 "Queue" = "Transparent" 才能成功渲染纹理
        Tags { "Queue" = "Transparent" "RenderType" = "Transparent" "RenderPipeline" = "UniversalPipeline" }

        Blend SrcAlpha OneMinusSrcAlpha
        Cull Off
        ZWrite Off

        Pass
        {
            Name "ForwardLit"
            //指定使用的光照模式为 UniversalForward
            Tags { "LightMode" = "UniversalForward" }

            HLSLPROGRAM
            //定义顶点着色器 vert
            #pragma vertex vert
            //定义片元着色器 frag
            #pragma fragment frag

            //导入Core.hlsl (相当于UnityCG.cginc)
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            struct Attributes
            {
                //模型空间坐标
                float4 positionOS   : POSITION;
                float2 uv           : TEXCOORD0;
                //顶点法线
                float3 normal       : NORMAL;
            };

            struct Varyings
            {
                //裁剪空间坐标
                float4  positionCS  : SV_POSITION;
                float2 uv           : TEXCOORD0;
                half3 normal        : NORMAL;
            };

            //声明纹理
            TEXTURE2D(_MainTex);
            //声明采样器
            SAMPLER(sampler_MainTex);

            //开启 SRP Batcher
            CBUFFER_START(UnityPerMaterial)
                float4 _MainTex_ST;
            CBUFFER_END

            Varyings vert(Attributes v)
            {
                Varyings o;
                //模型空间转裁剪空间要换成 TransformObjectToHClip()
                o.positionCS = TransformObjectToHClip(v.positionOS.xyz);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.normal = TransformObjectToWorldNormal(v.normal);
                return o;
            }

            half4 frag(Varyings i) : SV_Target
            {
                //纹理采样要换成 SAMPLE_TEXTURE2D()
                half4 mainTex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
                return mainTex;
            }
            ENDHLSL
        }

        Pass
        {
            //投影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

            // -------------------------------------
            // Universal Pipeline keywords

            // This is used during shadow map generation to differentiate between directional and punctual light shadows, as they use different formulas to apply Normal Bias
            #pragma multi_compile_vertex _ _CASTING_PUNCTUAL_LIGHT_SHADOW

            #pragma vertex ShadowPassVertex
            #pragma fragment ShadowPassFragment

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

    FallBack "Hidden/Universal Render Pipeline/FallbackError"
}

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号