STOL4S / 2D-Ambient-Occlusion

This repository contains a C# class that is capable of taking a 2D image and generating a shadow on the surface behind it, and shadows itself.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

2D Ambient Occlusion (Soft Shadowing)

This repository contains a C# class, and an HLSL file, that are capable of generating ambient occlusion shadows in a 2D scene. This algorithm works by generating a 3D position buffer from the 2D scene and then calculating soft shadows using this information. If self-shadowing is enabled, then another pass is done and each sprite is scanned individually and checked for occlusion on itself.

This was originally only a C# shader, but now is available as an HLSL shader in the AOHLSL folder. This shader was intended for use in MonoGame, but could be used in any DirectX project. The original C# function is still available and could potentially be used in other projects involving software rendering only.

Examples

No Shading

Buffer

Ambient Occlusion

GeneratedComposite

Generated Occlusion Map

AO

No Shading

Buffer

Ambient Occlusion (Self Shadow)

AO_SS

Rendered Scene

RenderedScene

How Does it Work?

The function starts with passing a Bitmap BackBuffer and an array of sprites to be drawn to the SSAO function. A position buffer is then calculated by ordering the sprites in drawing order. Sprites that are in the background will have lower red values on the position buffer than sprites in the foreground. The blue value in the position buffer corresponds with the local y position of the current pixel. This allows the algorithm to determine if the sprite is too high off the ground to cast a shadow onto the ground. Based on the information in the position buffer, depth-based SSAO can then be calculated for the 2D scene.

DepthBuffer

It is hard to see in the position buffer, but the trees all have a different red value from eachother with a blue gradient going upwards. Similar to how a position buffer would be generated in a 3D world. This gives just enough information to calculate some 3D shaders into this 2D scene.

About

This repository contains a C# class that is capable of taking a 2D image and generating a shadow on the surface behind it, and shadows itself.

License:GNU General Public License v3.0


Languages

Language:C# 89.5%Language:HLSL 10.5%