Algorithm Search⌘K

Algorithmadvanced

Weighted blended order-independent transparency

Also called weighted blended OIT.

Approximate the over operator by a weighted average of the transparent colors with depth-based weights, computed in one pass with additive blending.

Time
O(f + p) frameO(k) per_pixel
Space
O(p) frameO(1) per_pixel
Where
f = the number of fragments in the frame; k = the number of fragments of a pixel; p = the number of pixels
Properties
deterministicnot in placeonlineiterative
Origin
Morgan McGuire, Louis Bavoil, 2013

Weighted blended order-independent transparency is a one-pass approximation for order-independent transparency. It does not sort the fragments. Instead, it replaces the color part of the over operator by a weighted average of the fragment colors, and keeps the part that tells how much of the background shows through, which is a product and needs no order. The weight of each fragment is a function of its depth and opacity, larger for nearer fragments. On a GPU the whole method is two render targets with fixed blend modes and one full-screen pass, with a constant amount of memory per pixel. McGuire and Bavoil published it in 2013. The coverage is exact; the color is exact only in special cases, and the weight function is tuning.

In plain words

Blending glass over glass in the right order needs a sort, and a sort per pixel is slow on a GPU. This method gives up the exact answer. It adds up the colors of all transparent layers, each multiplied by a weight, and divides by the sum of the weights, so the result is an average color. Separately, it multiplies together how much light each layer lets through, which gives the exact amount of background that shows. Nearer layers get larger weights, so they count more, as they would in a correct blend. When all the layers have about the same color, the result is right. When a red pane is in front of a blue pane, the result is a mix that is too even.

The problem

The input is, for each pixel, an opaque background color C0C_0 and the transparent fragments (ci,αi,zi)(c_i, \alpha_i, z_i) of that pixel in the order in which the rasterizer produces them: cic_i is the premultiplied linear color, αi\alpha_i the opacity and zi>0z_i > 0 the view depth, smaller is nearer. The exact output is the composite in depth order defined on the problem page:

C=∑ici Ti+C0R,Ti=∏zj<zi(1−αj),R=∏i(1−αi).C = \sum_{i} c_i\, T_i + C_0 R, \qquad T_i = \prod_{z_j < z_i} (1 - \alpha_j), \qquad R = \prod_i (1 - \alpha_i).

The method must work in one geometry pass with O(1)O(1) memory per pixel and blend operations that are commutative, so that the arrival order does not matter. Its output is

C~=∑iciwi∑iαiwi(1−R)+C0R\tilde C = \frac{\sum_i c_i w_i}{\sum_i \alpha_i w_i} (1 - R) + C_0 R

for weights wi=w(ci,αi,zi)>0w_i = w(c_i, \alpha_i, z_i) > 0.

The algorithm

The loop runs over the fragments of one pixel in arrival order. On a GPU, lines L1 to L5 are the blend units of two render targets, and lines L6 and L7 are a full-screen composite pass.

  1. WEIGHTED-BLENDED-OIT(fragments, C0, WEIGHT, ε)
  2. 1A ← 0 (one sum per color channel); Aα ← 0; R ← 1
  3. 2for each fragment (c, α, z) of the pixel, in arrival order
  4. 3w ← WEIGHT(c, α, z)
  5. 4A ← A + c w; Aα ← Aα + α w ▷ additive blending
  6. 5R ← R (1 - α) ▷ multiplicative blending
  7. 6avg ← A / max(Aα, ε)
  8. 7return avg (1 - R) + C0 R

The render targets (lines L4 and L5). The first target holds (A,Aα)(A, A_\alpha) in a 16-bit or 32-bit float RGBA format. The fragment shader writes (cw,αw)(c w, \alpha w) and the blend mode is ONE, ONE, an addition. The second target holds RR in one channel, cleared to 1. The shader writes α\alpha and the blend mode is ZERO, ONE_MINUS_SRC_ALPHA, so the stored value is multiplied by 1−α1 - \alpha. Both blends commute, so the order of the fragments does not matter. The depth test is on against the opaque depth buffer, with depth writes off.

