# How to efficiently animate changing mesh on a fixed 3D plot?

Say I'd like to visualise a height function of a torus by animating a plane going upwards, and highlighting the intersection via Mesh/MeshFunctions. I draw the torus using ParametricPlot3D, and I would like it to be very smooth and nice looking, so I put somewhat high values of PlotPoints and/or MaxRecursion. I produce an animation using Table with the parameter being the value in Mesh.

Now the problem is, that each frame of my animation recomputes the torus, which takes a lot of time. Is there a way to only compute the torus once, and then just compute different meshes for each frame? Maybe I could compute all the meshes while computing the plot, and then just display one at the time? Can I extract meshes as, say, GraphicsComplex and combine it back with the plot in Show?

• Please edit your question to include the code you have so far. Your question is less likely to get answered if people have to spend a bunch of time just trying to figure out how to recreate the animation from your description alone. If people can just take your code and run it to see how it works, they're much more likely to be able to offer a solution if one exists. – MassDefect Jun 5 '20 at 17:38
• @MassDefect Right, I'll keep that in mind next time! – OnDragi Jun 5 '20 at 20:19

It's possible with some amount of effort. The key is to make separate plots of the surface without the meshlines, and a plot with just the meshlines. Since the OP did not bother to put in example code, I wrote up the following:

p1 = ParametricPlot3D[{(3 + Cos[u]) Cos[v], (3 + Cos[u]) Sin[v], Sin[u]},
{u, -π, π}, {v, -π, π}, Lighting -> "Classic", Mesh -> None,
PlotPoints -> 95, PlotStyle -> ColorData[97, 1]]

Animate[Show[p1,
ParametricPlot3D[{(3 + Cos[u]) Cos[v], (3 + Cos[u]) Sin[v], Sin[u]},
{u, -π, π}, {v, -π, π}, Mesh -> {{h}},
MeshFunctions -> {#3 &},
MeshStyle -> Directive[AbsoluteThickness[5],
ColorData[97, 4]],
PlotPoints -> 45, PlotStyle -> None]],
{h, -1, 1, 1/10}]


I have deliberately omitted showing the output, so that you can evaluate and see for yourself what happens with this code.

• Your last bit to this answer somehow reminds me of, “...left up to the reader.”, except that I would much rather have what you wrote, haha! How robust/smooth is Show when wrapped with something like Animate or Manipulate? Awesome answer as always, J.M.—I look forward to evaluating this myself later today :D – CA Trevillian Jun 5 '20 at 17:56
• Show[]'s only job is to appropriately combine any Graphics[]/Graphics3D[] object fed to it, and set options for the final display if supplied. So, the quality of the final result is really dependent on the graphics objects fed to it. – J. M.'s ennui Jun 5 '20 at 18:00
• Ah! Many thanks. I was mostly curious about things such as jittery animations & the-like, so that bodes well! Good deal. – CA Trevillian Jun 5 '20 at 18:02
• +1 The animation is smoother with finer steps, e.g., {h, -1, 1, 1/100} and adding the option BoxRatios -> {1, 1, 0.4}] to the Show makes the Mesh travel clearer. – Bob Hanlon Jun 5 '20 at 18:54
• Aha! That is simpler than I thought. So setting PlotStyle->None makes it compute just the Mesh, and I can still control how smooth the Mesh itself will be with PlotPoints. So not only does it speed up the process, I can also potentially make the Mesh more/less detailed than the rest of the plot. Sorry for not including an example, I'll keep that in mind next time (now you already wrote the important part of what I had). Thank you for a great quick answer! (for my working example ~60s-->~2s, and since two seconds are mostly the plot itself, it's really like \inf-->2s, hehe) Thanks! – OnDragi Jun 5 '20 at 20:13