## Two-sided material in matlab

Unfortunately there seems to be no builtin support for two-sided surfaces in matlab. There’s some rudimentary control over back-face lighting, but that’s all. At least you can determine the back-facing triangles for a given camera position:

N = normals(V,F);
BC = barycenter(V,F);
back_facing = sum(N.*bsxfun(@minus,BC,campos),2)<=0;


Here’s an example for an armadillo mesh:

t = tsurf(F,V,'EdgeColor','none','FaceLighting','phong');view(2);
axis equal;
camproj('persp')
t.FaceVertexCData = 1*(sum(N.*bsxfun(@minus,BC,campos),2)<=0)
apply_ambient_occlusion();


Of course, if you change the view, the coloring is no longer valid:

So you need to recompute the coloring:

You can also insert nans to achieve back-face culling:

t.FaceVertexCData(sum(N.*bsxfun(@minus,BC,campos),2)>0) = nan;


### 3 Responses to “Two-sided material in matlab”

Good Day,

I am curious about how you calculated the back face culling. According to the calculation you take the normal vector and multiple it with the difference between the bary center values and the the campos. However, your final value is a zero or one. How does one derive to the angle between the normal and view vector? I do understand it is the dot product between the two, but I can seem to get the correct points?

Sorry I previously posted this in under the wrong section.

2. ajx says:

sum(N.*bsxfun(@minus,BC,campos),2) is computing the dot product of the normal N and the view vector: the difference between the barycenters BC and the camera pos campos. Then just check whether this is greater than or equal to 0.

3. Guangshen Ma says:

Hello, thanks for the great idea. Is this code available for open source in MATLAB? Thanks.