The composite (lines L6 and L7). A full-screen pass reads both targets and blends avgavg over the opaque image with the factor 1−R1 - R. The guard ε\varepsilon stops a division by zero where no transparent fragment lies.

The weight (line L3). The weight should be larger for nearer fragments, because the exact weight of fragment ii is TiT_i, which falls as more fragments lie in front of it. The method cannot know TiT_i without the order, so it uses a fixed function of depth. McGuire's blog post of 2014 gives

w=max⁡(min⁡(1,max⁡(cr,cg,cb)), α)⋅clamp⁡(0.0310−5+(z/200)4, 10−2, 3⋅103),w = \max\big(\min(1, \max(c_r, c_g, c_b)),\ \alpha\big) \cdot \operatorname{clamp}\Big(\frac{0.03}{10^{-5} + (z / 200)^4},\ 10^{-2},\ 3 \cdot 10^{3}\Big),

where the code of this page uses the positive view depth for zz and the premultiplied color for cc. The first factor makes nearly invisible fragments count little. The constants suit a depth range of a few hundred units and must be tuned for other scenes; the clamp keeps the weights inside the range of a 16-bit float.

Example

A red fragment with c=(0.5,0,0)c = (0.5, 0, 0), α=0.5\alpha = 0.5 lies in front of a blue fragment with c=(0,0,0.5)c = (0, 0, 0.5), α=0.5\alpha = 0.5, over black. The exact result is (0.5,0,0.25)(0.5, 0, 0.25).

weights (red, blue) AA AαA_\alpha avgavg C~\tilde C error per channel
1, 1 (0.5,0,0.5)(0.5, 0, 0.5) 1 (0.5,0,0.5)(0.5, 0, 0.5) (0.375,0,0.375)(0.375, 0, 0.375) 0.125
3, 1 (1.5,0,0.5)(1.5, 0, 0.5) 2 (0.75,0,0.25)(0.75, 0, 0.25) (0.5625,0,0.1875)(0.5625, 0, 0.1875) 0.0625
2, 1 (1,0,0.5)(1, 0, 0.5) 1.5 (0.667,0,0.333)(0.667, 0, 0.333) (0.5,0,0.25)(0.5, 0, 0.25) 0

In all rows R=0.25R = 0.25, so 1−R=0.751 - R = 0.75, the exact coverage. The weights 2 and 1 are in the ratio of the exact transmittances T=(1,0.5)T = (1, 0.5), and they give the exact result. With equal weights, the two panes are mixed half and half, and the red pane in front does not dominate. The order of the two fragments in the loop makes no difference in any row.

Correctness

The method is not exact in general, so correctness here means a list of exact statements, which the proofs folder proves and the tests check in exact rational arithmetic.

  • Order independence. Lines L4 and L5 build two sums and a product, which do not depend on the order of their terms. The tests shuffle the fragments and get the same fraction each time.
  • Exact coverage. RR is the same product as in the exact composite, so the background term C0RC_0 R is exact.
  • A weighted mean. C~−C0R=(1−R)∑iλi(ci/αi)\tilde C - C_0 R = (1 - R) \sum_i \lambda_i (c_i / \alpha_i) with λi=αiwi/∑jαjwj\lambda_i = \alpha_i w_i / \sum_j \alpha_j w_j, a convex combination of the unpremultiplied colors. The exact composite has the same form with λi\lambda_i replaced by αiTi/(1−R)\alpha_i T_i / (1 - R), because ∑iαiTi=1−R\sum_i \alpha_i T_i = 1 - R by a telescoping sum.
  • Exact weights. So if wiw_i is proportional to TiT_i, the result is exact. The tests build this order-dependent weight and check equality.
  • Special cases. With one fragment, or with all fragments of one unpremultiplied color, the result is exact for every positive weight.

How close the result is for a real weight function is a measurement on a scene, not a theorem.

Proof: What weighted blended OIT gets exactly rightfull

