%% Isosurface Using Different Types of Surface Normals
% Compare the effect of different surface normals on the visual appearance of lit isosurfaces. In one case, the triangles used to draw the isosurface define the normals. In the other, the isonormals function uses the volume data to calculate the vertex normals based on the gradient of the data points. The latter approach generally produces a smoother-appearing isosurface.
% Define a 3-D array of volume data.

data = cat(3,[0 .2 0; 0 .3 0; 0 0 0],...
    [.1 .2 0; 0 1 0; .2 .7 0],...
    [0 .4 .2; .2 .4 0;.1 .1 0]);
data = interp3(data,3,'cubic');

%
% Draw an isosurface from the volume data and add lights. This isosurface uses triangle normals.

tiledlayout(1,2)
nexttile
fv = isosurface(data,.5);
p1 = patch(fv,'FaceColor','red','EdgeColor','none');
view(3)
daspect([1,1,1])
axis tight
camlight
camlight(-80,-10)
lighting gouraud
title('Triangle Normals')

%
% Draw the same lit isosurface using normals calculated from the volume data.

nexttile
fv = isosurface(data,.5);
p2 = patch(fv,'FaceColor','red','EdgeColor','none');
isonormals(data,p2)
view(3) 
daspect([1 1 1])
axis tight
camlight 
camlight(-80,-10) 
lighting gouraud
title('Data Normals')

% These isosurfaces illustrate the difference between triangle and data normals.
% Copyright 2019 The MathWorks, Inc.