1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
| // SRP Batcher兼容的Lit Shader示例 Shader "Custom/SRPBatcherCompatibleLit" { Properties { [MainTexture] _BaseMap("Base Map", 2D) = "white" {} [MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1) _BumpMap("Normal Map", 2D) = "bump" {} _BumpScale("Normal Scale", Range(0, 2)) = 1.0 _Metallic("Metallic", Range(0, 1)) = 0.0 _Smoothness("Smoothness", Range(0, 1)) = 0.5 // Instance-specific properties (必须使用UNITY_DEFINE_INSTANCED_PROP) _InstanceColor("Instance Color", Color) = (1, 1, 1, 1) _InstanceScale("Instance Scale", Vector) = (1, 1, 1, 1) }
SubShader { Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "UniversalMaterialType" = "Lit" "IgnoreProjector" = "True" "ShaderModel" = "4.5" } Pass { Name "ForwardLit" Tags { "LightMode" = "UniversalForward" }
HLSLPROGRAM #pragma target 4.5 // Material Keywords #pragma shader_feature_local _NORMALMAP #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF #pragma shader_feature_local _ENVIRONMENTREFLECTIONS_OFF #pragma shader_feature_local _SPECULAR_SETUP #pragma shader_feature_local_fragment _RECEIVE_SHADOWS_OFF
// Universal Render Pipeline keywords #pragma multi_compile _ _MAIN_LIGHT_SHADOWS #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS #pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS #pragma multi_compile_fragment _ _SHADOWS_SOFT // Unity defined keywords #pragma multi_compile_fog #pragma multi_compile_instancing // 必须启用实例化 #pragma instancing_options renderinglayer // 实例化选项 #pragma vertex LitPassVertex #pragma fragment LitPassFragment
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
// Instance-specific CBUFFER (SRP Batcher兼容的关键) // 所有材质实例属性必须在这里定义 UNITY_INSTANCING_BUFFER_START(PerInstance) UNITY_DEFINE_INSTANCED_PROP(float4, _InstanceColor) UNITY_DEFINE_INSTANCED_PROP(float3, _InstanceScale) UNITY_INSTANCING_BUFFER_END(PerInstance)
// Material CBUFFER (保持一致的布局) CBUFFER_START(UnityPerMaterial) float4 _BaseMap_ST; half4 _BaseColor; half _BumpScale; half _Metallic; half _Smoothness; CBUFFER_END
// Textures (必须在全局作用域定义) TEXTURE2D(_BaseMap); SAMPLER(sampler_BaseMap); TEXTURE2D(_BumpMap); SAMPLER(sampler_BumpMap);
// Input structure struct Attributes { float4 positionOS : POSITION; float3 normalOS : NORMAL; float4 tangentOS : TANGENT; float2 uv : TEXCOORD0; UNITY_VERTEX_INPUT_INSTANCE_ID // 必须包含实例ID };
// Varyings structure struct Varyings { float4 positionCS : SV_POSITION; float3 positionWS : TEXCOORD0; float3 normalWS : TEXCOORD1; float4 tangentWS : TEXCOORD2; float2 uv : TEXCOORD3; float4 shadowCoord : TEXCOORD4; UNITY_VERTEX_INPUT_INSTANCE_ID UNITY_VERTEX_OUTPUT_STEREO };
// Vertex function Varyings LitPassVertex(Attributes input) { Varyings output; UNITY_SETUP_INSTANCE_ID(input); // 设置实例ID UNITY_TRANSFER_INSTANCE_ID(input, output); // 传递实例ID UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
// 获取实例属性 float3 instanceScale = UNITY_ACCESS_INSTANCED_PROP(PerInstance, _InstanceScale); // 应用实例缩放 float3 scaledPosition = input.positionOS.xyz * instanceScale; VertexPositionInputs vertexInput = GetVertexPositionInputs(scaledPosition); VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS); output.positionCS = vertexInput.positionCS; output.positionWS = vertexInput.positionWS; output.normalWS = normalInput.normalWS; output.tangentWS = half4(normalInput.tangentWS, normalInput.sign); output.uv = TRANSFORM_TEX(input.uv, _BaseMap); #if defined(_MAIN_LIGHT_SHADOWS) output.shadowCoord = TransformWorldToShadowCoord(output.positionWS); #endif
return output; }
// Fragment function half4 LitPassFragment(Varyings input) : SV_Target { UNITY_SETUP_INSTANCE_ID(input); // 设置实例ID UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
// Sample base map half4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv); // 获取实例颜色 half4 instanceColor = UNITY_ACCESS_INSTANCED_PROP(PerInstance, _InstanceColor); // Combine base color with instance color and material color half3 color = baseMap.rgb * _BaseColor.rgb * instanceColor.rgb; // Sample and apply normal map if enabled #ifdef _NORMALMAP half4 normalMap = SAMPLE_TEXTURE2D(_BumpMap, sampler_BumpMap, input.uv); half3 normalTS = UnpackNormalScale(normalMap, _BumpScale); // Transform normal from tangent space to world space half3 normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, cross(input.normalWS, input.tangentWS.xyz) * input.tangentWS.w, input.normalWS)); normalWS = NormalizeNormalPerPixel(normalWS); #else half3 normalWS = input.normalWS; #endif // Get main light Light mainLight = GetMainLight(); // Simple lighting calculation half NdotL = saturate(dot(normalWS, mainLight.direction)); half3 lighting = mainLight.color * NdotL; // Apply lighting color *= (lighting + 0.2); // Add ambient // Apply shadows if enabled #if defined(_MAIN_LIGHT_SHADOWS) && !defined(_RECEIVE_SHADOWS_OFF) half shadowAtten = MainLightShadow(input.shadowCoord); color *= shadowAtten; #endif // Apply alpha half alpha = baseMap.a * _BaseColor.a * instanceColor.a; return half4(color, alpha); } ENDHLSL }
// Shadow Caster Pass (也需要SRP Batcher兼容) Pass { Name "ShadowCaster" Tags { "LightMode" = "ShadowCaster" }
ZWrite On ZTest LEqual ColorMask 0
HLSLPROGRAM #pragma target 4.5 #pragma multi_compile_instancing #pragma multi_compile_vertex _ _CASTING_PUNCTUAL_LIGHT_SHADOW
#pragma vertex ShadowPassVertex #pragma fragment ShadowPassFragment
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShadowCaster.hlsl"
float3 _LightDirection;
// Shadow pass也需要实例化CBUFFER UNITY_INSTANCING_BUFFER_START(PerInstanceShadow) UNITY_DEFINE_INSTANCED_PROP(float3, _InstanceScale) UNITY_INSTANCING_BUFFER_END(PerInstanceShadow)
struct Attributes { float4 positionOS : POSITION; float3 normalOS : NORMAL; float2 uv : TEXCOORD0; UNITY_VERTEX_INPUT_INSTANCE_ID };
struct Varyings { float2 uv : TEXCOORD0; float4 positionCS : SV_POSITION; UNITY_VERTEX_INPUT_INSTANCE_ID UNITY_VERTEX_OUTPUT_STEREO };
Varyings ShadowPassVertex(Attributes input) { Varyings output; UNITY_SETUP_INSTANCE_ID(input); UNITY_TRANSFER_INSTANCE_ID(input, output); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
// Apply instance scale in shadow pass too float3 instanceScale = UNITY_ACCESS_INSTANCED_PROP(PerInstanceShadow, _InstanceScale); float3 scaledPosition = input.positionOS.xyz * instanceScale; float3 positionWS = TransformObjectToWorld(scaledPosition); float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
output.positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _LightDirection));
#if UNITY_REVERSED_Z output.positionCS.z = min(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE); #else output.positionCS.z = max(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE); #endif
return output; }
half4 ShadowPassFragment(Varyings input) : SV_Target { return 0; } ENDHLSL } } Fallback "Universal Render Pipeline/Lit" }
|