MATLAB Answers

Transparency limited by rounding issues while rendering 2D plots in MATLAB

4 views (last 30 days)
I've found that 2D plot rendering calculations for transparency seem to suffer from an integer rounding issue. My guess is that this is an issue with the Matlab implementation, as the behavior occurs for both Painters and OpenGL renderers.
I expect that a sufficiently large number of overlapping transparent dots of a color should look the same as an opaque dot of the same color.
Yet, while attempting to visualize the density of point clouds using point transparency, I found that the color never saturated, no matter how dense the point cloud. Instead, the "final" color reached depended on the alpha of the dots, which it should not.
Please see this post for full details and example code:
Is there an explanation for this behavior?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Aug 2020
Edited: MathWorks Support Team on 27 Aug 2020
In order to get the desired behavior for 2D, please try the following methods:
  1. Change the axes 'SortMethod' property to 'childorder'. If there is 3D content in the axes, this property is set to 'depth'.
  2. Another work around is simply to not place objects in the same space, if the blending is important. The separation distance is a little challenging to calculate but may be determined via experimentation.
MATLAB graphics detects if you are trying to draw transparency in 3D and draws in a different way than it normally draws by using the 'Depth Peeling' method.
Depth Peeling is a mechanism used to draw transparency in a physically based scene. It essentially sorts the objects in each pixel from back to front and applies the blending equation layer by layer. It has a limitation in that it relies on the graphics depth buffer to do the sorting and can't handle drawing coincident surfaces (which is not a problem in physical models).
The blending equation is C = Sa + (1-a) D, where S is the next color, a is its opacity, and D is the destination color.
If surfaces could be coincident in the 3D reality, the actual color would be ambiguous as it's not clear which object is the source, and which is the destination.
There will be a numerical difference when calculating transparency based on the resolution of the color values since the values in the blending equation mentioned are 8-bit precision. In MATLAB, the resolution of the color values cannot reach a limit, i.e. the color, because of the limitation of using 8-bit values.
Please note that this is an issue with the way graphics rendering is done at the hardware level and MATLAB does not currently support HDR rendering.
The graphics card stores each pixel's color in a 32-bit integer: 8 bits each for Red, Green, Blue, and Alpha.
This means that each channel can have a value of 0-255. Usually, one can't see more than 255 gradations of each color, but the issue arises when we start to accumulate float values in 8-bit storage, which results in truncation error.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!