Skip to content

Commit

Permalink
Fringing / Chromatic Aberration added to render pass compose (#5870)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
  • Loading branch information
mvaligursky and Martin Valigursky committed Nov 30, 2023
1 parent dc20e8e commit 873d70d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
30 changes: 30 additions & 0 deletions examples/src/examples/graphics/post-processing.mjs
Expand Up @@ -157,6 +157,24 @@ function controls({ observer, ReactPCUI, React, jsx, fragment }) {
precision: 2
})
)
),
jsx(Panel, { headerText: 'Fringing' },
jsx(LabelGroup, { text: 'enabled' },
jsx(BooleanInput, {
type: 'toggle',
binding: new BindingTwoWay(),
link: { observer, path: 'data.fringing.enabled' }
})
),
jsx(LabelGroup, { text: 'intensity' },
jsx(SliderInput, {
binding: new BindingTwoWay(),
link: { observer, path: 'data.fringing.intensity' },
min: 0,
max: 100,
precision: 0
})
)
)
);
}
Expand Down Expand Up @@ -520,6 +538,14 @@ async function example({ canvas, deviceType, assetPath, glslangPath, twgslPath,
composePass.vignetteIntensity = value;
}
}
if (pathArray[1] === 'fringing') {
if (pathArray[2] === 'enabled') {
composePass.fringingEnabled = value;
}
if (pathArray[2] === 'intensity') {
composePass.fringingIntensity = value;
}
}
});

data.set('data', {
Expand All @@ -546,6 +572,10 @@ async function example({ canvas, deviceType, assetPath, glslangPath, twgslPath,
outer: 1.0,
curvature: 0.5,
intensity: 0.3
},
fringing: {
enabled: false,
intensity: 50
}
});

Expand Down
47 changes: 45 additions & 2 deletions extras/render-passes/render-pass-compose.js
Expand Up @@ -51,10 +51,31 @@ const fragmentShader = `
#endif
#ifdef FRINGING
uniform float fringingIntensity;
vec3 fringing(vec2 uv, vec3 color) {
// offset depends on the direction from the center, raised to power to make it stronger away from the center
vec2 centerDistance = uv0 - 0.5;
vec2 offset = fringingIntensity * pow(centerDistance, vec2(2.0, 2.0));
color.r = texture2D(sceneTexture, uv0 - offset).r;
color.b = texture2D(sceneTexture, uv0 + offset).b;
return color;
}
#endif
void main() {
vec4 scene = texture2D(sceneTexture, uv0);
vec3 result = scene.rgb;
#ifdef FRINGING
result = fringing(uv0, result);
#endif
#ifdef BLOOM
vec3 bloom = texture2D(bloomTexture, uv0).rgb;
result += bloom * bloomIntensity;
Expand Down Expand Up @@ -105,6 +126,10 @@ class RenderPassCompose extends RenderPassShaderQuad {

vignetteIntensity = 0.3;

_fringingEnabled = false;

fringingIntensity = 10;

_key = '';

constructor(graphicsDevice) {
Expand All @@ -115,6 +140,7 @@ class RenderPassCompose extends RenderPassShaderQuad {
this.bloomIntensityId = graphicsDevice.scope.resolve('bloomIntensity');
this.bcsId = graphicsDevice.scope.resolve('brightnessContrastSaturation');
this.vignetterParamsId = graphicsDevice.scope.resolve('vignetterParams');
this.fringingIntensityId = graphicsDevice.scope.resolve('fringingIntensity');
}

set bloomTexture(value) {
Expand Down Expand Up @@ -150,6 +176,17 @@ class RenderPassCompose extends RenderPassShaderQuad {
return this._vignetteEnabled;
}

set fringingEnabled(value) {
if (this._fringingEnabled !== value) {
this._fringingEnabled = value;
this._shaderDirty = true;
}
}

get fringingEnabled() {
return this._fringingEnabled;
}

set toneMapping(value) {
if (this._toneMapping !== value) {
this._toneMapping = value;
Expand Down Expand Up @@ -187,15 +224,17 @@ class RenderPassCompose extends RenderPassShaderQuad {
const key = `${this.toneMapping}` +
`-${this.bloomTexture ? 'bloom' : 'nobloom'}` +
`-${this.gradingEnabled ? 'grading' : 'nograding'}` +
`-${this.vignetteEnabled ? 'vignette' : 'novignette'}`;
`-${this.vignetteEnabled ? 'vignette' : 'novignette'}` +
`-${this.fringingEnabled ? 'fringing' : 'nofringing'}`;

if (this._key !== key) {
this._key = key;

const defines =
(this.bloomTexture ? `#define BLOOM\n` : '') +
(this.gradingEnabled ? `#define GRADING\n` : '') +
(this.vignetteEnabled ? `#define VIGNETTE\n` : '');
(this.vignetteEnabled ? `#define VIGNETTE\n` : '') +
(this.fringingEnabled ? `#define FRINGING\n` : '');

const fsChunks =
shaderChunks.decodePS +
Expand Down Expand Up @@ -224,6 +263,10 @@ class RenderPassCompose extends RenderPassShaderQuad {
this.vignetterParamsId.setValue([this.vignetteInner, this.vignetteOuter, this.vignetteCurvature, this.vignetteIntensity]);
}

if (this._fringingEnabled) {
this.fringingIntensityId.setValue(this.fringingIntensity / this.sceneTexture.height);
}

super.execute();
}
}
Expand Down

0 comments on commit 873d70d

Please sign in to comment.