[email protected]/manua...">

当前位置:网站首页>HDRP shader to get shadows (Custom Pass)

HDRP shader to get shadows (Custom Pass)

2022-08-11 09:25:00 East Bear in the morning

环境

Unity:2021.3.0f1
HDRP:12.1.6
以下程序均在 Custom full screen channel 中运行,详情

An example scene is shown in Fig
在这里插入图片描述

直接光阴影

It should probably be written that way(代码出处是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);

在这里插入图片描述

Remove the shadow on the back

This effect is not quite right,But obviously the back of the object cannot accept the shadow(After all, there is no light),So simply remove the shadow on the back.

shadow = dot(normalData.normalWS, L) < 0 ? 1 : shadow;

在这里插入图片描述

着色

There are indeed shadows left,But it looks kind of weird,So here is a black and white version of this picturelambert着色.

// shadow = dot(normalData.normalWS, L) < 0 ? 1 : shadow;
shadow *= saturate(dot(normalData.normalWS, L));

在这里插入图片描述

所有光源

for example,Added a spotlight here.
在这里插入图片描述

另一个API

额外添加 #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();

// Comment out the following lines
// 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在这里插入图片描述

可以看到,The shadow of the spotlight has come out,But the shadow behind the object also came out,下面继续修改,Here's a black and white version as welllambert着色.

创建 CustomHDShadowLoop.hlsl

in the same directory as the shader you wrote above新建一个文件,命名为CustomHDShadowLoop.hlsl
在这里插入图片描述
HDShadowLoop.hlsl 的所有内容拷贝到 CustomHDShadowLoop.hlsl

HDShadowLoop.hlsl The full path is presumably in :
“你的项目\Library\PackageCache\[email protected]\Runtime\Lighting\LightLoop\HDShadowLoop.hlsl”

在这里插入图片描述

修改 CustomHDShadowLoop.hlsl

  1. at the top of the file,把 #define SHADOW_LOOP_AVERAGE , 去除注释
    在这里插入图片描述
  2. 搜索 GetDirectionalShadowAttenuation,并添加代码
shadowD *= saturate(dot(normalWS, wi));

在这里插入图片描述

  1. 搜索 GetPunctualShadowAttenuation,并添加代码
shadowP *= saturate(dot(normalWS, L));

在这里插入图片描述

  1. Finally go back to your own shader,修改一下头文件
#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"
// Change to refer to the newly created file
#include "CustomHDShadowLoop.hlsl"

The effect came out
在这里插入图片描述

参考

  1. SebLagarde-HDRP getting shadow attenuation-https://forum.unity.com/threads/hdrp-getting-shadow-attenuation.863620/
  2. Material\Lit\Lit.shader 中的 “Forward” Pass(After all, the custom pass seems to be rendering forward…)

抛砖引玉,恳请批评指正!

原网站

版权声明
本文为[East Bear in the morning]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110907163589.html