Skip to content

Commit 9488984

Browse files
committed
Add support for colorized AO
Addresses #211
1 parent 754fa3c commit 9488984

2 files changed

Lines changed: 73 additions & 1 deletion

File tree

‎src/effects/SSAOEffect.js‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
Color,
23
LinearFilter,
34
RepeatWrapping,
45
RGBFormat,
@@ -70,6 +71,7 @@ export class SSAOEffect extends Effect {
7071
* @param {Number} [options.intensity=1.0] - The intensity of the ambient occlusion.
7172
* @param {Number} [options.bias=0.025] - An occlusion bias. Eliminates artifacts caused by depth discontinuities.
7273
* @param {Number} [options.fade=0.01] - Influences the smoothness of the shadows. A lower value results in higher contrast.
74+
* @param {Color} [options.color=null] - The color of the ambient occlusion.
7375
* @param {Number} [options.resolutionScale=1.0] - The resolution scale.
7476
* @param {Number} [options.width=Resizer.AUTO_SIZE] - The render width.
7577
* @param {Number} [options.height=Resizer.AUTO_SIZE] - The render height.
@@ -92,6 +94,7 @@ export class SSAOEffect extends Effect {
9294
intensity = 1.0,
9395
bias = 0.025,
9496
fade = 0.01,
97+
color = null,
9598
resolutionScale = 1.0,
9699
width = Resizer.AUTO_SIZE,
97100
height = Resizer.AUTO_SIZE
@@ -106,6 +109,7 @@ export class SSAOEffect extends Effect {
106109
["aoBuffer", new Uniform(null)],
107110
["normalDepthBuffer", new Uniform(null)],
108111
["luminanceInfluence", new Uniform(luminanceInfluence)],
112+
["color", new Uniform(null)],
109113
["scale", new Uniform(0.0)] // Unused.
110114
])
111115

@@ -202,6 +206,7 @@ export class SSAOEffect extends Effect {
202206
this.distanceScaling = distanceScaling;
203207
this.samples = samples;
204208
this.rings = rings;
209+
this.color = color;
205210

206211
// @todo Special case treatment added for backwards-compatibility.
207212
this.radius = (radius > 1.0) ? (radius / 100.0) : radius;
@@ -384,6 +389,59 @@ export class SSAOEffect extends Effect {
384389

385390
}
386391

392+
/**
393+
* The color of the ambient occlusion.
394+
*
395+
* @type {Color}
396+
*/
397+
398+
get color() {
399+
400+
return this.uniforms.get("color").value;
401+
402+
}
403+
404+
/**
405+
* Sets the color of the ambient occlusion.
406+
*
407+
* Set to `null` to disable colorization.
408+
*
409+
* @type {Color}
410+
*/
411+
412+
set color(value) {
413+
414+
const uniforms = this.uniforms;
415+
const defines = this.defines;
416+
417+
if(value === null) {
418+
419+
if(defines.has("COLORIZE")) {
420+
421+
defines.delete("COLORIZE");
422+
uniforms.get("color").value = null;
423+
this.setChanged();
424+
425+
}
426+
427+
} else {
428+
429+
if(defines.has("COLORIZE")) {
430+
431+
uniforms.get("color").value.set(value);
432+
433+
} else {
434+
435+
defines.set("COLORIZE", "1");
436+
uniforms.get("color").value = new Color(value);
437+
this.setChanged();
438+
439+
}
440+
441+
}
442+
443+
}
444+
387445
/**
388446
* Sets the occlusion distance cutoff.
389447
*

‎src/effects/glsl/ssao/shader.frag‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ uniform float luminanceInfluence;
1515

1616
#endif
1717

18+
#ifdef COLORIZE
19+
20+
uniform vec3 color;
21+
22+
#endif
23+
1824
void mainImage(const in vec4 inputColor, const in vec2 uv, const in float depth, out vec4 outputColor) {
1925

2026
float aoLinear = texture2D(aoBuffer, uv).r;
@@ -76,6 +82,14 @@ void mainImage(const in vec4 inputColor, const in vec2 uv, const in float depth,
7682
float l = linearToRelativeLuminance(inputColor.rgb);
7783
ao = mix(ao, 1.0, l * luminanceInfluence);
7884

79-
outputColor = vec4(vec3(ao), inputColor.a);
85+
#ifdef COLORIZE
86+
87+
outputColor = vec4(1.0 - (1.0 - ao) * (1.0 - color), inputColor.a);
88+
89+
#else
90+
91+
outputColor = vec4(vec3(ao), inputColor.a);
92+
93+
#endif
8094

8195
}

0 commit comments

Comments
 (0)