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

Retro AA - antialiasing for pixel art and voxel games

Discussion in 'Works In Progress - Archive' started by Andy-Korth, Jun 20, 2016.

  1. Andy-Korth

    Andy-Korth

    Joined:
    Jun 7, 2013
    Posts:
    144
    RetroAA is now in the store: https://www.assetstore.unity3d.com/en/#!/content/65509

    We're working on a new asset to help you achieve the perfect smooth big-pixel look for your retro styled game! Smoothly rotate, scale, and position your artwork without unsightly jaggies, moire, or swimming pixels. RetroAA keeps your pixels looking their best.

    RetroAA also works well with voxel games and is 3D ready! With anisotropic filtering enabled, your textures will keep their sharp pixel look even at very shallow viewing angles.

    Here's some example screenshots we've got:




    And here's a comparison between the default unity texture filtering options, and rendering with our new shader:


    For best effect, you can click on that image to see it at exactly 100% resolution.
     
    Last edited: Jun 28, 2016
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Right so what is the performance impact?
     
  3. slembcke2

    slembcke2

    Joined:
    Jun 26, 2013
    Posts:
    270
    Not much. It's a fairly efficient pixel shader. Higher quality and cheaper than screen space AA effects like FXAA. Significantly less than some of the built in shaders like displacement mapping for instance.
     
    Martin_H likes this.
  4. Huknar

    Huknar

    Joined:
    Mar 6, 2015
    Posts:
    40
    If this fixes the issues I've had with trying to maintain a low resolution pixel art style you guys are amazing. This looks perfect for one if the next games I want to work on.
     
  5. livingtech

    livingtech

    Joined:
    Aug 7, 2015
    Posts:
    9
    genius!
     
  6. megan_l_fox

    megan_l_fox

    Joined:
    Jul 10, 2010
    Posts:
    35
    Need beta testers? ;)
     
  7. slembcke2

    slembcke2

    Joined:
    Jun 26, 2013
    Posts:
    270

    Another good example of a 3D scene with RetroAA. On the right side point filtering quickly turns into noise (that looks even worse when moving), and the close up details have a lot of visible aliasing. RetroAA on the left side anti-aliases between texels close up and keeps them looking sharp as possible when they get further away.
     
  8. Huknar

    Huknar

    Joined:
    Mar 6, 2015
    Posts:
    40
    Absolutely amazing. I cannot wait for you guys to release this. Been a major problem for me with wanting pursue this 3D, low resolution, pixel art style. This has reignited my excitement.
     
  9. slembcke2

    slembcke2

    Joined:
    Jun 26, 2013
    Posts:
    270
    So one thing I think we didn't point out yet was that RetroAA gives you anti-aliased texture filtering, but it doesn't anti-alias polygon edges. So you'll want to use it to complement regular MSAA.
     
    Martin_H and Andy-Korth like this.
  10. Andy-Korth

    Andy-Korth

    Joined:
    Jun 7, 2013
    Posts:
    144
    Today I added a SpriteRenderer version of the shader. This one includes alpha blending and support for Unity's sprite atlases. Here's an enlarged picture to show how it looks with sprites that have been rotated slightly.
     
  11. slembcke2

    slembcke2

    Joined:
    Jun 26, 2013
    Posts:
    270
    RetroAA is now available on the Asset Store. :)
    https://www.assetstore.unity3d.com/en/#!/content/65509

    We have some more ideas that we've been working on as well such as applying an unsharp mask filter as part of the anti-aliasing to make the pixels even sharper, and we are experimenting with Unity 5.4's alpha-to-coverage feature for supporting anti-aliasing alpha cutouts for 3D games.
     
    Torbach78 likes this.
  12. Huknar

    Huknar

    Joined:
    Mar 6, 2015
    Posts:
    40
    Does this support any of the standard shader abilities? Metal/Specular maps/Bump Maps/AO/Height map?
     
  13. slembcke2

    slembcke2

    Joined:
    Jun 26, 2013
    Posts:
    270
    We provide a standard surface shader, an unlit shader, and a sprite one.

    The surface shader version started with the template, and we didn't add any properties (yet). We weren't sure which combinations people would want and if which channels they would want RetroAA applied to. For instance, you probably do want it applied to normals and specular, but maybe not AO. Parallax mapping probably would work really poorly with a discontinuities in the height map. On the other hand, it's a surface shader so it's as easy as possible to modify. We are also very open to requests.
     
    theANMATOR2b likes this.
  14. slembcke2

    slembcke2

    Joined:
    Jun 26, 2013
    Posts:
    270
    We are considering adding support for doing anti-aliased alpha cutouts using Unity 5.4's new alpha to coverage support. This would be great for anybody that wants to mix regular anti-alising and RetroAA together to anti-alias both polygon edges and the texturing.

    Is there any interest?
     
    theANMATOR2b likes this.
  15. megan_l_fox

    megan_l_fox

    Joined:
    Jul 10, 2010
    Posts:
    35
    There is DEFINITE interest in mixing this with regular AA, yep, though I'm a little unclear how you'd be leveraging alpha-to-coverage for that (I haven't looked into it at all). In case it matters, we're also using most of the Amplify plugins, so however you did it, odds are good you aren't the only post-processing system in play, if it's the sort of thing where you'd be moving beyond a relatively straightforward surface shader.
     
  16. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Very interested in getting this soon.
    Personally I'd like everything to be exposed, and simple to apply or not. All properties available in the standard shader would be best to have accessable in the custom shader. And a simple check box to apply RetroAA or not.
    That's the kind of ease of use I'd really like to see.
    Great work on the pack so far.
     
  17. slembcke2

    slembcke2

    Joined:
    Jun 26, 2013
    Posts:
    270
    The quicksplanation version is that the GPU generates a bitmask of which subsamples in a pixel are covered by a triangle. You can modify that bitmask by a shader to control the anti-aliasing strength for a certain pixel. Alpha-to-coverage basically means modifying that coverage bitmask using an alpha value. Ex: When the alpha is 25%, turn off all but 25% of the coverage bits that the hardware said were part of the triangle.
     
  18. musashi917

    musashi917

    Joined:
    Jan 6, 2013
    Posts:
    19
    Hello, does this asset come with non-obfuscated shader code ? I am interested in this but I use custom shaders so I will need to hack through the code to integrate it to my game.
     
    StaffanEk likes this.
  19. HowlingMoonSoftware

    HowlingMoonSoftware

    Joined:
    Sep 29, 2016
    Posts:
    5
    Absolutely! The implementation is even relatively simple, so it should be easy to extend and embed into other shaders if you want.
     
  20. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Hi,

    I'm using your shadow stuff at the moment. How easy is it to include this, and is it a yes or no on mobile?
     
  21. TheADrain

    TheADrain

    Joined:
    Nov 16, 2016
    Posts:
    48
    Oh wow this is super nice, I've written a couple tutorials on using a low resolution render texture and scaling it up to make pixel perfect low resolution games, but this is a really nice solution to the problem that doesn't make GUI a total pain in the butt.

    Great job!
     
  22. slembcke2

    slembcke2

    Joined:
    Jun 26, 2013
    Posts:
    270
    @hippocoder RetroAA isn't an expensive fullscreen effect, it works at the shader level when sampling textures. Since you seemed to understand how the SFSS object shader worked, I don't think you'll find it much more trouble than some copy pasting. That does remind me that I wanted to make a .cginc file for both projects though to make them easier to integrate into custom shaders.

    Mobile: It works ok on mobile, though it uses dependent texture reads which are slower on older GPUs. You certainly will want to be careful about overdraw. For instance, don't apply it to 4 separate, fullscreen parallax layers, add SFSS on top of it (since it uses more overdraw to generate the lightmaps) and expect it to run well on an iPad 2.

    @TheADrain Thanks!
     
  23. slembcke2

    slembcke2

    Joined:
    Jun 26, 2013
    Posts:
    270
    Ok, so I took some time to work on putting RetroAA into a .cginc. In the process I also added a new variation that uses alpha-to-coverage for a transparent cutout version. It works surprisingly well!



    The outside edge on the sprite is anti-aliased using MSAA. That means that it will work with the z-buffer and you can make something like Minecraft plants with it. We want to experiment with one last thing before pushing out an update, but stay tuned.
     
  24. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Sorcery.
     
  25. slembcke2

    slembcke2

    Joined:
    Jun 26, 2013
    Posts:
    270
    @hippocoder Turns out it was *way* easier than I thought, and that led me to find out that I didn't really realize how it worked in OpenGL either...

    Check this near the bottom:
    https://docs.unity3d.com/Manual/SL-Blend.html

    In OpenGL, the 3.x version of alpha-to-coverage was a simple feature to be toggled on and off, and that must be what Unity is targeting. The newer, more flexible version in 4.x was all that I was familiar with, and it involved fiddling with bitmasks in the fragment shader. Given that, I wonder why they didn't add that as a Unity feature earlier than 5.5...
     
  26. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Trying to second guess Unity is a huge mistake! but nice one on figuring it out :)
     
  27. slembcke2

    slembcke2

    Joined:
    Jun 26, 2013
    Posts:
    270
    Well, we submitted v1.1 with support for alpha-to-coverage and normals in the "standard" version of the surface shader.

    I was dragging my feet on normals for a bit because I thought it might look bad. Ideally the lit texels should be anti-aliased and filtered, but that's not very practical. The simple/obvious solution is to anti-alias the normal map texels. Highly specular materials end up looking like glass tiles though, so I wouldn't recommend that. More diffuse surfaces look just fine though.
     
    Howard-Day likes this.
  28. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    Really interested in the shader however i use custom shaders on my sprites which i made in Shaderforge, i don't have much shader programming knowledge, will i be able to integrate this in my project?
     
  29. Howard-Day

    Howard-Day

    Joined:
    Oct 25, 2013
    Posts:
    137
    Likely, yes. The snippets of shader code required for this to work are easily copied into new shaders - after purchasing it, I've already moved it over into all my existing custom shaders. It's really excellent stuff, and makes things look a thousand times better.
     
  30. slembcke2

    slembcke2

    Joined:
    Jun 26, 2013
    Posts:
    270
    I've never used ShaderForge before, but I have patched one of the shaders it outputs once (not for RetroAA though). The code it outputs is kinda hard to read, but it was otherwise straightforward. v1.1 of RetroAA made it a lot easier to use by wrapping everything up into a function you can call, but you would have to write some Cg code to use it.

    I guess I would recommend reading some of the shaderforge output, and trying to modify it. If you are making shaders with shaderforge, then you already understand about how shaders work, what they can do, and understand some of the math intuitively. You just don't know the syntax. ;)
     
  31. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    I got the shader and it does a nice job, but you guys should really consider adding a Sprite Diffuse shader, I tried reading a bit on shader programming and looked at Unity's sprite diffuse shader and what i understand so far is that i'd have to convert it to a surface shader for it to work with lights, i don't have time to learn shader programming, this is why i got shaderforge.

    Also while we're at it, you guys should provide an example of how to implement this to a basic shaderforge shader, i'm not sure what to copy/paste/change, it would really be helpful.
     
    Last edited: Jan 24, 2017
  32. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    Well i've spent all day learning about shader programming and i've managed to recreate one of my shaderforge fog shader using RetroAA/Sprite as a base, but i haven't figured out how to make it lit, i've managed to make it affected by the directional light but no other lights would light it up so i just gave up, i imagine making a proper vertex/frag lit shader is complicated and this is why Unity's Sprite Diffuse shader is a surface shader.
     
  33. slembcke2

    slembcke2

    Joined:
    Jun 26, 2013
    Posts:
    270
    Unity's builtin shader only needs a couple tweaks. I'll include it in the next update, but until then enjoy! :)

    Code (CSharp):
    1. Shader "RetroAA/SpriteDiffuse"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _Color ("Tint", Color) = (1,1,1,1)
    7.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    8.     }
    9.  
    10.     SubShader
    11.     {
    12.         Tags
    13.         {
    14.             "Queue"="Transparent"
    15.             "IgnoreProjector"="True"
    16.             "RenderType"="Transparent"
    17.             "PreviewType"="Plane"
    18.             "CanUseSpriteAtlas"="True"
    19.         }
    20.  
    21.         Cull Off
    22.         Lighting Off
    23.         ZWrite Off
    24.         Blend One OneMinusSrcAlpha
    25.  
    26.         CGPROGRAM
    27.         #pragma surface surf Lambert vertex:vert nofog keepalpha
    28.         #pragma multi_compile _ PIXELSNAP_ON
    29.  
    30.         #include "RetroAA.cginc"
    31.  
    32.         sampler2D _MainTex;
    33.         float4 _MainTex_TexelSize;
    34.         fixed4 _Color;
    35.         float _AlphaSplitEnabled;
    36.  
    37.  
    38.         struct Input
    39.         {
    40.             float2 uv_MainTex;
    41.             fixed4 color;
    42.         };
    43.        
    44.         void vert (inout appdata_full v, out Input o)
    45.         {
    46.             #if defined(PIXELSNAP_ON)
    47.             v.vertex = UnityPixelSnap (v.vertex);
    48.             #endif
    49.            
    50.             UNITY_INITIALIZE_OUTPUT(Input, o);
    51.             o.color = v.color * _Color;
    52.         }
    53.  
    54.         void surf (Input IN, inout SurfaceOutput o)
    55.         {
    56.             fixed4 c = RetroAA(_MainTex, IN.uv_MainTex, _MainTex_TexelSize) * IN.color;
    57.             o.Albedo = c.rgb * c.a;
    58.             o.Alpha = c.a;
    59.         }
    60.         ENDCG
    61.     }
    62.  
    63. Fallback "Transparent/VertexLit"
    64. }
    65.  
     
    Khena_B likes this.
  34. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    Thanks you so much, i'm sure it'll come in handy for others as well.
     
  35. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    I'm trying to figure out a good way to use RetroAA with shaderforge, while modifying the shader code can work, it gets automatically overwritten when the shader is opened in shaderforge so this is a problem.

    In shaderforge there is a code node, you guys probably don't use shaderforge so i'll post an image of it, do you think this could be used to add RetroAA to a shaderforge shader without having to edit the shader code?

    CodeNode.png

    Much appreciated
     
  36. slembcke2

    slembcke2

    Joined:
    Jun 26, 2013
    Posts:
    270
    There are two potential problems. First is that the RetroAA code is in a .cginc file to make it easier to include into custom shaders. I Googled briefly, and according to this it looks like it's on their radar, but I didn't see anything about it being finished. (Though I didn't look that hard) The other issue is that RetroAA requires a texture's properties uniform to be declared. I wouldn't be surprised if Shaderforge does that for all textures, but I wouldn't be surprised if it didn't.

    You might want to ask around on the Shaderforge forums if both of those are possible. At that point you probably aren't gaining any ease of use by using Shaderforge though. That's a lot of hoops to jump through, and a lot of specific knowledge you need to know where you might as well just write shaders the old fashioned way.
     
  37. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    I'm still trying to find a solution, i've managed to write some basic shaders but i've made this distorting shader in shaderforge for my foliage, i really want Retro AA to work with it but i don't have enough syntax knowledge to write it or time to learn it right now.

    I checked the page you linked and it seems the feature has been added 3 months ago, would it now be possible to make this work?
     
  38. Andy-Korth

    Andy-Korth

    Joined:
    Jun 7, 2013
    Posts:
    144
    Getting RetroAA working in shaderforge in the past has been a one-shot rewrite of the shaderforge output to work the way retroAA works. Unfortunately we don't have a automatic solution or something that can just plug into shaderforge.
    I see the mention of custom cginc files but I think it will still take some skill on the part of a shaderforge user to plug something in. If you do get it working, let us know!

    I have no idea if they can automatically deal with the RetroAA texture properties uniform requirements.
     
  39. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    Well i decided to just try and recreate the shader in shaderforge and it worked, i didn't think it would be that simple to translate.
     
  40. slembcke2

    slembcke2

    Joined:
    Jun 26, 2013
    Posts:
    270
    Version 1.2 is out! We added a variant for UIs. Enjoy!
     
    hippocoder likes this.
  41. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Hey guys, does it work on sprite shapes? Doesn't seem to... :(
     
    spryx likes this.
  42. slembcke2

    slembcke2

    Joined:
    Jun 26, 2013
    Posts:
    270
    Hmm. Looked into it slightly and found a workaround at least.

    Not sure if it's a Unity, SpriteShapeRenderer bug, but it's passing a random value for the _MainTexture_texelSize.zw. You can see it in the frame debugger, randomly changing every frame. The .xy components seem to be passed correctly though. I thought this was hardcoded into the Unity renderer, but maybe each rendering component implements it separately?

    The texel size variable passes texture size information to the shader. It's a float4 that contains (1/width, 1/height, width, height). Since .xy is passed correctly, you can just divide by .xy instead of multiplying by .zw. Open up RetroAA.cginc and fix the first line of the function there. It should be the only place you need to fix.

    I made a bug report here:
    https://fogbugz.unity3d.com/default.asp?1099636_s2i45t0rg5ngl6vg

    and posted it to the SpriteShape forum here:
    https://forum.unity.com/threads/spriteshape-preview-package.522575/page-4#post-3877081
     
    Last edited: Nov 10, 2018
    hippocoder likes this.
  43. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Thanks a lot for having a look!
     
  44. johnsolo

    johnsolo

    Joined:
    Jan 12, 2014
    Posts:
    7
    Hello I seem to be having a problem using this. Just purchased the asset and created a new material with it. Then I applied the material to my sprite. It doesn't seem to actually smooth out the jaggies though?

    Here is a ss of my sprite settings:

    https://cl.ly/bb1e7c3eb1c3/Screen Shot 2019-03-15 at 10.19.12 AM.png

    And here are my material settings:

    https://cl.ly/5a782102ba42/Screen Shot 2019-03-15 at 10.20.07 AM.png

    I've also tried the other shaders, like Unlit, but then my sprite just disappears:

    https://cl.ly/a4efc48f80fc/Screen Shot 2019-03-15 at 10.21.10 AM.png

    Unfortunately I could not find a support email link anywhere for Howling Moon Software, so I am posting here. Does anyone know how to contact Howling Moon? The "support website" doesn't have any way to contact them.

    Thanks!
     
  45. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    Is your sprite set to Bilinear filtering?
     
  46. johnsolo

    johnsolo

    Joined:
    Jan 12, 2014
    Posts:
    7
  47. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    For RetroAA to work, switch to Bilinear
     
  48. johnsolo

    johnsolo

    Joined:
    Jan 12, 2014
    Posts:
    7
    Woohoo, that did it! Thanks KhenaB!
     
    Khena_B likes this.
  49. slembcke2

    slembcke2

    Joined:
    Jun 26, 2013
    Posts:
    270
    Yup, check the README.txt file for more info. It also works with trilinear/anisotropic filtering if you want to use it in a 3D game.
     
  50. johnsolo

    johnsolo

    Joined:
    Jan 12, 2014
    Posts:
    7
    Totally my mistake for missing the README in the package! Thanks again everyone for your help