Setting. A pixel has an opaque background color C0C_0 and k≥1k \ge 1 fragments (ci,αi,zi)(c_i, \alpha_i, z_i) with premultiplied colors cic_i, opacities αi∈(0,1]\alpha_i \in (0, 1] and distinct depths. Rename them so that z1<z2<⋯<zkz_1 < z_2 < \dots < z_k (front to back). Let Ti=∏j<i(1−αj)T_i = \prod_{j < i} (1 - \alpha_j) be the transmittance in front of fragment ii, so T1=1T_1 = 1, and let R=Tk+1=∏i=1k(1−αi)R = T_{k+1} = \prod_{i=1}^{k} (1 - \alpha_i). The exact composite is

C=∑i=1kciTi+C0R.C = \sum_{i=1}^{k} c_i T_i + C_0 R.

The method takes weights wi>0w_i > 0 and returns

C~=∑iciwi∑iαiwi(1−R)+C0R.\tilde C = \frac{\sum_i c_i w_i}{\sum_i \alpha_i w_i} (1 - R) + C_0 R.

All claims are in exact arithmetic, as in the tests, which use fractions. The guard ε\varepsilon of line L6 is assumed to be smaller than ∑iαiwi\sum_i \alpha_i w_i, so it does not change the result. Fragments with αi=0\alpha_i = 0 add nothing to any of the sums and are left out.

Lemma 1 (telescoping). ∑i=1kαiTi=1−R\sum_{i=1}^{k} \alpha_i T_i = 1 - R.

Proof. Ti+1=Ti(1−αi)T_{i+1} = T_i (1 - \alpha_i), so αiTi=Ti−Ti+1\alpha_i T_i = T_i - T_{i+1}. The sum telescopes to T1−Tk+1=1−RT_1 - T_{k+1} = 1 - R. □\square

Theorem 1 (order independence and exact coverage). C~\tilde C does not depend on the order in which the fragments arrive, and its background term C0RC_0 R is the background term of CC.

Proof. Lines L4 and L5 build two sums and a product. Addition and multiplication are commutative and associative, so the order of the loop does not change ∑iciwi\sum_i c_i w_i, ∑iαiwi\sum_i \alpha_i w_i or RR, provided that each wiw_i depends only on its own fragment. RR is the same product as in CC. □\square

In floating point, the sums and the product round differently for different orders, so the result depends on the order at the level of rounding.

Theorem 2 (the color is a weighted mean). Let c^i=ci/αi\hat c_i = c_i / \alpha_i be the unpremultiplied color of fragment ii and λi=αiwi/∑jαjwj\lambda_i = \alpha_i w_i / \sum_j \alpha_j w_j. Then λi>0\lambda_i > 0, ∑iλi=1\sum_i \lambda_i = 1 and

C~−C0R=(1−R)∑iλic^i.\tilde C - C_0 R = (1 - R) \sum_i \lambda_i \hat c_i.

So in each channel, (1−R)min⁡ic^i≤C~−C0R≤(1−R)max⁡ic^i(1 - R) \min_i \hat c_i \le \tilde C - C_0 R \le (1 - R) \max_i \hat c_i.

Proof. ciwi=c^iαiwic_i w_i = \hat c_i \alpha_i w_i, so ∑iciwi/∑jαjwj=∑iλic^i\sum_i c_i w_i / \sum_j \alpha_j w_j = \sum_i \lambda_i \hat c_i. The weights λi\lambda_i are positive and sum to 1. A convex combination lies between the smallest and the largest value in each channel, and 1−R≥01 - R \ge 0. □\square

The exact composite has the same form. By Lemma 1, C−C0R=∑ic^iαiTi=(1−R)∑iμic^iC - C_0 R = \sum_i \hat c_i \alpha_i T_i = (1 - R) \sum_i \mu_i \hat c_i with μi=αiTi/(1−R)\mu_i = \alpha_i T_i / (1 - R). The two results differ only in the mixing weights: μi\mu_i is proportional to αiTi\alpha_i T_i, and λi\lambda_i is proportional to αiwi\alpha_i w_i.

Theorem 3 (exact weights). If wi=KTiw_i = K T_i for a constant K>0K > 0, then C~=C\tilde C = C.

