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

[FREE] Subpixel Morphological Antialiasing (SMAA)

Discussion in 'Assets and Asset Store' started by Chman, May 4, 2015.

  1. Chman

    Chman

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    721
    Hey,

    I needed something better than the infamous (and so bad I'd rather disable AA than use it) FXAA so I ported SMAA to Unity. It's a clean port and uses the original, up to date implementation (2.7) code. It's fast and looks great, even when compared to MSAA 4x.

    Tested with Unity 5+ (Personal or Pro). Works with the deferred & forward rendering paths, in gamma or linear color space, with Directx 9, Directx 11 and OpenGL targets.

    It comes with a few quality presets but you can easily build your own in the inspector. Every inspector setting comes with a help popup so you shouldn't have to dig into the (highly documented) source code.

    Right now it implements SMAA 1x (+ predication). Implementing Temporal SMAA (T2x) should be doable, but the spatial (S2x) and spatial + temporal (4x) variants aren't possible in Unity right now.

    This asset should come as a standard feature in Unity, so it's free, enjoy !

    Help & pull requests are welcome ! I'd love if we could get a complete (temporal) implementation working so that everyone can use it instead of FXAA (FXAA must die).

    And don't forget to check out my other assets (see my signature) ;)
     
    Last edited: May 6, 2015
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Nice! so unity needs to expose stuff for extended versions?
     
  3. Chman

    Chman

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    721
    To enable spatial multisampling (S2x and 4x), unfortunately yes. To put it simply you need to enable MSAA 2x, run SMAA on both subsample buffers and blend both buffers. But Unity doesn't give access to individual MSAA buffers, so...

    On the other end, T2x (temporal) should be possible. The only "problem" is that you need to generate a velocity buffer, which is generally done in vertex shaders. So unless you can find an efficient way to do it in screen space (using RenderWithShader wouldn't help and would probably tank the performances, think drawcalls x2), it has to be injected into your vertex shaders, so it would require a modified Standard shader (and any other you may use). If anyone can think of a good way around that... I would be most grateful :)
     
  4. lazygunn

    lazygunn

    Joined:
    Jul 24, 2011
    Posts:
    2,749
    Very nice cheers! I own colorful and ssao pro and theyre both great (theres a plug for you) so this is appreciated
     
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Lovely stuff. But a mini rant if I may?

    The direction Unity's headed in is terribly sub-optimal. We're seeing vast amounts of redundant calculation that could be stuff calculated earlier or elsewhere in the chain.

    For example colour correction, tone mapping, dof, antialiasing, motion blur, these are all effects that depend on resources that could be shared like velocity buffers, or blurred buffers, it's getting kind of insane and for framerate's sake might need standardising somehow.

    It's not important to a hobbyist but for a console dev or someone targeting lower powered hardware, it's practically half of your frametime gone when really it should just be a single minimal impact.
     
  6. Chman

    Chman

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    721
    You may ! I feel the exact same way. At least the _CameraDepthTexture is only generated once, so there's that.

    I'd love to have a proper and tightly integrated post-processing chain (I would adapt my assets right away to minimize the performance hit) instead of what we currently have, meaning one behaviour per effect. It's fine for two or three effects, but it can quickly get out of control and become terribly inefficient.

    Unless you're willing to dig into the code and merge what you want yourself. Which is what I'm doing in a project of mine, I've modded my Chromatica asset to add tonemapping etc so I get a full color correction stack & tonemapping in a single fast draw call.
     
  7. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Well perhaps we can get something going. If enough clever people sit down together, clever S*** happens. Essentially what we're looking for is a series of solutions for global textures to get generated. We know we need a number of high quality blur buffers and so on.

    I'm thinking much like you can define the deferred shader in Graphics options, you could configure and define textures that must be there. I'm not sure if that's the right place for it. It could be on the camera. Failing this we could just have the best authors work together. The comes a tipping point where it's necessary to collaborate with unity's support. @robert or @Aras might know best.

    For example @sonicether does great bloom. He generates a number of high quality smaller bloom textures. We can and should be using these everywhere in a standardised format if it's generated already.

    That still leaves the issue of combining but at least a lot of the setpass work is done for us. I'd like Unity's input on that. Sorry for derailing your awesome thread, but its semi-relevant at least.

    UE4 has the edge here, it's somewhat locked down and optimised, and Yebis goes much further, doing a number of effects all with minimal passes.
     
  8. Chman

    Chman

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    721
    Don't be, I'd really love to hear from Unity about this. Now, let see if we can summon a few Unity devs on this topic...
     
    Jorge_de_la_Cuesta likes this.
  9. SAOTA

    SAOTA

    Joined:
    Feb 9, 2015
    Posts:
    220
    woot.
     
  10. moure

    moure

    Joined:
    Aug 18, 2013
    Posts:
    184
    Thanks for this! Here is a comparison shot at 4x zoom between no aa , built in fxaa3, and your smaa ultra preset
    NoAA.png FXAA3.png SMAA_ULTRA.png
     
    xpack3, DennG and SAOTA like this.
  11. Chman

    Chman

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    721
    FXAA... More like blurry AA :p

    Anyway, the Ultra preset is generally overkill unless you have a lot of tiny tiny lines and objects. High should be good enough in most cases.

    Thanks for the screenshots !

    FXAA must die.
     
    xpack3, DennG, nxrighthere and 2 others like this.
  12. moure

    moure

    Joined:
    Aug 18, 2013
    Posts:
    184
    Yup fxaa -> no more texture detail or specular highlights for you :D

    edit: thanks for typogenic too, there is no project i dont use it ;)
     
  13. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    FXAA is still very quick though. Grabs his coat.
     
  14. Deleted User

    Deleted User

    Guest

    Wow. This is awesome. Looks fantastic and gave me a bunch of FPS back over FXAA. Thanks for the hard work and the liberal license!
     
  15. MornFall

    MornFall

    Joined:
    Jan 11, 2013
    Posts:
    160
    Thank you !!!!
     
    Deleted User likes this.
  16. Chman

    Chman

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    721
    OpenGL support is in ! It's not as well tested as DirectX right now but it seems to be working fine so far. Could someone test it on MacOS X ?
     
    xpack3, jcarpay and hippocoder like this.
  17. DennG

    DennG

    Joined:
    Dec 5, 2013
    Posts:
    158
    Amazing! I can't thank you enough.
    This is exactly what we needed right now.

    I'll test it on my Macbook later, if someone isn't faster.
     
  18. MornFall

    MornFall

    Joined:
    Jan 11, 2013
    Posts:
    160
    Sorry for the dumb question, but i could not find the answer in the docs...
    After a few hours, i finally managed to reference SMAA component from another script ( My GraphicOption script )...
    I add to " use.Smaa" at the top of the script to then be able to cache my " public SMAA var".
    What i am trying to do now, is to be able to change the quality via my GraphicOption script ... I tried different syntax but none seems to be working... If any of you could point me in the right direction, that would be really appreciated...

    Thanks !
     
  19. Chman

    Chman

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    721
    Code (CSharp):
    1. using Smaa;
    2.  
    3. // And in your code, something like :
    4. GetComponent<SMAA>().Quality = QualityPreset.High;
    Obviously, GetComponent should be called on the correct GameObject reference and should be cached for maximum efficiency. Then you simply have to set the Quality field to a value from the QualityPreset enum (you can of course change it dynamically at runtime). If you set it to QualityPreset.Custom, the component will use the data in the CustomPreset field (of type Smaa.Preset). There you can fine tune every settings but you shouldn't need it, the standard presets are good enough for most cases (Ultra is overkill).

    If you're working in Visual Studio or MonoDevelop, auto-complete should help a great deal ;)
     
  20. DennG

    DennG

    Joined:
    Dec 5, 2013
    Posts:
    158
    Tested on my Macbook Pro Early 2011 with AMD GPU.


    A comparison I've made on my PC:



    The difference clearly visible.
    Thanks for your awesome work!
     
    John-G and Chman like this.
  21. Chman

    Chman

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    721
    Thanks for taking the time to test it on your macbook ! How are the performances with OpenGL compared to FXAA ?
     
  22. Toad

    Toad

    Joined:
    Aug 14, 2010
    Posts:
    298
    Looks really nice. Thanks for making it available for the community for free!
     
  23. punk

    punk

    Joined:
    Jun 28, 2013
    Posts:
    408
    A thanks from me also, my game isn't ruined by crappy blur anymore, hurray!
     
  24. Chman

    Chman

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    721
    I took a bunch of comparison screenshots against FXAA and Supersampling.

    All in one :
    AllInOne.png

    No AA - FXAA3 - SMAA 1x - Supersampling 2x
    NoAA.png FXAA3.png SMAA1x.png Supersampling2x.png

    (bonus Supersampling 4x screenshot)
     
    ZJP, nxrighthere and DennG like this.
  25. DennG

    DennG

    Joined:
    Dec 5, 2013
    Posts:
    158
    What's the perfomance of Supersampling compared to SMAA?

    About OpenGL performance:
    I'll look into it when I'm at my Mac the next time.
     
  26. Chman

    Chman

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    721
    Supersampling is a framerate killer. It's very, very taxing on the GPU, which is why it's rarely used in realtime. Supersampling 4x is like rendering your game width*4 and height*4, so 16 times more pixels to render and downsample (or, to put it another way, rendering your game on 16 screens at once). It's way, WAY slower than SMAA, slower than MSAA, slower than any other AA technique in fact. I guess I'll push my supersampling code to github some day, but it's highly unpractical :)
     
    xpack3, nxrighthere and DennG like this.
  27. DennG

    DennG

    Joined:
    Dec 5, 2013
    Posts:
    158
    Ah, okay. It might be useful when taking screenshots for advertising your game or so.
    Would be awesome, if you can share it ;)
     
    xpack3 likes this.
  28. Chman

    Chman

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    721
    xpack3 and overthere like this.
  29. DennG

    DennG

    Joined:
    Dec 5, 2013
    Posts:
    158
    Thanks for the tip :)
    Are you currently working on the T2x version?
     
  30. Chman

    Chman

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    721
    Not at the moment as I have a few more pressing things to do, but I'd like to get to it soon. Anyone who's brave enough to contribute is welcomed, by the way :p
     
  31. MornFall

    MornFall

    Joined:
    Jan 11, 2013
    Posts:
    160

    Thanks a lot , i am gonna try that right away !
     
  32. DennG

    DennG

    Joined:
    Dec 5, 2013
    Posts:
    158
    Bump, since it's awesome.
     
    nxrighthere likes this.
  33. Mikeedee

    Mikeedee

    Joined:
    Jan 5, 2015
    Posts:
    42
    This looks awesome, but is there any way to have a specific version for Unity 4 ? At the moment it has some errors I was unable to fix and upgrading to U5 is not an option :(
     
  34. electroflame

    electroflame

    Joined:
    May 7, 2014
    Posts:
    177
    Wow, this looks great -- thanks for sharing! Unity's anti-aliasing is really, really crappy (and, weirdly, slow for the results you get) so this looks like a great alternative.

    Definitely can't wait to give it a go. Thanks!
     
  35. Chman

    Chman

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    721
    I could take a look, last time I checked I had issues with the shader compiler on Unity 4 on DX9 and OpenGL (it seems to work way better in 5). Could you copy/paste or attach a screenshot of the errors your getting ?

    If you want a slower alternative, I should release my implementation of Supersampling soon. I did a Supersampling 16x test and it looks glorious but brought my GTX 770 to its knees :D
     
  36. Steve-Tack

    Steve-Tack

    Joined:
    Mar 12, 2013
    Posts:
    1,240
    This sounds great. And free too!?

    If I add SMAA to a camera I get spammed with errors though. The first is:

    "NullReferenceException: Object reference not set to an instance of an object" on line 60 of SMAAEditor.cs:
    m_CustomDiagDetection = m_CustomPreset.FindPropertyRelative("DiagDetection");

    And another that seems to be from this on line 80:
    EditorGUILayout.PropertyField(m_DebugPass, new GUIContent("Debug Pass", "Use this to fine tune your settings when working in Custom quality mode."));

    EDIT: I can get around the errors if I drag the SMAA script into the component list rather than pick SMAA from the list.

    EDIT #2: Holy CRAP, this thing is great! Plugged it into my game and got excellent results with the default settings. I had just converted to HDR a few days ago and ended up just giving up on post anti-aliasing due to losing tons of detail. But this thing is great. Finally HDR with AA is a reality.
     
    Last edited: May 14, 2015
  37. Chman

    Chman

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    721
    I'm glad you like it !

    I still need to do some testing regarding full HDR support, so expect a few git commits soon.
     
  38. Chman

    Chman

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    721
    Here you go, I just pushed an update to Github with better/proper support for HDR :)
     
    xpack3, ZJP, Hikiko66 and 4 others like this.
  39. Deleted User

    Deleted User

    Guest

    Sweet! I friggin love you man!
     
    ZJP likes this.
  40. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    For some reason, I'm getting errors preventing it from working. I got three errors like this:

    Invalid pass number (1) for Graphics.Blit. Material=Hidden/Subpixel Morphological Antialiasing, npasses=1
    UnityEngine.Graphics:Blit(Texture, RenderTexture, Material, Int32)
    Smaa.SMAA:OnRenderImage(RenderTexture, RenderTexture) (at Assets/SMAA-Unity/SMAA/Scripts/SMAA.cs:237)

    And then it becomes disabled.

    Using Macbook Pro.
     
  41. Chman

    Chman

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    721
    Seems to be a shader compilation issue. Could you select the SMAA shader (/SMAA/Shaders/SMAA.shader) in the project panel and look into the inspector for compilation errors ?
     
  42. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    Ah, yes, I deleted it and re-imported it and it fixed the three errors. I'm just getting a warning now when I run the game:

    GLSL shader load error (stage 2 shader 62):
    ERROR: 0:132: Incompatible types (bvec2 and bool) in assignment (and no available implicit conversion)
    ERROR: 0:247: Incompatible types (bvec2 and bool) in assignment (and no available implicit conversion)

    And have no idea what that means. My shader knowledge is quite limited at the moment.
     
  43. Chman

    Chman

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    721
    Hmm ok, I'll look into it, thanks for the report !
     
  44. rebit

    rebit

    Joined:
    Nov 13, 2014
    Posts:
    133
    Great job Chman ! Finally a great antialiasing for Unity 5 !
     
  45. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    Muuuch better than FXAA.
    But losing against standard hardware AA when it comes to thin window frames in architecture facades.

    Still this cool thing could mean that I may use HDR in future - thanks a lot!
     
  46. Chman

    Chman

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    721
    Yeah unfortunately image-based AA will never be as good as MSAA, except for high value supersampling which can actually look better than everything else but is so expensive it makes it useless. Another possibility would be use SMAA + Supersampling 2x, this should work quite well on high-end GPUs and be close to perfect.
     
    Last edited: May 22, 2015
    overthere and nxrighthere like this.
  47. exitsimulation

    exitsimulation

    Joined:
    Feb 10, 2014
    Posts:
    82
    @Chman
    This is fantastic. Thanks!
    Although, it would be great if we could use it for Unity 4 as well. Unfortunately I am depending on Unity 4.0.1 for some projects and proper anti aliasing would really make a difference. Would gladly buy buy you a coffee if you could check it for Unity 4 compatibility. ;)
    Also, is SMAA eligible for mobile?
     
  48. Chman

    Chman

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    721
    T̶h̶e̶ ̶s̶h̶a̶d̶e̶r̶ ̶d̶o̶e̶s̶n̶'̶t̶ ̶w̶o̶r̶k̶ ̶o̶n̶ ̶m̶o̶b̶i̶l̶e̶,̶ ̶I̶'̶v̶e̶ ̶d̶i̶s̶a̶b̶l̶e̶ ̶s̶u̶p̶p̶o̶r̶t̶ ̶f̶o̶r̶ ̶t̶h̶e̶s̶e̶ ̶p̶l̶a̶t̶f̶o̶r̶m̶s̶ ̶b̶u̶t̶ ̶I̶ ̶d̶o̶n̶'̶t̶ ̶e̶x̶a̶c̶t̶l̶y̶ ̶r̶e̶m̶e̶m̶b̶e̶r̶ ̶w̶h̶y̶.̶

    As for Unity 4.x... I have no plan to support it at the moment. AFAIK, it works with Unity 4.x / DX11 but the shader breaks with DX9 and GL.

    Edit: I just added mobile support in the trunk. It's untested but it compiles, if anyone wants to test it...
     
    Last edited: May 27, 2015
  49. SAOTA

    SAOTA

    Joined:
    Feb 9, 2015
    Posts:
    220
    I can. Going to deploy test build to ios tonight, will include SMAA and let you know how it goes.
     
    Deleted User, boysenberry and Chman like this.
  50. exitsimulation

    exitsimulation

    Joined:
    Feb 10, 2014
    Posts:
    82
    Will test for Android 5.0 on Nexus 5 tonight! Thanks for the mobile support.