Using Filters for Shadows of 3D Surfaces

Even though SVG is a 2D vector format, plot2svg can be used to create stunning 3D effects. The SVG filter effects can be used to create shadows that enhance the 3D effect. Following example creates a sphere with semi-transparent surface together with a shadow. The current version of plot2svg does not support depth-buffer sorting of surfaces from different objects. Thus, the correct sequence has to be created manually.

figure
[x y z] = sphere(20);
% Note: We first draw the shadows. This leads to a correct
% depth sorting (there is no automatic depth sorting for
% different surfaces)

s = surface(x -0.7, y,-1.2 * ones(size(x)), ...
'facecolor', 'interp', ...
'cdata', z);
set(s, 'edgecolor', 'black', ...
'facealpha', 'flat', ...
'alphadata', x .* z);
svgBoundingBox(s, 'element', 10, 'off')
svgGaussianBlur(s, 'SourceGraphic', 2, 'blur');
svgComposite(s, 'blur', 'blur', 'arithmetic', 'final', [0 0 0.5 0]);
svgClipping(s, [-1.2 -1.2 1.2 1.2; -1.2 1.2 1.2 -1.2; -1.2 -1.2 -1.2 -1.2]')

s = surface(-1.2 * ones(size(x)), y, z -0.7, ...
'facecolor', 'interp', ...
'cdata', z);
set(s, 'edgecolor', 'black', ...
'facealpha', 'flat', ...
'alphadata', x .* z);
svgBoundingBox(s, 'element', 10, 'off')
svgGaussianBlur(s, 'SourceGraphic', 2, 'blur');
svgComposite(s, 'blur', 'blur', 'arithmetic', 'final', [0 0 0.5 0]);
svgClipping(s, [ -1.2 -1.2 -1.2 -1.2; -1.2 -1.2 1.2 1.2; -1.2 1.2 1.2 -1.2]')

s = surface(x, y, z, ...
'facecolor', 'interp', ...
'cdata', z);
set(s, 'edgecolor', 'black', ...
'facealpha', 'flat', ...
'alphadata', x .* z);
alpha('scaled');
axis equal
axis([-1.2 1.2 -1.2 1.2 -1.2 1.2])
box on
grid on
campos([2 13 10]);
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Sphere with Alpha Data and Shadow');
plot2svg('sphere.svg');

Let’s have a look at the result.

Pixelgraphics of the SVG Plot created with plo2svg

View the SVG plot

The first impression inside Matlab will be different. The Matlab plot engine does not support the additional SVG filter commands.

Original screen output (without the additional SVG features)

Categories