Message Boards Message Boards

3
|
6069 Views
|
0 Replies
|
3 Total Likes
View groups...
Share
Share this post:
GROUPS:

How to improve performance of Graphics3D rendering with Dynamics

Posted 10 years ago

This is an issue I noticed since I started using Manipulate. Even though Mathematica makes making 3D graphics very easy and fun, when I use it for physics simulation that involves many 3D objects, the frame per second starts to become much lower the more graphics are displayed. The rendering becomes much slower, even though Manipulate is running at full cpu speed with no delay in the loop. Sure, I expect some slow down, but even with few objects being updated, the frame per second become too low.

This is a typical simulation loop I do. Build up the graphics3D which can contain many objects updating the positions in time and space, display on the screen and repeat.

This loop is done in Manipulate at full speed, using one single control variable that is tickled inside the Manipulate expression each time and is the only control variable tracked. So no delay and no pauses are used by the user code.

One can notice that the more objects rendered, the slower the simulation becomes. This makes using Manipulate/Dynamics not really practical for doing physics simulation involving anything but the a very simple simulation due to only the rendering on the screen being slow to complete.

I am hoping someone might have a solution to this, even though I tried many things myself, and I do not see a way to speed it up. Here is a very minimal example. I have many more if needed. This example draws a 3D cube and rotates it at full speed. We see it runs fast at first. Now clicking on the checkbox to add another Plot3D to the Graphics group, we see now it slows down significantly.

The frame per second on my PC drops from 50 to about 3. The measurement of FPS is very crude and basic, but one does not even need these numbers to notice how slow it becomes as it is clear the rotation has slowed down by just looking at it. The only difference is adding the 3D plot. This slow down also happens if I add Opacity or Texture or number of other 3D objects.

The issue is not that it slows down, but that it slows down so much.

Again, this is only one minimal example. I used Plot3D here to show this (copied the Plot3D command from one of help examples).

Any ideas why Mathematica graphics3D can slow down rendering so much? I am on windows 7, 64 bit, 16 GB PC, Advanced intel CPU, Nvidia graphics card with lots of Ram, using Version 10 and version 9, same effect. Test example below and also attached notebook

enter image description here

Manipulate[
 tick;
 Module[{fps, plot, o},
  theta = Mod[theta + 0.1, 2 Pi];
  currentTime = AbsoluteTime[];
  fps = n/(currentTime - timeStarted);(*crude way to estimate FPS*)
  fps = If[fps < 1, 0, fps];
  n++;
  If[state == "RUN", tick = Not[tick]];
  o = If[add3DPlot, {o1, o2}, o1];
  Grid[{
    {Row[{"frame per second ", AccountingForm[fps, {4, 2}, NumberPadding -> {" ", " "}]}]},
    {Graphics3D[Rotate[GraphicsGroup[o], theta, {0, 0, 1}], SphericalRegion -> True, ImagePadding -> 5, 
      PlotRange -> {{-3, 3}, {-3, 3}, {-5, 1}}
      ]
     }}]
  ],
 Grid[{
   {Button["run", {state = "RUN"; timeStarted = currentTime = AbsoluteTime[]; tick = Not[tick]}, ImageSize -> {60, 40}],
    Button["stop", {state = "STOP"; n = 0; tick = Not[tick]}, ImageSize -> {60, 40}]
    },
   {"Plot3D?", Checkbox[Dynamic[add3DPlot, {add3DPlot = #; timeStarted = currentTime = AbsoluteTime[]; n = 0} &]]}
   }],
 {{add3DPlot, False}, None},
 {{timeStarted, 0}, None},
 {{currentTime, 0}, None},
 {{n, 0}, None},
 {{state, "STOP"}, None},
 {{tick, False}, None},
 {{theta, 0}, None},
 TrackedSymbols :> {tick},
 Initialization :>
  (
   o1 = {Red, Cuboid[]};
   o2 = First@ParametricPlot3D[{1.16^v Cos[v] (1 + Cos[u]), -1.16^v Sin[v] (1 + Cos[u]), -2 1.16^v (1 + Sin[u])}, {u, 0, 
       2 Pi}, {v, -1, 1}, PlotStyle -> Directive[Specularity[White, 30], Texture[ExampleData[{"ColorTexture", "WhiteMarble"}]]], 
      TextureCoordinateFunction -> ({#4, 2 #5} &), Lighting -> "Neutral", Mesh -> None, PlotRange -> All, 
      PerformanceGoal -> "Speed"];
   )
 ]
Attachments:
POSTED BY: Nasser M. Abbasi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract