UNITY URP主光及点光源等获取

对无法在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文件:

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值