Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

kode80SSR - Free Screen Space Reflections component

Discussion in 'Assets and Asset Store' started by kode80, Mar 24, 2015.

  1. kode80

    kode80

    Joined:
    Aug 1, 2013
    Posts:
    151
  2. chingwa

    chingwa

    Joined:
    Dec 4, 2009
    Posts:
    3,789
    I said this before, but I'll say it again.... UNBELIEVABLE!!!! :D
     
  3. shkar-noori

    shkar-noori

    Joined:
    Jun 10, 2013
    Posts:
    833
    THANKS, for your contributions to the community, really appreciated!!
     
  4. lilymontoute

    lilymontoute

    Joined:
    Feb 8, 2011
    Posts:
    1,181
    Fantastic.
     
  5. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Thanks. :cool:
     
  6. LudvigF

    LudvigF

    Joined:
    Mar 11, 2015
    Posts:
    3
    I love it!
     
  7. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,729
    Can this or SSR in general be used for window reflections or mirror reflections?

    I'm fiddling with an arch viz scene in Unity trying to get some insight into the new shaders and graphical features of Unity 5 and the ugliest thing in my scene at the moment are the windows. If it's dark outside and light inside an apartment you would expect the windows to act like mirrors, but no matter how I tweak the SSR component I can't get the windows to look at all like real windows.

    One issue seems to be that immediately when I change a material from opaque to transparent most, if not all, reflections go away immediately. Another issue is that the ray distance or something is way too short. I would expect to be able to see the entire room in the window reflection, but I can barely see the objects standing right next to the window, the rest is just seemingly grey fog.

    I know this isn't all that related to SSR, but I needed to subscribe to this thread and it's at least somewhat related. :p
     
  8. chingwa

    chingwa

    Joined:
    Dec 4, 2009
    Posts:
    3,789
    SSR won't work for objects that are outside the camera frustrum, or behind the camera... so if you want to do a mirror or a super reflective window they won't reflect anything with SSR when looking at them head-on. You would need to also use planar reflections for these objects... at least for the mirror. Glass isn't anywhere near as reflective as a mirror is though, and really you should only be able to see defined reflection in it from extreme angles...
     
  9. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,729
    Ahh, obviously. Completely forgot about the screen space part of it all.

    I guess there isn't any games with properly reflecting windows then? That's probably why all the Unreal Engine 4 arch viz demos decide to go for completely white overexposed windows as if you're looking through a camera with fixed exposure. I really don't want that though, I want everything to be dynamic. :)

    Guess I will have to look at some of the more recent AAA games to see if they do anything to try and get seemingly realistic reflecting windows.

    But thanks for the reply. At least I won't waste any more time trying to get this to work. I'll see if I can at least simulate some sort of usable reflections with reflection probes.

    I very much disagree with your opinion about the reflectivity of glass though. :p Just google "Window reflection" and you'll see that glass is extremely reflective and behaves exactly like a mirror if one side is lit and the other side is dark. I.e. like one-way mirrors in interrogation rooms. But as a human you're also able to either focus through the window or on the reflections, which is not easy to simulate in a game setting I guess. :p
     
  10. chingwa

    chingwa

    Joined:
    Dec 4, 2009
    Posts:
    3,789
    Well certainly, real life light and object properties are vastly more complex than anything we can simulate in a game or real-time environment :) However in general "glass" does follow typical rules for reflectance as other dielectric objects do. The more direct the facing angle of the glass (in relation to your viewpoint) the less reflective it will appear to be overall, whereas the more extreme the angle becomes the more reflective it becomes.

    This reflectance property doesn't really change if the environment beyond the glass becomes dark... but this is a complex subject and you're right human perception probably complicates it all.

    A nice physically-based exposure control would certainly help the issue, but I haven't seen an adequate one in Unity, and trying to get the built-in auto tonemapper to do this for you is an exercise in futility.

    Your idea of using a box-projected reflection probe would probably work fine. Though I still think planar reflection system would work best.

    Incidentally, it occurs to me that you might want to look into "carpaint" shaders, as they often also address windshield glass as well...
     
  11. chingwa

    chingwa

    Joined:
    Dec 4, 2009
    Posts:
    3,789
    But this doesn't preclude SSR and this system by kode80 from being important... a good SSR system, combined with more specific(planar or box-projected) reflections complement each other very nicely. Personally I'll be looking at integrating this into almost all the projects I do from here on out.... :)
     
  12. kode80

    kode80

    Joined:
    Aug 1, 2013
    Posts:
    151
    When it comes to proper mirror-like surfaces, planar reflections will definitely give you the best quality but add a lot of complexity if you want to balance performance - especially if you're looking to have lots of windows and/or have a highly complex scene to begin with. Reflection probes are almost certainly your best bet here and if you look at even the latest AAA games are what every one resorts to for windows (often at comically low resolution at that). Unfortunately SSR doesn't work very well with transparencies since transparent materials are rendered in a forward pass and SSR needs the deferred info to work.
     
    Last edited: Mar 26, 2015
  13. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    Giving this a bump as it deserves it
     
  14. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,367
    @kode80, very good job. It is almost production ready.

    Can you tell me how do we control the reflectivity of a surface?
    Just by the metallicity of the shader?
     
  15. sipon

    sipon

    Joined:
    Feb 8, 2009
    Posts:
    143
    Yep, awesome share ! Tks a lot !
     
  16. kode80

    kode80

    Joined:
    Aug 1, 2013
    Posts:
    151
    Currently yes, although it's a one line code change to switch that to use the roughness value if that works better for your project (use the alpha component rather than specular power from the GBuffer texture).
     
    elias_t likes this.
  17. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,367
    Thanks for the info!

    How difficult is it to make it work with other shaders then the new unity5 standard shader?
     
  18. kode80

    kode80

    Joined:
    Aug 1, 2013
    Posts:
    151
    Not difficult at all, the only requirement is that you're using deferred rendering. The only tie to the standard shader is using specular/smoothness to affect reflection blending/blur. You can easily remove all references to spec/smooth and replace them with your own implementation. For alpha, just remove the specular part (it'll still fade reflections based on distance to screen edge, iteration count etc.) and then either remove the blur pass altogether or use some other property in your implementation to control it.
     
  19. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,367
    Thanks again!
     
  20. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Giving this a bump as it deserves it
     
  21. Sir-Spunky

    Sir-Spunky

    Joined:
    Apr 7, 2013
    Posts:
    132
    @kode80

    Hi. This is a beautiful plugin and just what Unity needs to reach AAA quality. However, I get some unexpected artifacts that I can't get rid of.

    Here's a screenshot:
    http://i.imgur.com/Z553BO6.jpg

    Look beneath the tree branches and you can see the strange reflections. I tried different settings, and usually use Iterations 20 and Pixel Stride 20, but I get the same problem with Pixel Stride 1.

    The ground seems to be part of it, because I don't get those reflections looking at the sky. It's strange, because I even set Metallic and Smoothness to 0 on the ground but I still get reflections on it. I tried different scales on the ground plane and different clipping settings on the camera, but no difference.

    EDIT: Here's another screen with a more basic ground texture:
    http://i.imgur.com/05XTHun.jpg
     
    Last edited: Apr 8, 2015
  22. Sir-Spunky

    Sir-Spunky

    Joined:
    Apr 7, 2013
    Posts:
    132
    I think I got rid of the artifacts by changing from "Gamma" to "Linear" Color Space in Player Settings. I don't know why I had it set to Gamma, probably a leftover from an old Unity 4 project. Sorry about that, and thanks for releasing this amazing plugin for free.
     
  23. constantino

    constantino

    Joined:
    Jul 29, 2014
    Posts:
    1
    Hi @kode80

    I tried the kode80SSR and is awesome =)... is a lack that Unity 5 don´t came with it!!... you done a great job ;)

    But I notice that for dark Diffuse/Albedo Colors the SSR doesn´t reflect! You know why??
    It´s any kind of multiplication in the shader ??

    keep up the good work! ;)
     
  24. jessyOb

    jessyOb

    Joined:
    Feb 1, 2015
    Posts:
    4
    Hi kode!
    Thank you for your time and your Shader! :D

    I have a problem with artifacts during the movement of the camera... If i set ( in Roughness ) Max Blur Radius to 1, the problem disappear. When i try to set it to 2 or more, artifacts appears again. I think it depend on Normals Depth. So... Again, thank you good work!
     
  25. Ghosthowl

    Ghosthowl

    Joined:
    Feb 2, 2014
    Posts:
    228
  26. 2dchaos

    2dchaos

    Joined:
    Sep 11, 2013
    Posts:
    63
    Amazing effect, is there any way to enable horizontal reflections(eg: like mirrors)?
     
  27. one_one

    one_one

    Joined:
    May 20, 2013
    Posts:
    621


    Anyway, great job! I think the shot with the tiled walls shows pretty well just how useful this effect can be.
     
    2dchaos likes this.
  28. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,729
    @2dchaos This already works for horizontal reflections. But like chingwa said when I asked originally:
    Meaning your mirrors or horizontal reflections will only work for glancing angles.

    PS: I assume you mean vertical as I would consider a floor to be horizontal. :p
     
    2dchaos likes this.
  29. kenshin

    kenshin

    Joined:
    Apr 21, 2010
    Posts:
    940
    Great work, thanks for sharing!
     
  30. SlimeEnthusiast

    SlimeEnthusiast

    Joined:
    Mar 2, 2015
    Posts:
    7
    I'm finding that reflections still appear on materials that have their Metallic set to 0. Am I misunderstanding functionality, or is there a way to set it up so that 0 Metallic (or 0 Smoothness as I'm eventually hoping to use) translates to no reflection?
     
  31. SlimeEnthusiast

    SlimeEnthusiast

    Joined:
    Mar 2, 2015
    Posts:
    7
    Changing the reflectivity to run off of smoothness seemed to fix my problem. Thanks for the great (and free) asset!
     
  32. VertexSoup

    VertexSoup

    Joined:
    Nov 25, 2014
    Posts:
    44
    @kode80 this is great free asset! If you have time please continue with dev and support for this. You done GREAT job so far!

    Hmm maybe I'm doing something wrong or this effect is so subtle I can barley notice it. Is there any way how to pronounce it?
    I'm on deferred&linear, SSR is first script at camera (order secured by prefab)... In examples it is working as it should. Maybe I need to change it to smoothnes as well.
    @SlimeEnthusiast can you share where in code you made which changes? Line numbers etc. Thank You!
     
  33. SlimeEnthusiast

    SlimeEnthusiast

    Joined:
    Mar 2, 2015
    Posts:
    7
    @VertexSoup In SSR.shader I changed line 280 to

    Code (CSharp):
    1. float3 specularStrength = specRoughPixel.a;
    Also, commenting out line 306 as indicated in the comments above it seems to help a lot if what you're looking for is a simple "more smooth = more reflection" kind of SSR.
     
    VertexSoup likes this.
  34. VertexSoup

    VertexSoup

    Joined:
    Nov 25, 2014
    Posts:
    44
    Thank you Slime.
     
  35. kode80

    kode80

    Joined:
    Aug 1, 2013
    Posts:
    151
    If you have been experiencing any of the artifacts mentioned above it'd be great if you could join in this issues thread on github https://github.com/kode80/kode80SSR/issues/5 I've managed to rule out the DepthNormals camera setting being an issue and I'm still not able to replicate these artifacts on my machine so the more people that could contribute info from their projects/machines would be helpful in debugging this.

    thanks!
     
    VertexSoup likes this.
  36. VertexSoup

    VertexSoup

    Joined:
    Nov 25, 2014
    Posts:
    44
    Can you PM me you private email address @kode80 ? I have problems with line renderer component. It stretches bottom edge of it so it does not look like 5cm tape but like big vertical smudge.. Maybe some internals preventing it to be sampled correct way?
     
  37. kode80

    kode80

    Joined:
    Aug 1, 2013
    Posts:
    151
    I've updated the main kode80SSR repo. Smoothness is now used to control reflection strength by default (rather than metal). I've also merged in the blur fix by @jessyOb.

    I just tested the LineRenderer component in my test scene and figured out what the issue is. kode80SSR uses a backface depth buffer to calculate geometry thickness to allow rays to travel behind objects etc. Since the line renderer doesn't have back faces (it always recalculates it's mesh to face the camera) it doesn't get rendered to the backface depth buffer. This means that it's thickness is calculated based on whatever geometry is *behind* the line, resulting in the raycasting step thinking the line is thicker than it is (often infinitely thick if there's nothing behind it) and returning false positives - this is why you get the smears.

    As soon as I figured this out I immediately remembered the issue @Sir Spunky mentioned above with the tree's in his scene. Since the leaves of the tree are billboards, they also don't render back faces - which is why in his screenshots you're seeing the same smearing reflections from the leaves.

    For your line rendering, the best solution I can suggest would be to implement your own line renderer that generates real 3D lines with back faces, e.g. extrude a triangle/square/circle. This would be super simple to implement (I've created several variations on this kind of thing over the years for different purposes) and would give you the best results with re: to SSR.

    For billboard type geometry in general I would definitely suggest using custom shaders. For example, disable backface culling and offset the backfaces slightly so they add to the thickness buffer.

    The alternative would be for me to update kode80SSR to allow disabling thickness calculation and fallback to the constant pixel thickness that most other SSR implementations use. This would be a quick fix and would solve the issues with any kind of billboards in a scene (such as the leaves on trees), however it greatly reduces the flexibility of SSR as now every pixel in the scene is considered the same thickness and you have to tweak the SSR properties based on scene (and scenes with big geometry thickness variations will never be totally correct).
     
    VertexSoup likes this.
  38. VertexSoup

    VertexSoup

    Joined:
    Nov 25, 2014
    Posts:
    44
    Thank you for quick reply! Appreciated. I was suspecting it due fact that mesh is generated on runtime and missing that volume information. :) I'm glad I was right. I'll be doing custom implementation cause I need more functionality than line renderer can offer right now. (If you shared something regarding this would like to look at code to incorporate into mine) I use line renderer very often element in my game. So I would need good fix. I'll look into fresh repo today evening cuse I'm coding now and graphics is scheduled for evening/night. Will update you with my findings. I'm just curious that flares are ignored even if they are same geometry type. Or maybe I just didn't noticed yet. :p
    Thank you again and keep up good work @kode80 ! This thing is exactly what I need for correct look in one of my levels, you helped me tremendously with this FREE package!
    Kudos to you. :)
     
  39. kode80

    kode80

    Joined:
    Aug 1, 2013
    Posts:
    151
    SSR relies on deferred GBuffers for reflections. Since flares etc. aren't rendered into the GBuffers they don't affect reflections. Similarly any forward rendering you may have in your scene won't contribute to SSR either.

    I don't have any C# extruding code to hand (all my past efforts have been written in C for my own engine) but pretty much all you have to do is:
    1. Take an array of points that represent the shape you want to extrude
    2. Take an array of points that represent the path you want to extrude along
    3. Create an array of points (shape.Length * path.Length) large
    4. Loop through the path points copying the shape points to the new array, transforming so their position + rotation is correct for each segment of the line
    5. Create an array of ints ((shape.Length * path.Length - 1) * 6) large
    6. Loop through the path points creating 2 triangles (a quad) for each line in your shape between the current segment and the next one by adding the appropriate indices to your ints array
    Once you've initialized the mesh as above, you can just update the vertices when your path changes without having to regenerate the arrays/indices.
     
    VertexSoup likes this.
  40. VertexSoup

    VertexSoup

    Joined:
    Nov 25, 2014
    Posts:
    44
    I'm something like intermediate at C# :) I was just asking for potential reference to speed things up and compare. I'll recycle tube renderer implementation :p should be pretty straightforward. I'm just such a nerd that like to look at other's people code to learn some new tricks..
    Thank you, you are very helpful person indeed. If you interested I can offload you some shader work in future (from your background I think you are quite at home in this area:). After IndieGoGo campaign will finish. Right now I have my hands full of preparation for it so this came REALLY handy at right time.
     
  41. kode80

    kode80

    Joined:
    Aug 1, 2013
    Posts:
    151
    Cool, no problem. Sure, always interested in potential work, email is: ben AT kode80
     
    VertexSoup likes this.
  42. CoderPro

    CoderPro

    Joined:
    Feb 21, 2014
    Posts:
    327
    Do you have plan to make it with mobiles support ?
     
  43. ksam2

    ksam2

    Joined:
    Apr 28, 2012
    Posts:
    1,079
    Does it will work with other shaders like Alloy or it uses its own shader?
     
  44. gratyanolteanu

    gratyanolteanu

    Joined:
    Nov 18, 2014
    Posts:
    24
    Hello. First of all I have to do a little observation...
    If you want to do something of quality in unity.
    You should read the documentation.
    so. The first thing you need to do is change the order of the post effects.
    SSR need to be the first effect (On top)
    Anti Aliasing need to be the last post effect
    And Bloom after SSR
    Tonepammping after Bloom.
    and so on...
    But I also have problems with SSR. At an angle distorts the image and appear black areas, like I looked through binoculars.
     

    Attached Files:

  45. kode80

    kode80

    Joined:
    Aug 1, 2013
    Posts:
    151
    If possible, yes. I looked into it briefly a few weeks back and it definitely seems possible.

    I don't have Alloy so I can't test but as long as it is deferred compatible (which looking at their site it seems it is) then 'yes'. The one potential obstacle I foresee is if Alloy doesn't render roughness info to Unity's GBuffer - however this could be worked around, I'd just have to update the shader to have an Alloy/alternative shader option that uses some other way of getting roughness info.
     
    ksam2 likes this.
  46. kode80

    kode80

    Joined:
    Aug 1, 2013
    Posts:
    151
    Yes, the order of kode80SSR in the camera's effect stack is important since it's working in image space - any effects that alter the frame buffer will be reflected also. So make sure that things you don't want reflected (such as bloom etc.) are *after* kode80SSR in the stack.

    The issue @Sir Spunky mentioned is not related to this though. As mentioned a couple of posts up, the issue he is seeing is due to tree leaves being billboards and thus not having back faces - which messes up the geometry thickness calculations. This could be remedied with a custom shader that disables backface culling on billboards.

    Can you post some screens of the angle-distortion issue you're seeing? My guess is it's the usual SSR deficiency of lacking enough scene pixels for certain reflections - but I'd need to see it to be sure.
     
  47. gratyanolteanu

    gratyanolteanu

    Joined:
    Nov 18, 2014
    Posts:
    24
    You can replicate the artifacts if you put the Other Post effcts above SSR.
    Like this
    SSAO,
    Bloom
    Tonemapping
    Anti alliasing
    SSR.

    So, the best way is to put SSR the first post effect.
     
  48. kode80

    kode80

    Joined:
    Aug 1, 2013
    Posts:
    151
    Can you post your kode80SSR settings? Looking at your screens it looks like you have an almost perfect mirror and in the most obvious screenshot you're looking directly at the mirror. In this case SSR won't work since it has to reflect things that don't exist in the frame buffer such as geometry behind the camera etc. The reason I ask about your kode80SSR settings is these reflections should be faded out using the "Eye Fade" properties - which would make those 'distortions' invisible.

    i.e. I don't think the issues you're talking about have anything to do with effect order in the stack - they're just deficiencies of SSR in general and are handled by specific properties on the component.
     
  49. gratyanolteanu

    gratyanolteanu

    Joined:
    Nov 18, 2014
    Posts:
    24
    Yes Shure.
    I understud His problem, but the first thing I noticed in the picture he posted was the order of Post Effcets.
    And someone below complained of artifacts.
    So. Somehow I try to respond to 2 in one post.
     

    Attached Files:

  50. gratyanolteanu

    gratyanolteanu

    Joined:
    Nov 18, 2014
    Posts:
    24
     

    Attached Files:

    ksam2 likes this.