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

效果:
