133 points · by sebg · 4y ago · 69 comments
If you have two translucent objects, how do you combine the colors? This is hard to do with Z-buffer rendering. The obvious coloring operation is not commutative, and in a Z-buffer system, you don't know which face gets rendered first.
This is a huge pain. You can't just dump everything into the GPU and let it draw in parallel, which is what GPUs do fast. You usually have to depth-sort triangles, so the front triangle gets rendered last. This delays rendering while the CPU does the sort.
There's a GPU-based solution to this, but it involves some compromises in quality.[1]
[1] https://developer.nvidia.com/content/transparency-or-translu...
This is just the tip of the iceberg when it comes to the complexity of translucent objects. To get truly convincing results spectral rendering is needed, but of course that is prohibitively expensive for real-time use.
Looking through a yellow filter at a yellow flourescent light - you could easily see almost all the original light (bright yellow) or none (black) depending on the spectral transmission of the filter.
To be honest, the game developer way of solving this is to just not use transparency. Too many things rely on the G-buffer / depth buffer these days, and transparency is usually limited to a lower quality render anyway.
There’s another GPU-based solution with RTX and other ray tracing solutions that is high quality. Might be compromising efficiency in some typical cases, unless a lot of geometry and/or instancing and/or samples are used, and then in that case it may be both an efficiency and quality win.
If we're just talking transmission they would both be the same, ie transmitting all red photons and 50% of the green and blue ones while the other filter transmits all blue photons and 50% of the red/green ones. Either stack has 50% red values, 50% blue values and 25% green right?
However, if we're talking about how alpha blending actually works in software, then it can vary a lot.
For example, in Direct3D you would typically use the `D3DBLEND_SRCALPHA` and `D3DBLEND_INVSRCALPHA` for the source and destination blend modes respectively. In that case, the combined alpha is actually `src_alpha^2 + (1 - src_alpha) * dst_alpha` which is different again from the two calculations in the article.
There are even other ways to achieve a physically realistic result that use a different interpretation of the alpha channel. Or maybe you want to solve for something else (eg. order-independent blending)
then i remind myself that i probably have a few situations like that in my own mind and i should remain vigilant.
edit: and then i get just a little more frustrated next time someone resists a correction that i provide.
The thing that you might be missing is that "incorrect" is (usually) objective, but "unintuitive" isn't necessarily so. I can see how someone else would think additive opacity is intuitive, even if it's very easy to show that it isn't correct.
You are right that "intuitive" is a subjective property. But I'm completely lost trying to imagine what kind of life experiences can lead on to expect them to be additive.
Seem intuitive to me. Half + half = whole. When I make a chart in R and set the color to alpha 0.5, the result looks more or less like an opaque point if I plot 2 of them on top of each other.
Apparently that's wrong, but it's consistent with my not-very-precise observations, and I have never had a need or desire to learn more about how opacity works. One cannot be an expert in all things.
What intuition should a layperson have to suggest that obviously opacity/transmissivity is multiplicative? I don't see the value in deriding the incorrect intuition of uneducated people. In statistics at least we accept that statistics is hard specifically because it can be unintuitive.
I, on the other hand, am completely lost trying to imagine what kind of life experiences can lead one to believe the OP error requires some "unique/rare" life experience.
How about the very common life experience of
(a) not being good at math but still being a programmmer and having eventually to deal with something math-y?
or
(b) the life experience of someone who might now enough math to get this, but didn't give it alpha opacity much thought before, and the simple addition model was good enough for them (as they never had to do anything special with it)?
That said, I’m well aware that opacity is multiplicative, and yet it still caught me off guard before to realize that the commonly used lerp blending mode (SRC_ALPHA, ONE_MINUS_SRC_ALPHA) is incorrect in the alpha channel. Pre-multiplied alpha can also be unintuitive at first, even with a decent mental model.
Or are you just pretending not to know to seem more relatable?
So to give some examples, a material with 100% transmission would have a density of 0, a material with 10% transmission would have a density of 1.0, and a material with 1% transmission would have a density of 2.0.
Density values have some advantages, including:
- They can be combined with simple addition
- Compared to transmission or opacity values, they correspond more closely to human visual perception
Been too long since I've done computer graphics, but you made me think about implementing transparency pass using absorbance instead.
[1]: https://en.wikipedia.org/wiki/Absorbance#Absorbance_of_a_mat...
[2]: https://en.wikipedia.org/wiki/Transmittance#Beer%E2%80%93Lam...
https://en.wikipedia.org/wiki/Alpha_compositing
If your compositing operation is A over B, then transmissivity is multiplicative for the A&B region. If your compositing mode is A in B, then opacity (alpha) is multiplicative.
This has the same structure, mathematically, as thinking about logical operations like AND and OR, which are complementary to each other. If you think of AND as multiplication, well, you can think of OR as multiplication as well, just with the values flipped around.
It's all just an approximation. Don't lose sight of that!
[1] https://graphics.pixar.com/library/Compositing/paper.pdf
The best docs I could find in a short amount of time, are those from anti-grain[1] where the author describes it as a "second alpha channel.
[1] http://agg.sourceforge.net/antigrain.com/doc/basic_renderers...
I have a mental model of how an elevator works from using them many times, but it's absolutely a simplified understanding of the real mechanisms at work. I believe elevators are fully understandable but I don't need that level of detail to operate one. If it became important to know more about them (say I was in an elevator that malfunctioned) then I'd invest more in making my mental model more correct.
So to me, mental models aren't necessarily intentionally or consciously formed and they can certainly be used for quick simplified reasoning about things that may be in fact well-understood.
Its more mentally simple than 'opacity' ('transparent' is a more common word than 'opaque' to hear used in the real world - as 'opaque' is the default of most objects/surfaces, having 'transparency' is a special property that needs to be described), more correct than 'translucency' (also used commonly including in this thread but in real definition means an object that passes light but not a clear image of what is behind, like a frosted window - light is not travelling in a straight line but is being bent) or 'transmissivity' which the author uses (needlessly making it more technical and opaque to the end user - and actually less specific as transmissivity could be translucency also)
Rebelle: Experimental online paint software https://news.ycombinator.com/item?id=30191520 (3 days ago, 22 comments)
https://en.wikipedia.org/wiki/Alpha_compositing#Straight_ver...