Local Reflection是一个非常有创意的优化方法,用很小的代价获得了实时反射,这里的方法应该是和Crysis类似的,采了nSample次Depth来得到相交的点,其实不需要这么麻烦,后面我会专门写一个blog来详细描述wow是怎么用更优雅的方式达到更好的效果。
I've been experimenting a little bit with Real-Time Local Reflections (RLR) (or Screen Space Reflections).
With this technique you ray trace in screen space to approximate local reflections.

http://www.youtube.com/watch?v=6nTqxKBjYBw
As you can see my current implementation is far from perfect. And slow as hell on my laptop with a NVIDIA GT 435M
The video above was generated on a NVIDIA GTX 470 at 50-60 fps.
Resources
- I think this technique was first shown on the Beyond3D forums by the user Graham: http://forum.beyond3...ead.php?t=56095 .
- Crytek presentation for Siggraph 2011 "Secrets of CryENGINE 3 Graphics Technology" by Sousa, Kasyan and Schulz (slide 29 - 32).
- This movie on YouTube claims to show the difference between Real-Time Local Reflections on and off in Crysis 2: http://www.youtube.c...?v=907vQsHofPM. (I haven't played the game myself yet, must do!)
- Luminous Engine by Square Enix http://www.youtube.com/watch?v=8kzNSvSnCFY
My Implementation
I hope the shader shown below is pretty self-explanatory
First I calculate the reflection vector in view space. Then I transform it into screen space and I start to ray march according to the view space reflection vector until the depth in the sampled depth buffer is bigger than our current depth of our ray.
Currently I render the scene twice (the 1 st time without the reflections, the 2 nd time with). In this way I can use the depth buffer of the 1 st pass for my reflections in the 2 nd one. But of course with a deferred renderer you can use the depth buffer from your G-buffer and sample the reflected pixel color from the previous frame.
// Calculate View Space Reflection Vector!
float3 vspReflect = reflect(normalize(Input.ViewPos), normalize(Input.ViewNormal));
// Normalize, in this way we only need to check the .z component
// to know how hard the reflection vector is facing the viewer
vspReflect = normalize(vspReflect);
// If the view space reflection vector is facing to hard to the viewer
// then there is a high chance there is no available data!
#ifdef FADETOVIEWER
if (vspReflect.z > g_rlrOptions.y)
{
// We want to smoothly fade out the reflection when facing the viewer.
// Calculate this factor ...
float rcpfadefact = rcp(1.0 - g_rlrOptions.y /* minimum .z value of reflection */ );
float faceviewerfactor = (vspReflect.z - g_rlrOptions.y) * rcpfadefact;
#endif
// Transform the View Space Reflection to Screen Space
// This because we want to ray march in to the depth buffer in Screen Space (thus you can use the default hardware depthbuffer)
// Depth is linear in Screen Space per Screen Pixel
float3 vspPosReflect = Input.ViewPos + vspReflect;
float3 sspPosReflect = mul(float4(vspPosReflect, 1.0), g_mProj).xyz / vspPosReflect.z;
float3 sspReflect = sspPosReflect - Input.ScreenPos;
// Resize Screen Space Reflection to an appropriate length.
// We want to catch each pixel of the screen
float scalefactor =
g_rlrOptions2.y /* size of 1 pixel in screen space (I took this in the width (2/1280) because the width is almost always bigger than the height */
/ length(sspReflect.xy);
scalefactor *= g_rlrOptions.x /* how many pixels at once (value = 1) */;
sspReflect *= scalefactor;
// Initial offsets
// .xy for Screen Space is in the range of -1 to 1. But we want to sample from
// a texture, thus we want to convert this to 0 to 1.
float3 vCurrOffset = Input.ScreenPos + sspReflect ;
vCurrOffset.xy = float2(vCurrOffset.x * 0.5 + 0.5,vCurrOffset.y * -0.5 + 0.5);
float3 vLastOffset = Input.ScreenPos;
vLastOffset.xy = float2(vLastOffset.x * 0.5 + 0.5,vLastOffset.y * -0.5 + 0.5);
sspReflect = float3(sspReflect.x * 0.5 ,sspReflect.y * -0.5, sspReflect.z);
// Number of samples
int nNumSamples = (int)(g_rlrOptions2.x /* width of backbuffer (e.g. 1280) */ / g_rlrOptions.x) /* how many pixels at once (usualy 1) */;
int nCurrSample = 0;
// Calculate the number of samples to the edge! (min and maximum are 0 to 1)
#ifndef DEBUGRLR
float3 samplestoedge = ((sign(sspReflect.xyz) * 0.5 + 0.5) - vCurrOffset.xyz) / sspReflect.xyz;
samplestoedge.x = min(samplestoedge.x, min(samplestoedge.y, samplestoedge.z));
nNumSamples = min(nNumSamples, (int)samplestoedge.x);
#endif
float3 vFinalResult;
float vCurrSample;
float2 dx, dy;
dx = ddx( vCurrOffset.xy );
dy = ddy( vCurrOffset.xy );
while (nCurrSample < nNumSamples)
{
// Sample from depth buffer
vCurrSample = txPrevFrameDepth.SampleGrad(g_samParaboloid, vCurrOffset.xy, dx, dy).x;
if (vCurrSample < vCurrOffset.z)
{
// Calculate final offset
vLastOffset.xy = vLastOffset.xy + (vCurrSample - vLastOffset.z) * sspReflect.xy;
// Get Color
vFinalResult = txPrevFrameDiffuse.SampleGrad(g_samParaboloid, vLastOffset.xy, dx, dy).xyz;
const float blendfact = 0.6;
float2 factors = float2(blendfact, blendfact);
#ifdef FADETOVIEWER
// Fade to viewer factor
factors.x = (1.0 - faceviewerfactor);
#endif
#ifdef FADETOEDGES
// Fade out reflection samples at screen edges
float screendedgefact = saturate(distance(vLastOffset.xy , float2(0.5, 0.5)) * 2.0);
factors.y = screendedgefact;
#endif
// Blend
fvTotalDiffuse.xyz = lerp(vFinalResult, fvTotalDiffuse, max(max(factors.x /* linear curve */, factors.y * factors.y /* x^2 curve */), blendfact));
nCurrSample = nNumSamples + 1;
}
else
{
++nCurrSample;
vLastOffset = vCurrOffset;
vCurrOffset += sspReflect;
}
#ifdef DEBUGRLR
// Debugging....
if ((vCurrOffset.z < 0.0) || (vCurrOffset.z > 1.0) )
{
// Debug: Show blue color
vFinalResult = float3(0.0, 0.0 ,1.0);
fvTotalDiffuse = float3(0.0, 0.0, 1.0);
nCurrSample = nNumSamples + 1;
}
else if ( (vCurrOffset.x < 0.0) || (vCurrOffset.x > 1.0) || (vCurrOffset.y < 0.0) || (vCurrOffset.y > 1.0))
{
// Debug: Show red color
fvTotalDiffuse = float3(1.0, 0.0, 0.0);
nCurrSample = nNumSamples + 1;
}
#endif
}
#ifdef FADETOVIEWER
}
else
{
}
#endif
As you can see my implementation contains 2 techniques, as mentioned in the Crytek presentation, in order to hide broken reflections.
- Smoothly fade out if the reflection vector faces viewer as no data is available
- Smoothly fade out reflection samples at screen edges
Fade out when reflection vector faces viewer

Fade out when reflection samples reach screen border

Remaining problems
Currently the biggest problem that I still have is for the areas where there is no information (see screen shot below).
I'm thinking to experiment with comparing the depth of the neighboring pixels of the reflection intersection point in screen space.
If the difference is too big, fade away or something like that … not sure yet. But that will be for a next blog post.

So tips, comments and ideas are very welcome!
You can download the executable of the test project here:
RealTimeLocalReflections_LitheonJan2012.rar
(2.36MB)
downloads: 979 . (z=forward, s=backward, q=left, d=right (i'll adapt this later for qwerty))
But you will need a DirectX 11 video card (I've got feature level 11 enabled).
本文介绍了一种名为实时局部反射(RLR)的技术,该技术通过屏幕空间光线追踪近似实现局部反射效果。文中详细展示了作者的实现过程,并讨论了如何平滑地隐藏破损的反射效果。此外,还提供了一个可供下载的测试项目。
1345

被折叠的 条评论
为什么被折叠?



