9.2.2 urp多光源
Shader "Custom/chap9.2.2/Multi Light"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color("Main Color", Color) = (1,1,1,1)
[Toggle(_AdditionalLights)] _AddLights ("AddLights", Float) = 1
}
SubShader
{
Tags { "RenderPipeLine"="UniversalRenderPipeline" "RenderType"="Opaque" "LightMode"="UniversalForward" }
HLSLINCLUDE
#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.core/ShaderLibrary/SpaceTransforms.hlsl"
CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST;
half4 _Color;
CBUFFER_END
ENDHLSL
Pass
{
Tags { "LightMode"="UniversalForward" }
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma shader_feature _AdditionalLights
TEXTURE2D (_MainTex);
SAMPLER(sampler_MainTex);
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal: NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 worldNormal: TEXCOORD1;
float3 worldPos: TEXCOORD2;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = TransformObjectToHClip(v.vertex);
o.worldNormal = TransformObjectToWorldNormal(v.normal);
o.worldPos = TransformObjectToWorld(o.vertex).xyz;
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
float4 frag (v2f i) : SV_Target
{
float3 worldNormal = normalize(i.worldNormal);
Light light = GetMainLight();
float4 texColor = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex ,i.uv);
float3 albedo = texColor.rgb * _Color.rgb;
float3 ambient = _GlossyEnvironmentColor.xyz * albedo;
float3 worldLightDir = TransformObjectToWorldDir(light.direction);
float3 diffuse = light.color.rgb * albedo * saturate(dot(worldNormal, worldLightDir) * 0.5 + 0.5);
float3 col = ambient + diffuse;
#ifdef _AdditionalLights
int pixelLightCount = GetAdditionalLightsCount();
for(int index = 0; index < pixelLightCount; index++)
{
Light light = GetAdditionalLight(index, i.worldPos);
half Deputy_NdotL = saturate(dot(worldNormal,light.direction) * 0.5 + 0.5);
half3 Deputy_Radiance = light.color * ((light.distanceAttenuation * light.shadowAttenuation) * Deputy_NdotL) * _Color.rgb;
col += Deputy_Radiance;
}
#endif
return float4(col, 1.0);
}
ENDHLSL
}
}
}