Logo by Pauldelbrot - Contribute your own Logo!


it was a great time but no longer maintainable by c.Kleinhuis contact him for any data retrieval,
thanks and see you perhaps in 10 years again

this forum will stay online for reference
News: Visit us on facebook
Welcome, Guest. Please login or register. October 26, 2020, 05:40:26 PM

Login with username, password and session length

The All New FractalForums is now in Public Beta Testing! Visit FractalForums.org and check it out!

Pages: [1]   Go Down
Share this topic on DiggShare this topic on FacebookShare this topic on GoogleShare this topic on RedditShare this topic on StumbleUponShare this topic on Twitter
Author Topic: Cheap ambient occlusion approximation  (Read 2224 times)
0 Members and 1 Guest are viewing this topic.
« on: June 02, 2012, 07:00:44 PM »

I didn't know if to post this in New Theories and Research, or here, but I decided here.
This has probably been invented before, but I thought of it on my own, and better told and already known about then not told and not known about.

Goal of algorithm: Very cheap ambient occlusion (actually, I don't know if that's the right term), which means at least less than 10 distance estimator calls. This rules out shooting another ray, let alone the multitudes required for the method described at http://en.wikipedia.org/wiki/Ambient_occlusion.
The purpose is to decrease render time at the cost of shading accuracy. This is not a replacement for true ambient occlusion or global illumination, just a cheap approximation.

The roots of the idea are in http://www.iquilezles.org/www/material/nvscene2008/rwwtt.pdf that slideshow, near the bottom, under the title "Rendering with distance fields :: Ambient Occlusion" (ctrl-f it). The algorithm is as follows:
Start at a point 'p' that is almost exactly distance 'distance' away from the fractal (all pixels in a region should be the same distance away), and a normal 'dir' that represents the normalized normal at that point. Being exactly 'distance' away and having an accurate normal are critical to this method.
Then start a ray 'shot' starting from 'p' and going in the direction of the normal. Iterate only a few steps, 5 works well. Higher numbers mean everything is more shaded and more affected by distant objects. Once done, take the distance you could have gone if you were on a perfectly flat plane going straight up- which happens to be (initialDistance * pow(2, stepCount - 1)) if stepCount is the number of steps you took (it should be a constant). Divide total distance actually traveled by this "could have gone" distance, and you have your shading value.
The result is a average quality ambient occlusion test that only takes 3-7 distance estimator calls, depending on how shaded you want it to be.

Why it works: If the surroundings of a point you are testing is wide open, then the ray will go the same distance as if it were in a open plane, resulting in a shade value of 1. However, if there is a wall right in front of it, it will obviously not go as far, resulting in a shade value less than 1.

Some optimized hlsl code-

float OcclusionTest(float3 p, float3 dir, float delta) // delta is another name for 'distance'
float totalDistance = delta; // initialize totalDistance to delta, for we know that it is delta away from the fractal
for (int i = 0; i < AoStepcount; i++) // AoStepcount is about 5 for me
totalDistance += DE(p + dir * totalDistance); // standard simplified raytracing
return totalDistance / (delta * pow(2, AoStepcount)); // modified from formula in proposal, because we initialized totalDistance to delta and not zero

Attached is an image where only this method was used to color the Mandelbox.

* screenshot1.jpg (124.89 KB, 1000x563 - viewed 276 times.)
« Reply #1 on: June 02, 2012, 07:49:23 PM »

Thanks for sharing your ideas! This is another good one very similar to rrrola's code (taken from boxplorer)
// Ambient occlusion approximation.
float ambient_occlusion(vec3 p, vec3 n) {//here p is the ray and n the normal
  float ao = 1.0, w = ao_strength/ao_eps;
  float dist = 2.0 * ao_eps;

  for (int i=0; i<5; i++) {
    float D = d(p + n*dist);
    ao -= (dist-D) * w;
    w *= 0.5;
    dist = dist*2.0 - ao_eps;  // 2,3,5,9,17
  return clamp(ao, 0.0, 1.0);

It also steps away from the normal for a few steps. Probably this also came from IQ's idea originally??

OK just re-read the paper - credit to Alex Evans for this idea.
« Last Edit: June 02, 2012, 07:53:22 PM by eiffie » Logged
Fractal Molossus
Posts: 681

« Reply #2 on: June 03, 2012, 01:16:03 AM »

Yes, the IQ/Evans method is the most common way to do AO when sphere tracing (that, and screen space AO). I also use this method, I just sample at fixed offsets along the normal (like rrrola).

Notice, that there is a similar trick for soft shadows: http://www.iquilezles.org/www/articles/rmshadows/rmshadows.htm
« Reply #3 on: June 03, 2012, 01:59:20 AM »

Yes, the IQ/Evans method is the most common way to do AO when sphere tracing (that, and screen space AO). I also use this method, I just sample at fixed offsets along the normal (like rrrola).

Notice, that there is a similar trick for soft shadows: http://www.iquilezles.org/www/articles/rmshadows/rmshadows.htm
Did a double take there, my real name is Evan, tongue stuck out
I've actually seen that article before, and if I'm not mistaken, it requires another entire ray shot, which is quite a lot if you're going for speed.

Oh, and I don't like SSAO because it requires reading the pixel values of neighboring rays- This introduces the problem of a 100x100 image being different from a 1000x1000 image, because relative distance from one pixel to the next is different. Then again, I really want to do DOF, so I guess I'll have to break that self-rule.
« Last Edit: June 03, 2012, 05:24:11 PM by khyperia » Logged
Pages: [1]   Go Down
Jump to:  

Related Topics
Subject Started by Replies Views Last post
Faked ambient occlusion Mandelbulb Implementation « 1 2 » ZsquaredplusC 15 4338 Last post December 29, 2009, 02:57:27 PM
by cKleinhuis
screen space ambient occlusion 3D Fractal Generation Tglad 11 7920 Last post August 08, 2010, 01:23:38 PM
by Rrrola
Series approximation Images Showcase (Rate My Fractal) Kalles Fraktaler 0 1028 Last post July 01, 2013, 08:40:30 AM
by Kalles Fraktaler
series approximation Mandel Machine quaz0r 1 998 Last post July 29, 2016, 01:49:25 AM
by skychurch
Series approximation for M set Programming « 1 2 » skychurch 16 2572 Last post May 09, 2017, 08:44:13 PM
by skychurch

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines

Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM
Page created in 0.138 seconds with 27 queries. (Pretty URLs adds 0.01s, 2q)