对无法在ShaderGraph内获取光源信息,需要自定义.hlsl文件,然后在CustomNode内进行加载,具体代码如下:
#ifndef CUSTOM_LIGHTING_INCLUDED
#define CUSTOM_LIGHTING_INCLUDED
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
#pragma multi_compile _ _SHADOWS_SOFT
void MainLight_float(float3 WorldPos, out float3 Direction, out float3 Color, out float DistanceAtten, out float ShadowAtten)
{
#if SHADERGRAPH_PREVIEW
Direction = float3(-0.5, 0.5, 0);
Color = float3(1, 0.95, 0.8);
DistanceAtten = 1;
ShadowAtten = 1;
#else
#if SHADOWS_SCREEN
float4 clipPos = TransformWorldToHClip(WorldPos);
float4 shadowCoord = ComputeScreenPos(clipPos);
#else
float4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
#endif
Light mainLight = GetMainLight(shadowCoord);
Direction = mainLight.direction;
Color = mainLight.color;
DistanceAtten = mainLight.distanceAttenuation;
ShadowAtten = mainLight.shadowAttenuation;
#endif
}
void MainLight_half(float3 WorldPos, out half3 Direction, out half3 Color, out half DistanceAtten, out half ShadowAtten)
{
#if SHADERGRAPH_PREVIEW
Direction = half3(0.5, 0.5, 0);
Color = half3(1, 0.95, 0.8);
DistanceAtten = 1;
ShadowAtten = 1;
#else
#if SHADOWS_SCREEN
half4 clipPos = TransformWorldToHClip(WorldPos);
half4 shadowCoord = ComputeScreenPos(clipPos);
#else
half4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
#endif
Light mainLight = GetMainLight(shadowCoord);
Direction = mainLight.direction;
Color = mainLight.color;
DistanceAtten = mainLight.distanceAttenuation;
ShadowAtten = mainLight.shadowAttenuation;
#endif
}
#endif
点光及其它光源:
#ifndef CUSTOM_AdditionalLIGHTING_INCLUDED
#define CUSTOM_AdditionalLIGHTING_INCLUDED
#pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
#pragma multi_compile _ _SHADOWS_SOFT
void AdditionalLight_float(float3 WorldPos, out float3 Direction, out float3 Color, out float DistanceAtten, out float ShadowAtten, out bool isAddtalLight)
{
#if SHADERGRAPH_PREVIEW
Direction = float3(-0.5, 0.5, 0);
Color = float3(1, 0.95, 0.8);
DistanceAtten = 1;
ShadowAtten = 1;
isAddtalLight = false;
#else
#if SHADOWS_SCREEN
float4 clipPos = TransformWorldToHClip(WorldPos);
float4 shadowCoord = ComputeScreenPos(clipPos);
#else
float4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
#endif
#if _ADDITIONAL_LIGHT_SHADOWS
isAddtalLight = true;
#else
isAddtalLight = false;
#endif
uint tranPixelLightCount = GetAdditionalLightsCount();
for (uint lightIndex = 0u; lightIndex < tranPixelLightCount; ++lightIndex)
{
Light light = GetAdditionalLight(lightIndex, WorldPos);
Color = light.color;
Direction = light.direction;
DistanceAtten = light.distanceAttenuation;
ShadowAtten = light.shadowAttenuation;
}
#endif
}
void AdditionalLight_half(float3 WorldPos, out half3 Direction, out half3 Color, out half DistanceAtten, out half ShadowAtten , out bool isAddtalLight)
{
#if SHADERGRAPH_PREVIEW
Direction = half3(0.5, 0.5, 0);
Color = half3(1, 0.95, 0.8);
DistanceAtten = 1;
ShadowAtten = 1;
#else
#if SHADOWS_SCREEN
half4 clipPos = TransformWorldToHClip(WorldPos);
half4 shadowCoord = ComputeScreenPos(clipPos);
#else
half4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
#endif
#if _ADDITIONAL_LIGHT_SHADOWS
isAddtalLight = true;
#else
isAddtalLight = false;
#endif
uint tranPixelLightCount = GetAdditionalLightsCount();
for (uint lightIndex = 0u; lightIndex < tranPixelLightCount; ++lightIndex)
{
Light light = GetAdditionalLight(lightIndex, WorldPos);
Color = light.color;
Direction = light.direction;
DistanceAtten = light.distanceAttenuation;
ShadowAtten = light.shadowAttenuation;
}
#endif
}
#endif
在CustomNode下加载.hlsl文件: