MATLAB Answers

nad yah

Why does FaceAlpha affect ZData of different object?

Asked by nad yah
on 4 Aug 2015
Latest activity Commented on by nad yah
on 24 Nov 2015
Hi,Im working with matlab 2015a in linux.
trying this code (with the slice grid ,x and y in the mat file attached)
if true
load('FaceAlpha.mat'); % Loading SliceGrid and X,Y parametrs
hold on; xlabel('X'), ylabel('Y'), zlabel('Z'); axis equal;
axis image;
plot(SliceGrid.x(X),SliceGrid.y(Y),'blue.');hold on;
h=[-8 -0.5 0.5 1]
v=[7 4 4 7];
fill_2=fill(h,v,'blue'); % Creating blue object
x=[0 -0.5 0.5 1]
y=[9 4 4 9];
fill_1=fill(x,y,'red');% Creating red object
t=[-2 -0.5 0.5 2]
z=[9 2 2 0];
fill_3=fill(t,z,'yellow'); % Creating yellow object
set(fill_1,'FaceAlpha',0.5); % After this line everthing is ok.
set(fill_2, 'ZData', repmat(10, size(get(fill_2, 'XData')))); % After this line the outside blue plot is damaged
%%%%%%%%%%%% - as you can see in the above figure
%%%%%%%%%%%%if we omit the line -> set(fill_1,'FaceAlpha',0.5);
%%%%%%%%%%%%and leave the code with the ZData property, the outside blue plot doesn't damaged.
%%%%%%%%%%%%if we omit the line -> set(fill_2, 'ZData', repmat(10, size(get(fill_2, 'XData'))));
%%%%%%%%%%%%and leave the code with the FaceAlpha property, the outside blue plot doesn't damaged.
why and how does face alpha affect other object with ZData? and why are they doesn't work together? what is wrong?


Sign in to comment.

3 Answers

Answer by Mike Garrity
on 4 Aug 2015
Edited by Mike Garrity
on 4 Aug 2015
 Accepted Answer

There are some interesting interactions between transparency and depth sorting. You're encountering them here.
At this point in your script:
set(fill_1,'FaceAlpha',0.5); % After this line everthing is ok.
If you ask for the SortMethod of the Axes:
You'll get the answer "childorder". That means that it's simply drawing the objects in the order you created them.
But after this line:
set(fill_2, 'ZData', repmat(10, size(get(fill_2, 'XData')))); % After this line ...
you would get the answer 'depth'. That's because up until you executed that line all of your Z values were the same. If all of your Z values are the same, MATLAB will decide that you probably want 'childorder'. But when you moved the object fill_2 in Z, MATLAB decided that you probably wanted 'depth'. Those are just guesses that MATLAB is making based on your data. You can override them like so:
Here's what it looks like with 'depth':
and here's what it looks like with 'childorder':
So why does that make the picture look different?
When we're rendering opaque objects with depth sorting, then some simple hardware on the graphics card called a "Z-buffer" can take care of figuring out what color each pixel should be. But when there are transparent objects and we're doing depth sorting, things are a bit trickier. We need to draw all of the objects in the scene in the correct, back-to-front order. The graphics literature is full of different algorithms for performing this sort. All of them have different strengths and weaknesses.
The sorting technique that MATLAB is using here is called Order Independent Transparency (OIT). It's nice and fast on the current generation of graphics hardware, but it struggles a bit when it encounters coplanar objects.
You have a number of coplanar objects here. The obvious ones are the red and yellow patches. If you rotate your view into 3D, you'll see them flashing as the OIT struggles to figure out what order to draw them in.
But what about the blue line? That looks like it should be easy. It doesn't intersect any other objects. The issue there is that it is intersecting itself. It's kind of hard to see, but it's not drawing a line, it's drawing 700 dots which are all in the same plane and overlapping slightly. The OIT is trying to figure out what order it needs to draw all of these dots in.
In addition, OIT affects GraphicsSmoothing because they are both implemented using the same features of the GPU. This means that when you have transparent objects with SortMethod='depth', you lose your antialiasing.
I wrote about SortMethod last year on the MATLAB Graphics blog , and about GraphicsSmoothing last month. I expect that I'll be writing more about how these interact in the future.


Thank you for you answer! It indeed fix the problem!
I expanded this answer into a post on the MATLAB Graphics blog. It covers some other interesting details about how transparency works in 3D.

Sign in to comment.

Answer by Vivek Jadye on 23 Nov 2015

This issue is fixed in MATLAB R2015b.
The root cause of the problem is the fact that, in absence of a global anti-alising technique, modern OpenGL drivers draw smooth round points by introducing a translucent halo at the periphery of points during rasterization; and this translucent region was being blended with an incorrect background. This means that we need to be extra careful while drawing points because even the points with opaque colors may still need blending with the correct background.

  1 Comment

You are right! its working in 2015b, Thnaks!

Sign in to comment.

Answer by Walter Roberson
on 4 Aug 2015

I have not run your code, but the most common problem when you use Alpha values is that it forces the OpenGL renderer to be used, which can result in different appearances than the other renderers.

  1 Comment

Sign in to comment.