Skip to content

Commit

Permalink
Finished SSAO
Browse files Browse the repository at this point in the history
  • Loading branch information
kraifpatrik committed Jun 9, 2022
1 parent 8916cfd commit a8b0ac7
Show file tree
Hide file tree
Showing 22 changed files with 111 additions and 93 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions BBMOD_GML/objects/OMain/Create_0.gml
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ renderer = new BBMOD_Renderer();
renderer.UseAppSurface = true;
renderer.RenderScale = (os_browser == browser_not_a_browser) ? 1.0 : 0.8;
renderer.EnableShadows = true;
renderer.EnableGBuffer = true;
renderer.EnableSSAO = true;
renderer.SSAORadius = 32.0;
renderer.SSAODepthRange = 5.0;
renderer.SSAOPower = 2.0;
renderer.EnablePostProcessing = true;
renderer.ChromaticAberration = 3.0;
renderer.ColorGradingLUT = sprite_get_texture(SprColorGrading, 0);
if (os_browser == browser_not_a_browser)
{
renderer.EnableGBuffer = true;
renderer.EnableSSAO = true;
renderer.SSAORadius = 32.0;
renderer.SSAODepthRange = 5.0;
renderer.SSAOPower = 2.0;
renderer.Antialiasing = BBMOD_EAntialiasing.FXAA;
}

Expand Down
2 changes: 1 addition & 1 deletion BBMOD_GML/objects/OSky/Create_0.gml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
event_inherited();

day = (random(1.0) > 0.25);
day = true;//(random(1.0) > 0.25);

modSky = global.resourceManager.load(
"Data/BBMOD/Models/Sphere.bbmod",
Expand Down
2 changes: 1 addition & 1 deletion BBMOD_GML/options/amazonfire/options_amazonfire.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion BBMOD_GML/options/android/options_android.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion BBMOD_GML/options/html5/options_html5.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion BBMOD_GML/options/ios/options_ios.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion BBMOD_GML/options/linux/options_linux.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion BBMOD_GML/options/mac/options_mac.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion BBMOD_GML/options/operagx/options_operagx.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion BBMOD_GML/options/tvos/options_tvos.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion BBMOD_GML/options/windows/options_windows.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion BBMOD_GML/options/windowsuap/options_windowsuap.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions BBMOD_GML/scripts/BBMOD_Renderer/BBMOD_Renderer.gml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ function BBMOD_Renderer()
SurGBuffer = noone;

/// @var {Bool} Enables screen-space ambient occlusion. This requires
/// the G-buffer. Defaults to `false`.
/// the G-buffer. Defaults to `false`. Enabling this requires the
/// [SSAO submodule](./SSAOSubmodule.html)!
/// @see BBMOD_Renderer.EnableGBuffer
EnableSSAO = false;

Expand All @@ -167,8 +168,8 @@ function BBMOD_Renderer()
/// Default value is 1.
SSAOPower = 1.0;

/// @var {Real} SSAO angle bias in radians. Default value is 0.01.
SSAOAngleBias = 0.01;
/// @var {Real} SSAO angle bias in radians. Default value is 0.03.
SSAOAngleBias = 0.03;

/// @var {Real} Maximum depth difference of SSAO samples. Samples farther
/// away from the origin than this will not contribute to the effect.
Expand Down
71 changes: 36 additions & 35 deletions BBMOD_GML/shaders/bbmod_shssao/bbmod_shssao.fsh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Reference: https://de45xmedrsdbp.cloudfront.net/Resources/files/The_Technology_Behind_the_Elemental_Demo_16x9-1248544805.pdf
// Reference: http://frederikaalund.com/wp-content/uploads/2013/05/A-Comparative-Study-of-Screen-Space-Ambient-Occlusion-Methods.pdf

// Must be the same values as in the xSsaoInit script!
// The size of the SSAO kernel.
#define BBMOD_SSAO_KERNEL_SIZE 8

varying vec2 v_vTexCoord;
Expand Down Expand Up @@ -121,64 +121,65 @@ float AcosApprox(float x)
return (-0.69813170079773212 * x * x - 0.87266462599716477) * x + 1.5707963267948966;
}

