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 、法线贴图
  • 暂不支持 半透明对象

效果:

相关推荐
小李也疯狂18 小时前
Unity 中的立方体贴图(Cubemaps)
unity·游戏引擎·贴图·cubemap
牛掰是怎么形成的18 小时前
Unity材质贴图引用陷阱:包体暴涨真相
unity·材质·贴图
呆呆敲代码的小Y18 小时前
【Unity工具篇】| 超实用工具LuBan,快速上手使用
游戏·unity·游戏引擎·unity插件·luban·免费游戏·游戏配置表
EQ-雪梨蛋花汤18 小时前
【Unity优化】Unity多场景加载优化与资源释放完整指南:解决Additive加载卡顿、预热、卸载与内存释放问题
unity·游戏引擎
我的offer在哪里19 小时前
用 Unity 从 0 做一个「可以玩的」游戏,需要哪些步骤和流程
游戏·unity·游戏引擎
泡泡茶壶ᐇ20 小时前
Unity游戏开发入门指南:从零开始理解游戏引擎核心概念
unity·游戏引擎
YigAin21 小时前
Unity中的Lock,到底在锁什么,什么时候该用?
unity
Var_al21 小时前
抖小Unity WebGL分包命令行工具实践指南
unity·游戏引擎·webgl
lGSrsEpqUxi1 天前
双馈风机风电场经串补并网次同步振荡/谐振仿真模型探索
材质
天人合一peng1 天前
unity 通过代码修改button及其名字字体的属性
unity·游戏引擎