Search Home Members Contacts
About Us
Products
Downloads
Community
Support
Pages: [1]
  Print  
Author Topic: Static Ambient Occlusion sample with source [6.5]  (Read 5792 times)
Zaknafein
Customers
Community Member
*****
Posts: 2940


WWW
« on: February 09, 2007, 12:22:54 AM »

Here it is, my static ambient occlusion shader with sample and full source!

VB.NET Sample
http://zaknafein.hjcrusaders.com/samples/AmbientOcclusion/StaticAmbientOcclusion.rar (6.76Mb)
Mirror (thanks AriusMyst) : http://azazeldev.org/zak/StaticAmbientOcclusion.rar

See below posts for updates... last updated 03/03/2007.

Screenshots





Description
Traditional DirectX lighting models define ambient lighting as coming from all directions, and is added as a constant on all surfaces regardless of the geometry. Ambient occlusion acts as a factor to ambient lighting to take into account the cavities and concave areas in a model, or how much a surface is hidden from its environment.

It is calculated using ray collision tests on each vertex, and stored in the vertex colors. Then a shader takes that value and uses it to calculate ambient lighting.

Since the effect takes a long time to generate its data, it is stored in the model and can be saved in the TVM. It's clearly a pre-process step. I saved the highest quality of occlusion (289 rays per vertex) into each model so you don't have to regenerate it; just the floor mesh which I don't save.
To accelerate the processing on multi-core CPUs, this step is multi-threaded with up to 4 threads. The code is about 99% threadsafe; I have a random bug that occurs in Release out of the IDE so it's very hard to debug... so I ignored it.

A problem with it being static is that if you calculate the ambient occlusion with all surrounding geometry, then the objects shouldn't move in relation to each other or the effect won't appear correct.
One solution to this is to calculate self- ambient occlusion only for models that don't animate (so all TVMs) regardless of surrounding objects. Then the objects can move around and still have the correct occlusion, with a bit less realism.

The shader also has a full lighting pipeline built-in, with offset-bumpmapping (aka parallax mapping), specular mapping, and emissive mapping. It can process up to 4 point lights and a directional light in a single pass in the Shader Model 3 profile. In SM2.0 and SM2.x, the limit is set to 2 to avoid low framerates. It does not run on SM1.4 hardware.
It's also pretty much the best shader I've ever written in terms of code structure and optimization. Cheesy

For more information please see the In-Development thread.

Enjoy!
Logged

AriusMyst
Guest
« Reply #1 on: February 09, 2007, 09:37:41 AM »

Excellent Zak, I will certainly be taking a closer look at this. Thanks for letting us look at your source. Wink
Logged
AriusMyst
Guest
« Reply #2 on: February 09, 2007, 09:47:01 AM »

Mirrored: http://azazeldev.org/zak/StaticAmbientOcclusion.rar

Hope you don't mind the mirror Zak, if you do let me know and i'll take it down.

Edit: This mirror is currently up to date.
Logged
rootsage
Customers
Community Member
*****
Posts: 444

Gamer Enthusiast


WWW
« Reply #3 on: February 09, 2007, 03:02:39 PM »

Nice, downloading the evil 6.66mb RAR  :twisted: haha
Logged

while( !( succeed = try_again()) );
------
10 print "Is this recursive?"
20 goto 10
pizzayoyo
Customers
Community Member
*****
Posts: 1423


WWW
« Reply #4 on: February 10, 2007, 02:06:44 PM »

Looks awesome as usual! Nice work.
Logged

Zaknafein
Customers
Community Member
*****
Posts: 2940


WWW
« Reply #5 on: February 11, 2007, 06:06:40 PM »

Pretty important update!

I read a 1993 paper by Richard F. Lyon called "Phong Shading Reformulation for Hardware Renderer Simplification" which is available on his site. This paper proposes a reformulation of Blinn-Phong and Phong that uses distance calculations instead of a dot product to get the specular highlights.
Since there are a lot of artifact problems when working with Blinn-Phong and normal-mapping, I decided to give it a shot and convert the original MATLAB code to HLSL. And it works very well!

Blinn-Phong with Normal Mapping


Lyon-Blinn with Normal Mapping


Edit :
I got the authorization from Mr. Lyon so I'll be releasing the updated demo sometime this week.
« Last Edit: August 26, 2007, 09:37:37 AM by Zaknafein » Logged

Zaknafein
Customers
Community Member
*****
Posts: 2940


WWW
« Reply #6 on: February 15, 2007, 02:37:07 PM »

The sample has been updated!

New features :
- Lyon lighting model replaces Blinn-Phong of the old sample for the reasons cited above
- Fixed the rare multithreading issue... well worked around it with a try-catch, hehe
- A bit more comments in the source
- Added ability to calculate Bent Normals, which replace the mesh's normals and are implemented at 0 cost in the shader. This technique is usually meant for Image-Based Lighting, but I decided to give it a shot with standard lighting and I found it does look pretty good in some situations. It attenuates the darkening in occluded areas and somewhat compensates for the lack of indirect illumination.