Proof. Then λi=αiTi/∑jαjTj=μi\lambda_i = \alpha_i T_i / \sum_j \alpha_j T_j = \mu_i by Lemma 1, and the two expressions of Theorem 2 are equal. □\square

The weight TiT_i needs the fragments in front of fragment ii, so it depends on the order that the method avoids. A weight that falls with depth is an approximation of TiT_i by a function of ziz_i alone, and how well it works depends on the scene.

Theorem 4 (special cases). C~=C\tilde C = C for every choice of positive weights when (a) k=1k = 1, or (b) all fragments have the same unpremultiplied color c^\hat c.

Proof. (a) With one fragment, λ1=μ1=1\lambda_1 = \mu_1 = 1. (b) Both convex combinations of Theorem 2 equal c^\hat c. □\square

Theorem 5 (a counterexample). The method is not exact in general. Over a black background, let a red fragment c=(1/2,0,0)c = (1/2, 0, 0), α=1/2\alpha = 1/2 lie in front of a blue fragment c=(0,0,1/2)c = (0, 0, 1/2), α=1/2\alpha = 1/2. Then C=(1/2,0,1/4)C = (1/2, 0, 1/4). With equal weights, C~=(3/8,0,3/8)\tilde C = (3/8, 0, 3/8). With the weights 3 for red and 1 for blue, C~=(9/16,0,3/16)\tilde C = (9/16, 0, 3/16).

Proof. R=1/4R = 1/4. Equal weights give the mean (1/2,0,1/2)(1/2, 0, 1/2) of the unpremultiplied colors (1,0,0)(1, 0, 0) and (0,0,1)(0, 0, 1), times 3/43/4. The weights 3 and 1 give λ=(3/4,1/4)\lambda = (3/4, 1/4), so the mean is (3/4,0,1/4)(3/4, 0, 1/4), times 3/43/4. The exact weights are T=(1,1/2)T = (1, 1/2), which give λ=(2/3,1/3)\lambda = (2/3, 1/3) and (2/3,0,1/3)⋅3/4=(1/2,0,1/4)(2/3, 0, 1/3) \cdot 3/4 = (1/2, 0, 1/4). □\square

Theorem 6 (cost). For a pixel with kk fragments and nn color channels, the method does O(nk)O(n k) arithmetic and keeps n+2n + 2 numbers.

Proof. Each fragment costs one weight evaluation, n+1n + 1 multiply-adds and one multiplication. The state is the nn color sums, the alpha sum and RR. Line L7 costs O(n)O(n). □\square

Complexity

The model is the real RAM. For a pixel with kk fragments, the method does O(k)O(k) work and keeps O(1)O(1) numbers: the color sums, the alpha sum and RR, which is 5 numbers for RGB. For a frame with ff transparent fragments over pp pixels, the geometry pass costs O(f)O(f) and the composite O(p)O(p). At 1920 by 1080 pixels, an RGBA16F target and an R8 target take 2 073 600⋅9=18 662 4002\,073\,600 \cdot 9 = 18\,662\,400 bytes, about 17.8 MiB, whatever the depth complexity. There is no sort and no list, so the memory is known before the frame.

Properties

  • One geometry pass, bounded memory. No per-pixel storage grows with the number of fragments.
  • Order independent. The result does not depend on the draw order, apart from floating-point rounding. Particles and other dynamic transparent geometry do not flicker when their order changes.
  • Exact coverage, approximate color. The amount of background seen is right. The color is a weighted mean of the layers.
  • Weight is tuning. The weight function depends on the depth range and the content of the scene.
  • Deterministic. The same input gives the same output, except for rounding when the blend order changes on the GPU.
  • No refraction order. Effects that need to know which surface is in front, such as refraction, are not supported by the average itself.

