当前位置:网站首页>HDRP shader 获取阴影(Custom Pass)
HDRP shader 获取阴影(Custom Pass)
2022-08-11 09:08:00 【凌晨出没的东熊】
环境
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));
所有光源
为做示例,这里加了一盏聚光灯。
另一个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();
// 注释以下几行
// 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
在你上面写的着色器的同目录下新建一个文件,命名为CustomHDShadowLoop.hlsl
把 HDShadowLoop.hlsl 的所有内容拷贝到 CustomHDShadowLoop.hlsl
HDShadowLoop.hlsl 的完整路径大概是在:
“你的项目\Library\PackageCache\[email protected]\Runtime\Lighting\LightLoop\HDShadowLoop.hlsl”
修改 CustomHDShadowLoop.hlsl
- 在文件最顶上,把 #define SHADOW_LOOP_AVERAGE , 去除注释
- 搜索 GetDirectionalShadowAttenuation,并添加代码
shadowD *= saturate(dot(normalWS, wi));
- 搜索 GetPunctualShadowAttenuation,并添加代码
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"
效果就出来了捏
参考
- SebLagarde-HDRP getting shadow attenuation-https://forum.unity.com/threads/hdrp-getting-shadow-attenuation.863620/
- Material\Lit\Lit.shader 中的 “Forward” Pass(毕竟自定义通道似乎都是向前渲染来着…)
抛砖引玉,恳请批评指正!
边栏推荐
- OAuth Client默认配置加载
- 工业检测深度学习方法综述
- Filesystem Hierarchy Standard
- SDUT 2877: angry_birds_again_and_again
- 四级独创的阅读词汇表
- 深度学习100例 —— 卷积神经网络(CNN)识别眼睛状态
- ImportError: /usr/local/cuda-11.2/lib64/libcublas.so.10: version `libcublas.so.10‘ not found
- 向日葵安装教程--向日葵远程桌面控制
- Continuous Integration/Continuous Deployment (2) Jenkins & SonarQube
- WiFi cfg80211
猜你喜欢
STM32之串口传输结构体
IPQ4019/IPQ4029 support WiFi6 MiniPCIe Module 2T2R 2×2.4GHz 2x5GHz MT7915 MT7975
excel将数据按某一列值分组并绘制分组折线图
Jupyter Notebook 插件 contrib nbextension 安装使用
基础SQL——DDL
新一代开源免费的轻量级 SSH 终端,非常炫酷好用!
【wxGlade学习】wxGlade环境配置
Rust从入门到精通06-函数
LoRa芯片的特征
Audio and video + AI, Zhongguancun Kejin helps a bank explore a new development path | Case study
随机推荐
MATLAB实战Sobel边缘检测(Edge Detection)
中国电子学会五级考点详解(一)-string类型字符串
golang 字符串操作
MongoDB 对索引的创建查询修改删除 附代码
Features of LoRa Chips
Interview questions about Android Service
wordpress插件开发02-首页文章自动摘要插件开发
Nuget can't find the package problem
向日葵安装教程--向日葵远程桌面控制
腾讯电子签开发说明
Quickly submit a PR (Web) for OpenHarmony in 5 minutes
当你领导问你“还有其他的么”
Essential C# scripting skills for Unity developers
Filesystem Hierarchy Standard
LoRa芯片的特征
Getting Started with Kotlin Algorithms Calculating Prime Factors
Design of Cluster Gateway in Game Server
【系统梳理】微服务的注册和发现中心
软件定制开发——企业定制开发app软件的优势
The no-code platform helps Zhongshan Hospital build an "intelligent management system" to realize smart medical care