float GetSampleAngle(vec3 origin, vec2 uv, inout float pairWeight)
{
float sampleDepth = xDecodeDepth(texture2D(gm_BaseTexture, uv).rgb) * u_fClipFar;
vec3 samplePos = xProject(u_vTanAspect, uv, sampleDepth);
vec3 s = normalize(samplePos - origin);
vec3 v = normalize(-origin);
float cosAngle = dot(v, s);
float depthDifference = (origin.z - samplePos.z);
if (depthDifference >= u_fDepthRange)
{
pairWeight -= 0.5;
cosAngle = max(dot(v, -s), 0.0);
}
else if (abs(depthDifference) <= 0.01)
{
cosAngle = 0.0;
}
return max(AcosApprox(cosAngle - u_fAngleBias), 0.0);
}

void main()
{
// Origin
float depth = xDecodeDepth(texture2D(gm_BaseTexture, v_vTexCoord).rgb);
float depth = xDecodeDepth(texture2D(gm_BaseTexture, v_vTexCoord).rgb) * u_fClipFar;

if (depth == 0.0 || depth == 1.0)
if (depth == 0.0 || depth == u_fClipFar)
{
gl_FragColor = vec4(1.0);
return;
}

depth *= u_fClipFar;
vec3 origin = xProject(u_vTanAspect, v_vTexCoord, depth);

vec2 noise = texture2D(u_texNoise, v_vTexCoord * u_vNoiseScale).xy * 2.0 - 1.0;

mat2 rot = mat2(
noise.x, -noise.y,
noise.y, noise.x
);

// Occlusion
float weightSum = 0.0001;
float occlusion = 0.0;

for (int i = 0; i < BBMOD_SSAO_KERNEL_SIZE; ++i)
{
float pairWeight = 1.0;
vec2 dir = (rot * u_vSampleKernel[i].xy) * u_fRadius;
vec2 sampleLeftUV = v_vTexCoord + dir * u_vTexel;
vec2 sampleRightUV = v_vTexCoord - dir * u_vTexel;
float angle = GetSampleAngle(origin, sampleLeftUV, pairWeight)
+ GetSampleAngle(origin, sampleRightUV, pairWeight);
occlusion += angle * pairWeight;
weightSum += pairWeight;
vec2 uv1 = v_vTexCoord + dir * u_vTexel;
vec2 uv2 = v_vTexCoord - dir * u_vTexel;

float angle = 1.0;

if (uv1.x > 0.0 && uv1.x < 1.0
&& uv1.y > 0.0 && uv1.y < 1.0
&& uv2.x > 0.0 && uv2.x < 1.0
&& uv2.y > 0.0 && uv2.y < 1.0)
{
float depth1 = xDecodeDepth(texture2D(gm_BaseTexture, uv1).rgb) * u_fClipFar;
vec3 pos1 = xProject(u_vTanAspect, uv1, depth1);
vec3 diff1 = pos1 - origin;

float depth2 = xDecodeDepth(texture2D(gm_BaseTexture, uv2).rgb) * u_fClipFar;
vec3 pos2 = xProject(u_vTanAspect, uv2, depth2);
vec3 diff2 = pos2 - origin;

float cosAngle = dot(diff1, diff2) / (length(diff1) * length(diff2));
angle = max(AcosApprox(cosAngle - u_fAngleBias), 0.0) / X_PI;

if (-diff1.z - diff2.z < 0.01)
{
angle = 1.0;
}

float att = (abs(diff1.z) + abs(diff2.z)) / (u_fDepthRange * 2.0);
att = clamp(att * att, 0.0, 1.0);
angle = mix(angle, 1.0, att);
}

occlusion += angle;
}

occlusion /= weightSum * X_PI;
occlusion /= float(BBMOD_SSAO_KERNEL_SIZE);
occlusion = pow(occlusion, u_fPower);

// Output
Expand Down
45 changes: 16 additions & 29 deletions BBMOD_GML/shaders/bbmod_shssaoblur/bbmod_shssaoblur.fsh
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Size of the SSAO noise texture.
#define BBMOD_SSAO_NOISE_TEXTURE_SIZE 4

varying vec2 v_vTexCoord;

uniform sampler2D u_texDepth;
uniform vec2 u_vTexel; // (1/screenWidth,0) for horizontal blur, (0,1/screenHeight) for vertical
uniform vec2 u_vTexel; // (1 / screenWidth, 0) for horizontal blur, (0 , 1 / screenHeight) for vertical
uniform float u_fClipFar;

#pragma include("DepthEncoding.xsh", "glsl")
Expand Down Expand Up @@ -35,33 +38,17 @@ float xDecodeDepth(vec3 c)

void main()
{
gl_FragColor = vec4(0.0);
float depth = xDecodeDepth(texture2D(u_texDepth, v_vTexCoord).rgb) * u_fClipFar;
float sampleDepth;
vec4 color = texture2D(gm_BaseTexture, v_vTexCoord) * 0.2270270270;
float weightSum = 0.2270270270;
float weight;
vec2 offset1 = u_vTexel * 1.3846153846;
vec2 offset2 = u_vTexel * 3.2307692308;

sampleDepth = xDecodeDepth(texture2D(u_texDepth, v_vTexCoord + offset1).rgb) * u_fClipFar;
weight = 0.3162162162 * step(depth, sampleDepth);
color += texture2D(gm_BaseTexture, v_vTexCoord + offset1) * weight;
weightSum += weight;

sampleDepth = xDecodeDepth(texture2D(u_texDepth, v_vTexCoord - offset1).rgb) * u_fClipFar;
weight = 0.3162162162 * step(depth, sampleDepth);
color += texture2D(gm_BaseTexture, v_vTexCoord - offset1) * weight;
weightSum += weight;

sampleDepth = xDecodeDepth(texture2D(u_texDepth, v_vTexCoord + offset2).rgb) * u_fClipFar;
weight = 0.0702702703 * step(depth, sampleDepth);
color += texture2D(gm_BaseTexture, v_vTexCoord + offset2) * weight;
weightSum += weight;

sampleDepth = xDecodeDepth(texture2D(u_texDepth, v_vTexCoord - offset2).rgb) * u_fClipFar;
weight = 0.0702702703 * step(depth, sampleDepth);
color += texture2D(gm_BaseTexture, v_vTexCoord - offset2) * weight;
weightSum += weight;

gl_FragColor = color / weightSum;
float weightSum = 0.001;
for (float i = 0.0; i < float(BBMOD_SSAO_NOISE_TEXTURE_SIZE); i += 1.0)
{
vec2 uv = v_vTexCoord + u_vTexel * i;
float sampleDepth = xDecodeDepth(texture2D(u_texDepth, uv).rgb) * u_fClipFar;
float weight = 1.0 - clamp(abs(depth - sampleDepth) / 2.0, 0.0, 1.0); // TODO: Configurable blur depth range?
gl_FragColor.rgb += texture2D(gm_BaseTexture, uv).rgb * weight;
weightSum += weight;
}
gl_FragColor.rgb /= weightSum;
gl_FragColor.a = 1.0;
}
18 changes: 18 additions & 0 deletions docs_src/Changelog/Changelog3.6.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Changelog 3.6.0
This release adds a new rendering submodule - SSAO (screen-space ambient occlusion).

## GML API:
### Rendering module:
#### Renderer submodule:
* Added new property `EnableGBuffer` to `BBMOD_Renderer`, which enables rendering into a G-buffer surface in the deferred pass.
* Added new property `GBufferScale` to `BBMOD_Renderer`, which is a resolution multiplier for the G-buffer surface.
* Added new property `EnableSSAO` to `BBMOD_Renderer`, which enables screen-space ambient occlusion. This requires G-buffer and the SSAO submodule!
* Added new property `SSAOScale` to `BBMOD_Renderer`, which is a resolution multiplier for SSAO surface.
* Added new property `SSAORadius` to `BBMOD_Renderer`, which is a screen-space radius of SSAO.
* Added new property `SSAOPower` to `BBMOD_Renderer`, which is the strength of the SSAO effect.
* Added new property `SSAOAngleBias` to `BBMOD_Renderer`, which is SSAO angle bias in radians.
* Added new property `SSAODepthRange` to `BBMOD_Renderer`, which is the maximum depth difference of SSAO samples. Samples farther way from the origin than this will not contribute to the effect.

#### SSAO submodule:
* Added new submodule - SSAO.
* Added new function `bbmod_ssao_draw`, which renders SSAO into a surface.
1 change: 1 addition & 0 deletions docs_src/Changelog/Changelog_.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
In this section you can find changelogs for all releases of BBMOD since 3.0.0.

## Contents
* [3.6.0](./Changelog3.6.0.html)
* [3.5.1](./Changelog3.5.1.html)
* [3.5.0](./Changelog3.5.0.html)
* [3.4.3](./Changelog3.4.3.html)
Expand Down
13 changes: 7 additions & 6 deletions docs_src/Modules/Rendering/RenderingModule.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
Encapsulates submodules related to rendering.

## Contents
* [Cubemap submodule](CubemapSubmodule.html)
* [Depth buffer submodule](DepthBufferSubmodule.html)
* [FXAA submodule](FXAASubmodule.html)
* [PBR submodule](PBRSubmodule.html)
* [Post-processing submodule](PostProcessingSubmodule.html)
* [Renderer submodule](RendererSubmodule.html)
* [Cubemap submodule](./CubemapSubmodule.html)
* [Depth buffer submodule](./DepthBufferSubmodule.html)
* [FXAA submodule](./FXAASubmodule.html)
* [PBR submodule](./PBRSubmodule.html)
* [Post-processing submodule](./PostProcessingSubmodule.html)
* [Renderer submodule](./RendererSubmodule.html)
* [SSAO submodule](./SSAOSubmodule.html)
7 changes: 7 additions & 0 deletions docs_src/Modules/Rendering/SSAOSubmodule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SSAO submodule
This module contains shaders and code required for screen-space ambient occlusion,
which can be enabled when using a [renderer](./BBMOD_Renderer.html).

## Scripting API
### Functions
* [bbmod_ssao_draw](./bbmod_ssao_draw.html)
2 changes: 1 addition & 1 deletion docs_src/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# BBMOD
Welcome to the official documentation of BBMOD 3.5.1!
Welcome to the official documentation of BBMOD 3.6.0!

BBMOD is an advanced 3D rendering solution for GameMaker. It consists of a
custom model and animation file formats (`*.bbmod`, `*.bbanim`), a model
Expand Down
6 changes: 4 additions & 2 deletions gmdoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"title": "BBMOD Docs",
"author": "BlueBurn",
"prefix": ["bbmod", "__bbmod"],
"version": "3.5.1",
"version": "3.6.0",
"analytics": "",
"api": {
"rating": "/bbmod/page_rating.php"
Expand All @@ -15,6 +15,7 @@
"Changelog": {
"file": "Changelog/Changelog_.md",
"pages": {
"3.6.0": "Changelog/Changelog3.6.0.md",
"3.5.1": "Changelog/Changelog3.5.1.md",
"3.5.0": "Changelog/Changelog3.5.0.md",
"3.4.3": "Changelog/Changelog3.4.3.md",
Expand Down Expand Up @@ -89,7 +90,8 @@
"FXAA submodule": "Modules/Rendering/FXAASubmodule.md",
"PBR submodule": "Modules/Rendering/PBRSubmodule.md",
"Post-processing submodule": "Modules/Rendering/PostProcessingSubmodule.md",
"Renderer submodule": "Modules/Rendering/RendererSubmodule.md"
"Renderer submodule": "Modules/Rendering/RendererSubmodule.md",
"SSAO submodule": "Modules/Rendering/SSAOSubmodule.md"
}
},
"Resource manager module": "Modules/ResourceManagerModule.md",
Expand Down

0 comments on commit a8b0ac7

Please sign in to comment.