I'm trying to shade 3D parametric surfaces using a basic 3D lighting model, but this answer says that TikZ has no support for lighting. The only option is to use color gradients to shade vertices. This may be acceptable for certain graphs, but not when you're trying to show the actual shape of a 3D object.

But TikZ clearly has all the data available to do this, though. Derivatives can be calculated numerically for each vertex (i.e. without the user needing to analytically derive them by hand for each surface). These can then be use to construct the normals. Let the user specify a point light location and presto, you have diffuse lighting. Snag the camera position and you have specular lighting as well.

I know that packages like Asymptote can create nice 3D images, but these solutions create raster graphics, and I need vector graphics.

How can I add 3D lighting and shading to TikZ? Is there a reason it doesn't exist already? I don't know how TikZ is implemented, and I haven't written any extensions before, so I have no idea how I would add this feature.

  • Welcome to TeX-SE. It is true that TikZ has all the data necessary to compute a realistic shading in principle. However, realizing this in practice is a very different story. Even though automatically hiding hidden surfaces is an in principle solved problem, realizing it in practice is tough. That's why asymptote is not super short package. And asymptote does allow you to produce 3d vector graphics, see p. 55 of this nice tutorial, albeit with limitations. – marmot Mar 3 at 18:37
  • You might be able to use Sage combined with the sagetex package. Sage documentation here gives examples with 3D and lighting and parametric. You can see what code looks like by going here, copying and pasting code samples. Images can be saved as svg. Try code, for example, of " 4 spheres that illustrates various uses of the texture command". Worth mentioning Sage should be able to do the math you talk about and work into the tikzpicture. – DJP Mar 3 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.