一个简单的 URP Shader

作者:追风剑情 发布于:2025-7-7 15:56 分类:Shader

Shader

Shader "Custom/SimpleURP"
{
    Properties
    {
        [MainTexture] //标记为主纹理
        _BaseMap("Base Map", 2D) = "white" {}
        [MainColor] //标记为主颜色
        _BaseColor("Base Color", Color) = (1, 1, 1, 1)
        _Smoothness("Smoothness", Range(0,1)) = 0.5
    }

    SubShader
    {
        Tags 
        { 
            "RenderType" = "Opaque" 
            //声明此Shader只在URP管线中执行
            "RenderPipeline" = "UniversalPipeline"
        }

        Pass
        {
            Blend One Zero
			ZTest Always
			ZWrite On
			Cull Back

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            //所有GPU实例相关的宏,只有在Inspector窗口启用 "Enable GPU Instancing" 时才会生效
            //开启GPU实例
            #pragma multi_compile_instancing
            //开启每实例渲染层
            #pragma instancing_options renderinglayer

            //Core.hlsl相当于BRP中的UnityCG.cginc
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            //包含核心光照库
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

            struct Attributes
            {
                //模型空间中的顶点坐标
                float4 positionOS : POSITION;
                //纹理UV坐标
                float2 uv : TEXCOORD0;
                //模型空间法线
                float3 normalOS : NORMAL;
                //声明GPU实例ID
				UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct Varyings
            {
                //剪裁空间中的顶点坐标
                float4 positionHCS : SV_POSITION;
                //纹理UV坐标
                float2 uv : TEXCOORD0;
                //世界空间法线
                float3 normalWS : TEXCOORD1;
                //世界空间坐标
                float3 positionWS : TEXCOORD2;
                //声明GPU实例ID
				UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            //纹理和对应的采样器定义
            TEXTURE2D(_BaseMap);
            SAMPLER(sampler_BaseMap);

            //静态变量放在 CBUFFER 中
            CBUFFER_START(UnityPerMaterial)
                //纹理的缩放和偏移参数
                float4 _BaseMap_ST;
                float _Smoothness;
            CBUFFER_END

            //实例变量放 INSTANCING_BUFFER 中
            UNITY_INSTANCING_BUFFER_START(Props)
                UNITY_DEFINE_INSTANCED_PROP(float4, _BaseColor) //每实例颜色
            UNITY_INSTANCING_BUFFER_END(Props)

            Varyings vert(Attributes input)
            {
                Varyings output;
                //参见UnityInstancing.hlsl
                //设置当前处理的GPU实例ID
                UNITY_SETUP_INSTANCE_ID(input);
                //将GPU实例ID从input传递到output
				UNITY_TRANSFER_INSTANCE_ID(input, output);
                //坐标变换
                output.positionHCS = TransformObjectToHClip(input.positionOS.xyz);
                output.positionWS = TransformObjectToWorld(input.positionOS.xyz);
                output.normalWS = TransformObjectToWorldNormal(input.normalOS);
                //计算纹理坐标(宏中封装了缩放和偏移处理)
                output.uv = TRANSFORM_TEX(input.uv, _BaseMap);
                return output;
            }

            half4 frag(Varyings input) : SV_Target
            {
                //设置当前处理的GPU实例ID
                UNITY_SETUP_INSTANCE_ID(input);
                //访问实例变量
                float4 baseColor = UNITY_ACCESS_INSTANCED_PROP(Props, _BaseColor);
                //纹理采样
                half4 color = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv) * baseColor;

                //准备光照输入数据
                InputData lightingInput = (InputData)0;
                lightingInput.positionWS = input.positionWS;
                lightingInput.normalWS = normalize(input.normalWS);
                lightingInput.viewDirectionWS = GetWorldSpaceViewDir(input.positionWS);

                //准备表面数据
                SurfaceData surfaceData = (SurfaceData)0;
                surfaceData.albedo = color.rgb;
                surfaceData.alpha = color.a;
                surfaceData.smoothness = _Smoothness;
                surfaceData.metallic = 0; //非金属材质

                //计算PBR光照
                return UniversalFragmentPBR(lightingInput, surfaceData);
            }
            ENDHLSL
        }
    }
}

工程截图

22222.png

效果

111111.png

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号