Why does FaceAlpha affect ZData of different object?

3 visualizaciones (últimos 30 días)
nad yah
nad yah el 4 de Ag. de 2015
Comentada: nad yah el 24 de Nov. de 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
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?

Respuesta aceptada

Mike Garrity
Mike Garrity el 4 de Ag. de 2015
Editada: Mike Garrity el 4 de Ag. de 2015
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.
  2 comentarios
nad yah
nad yah el 5 de Ag. de 2015
Thank you for you answer! It indeed fix the problem!
Mike Garrity
Mike Garrity el 6 de Ag. de 2015
I expanded this answer into a post on the MATLAB Graphics blog. It covers some other interesting details about how transparency works in 3D.

Iniciar sesión para comentar.

Más respuestas (2)

Vivek Jadye
Vivek Jadye el 23 de Nov. de 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.

Walter Roberson
Walter Roberson el 4 de Ag. de 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.

Categorías

Más información sobre Graphics Object Programming en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by