//声明变体并且引用文件
#pragma shader_feature _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
//在数据结构体中声明需要使用的数据
struct Attributes
{float4 positionOS : POSITION;float2 texcoord : TEXCOORD0;#if defined(_ENABLELIGHTING_ON)float3 normalOS : NORMAL;#endif};struct Varyings
{float4 positionHS : SV_POSITION;float2 uv : TEXCOORD0;#if defined(_ENABLELIGHTING_ON)half3 lightColor : TEXCOORD1;#if defined(_ADDITIONAL_LIGHTS)float3 positionWS : TEXCOORD2;float3 normalWS : TEXCOORD3;#endif#endif
};
_ADDITIONAL_LIGHTS_VERTEX 是在顶点Shader中处理平行光额外增加的光源,_ADDITIONAL_LIGHTS是在片元Shader中处理平行光额外增加的光源
顶点Shader:
//光照#if _ENABLELIGHTING_ONfloat3 normalWS = TransformObjectToWorldNormal(v.normalOS.xyz);Light main_light = GetMainLight();half3 main_light_dir = normalize(main_light.direction);half diffuse_term = dot(normalWS, main_light_dir) * 0.5 + 0.5;half3 lightColor = diffuse_term * main_light.color;#ifdef _ADDITIONAL_LIGHTS_VERTEXhalf3 vertexLight = VertexLighting(positionWS, normalWS);lightColor += vertexLight;#elif _ADDITIONAL_LIGHTSo.positionWS = positionWS;o.normalWS = normalWS;lightColor = half3(0, 0, 0);#endifo.lightColor = lightColor;#endif
片元Shader:
#if _ENABLELIGHTING_ON#ifdef _ADDITIONAL_LIGHTSuint pixelLightCount = GetAdditionalLightsCount();for (uint lightIndex = 0u; lightIndex < pixelLightCount; ++lightIndex){Light light = GetAdditionalLight(lightIndex, i.positionWS.xyz);half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);i.lightColor += LightingLambert(attenuatedLightColor, light.direction, i.normalWS);}#endiffinal_color.rgb = i.lightColor * final_color.rgb;
#endif
也就是启用了_ADDITIONAL_LIGHTS_VERTEX宏
也就是启用了_ADDITIONAL_LIGHTS宏