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

Per-vertex AO solver (and goodies)

Discussion in 'Works In Progress - Archive' started by hausmaus, Mar 8, 2012.

  1. hausmaus

    hausmaus

    Joined:
    Dec 9, 2011
    Posts:
    105
    In an effort to contribute something potentially useful rather than lurk more stylishly, I'd like to share my current progress on a semi-flexible per-vertex AO solver for use within Unity.

    Because it’s a purely visual effect and it’s somewhat complex, I posted a Youtube video with all of the details and examples: http://www.youtube.com/watch?v=eeKQAXg-Qo8

    Note: This was recorded in XSplit, which uses b-frame compression, which is not entirely compatible with YouTube. As a result, the first 10 seconds are grey, and I put up a little annotation to explain that. Also, if watching in HD, the audio is cut a bit short.

    The project files are included in the video description, and can also be found here:

    http://adrianswall.com/shared/unity_vertex_ao.rar (project directory archive)
    http://adrianswall.com/shared/WizardBakeVertAO.cs (ao script itself)

    http://adrianswall.com/?p=28 (link to blog post)

    The basic approach is very straightforward: Iterate over a selected object's vertices, casting N sample rays (default 512) in a hemisphere aligned with the vertex normal. Any collisions contribute to total occlusion at that vertex based on the distance to the collider. The result is stored as vertex alpha, and the project includes shaders for debugging that result and using it to multiply in a color for the AO effect on a more standard shader.

    There is also a little "p_pointsample" sphere which can be placed over a vertex manually. If the sphere's Z+ local axis is aligned well enough with the normal at that vertex (world space), it will generate a series of debug spheres at the endpoints of the ray, as shown in the video. It also generates blue sphere which show the little backwards hop it takes away from the sample target before casting the ray to solve certain coplanar/corner issues, although the distance is greatly amplified to make the blue sphere distinct.

    If you wanted to try this in realtime, simply storing the ray directions in an array and using a low vert mesh with low samples seems actually to work pretty well. More on that later.

    If anyone finds it useful, or just hates it to death, or thinks it would be 100x faster if I'd just take their awesome advice, please let me know :)

    Cheers,
    -abm
     
  2. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,367
    Thanks. It seems a good workable solution.
     
  3. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,056
    Looks very well done and looks to work very nicely indeed, I am intrigued why not use SSAO? Or is this aimed at iphone for your game project.
     
  4. CrashKonijn

    CrashKonijn

    Joined:
    Mar 4, 2010
    Posts:
    245
    Wooow I just love it! :D I really wish I was able to create such cool stuff... :)
     
  5. hausmaus

    hausmaus

    Joined:
    Dec 9, 2011
    Posts:
    105
    Thanks for the comments, guys :)

    MikeUpchat: I do like the SSAO included in Unity, and we are using a number of image effects for our look (I'll show our process soon, it's kind of fun). However, this approach seems to be more stable while offering a lot of control, especially over depth in specific areas. And of course it is less prone to artifacts. We're not targeting mobile devices, but I imagine some approach like this would be necessary if we were.

    Thanks,
    -abm
     
  6. janpec

    janpec

    Joined:
    Jul 16, 2010
    Posts:
    3,520
    Hausmaus if i understand correctly this approach needs meshes that have decent number of vertex/ triangles otherwise it doesnt give good results right? If not is there any way that you could demonstrate it on some low poly mesh?
     
  7. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    Interesting technique and some nice examples used in the video. Might just have to download it and have a poke around! Thanks hausmaus :)
     
  8. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    It does indeed. The plane under the object in his video is a pretty low poly mesh with bad triangulation (for the given direction of light), but even then the results are fairly pass-able (in my view anyway) especially when used to conjunction with the real time soft shadows.
     
  9. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Kind of sexy. But this ideally should be built into beast, and let beast bake lighting to vert colours.
     
  10. niosop2

    niosop2

    Joined:
    Jul 23, 2009
    Posts:
    1,059
    Very nice, thanks for releasing this. This could help a lot with runtime generated levels. Even if it takes a while to run, you could make it part of the loading screen and get some visual improvement that wouldn't be possible with Beast.

    Just your deep copy script is going to come in very handy, and I could see storing AO info in the vertex alpha and some form of GI or bounce lighting in the vertex RGB that could be calculated at load time.
     
  11. hausmaus

    hausmaus

    Joined:
    Dec 9, 2011
    Posts:
    105
    I agree. Although we're not pursuing lightmapping for our current project, I was able to put together a Beast->vertex color transfer method, shown here:

    http://www.youtube.com/watch?v=MeMIwoqOp9o&hd=1

    Project archive (includes everything, everything I say):
    http://vertexcolors.com/shared/unity_beast_to_vertex_colors.zip

    The lightmap transfer function itself:
    http://vertexcolors.com/shared/LightmapToVertexColor.cs

    Example vertex/fragment shader
    http://vertexcolors.com/shared/AEVertexGI.shader

    Debug vertex color shader:
    http://vertexcolors.com/shared/AEFastVertexColors.shader


    This may be helpful for people who prefer Beast's AO or just want any of its 5 million other cool features to be written to vertex colors in Unity.

    I make a lot of assumptions in the scripts that do the transfer, but in any "standard" case I believe it should work. Please let me know what you find.

    Cheers,
    -abm
     
  12. edux404

    edux404

    Joined:
    Feb 14, 2014
    Posts:
    5
    Can I use this in Unity Free? And can I use it on my game? :rolleyes:
    Love it tho, amazing work! :D
     
  13. smd863

    smd863

    Joined:
    Jan 26, 2014
    Posts:
    292
    This is just baking data into vertex colours so there is no reason it would require Unity Pro. You will have to read the license included in the project to see if you can use it in your game, but given that he posted it publicly like this, it seems he intended for people to use it.
     
  14. edux404

    edux404

    Joined:
    Feb 14, 2014
    Posts:
    5
    Thank you for info :3
     
  15. sleekdigital

    sleekdigital

    Joined:
    May 31, 2012
    Posts:
    133
    I can get the per-vertex AO to work fine with a cube on a plane using Unity's primitives. But when I import models from Blender, I can't get it to work. I even made sure read/write is enabled. Anyone have any idea what the problem could be?
     
  16. Rusfighter

    Rusfighter

    Joined:
    Jan 18, 2014
    Posts:
    60
    Hello hausmaus, could you reupload the Per-Vertex Ambient Occlusion scripts please? The site is down and we are unable to download the scripts & shaders.
     
  17. N3zix

    N3zix

    Joined:
    Oct 22, 2014
    Posts:
    23
    unity_NzFhFR1tp6v5RQ likes this.