Variants and improvements

  • Other weight functions. The paper gives several functions of depth with different falloffs. The choice is a trade between near and far layers in the scene at hand.
  • Coverage only. Without the color targets, the second target alone gives the exact transmittance of all transparent surfaces, for example for colored shadow maps.
  • Colored transmission. Keep RR per color channel, so that colored glass tints the background differently in each channel. The product stays order independent.
  • Mixed methods. Render the nearest few layers exactly, for example with a small fragment array or depth peeling, and the rest with weighted blending.
  • Moment-based and phenomenological methods. Later methods approximate the transmittance T(z)T(z) as a function of depth with a few moments per pixel and use it as the weight, at the cost of a second pass.

When to use it

Use it for particles, smoke, foliage cards, glass panels and other transparent content where many layers overlap, the layers have similar colors, or speed matters more than an exact order. It fits a deferred shading renderer as a forward pass after the opaque pass. Do not use it where a strongly colored surface in front must cover another strongly colored surface behind it, or where the exact image matters; then use per-pixel linked lists or depth peeling.

Pitfalls

  • Overflow in 16-bit floats. Large weights times many layers overflow half floats, whose largest value is 65504. Clamp the weight, or use a 32-bit target.
  • Wrong depth space. The constants of a weight function refer to a depth in given units. A nonlinear depth buffer value or a scene in other units changes the falloff.
  • Opaque fragments. A fragment with α=1\alpha = 1 sets R=0R = 0, but its color is still averaged with the others. Draw opaque surfaces in the opaque pass.
  • Premultiplied or not. The accumulation must use premultiplied colors. Mixing the two forms gives colors that are too dark or too bright.
  • Division by zero. A pixel with no transparent fragment has Aα=0A_\alpha = 0. The guard ε\varepsilon or a test on R=1R = 1 avoids the division.
  • Expecting exact results. Two strongly colored layers in the wrong ratio of weights give a visibly wrong mix, as the example shows.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

weighted_blended_oit.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/weighted-blended-oit/. The authors are listed in prov/ZP9G80.jsonl.

"""Weighted blended order-independent transparency (McGuire and Bavoil 2013).

A fragment is (c, alpha, z): a premultiplied linear RGB color c, an opacity
alpha in [0, 1] and a positive view depth z (smaller is nearer). The method
accumulates, in any order,

    A.rgb = sum c_i w_i,  A.a = sum alpha_i w_i,  R = prod (1 - alpha_i)

and returns (A.rgb / A.a) (1 - R) + C0 R. The coverage 1 - R is exact. The
color is a weighted average and is exact only in special cases.

All functions accept floats or fractions.Fraction values, so that the tests
can check the exact claims without rounding.
"""

from typing import Callable, Sequence

Color = tuple
Fragment = tuple  # (c, alpha, z)


def over_composite(fragments: Sequence[Fragment], background: Color) -> Color:
    """The exact reference: sort by depth, then c over (... over C0)."""
    result = tuple(background)
    for c, a, _ in sorted(fragments, key=lambda f: f[2], reverse=True):
        result = tuple(ci + (1 - a) * ri for ci, ri in zip(c, result))
    return result


def transmittance(fragments: Sequence[Fragment]):
    t = 1
    for _, a, _ in fragments:
        t = t * (1 - a)
    return t


def constant_weight(c: Color, a, z):
    return 1


def mcguire_blog_weight(c: Color, a: float, z: float) -> float:
    """The weight of McGuire's 2014 blog post, with c premultiplied (so the
    unpremultiplied color times alpha is c itself). Tuning, not physics: the
    constants 0.03, 200, 1e-2 and 3e3 suit a depth range of a few hundred units."""
    m = max(min(1.0, max(c)), a)
    return m * min(3e3, max(1e-2, 0.03 / (1e-5 + (z / 200.0) ** 4)))


def wboit(
    fragments: Sequence[Fragment],
    background: Color,
    weight: Callable = mcguire_blog_weight,
    eps=1e-5,
) -> Color:
    """Composite the fragments in the order given."""
    n = len(background)
    acc = [0] * n  # @line L1
    acc_a = 0
    reveal = 1
    for c, a, z in fragments:  # @line L2
        w = weight(c, a, z)  # @line L3
        for k in range(n):  # @line L4
            acc[k] = acc[k] + c[k] * w
        acc_a = acc_a + a * w
        reveal = reveal * (1 - a)  # @line L5
    denom = acc_a if acc_a > eps else eps
    avg = [x / denom for x in acc]  # @line L6
    return tuple(avg[k] * (1 - reveal) + background[k] * reveal for k in range(n))  # @line L7


