Light object / source in PGF / TikZ / PGFplots pictures like in PSTricks

I came across the PSTricks animation located at

and noticed that it uses a light object / source for the color / shading of the object. This can be seen from the code as found at

http://melusine.eu.org/syracuse/pstricks/pst-solides3d/animations/a43/anim.tex

I make all of my pictures with PGF and would like to use light sources in a similar manner as done in the PSTricks animation above. Unfortunately, this has is not implemented (yet) in PGF.

Is it perhaps possible to do some post processing of my PGF pictures to get the effect of a light source? I am thinking of PSTricks, Asymptote and Adobe Illustrator, but I can't seem to manage.

I think it would be great if we could produce vector graphics pictures with light sources in PGF like for instance

• @Guuk Simply to make my pictures look nicer and with that hopefully my publications more memorable and appreciated ;). But in the future, it could indeed also be for vector graphics animations. – Adriaan Apr 7 '15 at 9:42

The only obstacle is TeX.

Postscript is used to do the heavy lifting and the linear algebra but we don't have the luxury to rely on some big brother using PGF. Your best bet is to find a library that understands both the Z-buffer and solid reflections in another language and call it within.

pgfplots don't know that yet using only TeX. Even if this was possible which I believe the contrary, it would have taken a huge amount of time to compile the simplest case, again, even with neglecting the TeX imprecision in fixed-point arithmetic.

A far more comprehensive explanation of why is here Fundamental differences : PSTricks, TikZ/PGF and others and Herbert's answer in particular.

As answered by @percusse, TeX is the obstacle here. I avoided TeX by creating a (rough) SAGE/python script that will take a SAGE 3d picture, already triangulated, and translate it to tikz 2d polygons, with some sort of light/shade computations. You do need SAGE to create the tikz file though.

It is available in github, in case you want to experiment with it.

You just write a few-liner python script (called from within the editor) that takes a SAGE-python snippet like this (this an implicit surface with a singular point):

P=implicit_plot3d( x**3+y**2-z**2 ==0,(x,-2,2), (y,-2,2),(z,-2,2), plot_points=100)
P.triangulate()
Picture(P.face_list(), boxed=False, axes=True,
nodes=[{"Position": (0,0,0), "Text": "" ,
"Options": "circle, inner sep=4pt,black!80, pin={[pin edge={black!80, semithick}]-120:Singular point}, draw"}]
).render()


and produces a standalone file like this:

\documentclass[border=1pt]{standalone}
....
\begin{document}
\begin{tikzpicture}[scale=25.361, line cap=round, line width=.1pt, line join=round, line cap=round]
\begin{pgfonlayer}{textLayer}
\node[circle, inner sep=4pt,black!80, pin={[pin edge={black!80, semithick}]-120:Singular point}, draw] at (0.000000,0.000000) {};
\end{pgfonlayer}
\filldraw[fColor=32] (-0.005352,-0.005537)--(-0.005287,-0.005489)--(-0.005163,-0.005353)--cycle;
\filldraw[fColor=32] (-0.004111,-0.004313)--(-0.005163,-0.005353)--(-0.005287,-0.005489)--cycle;
... lots more little polygons.
\end{tikzpicture}
\end{document}


When you compile this file with lualatex (this is important, the huge number of polygons will need a huge amount of memory), gets you something like

Whatever lights/shades there are, are built into the python script as are the all the computations pertaining to 3d, view, projection, axis, translation of coordinates, etc.

PRO: you get a beautiful image (this of course is a matter of taste).

CONS:

• You need some heavy duty math software, in this case SAGE.

• You are dependent on external scripts. You better get a good editor with external script calling capabilities.

• Compilation is still very slow. You probably want to use tikz's externalize trickery.

• You are dependent on lualatex compiling for big figures (externalising somewhat alleviates this).

• Finally, I only implemented one source of white light, one foreground color and one background color.

Other examples:

If you are interested in this script, you can get it from github.

• Sweet! I'll definitely use this. – percusse Apr 7 '15 at 13:04
• Looks great! Is it also possible to use the script when you already have coordinate data for a surface? Is it possible to 'inject' this data via, for instance, an external table file? – Adriaan Apr 7 '15 at 16:16
• The script just does the projecting and coloring of plane polygons (triangles if they come from sage). So it could -in principle- be tailored for precomputed data, as long as it is given in the correct format: a list of lists of vertices of polygons. Anything else will need python programming... – Koji Apr 7 '15 at 17:15