Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Real time ambient occlusion - now with textures!

Discussion in 'Made With Unity' started by forestjohnson, Feb 5, 2008.

Thread Status:
Not open for further replies.
  1. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    *I updated it with textures and the ability to see either the final composite, no ambient occlusion at all or just the ambient result with no objects.*

    Some of you might have seen a thread in ShaderLab about this: http://forum.unity3d.com/viewtopic.php?t=9249

    I got extra interested in this method because it operates on the level of the pixels flat on the screen and creates a very strong effect that catches the human eye and makes the image seem more "real". I've always wondered if rendering could be done with more focus to what is actually on the screen similar to how a painter creates a realistic image with oils and brushes, by many tricks pulled on the viewer's eyes to make things appear real and in perspective when they actually aren't at all. This effect seems like a step in the right direction for that type of rendering so I was instantly a fan of it.



    Well I almost pulled it off, it works fully but has some problems:

    1: It doesn't work transparent objects at all (they don't appear in the final composite frame), but alphatest is ok.

    2: Due to precision issues the range is limited to about 120 meters (though it handles this limit well, its still there)

    3: It doesn't always look good (duh) and can produce artifacts sometimes

    4: It uses an unhealthy mix of immediate mode and normal rendering and is not robust in any way.

    But it works! It is pretty fast for small resolutions too, considering I didn't even try to optimize at all.

    All Important Webplayer Link (not guaranteed stable) : http://yogware.bluegillstudios.com/SSAO/SSAO.html

    And Unity package:
     

    Attached Files:

    Kos-Dvornik likes this.
  2. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    You're a madman :wink:

    It looks quite convincing and runs fairly well even on my machine. I'm curious what happens when you increase the resolution, does it look better or do the artifacts become more pronounced?

    Great work as always.
     
  3. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
  4. defmech

    defmech

    Joined:
    Feb 24, 2007
    Posts:
    506
    That's awesome! It's also more fun than I expected flinging those objects around.

    It's also really interesting to see how different developers approach this problem. I've seen quite a few different examples and all have slightly different results and levels of success.
     
  5. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Nice stuff!
     
  6. Obscurity

    Obscurity

    Joined:
    Nov 13, 2007
    Posts:
    203
    That is fantastic! I am very much in agreement with you that there should be more "painterly" tricks in real time renderings. Hope to see more of this if you explore it further.
     
  7. defmech

    defmech

    Joined:
    Feb 24, 2007
    Posts:
    506
  8. drJones

    drJones

    Joined:
    Oct 19, 2005
    Posts:
    1,351
    wow forest, very well done (i like the simple light setup too - there's a game hiding in there somewhere ; )
     
  9. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Very nice!

    Another totally hacked trick that might be worth trying: just blurring the depth buffer and subtracting from unblurred one. See here: http://mpan3.homeip.net/?ssaowcn
     
  10. taumel

    taumel

    Joined:
    Jun 9, 2005
    Posts:
    5,292
    I like that look too but sadly it crashes here...
     
  11. boxy

    boxy

    Joined:
    Aug 10, 2005
    Posts:
    675
    Superb. I think even though you'll improve on it, once you have your textures, lightmaps bumps etc on the surfaces that is going to be totally useable.
    I'd love to see how it looks with the other texture layers applied.
    Cheers
    Boxy
     
  12. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Its very cool Yoggy, I got between 15 and 20 fps on my abacus. It amazaes me the stuff you come up with. Good job.
    AaronC
     
  13. noradninja

    noradninja

    Joined:
    Nov 12, 2007
    Posts:
    420
    This is pretty awesome. Will say that on my Mac Pro, it gets around 30FPS in fullscreen, altho I think it loses quality. If that is the case or not, awesome work man, would love to see this with some textures/materials applied. Gives me yet another reason to get the 8800 when they release it for the original Mac Pros...
     

    Attached Files:

  14. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    The loss of quality at high resolution is because the render texture I use still a 512x512, in my high res test I used a higher resolution render texture.

    I updated it with textures and the ability to see either the final composite, no ambient occlusion at all or just the ambient result with no objects.
     
  15. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    15fps with all
    22-25 AO only
    25ish No AO

    Looking great Yoggy
    AC
     
  16. holmeren

    holmeren

    Joined:
    Dec 12, 2006
    Posts:
    300
    HAHA Dude this is SO cool...
    Good work!
     
  17. boxy

    boxy

    Joined:
    Aug 10, 2005
    Posts:
    675
    It looks great with textures though yes there are one a two artifacts full screen. I'm getting between 75 and 100 fps on my macbook pro. Great job.
     
  18. Steffen

    Steffen

    Joined:
    Aug 11, 2005
    Posts:
    70
    great work. i get 200fps at fullscreen with a 8800gt.
     
  19. Obscurity

    Obscurity

    Joined:
    Nov 13, 2007
    Posts:
    203
    Once again, I have to say how cool this is. Someday soon I won't have to bake ambient occlusion anymore!

    And just to help with your data: With all lighting modes on I dip down to 48 fps in the webplayer window and down to 28 fps in full screen mode. This is on a mac book pro in OS X.
     
  20. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    Very nice!
     
  21. noradninja

    noradninja

    Joined:
    Nov 12, 2007
    Posts:
    420
    Mac Pro Quad 2.66
    2GB RAM
    GForce 7300
    ------------

    45-50 FPS in web player
    30 FPS Fullcreen @ 1024x768

    Forrest, this is amazing. The fact that it doesn't hit performance as much as I would expect it to just blows me away. 30FPS is totally acceptable, IMO. Might just save me from baking AOC into my textures.

    Do you think you could combine the shader with lightmaps, or is this a render to texture effect (and thus would need Pro to show it)? I'm going to run a test, but does having multiple pixel or vert lights slow it down?
     
  22. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    First off, this is not a shader. As far as what someone has to do to use it, mainly they just have to drag a script onto their camera. They also may have to attach a script/layer to every non-solid object in their game.

    For optimization purposes they might build lower res combined versions of their level meshes to be used for depth rendering.

    I don't think it really replaces baked AO, but it might let you keep a consistent indirect lighting "look" to a game with lots of moving objects that are close to eachother.


    Here is what it does performance wise that causes slowdown:

    Render the entire scene into a texture with all objects using a not very complex pixel shader, at a lowish resolution.

    Do a complex pixel shader calculation for each screen pixel, sampling from the previously rendered texture. Repeat x times, where x is the quality setting.

    Do another pixel shader calculation for each screen pixel, but this time it is simpler.

    For some scenes rendering all the objects again will incur draw call or triangle count bottlenecks. For simple scenes though the full screen pixel shaded quad draws are what really slows it down. They could probably be optimized a bit on the shader level and made to work well with less iterations, but I don't have time to do it myself right now.

    Also keep in mind this can't be just plugged into a game as is, it will break some things like transparent objects. (And I haven't a clue how it would handle things like terrain, etc)
     
  23. spawrks

    spawrks

    Joined:
    Nov 16, 2007
    Posts:
    89
    Is it normal for this to be just black on a macbook?

    (GMA950 here)
     
  24. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    I've heard of that happening before, I think that means that the shader.isSupported returns true, but when it comes time to render it actually doesn't work.

    I can't think of a way around other than to play with things on a GMA and see if I can get it to work, or just check the hardware name and disable based on that.
     
  25. Ricko

    Ricko

    Joined:
    Dec 9, 2007
    Posts:
    169
    I have seen the same issue many times on an engine I wrote when it comes to the 950. The driver is notorious for reporting its capabilities incorrectly, and those run-time reports from the drivers are all the engine has to go on for scaling the rendering to fit the graphics device.

    Ricko
     
  26. groovista

    groovista

    Joined:
    Feb 15, 2007
    Posts:
    21
    Alas, yoggy, no joy here in Big Sur (no decent graphics card yet). Pfft.

    Please keep exploring the more "painterly" rendering techniques!
     
  27. spawrks

    spawrks

    Joined:
    Nov 16, 2007
    Posts:
    89
    for what its worth Yoggy, I wholeheartedly agree with you about the post-rendering FX. Our world has shown us that photography is not so much in what you're taking a picture of, but how you display it. I look forward to more ..."paintery" fx like this one!
     
  28. llavigne

    llavigne

    Joined:
    Dec 27, 2007
    Posts:
    977
    Very nice.
    Let's hope the mothership doesn't find you.
     
  29. mrbdrm

    mrbdrm

    Joined:
    Mar 22, 2009
    Posts:
    510
    Hi
    i love it , great work :D
    i have problem tho , i get a black screen when i test it in unity editor but when i change the alpha value to : 0 i see the scene without the ssao :cry:
    but the one you posted in the web player it works perfect
    -----
    dell xps m1530
    vista
    8600 gt
     
  30. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,595
    60fps....
    Quadro FX 570

    but on windows its so dark I can barely see the difference between it being on or off... gamma issues between mac and pc?
     
  31. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    yes, you need a SM3 capable 3D graphic card for AO, Intels CPU to DVI adapters sadly don't qualify as such.
     
  32. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,359
    Totally black on GF 9800 GTX+ in Windows ( I don't try your work in Mac).

    Unity Pro 2.5
     
  33. turi

    turi

    Joined:
    Dec 11, 2008
    Posts:
    22
    ...black on my MacBookPro as well. Could it be related to the latest Unity release?
    Forest you have any ideas about this? Thanks.
     
  34. llavigne

    llavigne

    Joined:
    Dec 27, 2007
    Posts:
    977
  35. Polygans

    Polygans

    Joined:
    Apr 19, 2009
    Posts:
    79
    I'm getting a few warnings with this along with it not working :(

    Any idea why?

    Here are the warnings:

     
  36. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Warnings wouldn't make it not working (the first one is obvious, the second one related to the scripts not beeing Unity 2.5)


    not working is commonly related to the gpu not strong enough or you not owning Unity Pro
     
  37. Spacemonkey

    Spacemonkey

    Joined:
    Oct 24, 2008
    Posts:
    89
    Works fine here. A bit slow of course, 11-12 fps and 7-8 fps in fullscreen, but renders as it should (GREAT job on the author btw). I have an Intel single core 1.66 GHZ with GMA945. I'm running on windows though so maybe it works better with directx?
     
  38. medafor

    medafor

    Joined:
    Jun 22, 2009
    Posts:
    20
    anyone ever come up with a solution to solve the black screen. Forest web player demo works fine on my gpu. dont know how it would be the gpu. If that were the case my web player output would work also, but it is also black ...unlike Forest web demo.
     
  39. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,848
    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.  
     
  40. medafor

    medafor

    Joined:
    Jun 22, 2009
    Posts:
    20
    yeah tried that from your tip in the other thread, didnt work for me :(
     
  41. yty

    yty

    Joined:
    Aug 10, 2009
    Posts:
    81
    my system = windows xp3
    my unity = 2.5.0f5(21627)
    my Graphics = 8800GT

    The following is an error message.
    Please help me, I am very grateful.

    screenviewcoord[1] < 0 || screenviewcoord[3] < 0
    UnityEngine.RenderTexture:set_active(RenderTexture)
    UnityEngine.RenderTexture:set_active(RenderTexture)
    SSAOSetup:OnPreCull() (at Assets\SSAO\ScriptAndShaders\SSAOSetup.js:141)
    UnityEditor.EditorGUIUtility:RenderGameViewCameras(Rect, Rect, Boolean, Boolean)
    UnityEditor.EditorGUIUtility:RenderGameViewCameras(Rect, Rect, Boolean, Boolean)
    UnityEditor.GameView:OnGUI() (at C:\builds\unity-trunk\unity\Editor\Mono\GameView\GameView.cs:192)
    System.Reflection.MonoMethod:InternalInvoke(Object, Object[])
    System.Reflection.MonoMethod:InternalInvoke(Object, Object[])
    System.Reflection.MonoMethod:Invoke(Object, BindingFlags, Binder, Object[], CultureInfo)
    System.Reflection.MethodBase:Invoke(Object, Object[])
    UnityEditor.HostView:Invoke(String) (at C:\builds\unity-trunk\unity\Editor\Mono\GUI\DockArea.cs:156)
    UnityEditor.DockArea:OnGUI() (at C:\builds\unity-trunk\unity\Editor\Mono\GUI\DockArea.cs:491)

    [..\..\Runtime\Camera\CameraUtil.cpp line 37]
    (Filename: Assets/SSAO/ScriptAndShaders/SSAOSetup.js Line: 141)
    :(
     

    Attached Files:

  42. Deleted User

    Deleted User

    Guest

    Has anyone fixed the black screen? :/

    Running GeForce 9400GT here with Win7 and screen just goes black. How ever it works just fine on the webplayer test that Forest has posted.. :/

    EDIT:
    might be something with blur shader (just a guess)
     
  43. Udit_N

    Udit_N

    Joined:
    Mar 9, 2015
    Posts:
    17
    Hi, my issue/ query is bit different from the discussion what's going on in here, actually I'am building a platform for android where in a room I will be placing furniture like sofas, tables etc. where I want real time AO on those objects as it gives a realistic feel but unfortunately SSAO doesnt work for android or mobile platforms. Is there any other option which can give same or somewhat results as SSAO gives? as per my research I dint get any other script which gives the same effect, but if anyone over here knows anything about it please let me know, will be grateful! please help
     
  44. Mauri

    Mauri

    Joined:
    Dec 9, 2010
    Posts:
    2,657
    SSAO is an expensive post effect and thus it wouldn't really work on mobile devices.
    You could bake a lightmap with AO enabled.
     
  45. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Locked because of necro for no good reason whatsoever. It wasn't even on topic. Make a new topic if a thread is wildly out of date or you're deviating from the discussion.
     
Thread Status:
Not open for further replies.