当前位置:网站首页>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
- at the top of the file,把 #define SHADOW_LOOP_AVERAGE , 去除注释
- 搜索 GetDirectionalShadowAttenuation,并添加代码
shadowD *= saturate(dot(normalWS, wi));
- 搜索 GetPunctualShadowAttenuation,并添加代码
shadowP *= saturate(dot(normalWS, L));
- 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
参考
- SebLagarde-HDRP getting shadow attenuation-https://forum.unity.com/threads/hdrp-getting-shadow-attenuation.863620/
- Material\Lit\Lit.shader 中的 “Forward” Pass(After all, the custom pass seems to be rendering forward…)
抛砖引玉,恳请批评指正!
边栏推荐
猜你喜欢
随机推荐
Oracle database use problems
阿里云OSS上传文件超时 探测工具排查方法
Adobe LiveCycle Designer 报表设计器
【剑指offer】左旋字符串,替换空格,还有类题!!!
Network model (U - net, U - net++, U - net++ +)
网络模型(U-net,U-net++, U-net+++)
Network Models (DeepLab, DeepLabv3)
设置Vagrant创建的虚拟机名称和内存
pycharm中绘图,显示不了figure窗口的问题
MySQL select count(*) count is very slow, is there any optimization solution?
自定义卷积核的分组转置卷积如何实现?
Software custom development - the advantages of enterprise custom development of app software
Three handshakes and four waves
Birth of the Go language
MySql的索引
Typescript基本类型---上篇
Detailed Explanation of the Level 5 Test Center of the Chinese Institute of Electronics (1)-string type string
磁盘管理:磁盘结构
训练一个神经网络要多久,神经网络训练时间过长
软件定制开发——企业定制开发app软件的优势