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

效果:

相关推荐
yi碗汤园2 小时前
【一文了解】网络请求
网络·unity
EucliwoodXT2 小时前
【Unity】项目部署Linux服务器
linux·unity·游戏引擎
微光守望者2 小时前
Unity小知识【2】:Transform与RectTransform,UI和3D对象的空间转换秘诀
ui·3d·unity
心之所向,自强不息2 小时前
URP的渲染流程
unity
CG_MAGIC11 小时前
Substance Painter 纹理烘焙:法线贴图与 AO 贴图制作指南
3d·贴图·substance painter·建模教程·渲云渲染
变身缎带18 小时前
Unity里基于Luban的buff系统
数据库·unity·游戏引擎
哎呦哥哥和巨炮叔叔1 天前
C4D + Corona 渲染玻璃材质详解:Thin Shell 与实心玻璃的正确使用方式
材质·建筑可视化·渲染技术·c4d·渲染101云渲染·corona渲染·材质系统
变身缎带1 天前
Unity中较为完善的NetworkManager
unity·游戏引擎
作孽就得先起床1 天前
unity webGL导出.glb模型
unity·c#·游戏引擎·webgl