Here are some comparison shots. The difference is very subtle...

---------- STANDARD NORMALS --------- | ------------- BENT NORMALS ------------







Logged

AriusMyst
Guest
« Reply #7 on: February 15, 2007, 02:50:03 PM »

Nice work Zak. Mirror is updated also. Smiley
Logged
Zaknafein
Customers
Community Member
*****
Posts: 2940


WWW
« Reply #8 on: March 03, 2007, 03:27:14 PM »

Hopefully final update to this demo, but an important one. Link is updated above, and now on my blog's server, so much faster downloads Cheesy
Apart from some more commenting and code separation, here's the new features list :

- I have implemented a second type of ambient occlusion "computer", or a method of computing it. It uses hemicube rendering and mip-mapping/shaders to find out how much geometry occludes every vertex. The cool thing about it is that it's about 10 times faster than ray casting (reaching 190 vertices per second), and is nearly samples-count independant, which means you can go to ludicrously samples count like 50000 and still get similar speed.

- I have rewritten and fixed the ray caster's ray distribution (see this thread for a follow-up) which means that the ray caster still works, the speed hasn't changed, but it's perfectly accurate. Even more accurate than the hemicubes, because the ray distribution is almost perfectly uniform.

- I have switched the "ambient occlusion factor" to a "diffuse occlusion factor", which means the occlusion value can filter out diffuse and specular light as well. This allows for even more occlusion, might look good in some cases.

Some screenshots of the new stuff :

.............. RAYTRACER ............................... HEMICUBES ..................
|

.. ONLY AMBIENT OCCLUSION .... WITH DIFFUSE OCCLUSION ..
|
Logged

AriusMyst
Guest
« Reply #9 on: March 03, 2007, 04:57:09 PM »

You got the hemicube stuff integrated, thats awesome man( you know that means a good deal to my current project Wink ). I'll mirror this tommorow morning. Top stuffs. Wink
Logged
AriusMyst
Guest
« Reply #10 on: March 06, 2007, 02:51:37 AM »

My mirror is now up to date. Apologies for taking so long to update it Zak, I totally forgot.
Logged
Lil`Buh
Community Member
*
Posts: 60


« Reply #11 on: March 07, 2007, 06:55:05 AM »

just an idea: how about putting version number / date in the filename ? Wink
filename-03062007.rar :p
just in case Wink

thanx both of you ( for coding such great sharders and for mirroring )
Logged
tedlin01
Customers
Community Member
*****
Posts: 143


WWW
« Reply #12 on: December 15, 2010, 01:35:12 PM »

Would it be possible to get this working with a CTVLandscape?
Logged

tedlin01
Customers
Community Member
*****
Posts: 143


WWW
« Reply #13 on: December 18, 2010, 10:06:05 PM »

I got it working on the landscape. I just stored the occlusiondata in a texture instead of saving it into the mesh itself. Works wonders Smiley
Logged

arnienet
Customers
Community Member
*****
Posts: 263


WWW
« Reply #14 on: December 19, 2010, 10:32:18 AM »

Amazing stuff Zak, thanks for sharing and helping us all get better and better effects through TV3D.

@tedlin01, good idea about using a texture to store the occlusion data, do you have any before and after images? It'd be interesting to see.
Logged

Total Dev time = 50% to code, 50% to test, 50% to find errors, 50% to fix, that's why it takes twice as long.

Dawn World MMO
tedlin01
Customers
Community Member
*****
Posts: 143


WWW
« Reply #15 on: December 19, 2010, 12:25:30 PM »

Amazing stuff Zak, thanks for sharing and helping us all get better and better effects through TV3D.

@tedlin01, good idea about using a texture to store the occlusion data, do you have any before and after images? It'd be interesting to see.

Okey, here's my experience and thoughts about this so far. I have maps with over 500 meshes. If I should use SSAO on them I can do it using this technique and save the data into each mesh. This will take some time to do the first time though. For the landscape it takes some time to do this in high detail as well, but once it's done you don't have to do it again. Anyway I thought of another solution. To simulate a more natural look then pre-SSAO in the game I could probably just try to simulate a contactsurface. So instead of using a Octahedron Sphere I could just do a simple upwards collision to detect if there's an object above the terrain. Once that's done I could just run through the pixels and compute a blur on the texture. Hopefully I can get almost the same results in my game using this technique instead of real SSAO and it would save me alot of precomputing time.

As for the meshes I would just compute the worldaltitude on the pixels in the shader and lit it depending on how close to landscape it is.

Anyway here's two printscreens with the real SSAO-technique on the landscape (not on meshes though). The size of the landscape is 1024x1024 fit into a 128*128 texture(low detail yeah), took aprox 5 min to compute with ~300 meshes on the level.


Logged

Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.3 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks