Unity:2021.3.0f1
HDRP:12.1.6
以下程序均在 自定义全屏通道 中运行,详情
示例场景如图
应该可能是这么写吧(代码出处是Lighting\LightLoop\LightLoop.hlsl)
#pragma multi_compile SHADOW_LOW SHADOW_MEDIUM SHADOW_HIGH SHADOW_VERY_HIGH
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/NormalBuffer.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/PunctualLightCommon.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/HDShadow.hlsl"
NormalData normalData;
DecodeFromNormalBuffer(posInput.positionSS, normalData);
HDShadowContext shadowContext = InitShadowContext();
DirectionalLightData light = _DirectionalLightDatas[_DirectionalShadowIndex];
float3 L = -light.forward;
float3 shadow = GetDirectionalShadowAttenuation(shadowContext,
posInput.positionSS, posInput.positionWS, normalData.normalWS,
light.shadowIndex, L);
这个效果不太对劲,不过显然物体背面是接受不到影子的(毕竟照不到光),所以索性去掉背面的阴影。
shadow = dot(normalData.normalWS, L) < 0 ? 1 : shadow;
确实是剩下影子了,不过这样看有点怪怪,所以这里来给这幅图加一个黑白版lambert着色。
// shadow = dot(normalData.normalWS, L) < 0 ? 1 : shadow;
shadow *= saturate(dot(normalData.normalWS, L));
为做示例,这里加了一盏聚光灯。
额外添加 #define 和 #include
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/NormalBuffer.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/PunctualLightCommon.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/HDShadow.hlsl"
// 添加以下内容
#define LIGHTLOOP_DISABLE_TILE_AND_CLUSTER
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoopDef.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/HDShadowLoop.hlsl"
如下修改代码(代码出处是Sky\HDRISky\HDRISky.shader)
NormalData normalData;
DecodeFromNormalBuffer(posInput.positionSS, normalData);
HDShadowContext shadowContext = InitShadowContext();
// 注释以下几行
// DirectionalLightData light = _DirectionalLightDatas[_DirectionalShadowIndex];
// float3 L = -light.forward;
// float3 shadow = GetDirectionalShadowAttenuation(shadowContext,
// posInput.positionSS, posInput.positionWS, normalData.normalWS,
// light.shadowIndex, L);
// shadow = saturate(dot(normalData.normalWS, L));
float3 shadow3;
ShadowLoopMin(shadowContext, posInput, normalData.normalWS,
LIGHTFEATUREFLAGS_PUNCTUAL | LIGHTFEATUREFLAGS_DIRECTIONAL | LIGHTFEATUREFLAGS_AREA,
DEFAULT_LIGHT_LAYERS, shadow3);
float3 shadow = dot(shadow3, float3(1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0));
![在这里插入图片描述](https://img-blog.csdnimg.cn/2723d58649b54bedab64eb4bb0ef064b.png
可以看到,聚光灯的影子已经出来了,不过物体背面的影子也出来了,下面继续修改,在这里也来加一个黑白版lambert着色。
在你上面写的着色器的同目录下新建一个文件,命名为CustomHDShadowLoop.hlsl
把 HDShadowLoop.hlsl 的所有内容拷贝到 CustomHDShadowLoop.hlsl
HDShadowLoop.hlsl 的完整路径大概是在:
“你的项目\Library\PackageCache\com.unity.render-pipelines.high-definition@12.1.6\Runtime\Lighting\LightLoop\HDShadowLoop.hlsl”
shadowD *= saturate(dot(normalWS, wi));
shadowP *= saturate(dot(normalWS, L));
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/NormalBuffer.hlsl"
#define LIGHTLOOP_DISABLE_TILE_AND_CLUSTER
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/PunctualLightCommon.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/HDShadow.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/LightLoopDef.hlsl"
// 注释掉这行
// #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightLoop/HDShadowLoop.hlsl"
// 改成引用刚刚新建的文件
#include "CustomHDShadowLoop.hlsl"
效果就出来了捏
抛砖引玉,恳请批评指正!