def visibility_weight(fragments: Sequence[Fragment]) -> Callable:
    """The order-dependent weight that makes the method exact: the product of
    (1 - alpha) of all nearer fragments. It needs the sort that the method
    avoids, so it is not a practical weight; the tests use it to check the
    theory. The depths must be distinct."""
    table = {}
    t = 1
    for _, a, z in sorted(fragments, key=lambda f: f[2]):
        table[z] = t
        t = t * (1 - a)
    return lambda c, a, z: table[z]
test_weighted_blended_oit.py 134 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/weighted-blended-oit/. The authors are listed in prov/ZP9G80.jsonl.

"""Weighted blended OIT against exact sorted over compositing, in Fractions."""

import random
from fractions import Fraction as F

from weighted_blended_oit import (
    constant_weight,
    mcguire_blog_weight,
    over_composite,
    transmittance,
    visibility_weight,
    wboit,
)


def random_fragments(rng: random.Random, k: int, same_color=None):
    frags = []
    depths = rng.sample(range(1, 1000), k)
    for z in depths:
        a = F(rng.randint(1, 99), 100)
        base = same_color or (F(rng.randint(0, 100), 100), F(rng.randint(0, 100), 100), F(rng.randint(0, 100), 100))
        frags.append((tuple(a * x for x in base), a, F(z, 10)))
    return frags


def rational_depth_weight(c, a, z):
    """A positive weight that falls with depth, in exact arithmetic. For the
    depths of the tests (z < 100) and alpha >= 0.01 it keeps alpha * w above
    the guard eps of wboit, so the guard never changes an exact result."""
    return 1 / (1 + z / 10)


def test_coverage_is_exact_in_any_order() -> None:
    rng = random.Random(41)
    bg = (F(1, 5), F(2, 5), F(3, 5))
    for _ in range(60):
        frags = random_fragments(rng, rng.randint(1, 8))
        rng.shuffle(frags)
        # With a black background the alpha of the result is not needed;
        # the transmittance is read from the blend of a white background.
        white, black = (F(1),), (F(0),)
        mono = [((F(0),), a, z) for _, a, z in frags]
        assert wboit(mono, white, constant_weight) == over_composite(mono, white)
        assert wboit(mono, black, rational_depth_weight)[0] == 0
        assert transmittance(frags) == over_composite(mono, white)[0]
        out = wboit(frags, bg, rational_depth_weight)
        assert len(out) == 3


def test_order_independent_in_exact_arithmetic() -> None:
    rng = random.Random(42)
    bg = (F(1, 3), F(1, 7), F(0))
    for _ in range(40):
        frags = random_fragments(rng, rng.randint(2, 6))
        ref = wboit(frags, bg, rational_depth_weight)
        for _ in range(5):
            rng.shuffle(frags)
            assert wboit(frags, bg, rational_depth_weight) == ref


def test_exact_for_one_fragment() -> None:
    rng = random.Random(43)
    bg = (F(1, 2), F(1, 4), F(1, 8))
    for _ in range(50):
        frags = random_fragments(rng, 1)
        assert wboit(frags, bg, rational_depth_weight) == over_composite(frags, bg)


def test_exact_when_all_colors_are_equal() -> None:
    rng = random.Random(44)
    bg = (F(1, 2), F(1, 4), F(1, 8))
    color = (F(9, 10), F(3, 10), F(1, 10))
    for _ in range(50):
        frags = random_fragments(rng, rng.randint(1, 7), same_color=color)
        for weight in (constant_weight, rational_depth_weight):
            assert wboit(frags, bg, weight) == over_composite(frags, bg)


def test_exact_with_the_visibility_weight() -> None:
    rng = random.Random(45)
    bg = (F(1, 2), F(1, 4), F(1, 8))
    for _ in range(50):
        frags = random_fragments(rng, rng.randint(1, 7))
        rng.shuffle(frags)
        assert wboit(frags, bg, visibility_weight(frags)) == over_composite(frags, bg)


