Real-Time Local Reflections


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.


Posted Image

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  Posted Image  (5 fps or 172 ms).
The video above was generated on a NVIDIA GTX 470 at 50-60 fps.  Posted Image

Resources

My Implementation


I hope the shader shown below is pretty self-explanatory  Posted Image
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
They also mention that they add jitering tot hide noticeable step artifacts. I did not implement this.

Fade out when reflection vector faces viewer


Posted Image



Fade out when reflection samples reach screen border


Posted Image


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.

Posted Image


So tips, comments and ideas are very welcome!

You can download the executable of the test project here: Attached File    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). 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值