Unity URP用于 光照贴图(Lightmap)的材质Shader

cs 复制代码
Shader "Custom/URP/Lightmapped_PBR_RGBM"
{
    Properties
    {
        _Color ("Base Color", Color) = (1,1,1,1)
        _MainTex ("Albedo", 2D) = "white" {}

        _LightMap ("Lightmap (RGBM)", 2D) = "black" {}
        _RGBMRange ("RGBM Range", Float) = 4.59482

        _BumpMap ("Normal Map", 2D) = "bump" {}
        _NormalScale ("Normal Scale", Range(0,2)) = 1

        _Metallic ("Metallic", Range(0,1)) = 0
        _Smoothness ("Smoothness", Range(0,1)) = 0.5
    }

    SubShader
    {
        Tags
        {
            "RenderPipeline"="UniversalPipeline"
            "RenderType"="Opaque"
            "Queue"="Geometry"
        }


        Pass
        {
            Name "UniversalForward"
            Tags { "LightMode"="UniversalForward" }

            ZWrite On
            Cull Back
            ZTest LEqual





            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_instancing

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

            TEXTURE2D(_MainTex);      SAMPLER(sampler_MainTex);
            TEXTURE2D(_LightMap);     SAMPLER(sampler_LightMap);
            TEXTURE2D(_BumpMap);      SAMPLER(sampler_BumpMap);

            CBUFFER_START(UnityPerMaterial)
                float4 _Color;
                float4 _MainTex_ST;
                float4 _LightMap_ST;
                float _RGBMRange;

                float _NormalScale;
                float _Metallic;
                float _Smoothness;
            CBUFFER_END

            struct Attributes
            {
                float3 positionOS : POSITION;
                float3 normalOS   : NORMAL;
                float4 tangentOS  : TANGENT;
                float2 uv  : TEXCOORD0;
                float2 uv2 : TEXCOORD1;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                float2 uv  : TEXCOORD0;
                float2 uv2 : TEXCOORD1;

                float3 normalWS  : TEXCOORD2;
                float3 tangentWS : TEXCOORD3;
                float3 bitangentWS : TEXCOORD4;
                float3 positionWS : TEXCOORD5;

                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

             float _ReflectionProbeIntensity;

            // RGBM 解码(与你原来一致)
            half3 DecodeRGBM2(half4 rgbm)
            {
                return rgbm.rgb * (rgbm.a * _RGBMRange) * (rgbm.a * _RGBMRange);
            }



            Varyings vert (Attributes v)
            {
                Varyings o;
                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_TRANSFER_INSTANCE_ID(v, o);

                VertexPositionInputs pos = GetVertexPositionInputs(v.positionOS);
                VertexNormalInputs   nor = GetVertexNormalInputs(v.normalOS, v.tangentOS);

                o.positionCS = pos.positionCS;
                o.positionWS = pos.positionWS;

                o.normalWS   = nor.normalWS;
                o.tangentWS  = nor.tangentWS;
                o.bitangentWS = nor.bitangentWS;

                o.uv  = TRANSFORM_TEX(v.uv, _MainTex);
                o.uv2 = TRANSFORM_TEX(v.uv2, _LightMap);
                return o;
            }

            half4 frag (Varyings i) : SV_Target
            {
                UNITY_SETUP_INSTANCE_ID(i);

                // === Albedo ===
                half4 base = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
                half3 albedo = base.rgb * _Color.rgb;

                // === Normal ===
                half3 normalTS = UnpackNormalScale(
                    SAMPLE_TEXTURE2D(_BumpMap, sampler_BumpMap, i.uv),
                    _NormalScale
                );

                float3x3 TBN = float3x3(
                    normalize(i.tangentWS),
                    normalize(i.bitangentWS),
                    normalize(i.normalWS)
                );

                half3 normalWS = normalize(mul(normalTS, TBN));

                // === Lightmap ===
                half4 lmTex = SAMPLE_TEXTURE2D(_LightMap, sampler_LightMap, i.uv2);
                half3 bakedGI = DecodeRGBM2(lmTex);

                // === View / Reflect ===
                half3 viewDir = normalize(GetWorldSpaceViewDir(i.positionWS));
                half3 reflectDir = reflect(-viewDir, normalWS);

                // === 间接高光(近似)===
                half perceptualRoughness = 1.0 - _Smoothness;
                half specPower = lerp(16, 128, _Smoothness);
                half specular = pow(saturate(dot(reflectDir, viewDir)), specPower);

                half3 specColor = lerp(0.04.xxx, albedo, _Metallic);

                // ===  ===
                 half3 envSpec = GlossyEnvironmentReflection(reflectDir , 1.0 - _Smoothness , 1.0);
                 half3 color = albedo * bakedGI + envSpec * specColor;

                

                #if defined(UNITY_COLORSPACE_GAMMA)
                    color = pow(color, 1.0 / 2.2);
                #endif

                return half4(color, 1.0);
            }
            ENDHLSL






        }




    }
}
  • 支持金属度、光滑度、 Reflection Probe 、法线贴图
  • 暂不支持 半透明对象

效果:

相关推荐
xcLeigh20 小时前
Unity基础:GameObject与Component——Unity核心架构思想彻底理解
unity·教程·component·gameobject
郝学胜-神的一滴1 天前
中级OpenGL教程 022:探秘三维世界的血脉传承——物体父子关系与矩阵递归奥义
c++·线性代数·unity·矩阵·游戏引擎·unreal engine·opengl
法雅特吉他1 天前
吉他日常保养完全指南:从环境参数到部位养护的系统化方案
大数据·经验分享·新媒体运营·学习方法·材质
weixin_424294672 天前
Unity的测试Edit Mode和Play Mode,有什么区别?
unity
玖玥拾2 天前
Unity 3D 笔记(八)ScrollRect 滚动视图、NavMesh 自动寻路系统
笔记·3d·unity
淡海水3 天前
06-04-YooAsset源码-Unity加密解密服务
前端·unity·性能优化·c#·游戏引擎·yooasset
cd_949217213 天前
Unity游戏角色资产怎么快速制作?用V2Fun跑通生成、绑定和导入测试
游戏·unity·游戏引擎
HH‘HH4 天前
Unity 项目创建标准指南:分辨率、尺寸、文件路径与命名规范
unity·游戏引擎