Skip to content

Instantly share code, notes, and snippets.

@sakrist
Created January 31, 2017 15:09
Show Gist options
  • Save sakrist/88171c80347c6f7ac7a687c1f8c48d3c to your computer and use it in GitHub Desktop.
Save sakrist/88171c80347c6f7ac7a687c1f8c48d3c to your computer and use it in GitHub Desktop.
part of DefaultRenderer_OIT_writePixel.glsl from G3D
// colorData[0] - accum (Ar, Ag, Ab, Aa)
// colorData[2] - modulate (Br, Bg, Bb, D^2)
// colorData[3] - refraction (deltax, deltay)
void writePixel (vec3 premultipliedReflectionAndEmission, float coverage, vec3 transmissionCoefficient) {
/*
,
float collimation,
float etaRatio,
vec3 csPosition,
vec3 csNormal) {
*/
vec3 color = premultipliedReflectionAndEmission * coverage;
// Perform this operation before modifying the coverage to account for transmission
colorData[2].rgb = coverage * (vec3(1.0) - transmissionCoefficient);
/* Modulate the net coverage for composition by the transmission. This does not affect the color channels of the
transparent surface because the caller's BSDF model should have already taken into account if transmission modulates
reflection. See:
McGuire and Enderton, Colored Stochastic Shadow Maps, ACM I3D, February 2011
http://graphics.cs.williams.edu/papers/CSSM/
for a full explanation and derivation.*/
coverage *= 1.0 - (transmissionCoefficient.r + transmissionCoefficient.g + transmissionCoefficient.b) * (1.0 / 3.0);
// Alternative weighting functions:
//float tmp = 2.0 * max(1.0 + csPosition.z / 200.0, 0.0); tmp *= tmp * 1e3;
// Looks better on clouds
float tmp = 1.0 - gl_FragCoord.z * 0.99;
tmp *= tmp * tmp * 1e4;
tmp = clamp(tmp, 1e-3, 1.0);
/*
If you're running out of compositing range and overflowing to inf, multiply the upper bound (3e2) by a small
constant. Note that R11G11B10F has no inf and maps all floating point specials to NaN, so you won't actually
see inf in the frame buffer.
*/
// Weight function tuned for the general case
float w = clamp(coverage * tmp, 1e-3, 1.5e2);
// another function for weight
// float tmp = (coverage * 8.0 + 0.01) * (-gl_FragCoord.z * 0.95 + 1.0);
// float w = clamp(tmp * tmp * tmp * 1e3, 1e-2, 3e2);
colorData[0] = vec4(color, coverage) * w;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment