Search Unity

This crazy real time ambient occlusion

Discussion in 'Shaders' started by forestjohnson, Feb 2, 2008.

  1. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    I saw this link on Aras' site to a demoscene that features real time ambient occlusion and a paper about it..

    Heres the demo: http://www.youtube.com/watch?v=9AX8gNyrSWc

    And paper: http://rgba.scenesp.org/iq/computer/articles/ssao/ssao.htm

    As for how they generate the mesh in the demo I have no clue whatsoever but it looks awesome.

    Anyway, now comes the inevitable question... Is this possible in unity? Seems like it should be not that hard. I did a little experiment and drew the depth of the scene on a cube in front of the camera.

    I have some questions. Right now on LateUpdate I do a switcheroo of all the materials and the camera settings, then camera.Render();, then set everything back the way it was and place the RenderTexture on the cube.

    How would one really approach this? It almost seems like it could be a full screen image effect. I tried at first to make a full screen pixel correct render texture but that didn't work, I got an error message when placing the render texture on the cube saying the dimensions were wrong or something, and I got garbage on the cube. Making it power of two using mipmaps fixed it.

    From the screenshots I've seen it looks like it actually works, and the shader doesn't look extremely complex to make so this is exciting.

    <excitement>

    </excitement>
     

    Attached Files:

  2. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    Reminds me of one of my favorite pieces...Gestalt... not realtime but uses similar fractal quaternion geometry.

    Let me know Forest if you're getting fancy with realtime AO and quaternion fractal based race tracks... our race cars might need some modding to cope with this :wink:

    Seriously though, any experiments on this front are very interesting.

    Ethan
     
  3. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    There are several components to make such realtime AO (SSAO for "screen space ambient occlusion") practical:

    1. Support for high precision, single channel render textures, so you can render linear depth to them. D3D9 has that, the hardware supports that since 2002, but unfortunately OpenGL does not have that. The only workaround on OpenGL is to burn 4x times more VRAM and 4x times more bandwidth and use a 4 channel floating point render texture. Duh. This is probably the major reason why high precision single channel render textures are not exposed in Unity; as it's just not practical on OpenGL.

    2. An easy way to say "render from this camera, and make everything use this shader". Currently this can be done with some manual work; just like you did - swap the materials before rendering, restore them back after rendering. Someday we'll add a built-in feature to do this.

    3. Support for quite long fragment shaders, as good looking SSAO needs lots of samples to look good. Again, this would be easy on D3D9 with pixel shader 3.0; and on OpenGL possibly GLSL could be used (with lots of praying that it will actually work).

    So yeah, the effect is cool, but it's tricky to do in practical way mostly because of 1st and 3rd points.


    ...oh, and that demo does raytracing against a fractal in a pixel shader. So actually there's no fractal geometry whatsoever. And it runs oh so slow, but then it's also very cool.
     
  4. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    So you are saying with point 1 that simply encoding the depth into the render texture as the alpha channel or a color channel will not be precise enough? It seems one could also spread the depth out over a few channels to get more precision, but that just makes your depth shader and SSAO shader more complicated, right?

    And point 3 I can understand, although I have never tested the limits of how long a fragment program can be. Is it a hard limit or specific to each card?

    Maybe I shouldn't have been thinking about this so hard, cause now I'm really itching to try it out. I had some other ideas about how to improve it, like storing the "average depth" of each object in an unused channel in the render texture to help get rid of artifacts where you get a black line around things in the foreground.

    (this problem seen in the bottom right project offset screenshot)

     
  5. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Its alive!
     

    Attached Files:

  6. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
    Awesome Yoggy.

    I've been playing a bit with this too, the effect is too cool to stay away from. I already had the depthmap from my dof effect so I thought it would be easy. Haven't cracked it yet though. Good work

    /Patrik
     
  7. drJones

    drJones

    Joined:
    Oct 19, 2005
    Posts:
    1,351
    sweet yoggy - though what is causing the blotchy effect?
     
  8. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Ooh. :) The artifacts are actually rather pretty.
    I'd guess he's not yet got to the "Second Trick" section here, which talks about banding:
    http://rgba.scenesp.org/iq/computer/articles/ssao/ssao.htm

    Cheers,
    -Jon
     
  9. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Yeah I thought so too, jon. Looks like pencil shading sometimes :D

    Its caused by depth buffer imprecision induced by the fact that I am only using 1 channel of a render texture to store the depth.

    I am going to try to save up enough instructions to add noise too it too, which will probably help quite a bit. Then I've got to figure out the blur pass too.

    Oh and patrik / anyone else who wants to have a look so far, heres a WIP package:
     

    Attached Files:

  10. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    "The second trick"
     

    Attached Files:

  11. llavigne

    llavigne

    Joined:
    Dec 27, 2007
    Posts:
    977
    just too cool - and I agree - the artifacts actually add to it, kill the CG ness by making it gritty, sorta like sharcoal - wonder if you can make it look like watercolor or clay too
     
  12. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Its coming along quite nicely, I added some interactivity to showcase it's realtimeness.

    Just need to figure out the blur now.

    http://yogware.bluegillstudios.com/SSAO/SSAO.html

    Edit: The shader isn't working in the webplayer here.. Oh well, I need to sleep...
     

    Attached Files:

  13. copenhagenjazz

    copenhagenjazz

    Joined:
    Oct 2, 2005
    Posts:
    45
    hi Yoggy

    Just wanted to let you know its working in the webplayer here on my pc:
    windows xp sp2
    explorer 7
    geforce 8800 gts 320mb
    2 gb ram

    Also; thats bloody amazing! I remember being in awe when I saw a demonstration of CryEngine2 that included realtime AO and now Unity has it too. Thats just soo cool!

    Congrats on your smartness :p
     
  14. boxy

    boxy

    Joined:
    Aug 10, 2005
    Posts:
    675
    Yoggy that is stunning. I also agree the rougher dotted artifacts really help get rid of that too clean computer generated look, but the web player is magnificant. I want one :D
    A question which is way too early to ask - could this be applied to the terrain stuff so that trees and detail meshes look properly grounded? :)
    Nice work
    Boxy
     
  15. Joe ByDesign

    Joe ByDesign

    Joined:
    Oct 13, 2005
    Posts:
    841
    Very cool Forest!
     
  16. Francis

    Francis

    Joined:
    Mar 29, 2008
    Posts:
    40
    I download the SSAO.unityPackage and import this to a new scene, but I get blank screen when I run this!!
     
  17. dolphin boy

    dolphin boy

    Joined:
    Nov 11, 2008
    Posts:
    29
    Amazing stuff, I really hope you continue the work and release an even more pulished version.

    Just one question: it only works on Unity Pro, isn't it?
     
  18. Hilm

    Hilm

    Joined:
    Nov 2, 2007
    Posts:
    338
    Really looking forward to updates on this :D.

    I have a friend who runs a local games studio and he's working on a little xbla game and is using this kind of thing (AO is calculated in real time, while all his models etc mostly just have block colours on)
     
  19. Hilm

    Hilm

    Joined:
    Nov 2, 2007
    Posts:
    338
    I get a blank screen when running it maximized, and non maximized it dosent seem to work :S

    It does work in the webplayer tho on both my mac and pc.

    When loading up the package in Unity I get the error "Cg in program "frag": error C6003: Arithmatic instruction limit of 64 exceeded; 70 arithmatic instructions needed to compile program at line 21."
     
  20. Anim

    Anim

    Joined:
    Aug 11, 2008
    Posts:
    289
    Thats really cool.

    To speed things up, couldn't you kinda bake any self AO or static AO?

    Just an idea.

    Keep it up.

    Geoff
     
  21. Unclet

    Unclet

    Joined:
    Apr 12, 2007
    Posts:
    92
    I see the same error here :?

    <edit >
    Tracing back through the error log, it seems the problem might be with munged line endings on the ScreenSpaceAO.js. Fixing that made the error go away.

    However, I still too get only black after the thing starts. (Unity Pro here, too).

    </edit>

    really cool stuff though, wtg Yoggy -- just stumbled across this...
     
  22. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    429
    This seems like the way forward. Again, this is amazing.
     
  23. Thomas-Pasieka

    Thomas-Pasieka

    Joined:
    Sep 19, 2005
    Posts:
    2,174
    Any news or updates on this Forest? Would love to see this developed further.
     
  24. ClamsTheCat

    ClamsTheCat

    Joined:
    Apr 16, 2008
    Posts:
    57
    Bump. I would really like to see if we can get some sweet darkened scenes with this effect.
     
  25. pixu

    pixu

    Joined:
    Nov 24, 2008
    Posts:
    8
    Same here. I tried to remove some iterations from the shader - errors disappeared, but there's no AO.

    Does this only work on Unity Pro?
     
  26. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Updated version.
     
  27. cgoran

    cgoran

    Joined:
    Jun 11, 2005
    Posts:
    176
    I've been hoping and asking for this for years...
    I'd like to run the calculation once in the editor and cache the result.

    Thanks!
     
  28. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    Cgorgan: I think you'd be better off baking the AO for static objects in your scene into a texture map in your 3d graphics program, and use the AO shader for moving objects. :wink:
     
  29. diese440

    diese440

    Joined:
    May 25, 2007
    Posts:
    105
    Hi,

    I only get a black screen. Pressing the "c" switch key i can see the [normal] scene, then pressing "C" again i return to a black screen [no AO]. The aimer and all GUI objects are visible over the black scene.

    This happens on Mac in Editor, WebPlayer and exported application.
    I just try the WebPlayer on Windows : Crash at launch, IE or FF exits with no error.

    Tests done with
    MAC : Unity Pro 2.1.0f5 (16146) - MacIntel 10.4.11 RadeonX1600
    WIN : XP pro SP2 - GeForce 8500 GT (IE Firefox [same result]) :wink:

    Btw Forest this is an amazing work. Thanks :D
     
  30. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    I'm not sure that would work. My impression was that it's a full-screen effect. Even if each X and O had its own self-occlusion map, it would still need to check each pixel of each X and O the image to see if it occluded something else... right? You'd basically be adding a bunch of textures to the render pipeline but not taking anything off of it, since each pixel needs to be calculated.

    It would be like baking your lights to your scene via a script in Unity... and then not deleting these lights because you want to take advantage of bump-mapping on the baked object. Right? Unless I'm missing something... please correct me if I'm wrong! :D
     
  31. Anim

    Anim

    Joined:
    Aug 11, 2008
    Posts:
    289
    I have no idea, i'm more artist than programmer but when watching the demo noticed that quite a lot of the realtime AO never changes so suggested it could be baked, ignored or something. As to the how...well thats for somebody else to work out :D

    Geoff
     
  32. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    the webplayer works fine on both systems here and fast as well :)

    82FPS on my MacBookPro 8600M GT 256MB, OSX 10.5.6
    280FPS on my Vista64 Desktop, core i7 920, GTX280
     
  33. cgoran

    cgoran

    Joined:
    Jun 11, 2005
    Posts:
    176
    Yeah of course you can back it into the scene. But that would mean you would have to know your exact scene set up out side of unity. also you could not add prefabs to your scene or other objects and have the lighting match easily. I'd like to see it as an option. It would be really handy.

    -Realtime
    -first frame only
    -baked in editor
     
  34. ClamsTheCat

    ClamsTheCat

    Joined:
    Apr 16, 2008
    Posts:
    57
    I think the baking is also an issue when you come to having a bunch of moving items in the scene. This is definitely a work in progress but its very impressive nonetheless.
     
  35. br0kenp0ly

    br0kenp0ly

    Joined:
    Jun 3, 2008
    Posts:
    481
    cool stuff indeed! (if i only could get it to work :( ) i also get a black screen when i hit C, so any suggestions to what the problem might be are very welcome!

    btw. macbook pro/8600GT 512 / OSX 10.5.5
     
  36. ClamsTheCat

    ClamsTheCat

    Joined:
    Apr 16, 2008
    Posts:
    57
    Are you using pro?
     
  37. br0kenp0ly

    br0kenp0ly

    Joined:
    Jun 3, 2008
    Posts:
    481
    Yup! Please enlighten me anyone :)
     
  38. Joe ByDesign

    Joe ByDesign

    Joined:
    Oct 13, 2005
    Posts:
    841
    If it helps, I see the same exact behavior here (also, line ending errors, and deprecated methods in MouseLook, as pre v2.0).

    The textured version of this scene looks nice, would love to see it with this AO implementation! :)

    Using Unity Pro on latest Macbook Pro (of this writing).
     
  39. Faber

    Faber

    Joined:
    Dec 6, 2007
    Posts:
    8
    I'm having the same issue with the black screen (on a Macbook Pro).
     
  40. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    You can't do this with SSAO at all.
    As the name says, it is SCREEN SPACE Ambient Occlusion.
    This means that it is calculated on SCREEN SPACE.
    So just imagine that it is NOT calculated for the 50% of polygons that are on the side opposite to your point of view (to tell it short).
    This said, if you want to bake lighting on your objects, once you are "trashing" lot of memory for static litghint, better do it for good and put in it radiosity, final gather, AO, ect.
     
  41. BananaRaffle

    BananaRaffle

    Joined:
    Feb 24, 2009
    Posts:
    85
    Very cool!

    We're not far from convincing approximations of physically accurate lighting in real time. For example, this renders at 15-20 fps (on the right hardware :)

    http://graphics.cs.umass.edu/pubs/srt/

    (watch the video, it's great)
     
  42. BananaRaffle

    BananaRaffle

    Joined:
    Feb 24, 2009
    Posts:
    85
    You can do this with blender.
     
  43. Dorian

    Dorian

    Joined:
    Mar 31, 2009
    Posts:
    26
    Hello everyone, A good start this is,
    i've been interested in an ambient occlusion shader myself, for the record i'm an artist and not a programmer.

    perhaps i can point you towards a direction you might want to consider as well, now visualy an ambient occlusion adds alot, and i consider it a must have in any creation. however you got a couple of things to consider here both performance wise and visual/control, lets say you got this level full of objects cmodels stuff all over, adding a realtime AO all over this will drasticly slow things down not to mention the shaders/lights that will be acting on top of all this, now if you had an engine like crysis this may not be a problem, but lets stay realistic here with the tools we've got and try to make the best out of them.. i suggest to focus on couple of things here, and those things are the moving object(s) in your scene , and ONLY the moving objects, this would be the characters lets say most of the time which are the center of the players attention.
    have the entire other static objects baked with AO in a software like blender /max etc. which give you more control over the look, and even have your characters static mesh baked with AO as well, when you place your character in the engine along with the scene everything will match up with the exception of one thing, only major visible problem here is that the characters feet lets say do not interact with the floor AO wise , now the trick part comes in on how to get a second layer of AO or any other similar trick only the character in order for it to create that gap of an illusion of its interaction with the environment.

    But then again this is only a suggestion if it makes life any easier for you, if you can get the whole thing working then thats great. and best of luck!

    keep up the great work guys!
     
  44. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    429
    All it has to do is bake one frame for the entire scene. :]

    Make it an editor plugin maybe?
     
  45. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Those of you asking about baking AO are missing the point: SSAO is a real-time solution. It doesn't look nearly as good as anything you could bake with a program. SSAO is just a rough approximation to actual ambient occlusion (which is itself an approximation of real global lighting). However, it has several advantages to baked solutions:

    Runs in real time
    It gets re-calculated every frame, meaning it works with moving objects as well as stationary ones.

    Runs in constant time
    This is really important: because it uses only the Z-buffer, it runs with speed proportional to screen resolution. If you put a million triangles onscreen, SSAO will cause the same performance hit as if you just had 10 triangles. There is no performance hit from SSAO with more objects, and inversely no gain from only doing it on some objects.

    No preprocessing
    It just works, and can be turned off at runtime depending on whether it's feasible on a given piece of hardware.

    No caching
    You don't have to bake it, because it's calculated in real time.

    I think it's possible to combine SSAO with baked AO, but it would require rendering to two different depth buffers in order to avoid undesired stacking of baked and real-time AO.
     
  46. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    adding this line:
    Code (csharp):
    1. #pragma target 3.0
    to the ssao shader did the job... at least for me.

    lars


    Code (csharp):
    1.  
    2. Shader "SSAO" {
    3. Properties {
    4.     _MainTex ("Texture", 2D) = "white" { }
    5.     _NoiseTex ("Texture", 2D) = "white" { }
    6. }
    7. SubShader {
    8.     // First pass called with Material.SetPass(), draw opaque to clear the render texture
    9.     Pass {
    10.         CGPROGRAM
    11.             #pragma target 3.0
    12.             #pragma vertex vert
    13.             #pragma fragment frag
    14.             #pragma fragmentoption ARB_fog_exp2
    15.             #pragma fragmentoption ARB_precision_hint_nicest
    16.             #include "SSAO.cginc"
    17.         ENDCG
    18.     }
    19.    
    20.     // Called with Material.SetPass() for all passes after the first, multiply the result into the texture
    21.     Pass {
    22.         Blend DstColor Zero
    23.         CGPROGRAM
    24.         #pragma target 3.0
    25.             #pragma vertex vert
    26.             #pragma fragment frag
    27.             #pragma fragmentoption ARB_fog_exp2
    28.             #pragma fragmentoption ARB_precision_hint_nicest
    29.             #include "SSAO.cginc"
    30.         ENDCG
    31.     }
    32. }
    33. Fallback "VertexLit"
    34. }
    35.  
     
  47. nikko

    nikko

    Joined:
    Mar 20, 2009
    Posts:
    436
    Black Screen here too : Unity v2.5 Widows Vista Ultimate : Unity PRO
     
  48. jr57k

    jr57k

    Joined:
    Oct 28, 2006
    Posts:
    12
    Got it working by following post about adding

    #pragma target 3.0

    to the SSAO shader
     
  49. f3rDz

    f3rDz

    Joined:
    May 15, 2009
    Posts:
    15
    i'm having problems importing the package into unity. error says unity couldnt decompress the package.

    and as per the posted code above, there's an ssao.cginc include file needed. where can i get this?

    am i missing doing something. :(
     
  50. showoff

    showoff

    Joined:
    Apr 28, 2009
    Posts:
    273
    a forest i think ur blend files are corrupted or something in this ssao package. my unity crashes every time it gets to bringing in the room.blend file. Also when i try to reopen the files after i have close down unity it freezes up unless i delete the blend files.