GraphicsLab Project 之 Screen Space Planar Reflection
作者:i_dovelemon
日期:2020-06-23
主题:Screen Space Planar Reflection, Compute Shader
引言
前段时间,同事发来一篇讲述特化版本的 Screen Space Reflection 实现 Planar Reflection 的文章。出于好奇,实验了下,看看效果如何。如下是目前实现出来的基础版本的效果:
原理
对于上图来说, Water Plane 表示水面,上半部分为实际场景的山体,下半部分为以水面为镜像进行反射之后的山体效果。
对于山体上某一个点(图中白色点)来说,它对应的镜像点为黄色点。
我们可以从 Screen Position 以及 Depth Texture 信息,计算出来白点的世界坐标位置 WorldPosition。
然后可以以 Water Plane 所在的平面对该 WorldPosition 作镜像操作,得到 ReflectionPosition。
得到 ReflectionPosition 之后,我们就能够计算出来 ReflectionPostion 所对应的屏幕坐标 Reflection Screen Position。
根据前面的操作,我们就可以知道,此时 Reflection Screen Position 所反射的颜色即为 Screen Positon 所表示的颜色。
基础原理十分简单,但是实际实现的时候,会发现有很多问题。接下里一一讲述。
问题
闪烁
根据上面的原理,可以想到,有多个像素可能会被反射到相同的位置,如下图所示:
这样由于 GPU 执行顺序的不确定性,就会导致画面出现闪烁,如下所示:
针对这样的问题,我们实际需要的反射点是最近的反射点。可以考虑使用 HLSL 中提供的 InterlockedMin/InterlockedMax (参考[1],[2]) 之类的指令,在写入数据时进行大小比较,从而实现保存最近反射点的功能。
前面的指令虽然能够实现大小比较,以此进行排序。但是根据前面的描述,我们实际保存的是反射点的颜色。没有办法只根据颜色进行排序,所以我们需要保存其他便于排序的信息,这里选择使用反射点的 Screen Position。并且按照如下方式进行编码,从而实现获取最近反射点的效果:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
uint2 SrcPosPixel = uint2(DepthPos.x, DepthPos.y);
uint2 ReflPosPixel = ReflPosUV * uint2(ReflectWidth, ReflectHeight); int Hash = SrcPosPixel.y << | SrcPosPixel.x;
int dotCare = ;
InterlockedMin(HashResult[ReflPosPixel], Hash, dotCare);
Encode and Sort
孔洞
根据先前算法的描述,我们知道,我们先要根据 Depth 信息和 Screen Position 信息计算出 World Positon,然后镜像之后,在转化为新的屏幕坐标。在这一系列操作中,由于数值计算的不精确性,导致有些地方没有存储到有效的反射点位置信息,从而导致最终显示时画面上有孔洞的情况,如下图所示:
幸运的是,从结果看这些孔洞并不会聚集在一起,形成大块的黑块。对于这种情况,我们只要在生成反射贴图的时候,检测到没有保存有效位置信息时,遍历下周围的像素,寻找到一个拥有有效像素的值即可解决这个问题,如下代码所示:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
uint Hash = HashTexture[id.xy].x;
if (Hash == 0x0FFFFFFF)
Hash = HashTexture[uint2(id.x, id.y + )].x;
if (Hash == 0x0FFFFFFF)
Hash = HashTexture[uint2(id.x, id.y - )].x;
if (Hash == 0x0FFFFFFF)
Hash = HashTexture[uint2(id.x + , id.y)].x;
if (Hash == 0x0FFFFFFF)
Hash = HashTexture[uint2(id.x - , id.y)].x; if (Hash != 0x0FFFFFFF)
{
uint x = Hash & 0xFFFF;
uint y = Hash >> ;
ReflectionTexture[id.xy] = ColorTexture[uint2(x, y)];
}
else
{
ReflectionTexture[id.xy] = float4(0.0f, 0.0f, 0.0f, 0.0f);
}
Hole
如下是修正孔洞之后的效果:
实现
本文的代码是使用 Unity 实现的,实现起来比较简单。比较坑的地方在于 Unity 里面获取 Projection Matrix 要通过 GL.GetGPUProjectionMatrix (文献[3]) 转化一下才能变成传递到 GPU 上用于渲染的投影矩阵。如下是功能核心的 Compute Shader 代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
// Each #kernel tells which function to compile; you can have many kernels
#pragma enable_d3d11_debug_symbols
#pragma kernel SSPRClear_Main
#pragma kernel SSPRHash_Main
#pragma kernel SSPRResolve_Main //-----------------------------------------------------------------
float4x4 VPMatrix;
float4x4 InvVPMatrix;
uint Width;
uint Height;
uint ReflectWidth;
uint ReflectHeight; //--------------------------------------------------------------------
RWTexture2D<int> ClearHashTexture; [numthreads(, , )]
void SSPRClear_Main(uint3 id : SV_DispatchThreadID)
{
if (id.x < ReflectWidth && id.y < ReflectHeight)
{
ClearHashTexture[id.xy] = 0x0FFFFFFF;
}
} //---------------------------------------------------------------
Texture2D<float> DepthTex;
RWTexture2D<int> HashResult; #define DownSampleFactor (1) float3 Unproject(float3 clip)
{
float4 clipW = float4(clip, 1.0f);
clipW = mul(InvVPMatrix, clipW);
clipW.xyz = clipW.xyz / clipW.w;
return clipW.xyz;
} float2 Project(float3 world)
{
float4 worldW = float4(world, 1.0f);
worldW = mul(VPMatrix, worldW);
worldW.xy = worldW.xy / worldW.w;
worldW.xy = (worldW.xy + float2(1.0f, 1.0f)) / 2.0f;
return worldW.xy;
} [numthreads(, , )]
void SSPRHash_Main(uint3 id : SV_DispatchThreadID)
{
for (uint i = ; i < DownSampleFactor; i++)
{
for (uint j = ; j < DownSampleFactor; j++)
{
uint2 DepthPos = uint2(id.x * DownSampleFactor + i, id.y * DownSampleFactor + j);
if (DepthPos.x < Width && DepthPos.y < Height)
{
float depth = DepthTex.Load(int3(DepthPos.x, DepthPos.y, )).x; if (depth > 0.0f)
{
float2 uv = (DepthPos.xy * 1.0f) / float2(Width, Height);
uv = uv * 2.0f - float2(1.0f, 1.0f);
uv.y = -uv.y; float3 PosWS = Unproject(float3(uv, depth)); if (PosWS.y > 0.0f)
{
float3 ReflPosWS = float3(PosWS.x, -PosWS.y, PosWS.z);
float2 ReflPosUV = Project(ReflPosWS); uint2 SrcPosPixel = uint2(DepthPos.x, DepthPos.y);
uint2 ReflPosPixel = ReflPosUV * uint2(ReflectWidth, ReflectHeight); int Hash = SrcPosPixel.y << | SrcPosPixel.x;
int dotCare = ;
InterlockedMin(HashResult[ReflPosPixel], Hash, dotCare);
}
}
}
}
}
} //------------------------------------------------------------------------------
Texture2D<int> HashTexture;
Texture2D<float4> ColorTexture;
RWTexture2D<float4> ReflectionTexture; [numthreads(, , )]
void SSPRResolve_Main(uint3 id : SV_DispatchThreadID)
{
if (id.x < ReflectWidth && id.y < ReflectHeight)
{
uint Hash = HashTexture[id.xy].x;
if (Hash == 0x0FFFFFFF)
Hash = HashTexture[uint2(id.x, id.y + )].x;
if (Hash == 0x0FFFFFFF)
Hash = HashTexture[uint2(id.x, id.y - )].x;
if (Hash == 0x0FFFFFFF)
Hash = HashTexture[uint2(id.x + , id.y)].x;
if (Hash == 0x0FFFFFFF)
Hash = HashTexture[uint2(id.x - , id.y)].x; if (Hash != 0x0FFFFFFF)
{
uint x = Hash & 0xFFFF;
uint y = Hash >> ;
ReflectionTexture[id.xy] = ColorTexture[uint2(x, y)];
}
else
{
ReflectionTexture[id.xy] = float4(0.0f, 0.0f, 0.0f, 0.0f);
}
}
}
ScreenSpacePlanarReflection
结论
本文只是探索这个方法的可能性,更加复杂的实现,更加高效的优化可以参考文献[4][5],这也是本文主要参考的对象。
相比于传统的绘制场景两边的方法来说,这个方案的性能更加高效,同时也没有 SSR 那样的高需求。在条件满足的情况下,使用该方案能够带来显著的效果提升,推荐可以尝试。
完整代码在这里:https://github.com/idovelemon/UnityProj/tree/master/ScreenSpacePlanarReflection
参考文献
[4] Screen Space Planar Reflection
[5] Optimized Pixel Projected Reflections for Planar Reflectors
GraphicsLab Project 之 Screen Space Planar Reflection的更多相关文章
- GraphicsLab Project学习项目
作者:i_dovelemon 日期:2016 / 05 / 30 主题:3D,Graphics 引言 进公司以来,主要在学习的就是如何保证代码的质量,以前热爱的图形学也放置了.但是,作为游戏程序员,特 ...
- screen space reflection/soft alpha test/
http://www.crytek.com/cryengine/presentations/secrets-of-cryengine-3-graphics-technology 很多宝贝里面 不止题目 ...
- 在Unity中实现屏幕空间反射Screen Space Reflection(4)
第四部分讲一下如何在2D屏幕空间步进光线. http://casual-effects.blogspot.com/2014/08/screen-space-ray-tracing.html 中的代码感 ...
- 在Unity中实现屏幕空间反射Screen Space Reflection(2)
traceRay函数 在上一篇中,我们有如下签名的traceRay函数 bool traceRay(float3 start, float3 direction, out float2 hitPixe ...
- GraphicsLab Project之Diffuse Irradiance Environment Map
作者:i_dovelemon 日期:2020-01-04 主题:Rendering Equation,Irradiance Environment Map,Spherical Harmonic 引言 ...
- 基于屏幕空间的实时全局光照(Real-time Global Illumination Based On Screen Space)
目录 Reflective Shadow Maps(RSM) RSM 的重要性采样 RSM 的应用与缺陷 Screen Space Ambient Occulsion(SSAO) SSAO Blur ...
- screen space shadowmap unity
unity用到了screen space shadow map 1.camera 在light pos 生成depth1 2.screen space depth2 3.根据depth1 depth2 ...
- GraphicsLab Project之辉光(Glare,Glow)效果 【转】
作者:i_dovelemon 日期:2016 / 07 / 02 来源:CSDN 主题:Render to Texture, Post process, Glare, Glow, Multi-pass ...
- updatedepthtexture 和 screen space shadow 开关
2018.0.3f 里面directional light开了shadow 就会有一张updatedepth 如果距离远 没有阴影就没有shadow pass 但是updatedepth没有关掉 管线 ...
随机推荐
- 震惊!Windows Service服务和定时任务框架quartz之间原来是这种关系……
过场CG: 接到公司领导的文件指示,“小熊”需要在6月底去海外执行一个行动代号为[定时任务]的营救计划,这个计划关系到公司某个项目的生死(数据安全漏洞),作战部拟定两个作战方案: 方案一:使用务定 ...
- Shiro (Shiro + JWT + SpringBoot应用)
Shiro (Shiro + JWT + SpringBoot应用) 目录 Shiro (Shiro + JWT + SpringBoot应用) 1.Shiro的简介 2.Shiro + JWT + ...
- Java实现 蓝桥杯VIP 算法训练 麦森数
算法训练 麦森数 时间限制:1.0s 内存限制:256.0MB 问题描述 形如2P-1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果P是个素数,2P-1不一定也是素数.到1998年底, ...
- Java实现 LeetCode 101 对称二叉树
101. 对称二叉树 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2 ...
- Java实现选择问题
选择问题是求一个n个数列表的第k个最小元素的问题. 那么如何寻找n个元素中第k个最小元素呢? package com.liuzhen.chapter4; public class SelectProb ...
- java实现排他平方数
题目标题: 排它平方数 小明正看着 203879 这个数字发呆. 原来,203879 * 203879 = 41566646641 这有什么神奇呢?仔细观察,203879 是个6位数,并且它的每个数位 ...
- 如何安装vue脚手架?
前提(已经安装好node,可以正常使用npm) 一.cmd输入 npm install vue-cli -g ---- 全局安装vue-cli工具 安装好过后,再输入指令 vue --version ...
- 全网最全postman接口测试教程和接口项目实战~从入门到精通!!!
Postman实现接口测试内容大纲一览: 一.什么是接口?为什么需要接口? 接口指的是实体或者软件提供给外界的一种服务. 因为接口能使我们的实体或者软件的内部数据能够被外部进行修改.从而使得内 ...
- python IDE pycharm的安装与使用
Python开发最牛逼的IDE——pycharm (其实其它的工具,例如eclipse也可以写,只不过比较麻烦,需要安装很多的插件,所以说pycharm是最牛逼的) pycharm,下载专业版的,不要 ...
- Andorid中写文件后在电脑上看不到的解决办法
每次通过输出流往SD卡写入文件,连接上电脑,用MTP的方式模拟一个移动磁盘,打开磁盘却没有这样一个文件,而通过adb的方式查看就有,造成这个现象的原因是,每次写入之后,MTP的数据库并没有更新,因为更 ...