### Quick Ambient Occlusion

fixed4 raymarch (float3 position, float3 direction) { for (int i = 0; i < _Steps; i++) { float distance = map(position); if (distance < _MinDistance) { fixed4 color = renderSurface(position, direction); float ao = 1 - float(i) / (_Steps-1); color.rgb *= ao; return color; } position += distance * direction; } return fixed4(1,1,1,1); }

The following animation shows a complex geometry being rotated and sliced at the same time. The right part is rendered in white; shade on the left side indicates how many steps were required by the raymarching loop to touch the surface.

This information does not require any additional computation, and is already a good estimation for the ambient occlusion.

### Real Ambient Occlusion

This solution provide a nice, yet not very reliable measure for space occlusion. Alex Evans came up with a much better solution (link here). The idea behind is is to sample the distance field along the normal direction. If no closer object is found, it is a good indication that the surrounding area is free from obstacles.

For more reliable results, we repeat this process for `_AOStepSize`

times, each time going `_AOStepSize`

units further away from the surface. If no closer object is found, the sum of the sampled distances will be equal to `_AOStep * _AOStepSize`

. This gives us a value we can interpolate from, resulting in an ambient occlusion coefficient that goes from 0 to 1.

float ambientOcclusion (float3 pos, float3 normal) { float sum = 0; for (int i = 0; i < _AOSteps; i ++) { float3 p = pos + normal * (i+1) * _AOStepSize; sum += map(p); } return sum / (_AOStep * _AOStepSize); }

This solution is sensibly slower than the cheap ambient occlusion presented in the previous section. However, it does not depend on the distance from the camera and is much easier to control.

### Exponential Decay

A more realistic improvement can be obtained by weighting those samples differently. The further away we are from the original point, the less importance we give to the sample. We can do this by introducing an exponential decay:

float ambientOcclusion (float3 pos, float3 normal) { float sum = 0; float maxSum = 0; for (int i = 0; i < _AOSteps; i ++) { float3 p = pos + normal * (i+1) * _AOStepSize; sum += 1. / pow(2., i) * map(p); maxSum += 1. / pow(2., i) * (i+1) * _AOStepSize; } return sum / maxSum; }

If we found no closer point, then `sum`

is equal to `maxSum`

. Their fraction is 1, which indicates no occlusion. The smaller `sum`

is, the more occlusion we have. The following animation shows only the contribution from ambient occlusion:

It’s clear that, compared to the previous animation, the internal ridges of the geometries are shaded in a much more realistic way.

### Conclusion

#### Other Resources

- Part 1: Volumetric Rendering
- Part 2: Raymarching
- Part 3: Surface Shading
- Part 4: Signed Distance Fields
**Part 5: Ambient Occlusion**- Part 6: Hard and Soft Shadows
- Part 7: Volume Raycasting

##### 💖 Support this blog

This websites exists thanks to the contribution of patrons on Patreon. If you think these posts have either helped or inspired you, please consider supporting this blog.

##### 📧 Stay updated

You will be notified when a new tutorial is relesed!

##### 📝 Licensing

You are free to use, adapt and build upon this tutorial for your own projects (even commercially) as long as you credit me.

You are not allowed to redistribute the content of this tutorial on other platforms. Especially the parts that are only available on Patreon.

If the knowledge you have gained had a significant impact on your project, a mention in the credit would be very appreciated. ❤️🧔🏻

Hello,

will you continue this tutorial/guide on raymarching? It’s very interesting!

Hey!

Sure I will! At the moment the next four posts are available for preview to $5+ Patrons on Patreon. Since the series got so much attention, I’ll definitely write some more about it.

by the next four posts are u meaning this one?

Hey! Yes, this one and the other one in the series.

They’are available for preview, even though they are not complete yet! T_T

Hi,I I have already paid for $5+ Patrons on Patreon，but I can not

see the nex tutorials! why to get access?

Hey Max!

If you are a $5+ patrons you can find the password to access my latest content on this page. https://www.patreon.com/posts/preview-in-unity-13006058

The password changes every now and then, so just keep an eye on the most recent Patreon posts tagged with “[PREVIEW]”.

Some of the posts in this series are not been completed yet, but they are still available for preview. Most posts that have downloadable content are available to $10+ patrons.

I hope this helps!

hello i am on patrons

but dont find where those are 😉

Hi, is there any ETA on the Ambient Occlusion part of the series?

Thanks 🙂

Hey! Not yet, unfortunately. But most of the code is already there! :-p

Non of your password works for this tutorial. Both old and new, including the one you just linked in comments. 🙁