MATLAB: Does FaceAlpha affect ZData of different object

MATLABmatlab guiplotprogrammingr2014bgraphicssimulation

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
clear
clc
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.
end
why and how does face alpha affect other object with ZData? and why are they doesn't work together? what is wrong?

Best 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:
get(gca,'SortMethod')
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:
set(gca,'SortMethod','childorder')
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.