def test_red_and_blue_example() -> None:
    red = ((F(1, 2), F(0), F(0)), F(1, 2), F(1))
    blue = ((F(0), F(0), F(1, 2)), F(1, 2), F(2))
    black = (F(0), F(0), F(0))
    assert over_composite([red, blue], black) == (F(1, 2), F(0), F(1, 4))
    assert wboit([red, blue], black, constant_weight) == (F(3, 8), F(0), F(3, 8))
    near_first = wboit([red, blue], black, lambda c, a, z: 3 if z == 1 else 1)
    assert near_first == (F(9, 16), F(0), F(3, 16))


def test_color_is_in_the_hull_times_coverage() -> None:
    rng = random.Random(46)
    black = (F(0), F(0), F(0))
    for _ in range(50):
        frags = random_fragments(rng, rng.randint(1, 6))
        cov = 1 - transmittance(frags)
        out = wboit(frags, black, rational_depth_weight)
        for k in range(3):
            lo = min(c[k] / a for c, a, _ in frags)
            hi = max(c[k] / a for c, a, _ in frags)
            assert lo * cov <= out[k] <= hi * cov


def test_blog_weight_is_positive_and_falls_with_depth() -> None:
    c, a = (0.4, 0.2, 0.1), 0.5
    prev = None
    for z in (0.1, 1.0, 10.0, 50.0, 100.0, 200.0, 400.0, 1000.0, 1e5):
        w = mcguire_blog_weight(c, a, z)
        assert 0.0 < w <= 3e3
        if prev is not None:
            assert w <= prev
        prev = w
    assert mcguire_blog_weight(c, a, 1e5) == 0.5 * 1e-2


def test_float_run_is_close_to_exact() -> None:
    rng = random.Random(47)
    bg = (0.2, 0.3, 0.4)
    for _ in range(30):
        frags = random_fragments(rng, rng.randint(1, 6))
        fl = [(tuple(float(x) for x in c), float(a), float(z)) for c, a, z in frags]
        exact = wboit(frags, tuple(F(x) for x in bg), lambda c, a, z: F(mcguire_blog_weight(tuple(float(x) for x in c), float(a), float(z))))
        approx = wboit(fl, bg)
        assert all(abs(float(e) - x) < 1e-9 for e, x in zip(exact, approx))

History

The over operator of Porter and Duff (1984) needs a back-to-front order, and exact methods for unsorted fragments, such as the A-buffer and depth peeling, need memory or passes that grow with the depth complexity. Earlier one-pass methods replaced the over operator by sums of colors that did not depend on order, but they did not keep the coverage exact or did not weight the layers by depth. McGuire and Bavoil (2013) combined a weighted average of the colors with the exact product of the transmissions, gave weight functions of depth, and mapped the method to two render targets with standard blend modes. McGuire published shader code and a revised weight in a blog post in 2014.

References

  1. Morgan McGuire and Louis Bavoil, Weighted blended order-independent transparency, Journal of Computer Graphics Techniques 2(2), 122-141, 2013, the weighted average with the coverage term, the two render targets and their blend modes, and the weight functions of depth
  2. Morgan McGuire, Weighted blended order-independent transparency, Casual Effects blog, March 2014, the shader code and the weight function with the constants 0.03, 200, 1e-2 and 3e3
  3. Thomas Porter and Tom Duff, Compositing digital images, SIGGRAPH 1984, Computer Graphics 18(3), 253-259, the premultiplied alpha and the over operator that the method approximates
  4. Tomas Akenine-Möller, Eric Haines, Naty Hoffman, Angelo Pesce, Michał Iwanicki and Sébastien Hillaire, Real-Time Rendering, fourth edition, CRC Press, 2018, Section 5.5, Transparency, Alpha, and Compositing (the part on order-independent methods)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.frame
  • a full proof of time.per_pixel
  • a full proof of space.frame
  • a full proof of space.per_pixel
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example