Skip to content

Instantly share code, notes, and snippets.

@sakrist
Created January 31, 2017 14:58
Show Gist options
  • Save sakrist/1da175ffae9feccc96bdd368530b0dba to your computer and use it in GitHub Desktop.
Save sakrist/1da175ffae9feccc96bdd368530b0dba to your computer and use it in GitHub Desktop.
composite shader for OIT
void main()
{
ivec2 C = ivec2(gl_FragCoord.xy);
vec4 backgroundModulationAndDiffusion = texelFetch(modulationSampler, C, 0);
vec3 backgroundModulation = backgroundModulationAndDiffusion.rgb;
vec4 background = texelFetch(backgroundSampler, C, 0);
if (minComponent(backgroundModulation) == 1.0) {
// Save the color texture fetch cost; there is no transparency
// at this pixel
colorOut = background;
} else {
vec4 accum = texelFetch(accumSampler, C, 0);
float revealage = accum.a;
// In the case where the denominator overflowed, at least preserve some color
// instead of writing zero by dividing through by infinity
if (isinf(accum.a)) {
accum.a = maxComponent(accum.rgb);
}
// Suppress overflow of the numerator by outputting white
if (isinf(maxComponent(accum.rgb))) {
accum = vec4(isinf(accum.a) ? 1.0 : accum.a);
}
// Attempt to fake transmission on the additive term by blending in a little bit of the
// background modulation.
{
const float epsilon = 0.001;
accum.rgb *= vec3(0.5) + max(backgroundModulation, vec3(epsilon)) / (2.0 * max(epsilon, maxComponent(backgroundModulation)));
}
// dst' = (accum.rgb / accum.a) * (1 - revealage) + dst
// [dst has already been modulated by the transmission colors and coverage and the blend mode
// inverts revealage for us]
// colorOut += vec4(accum.rgb / max(accum.a, 0.00001), revealage);
colorOut.a = 1.0 - revealage;
colorOut.rgb = (background.rgb * backgroundModulation + (vec3(1.0) - backgroundModulation) * accum.rgb / max(accum.a, 0.00001));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment