• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
  • entries
    3
  • comments
    3
  • views
    35861

Real-Time Local Reflections

Sign in to follow this  
Followers 0
Litheon

26054 views

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.


gallery_63508_366_112019.png

[media]

[/media]



As you can see my current implementation is far from perfect. And slow as hell on my laptop with a NVIDIA GT 435M sad.png (5 fps or 172 ms).
The video above was generated on a NVIDIA GTX 470 at 50-60 fps. smile.png

[subheading]Resources[/subheading]

  • 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


    [subheading]My Implementation[/subheading]
    I hope the shader shown below is pretty self-explanatory smile.png
    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[sup]st[/sup] time without the reflections, the 2[sup]nd[/sup] time with). In this way I can use the depth buffer of the 1[sup]st[/sup] pass for my reflections in the 2[sup]nd[/sup] 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.

      [subheading]Fade out when reflection vector faces viewer[/subheading]

      gallery_63508_366_126881.png




      [subheading]Fade out when reflection samples reach screen border[/subheading]

      gallery_63508_366_95789.png



      [subheading]Remaining problems[/subheading]
      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.

      gallery_63508_366_46272.png



      So tips, comments and ideas are very welcome!

      You can download the executable of the test project here:RealTimeLocalReflections_LitheonJan2012.rar. (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).


2
Sign in to follow this  
Followers 0


2 Comments


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][left]"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"[/left][/font][/color]

[left][font="helvetica, arial, verdana, tahoma, sans-serif"][color="#282828"]Is stopping at (viewport.z > sample ray.z) or (when sample ray is no longer occluded) necessarily correct?[/color][/font][/left]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][left]Imagine 3 spheres [/left][/font][/color]
1 2 3
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][left] o o o[/left][/font][/color]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][left]z=4 z = 2 z = 3[/left][/font][/color]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][left]And the reflection ray starts at 3 and goes towards 1. You will hit a pixel occupied by 2... and callit quits. [/left][/font][/color][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][left]I currently[/left][/font][/color]
0

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now