-4

Is there a way to turn off the default depth sorting of curves in a 3D output of several complicated curves ? Currently, I suspect that depth sorting has a very strong impact on performances on my Manipulate box, and would like to turn it off, to see if there's an improvement (I'm sure it will !).

Here's a cheap MWE to work with :

curve1[t_] := ParametricPlot3D[
{Sin[3 Pi s], Cos[5 Pi s^2], Cos[3 Pi s] Sin[3 Pi s]},
{s, 0.001, t},PlotStyle -> Directive[Thickness[0.02], Red]]

curve2[t_] := ParametricPlot3D[
{1.3 Sin[7 Pi s], 0.5 Cos[2 Pi s], 0.4 Sin[6 Pi s^2]},
{s, 0.001, t},PlotStyle -> Directive[Thickness[0.02], Blue]]

Manipulate[Show[
    {curve1[t], curve2[t]},
    PlotRange -> {{-1.5, 1.5}, {-1.5, 1.5}, {-1.5, 1.5}},
    Boxed -> True, Axes -> True, AxesOrigin -> {0, 0, 0},
    SphericalRegion -> True,
    Method -> {"RotationControl" -> "Globe"},
    ImageSize -> 600
    ], {{t, 1, "t"}, 0, 12, 1}]

EDIT 1 : Depth sorting is the ordering of elements in 3D space. One in front of the other, as seen by the observer. This is a standard concept in 3D modeling, games, etc... Mathematica clearly do it too (by default), if you watch closely its 3D output of thick curves. Depth sorting is necessary when there are surfaces.

In my special case, I have no surfaces, just a single complicated thin curve. I don't need depth sorting in its case. Turning off depth sorting of that curve elements should improve a lot performances.


EDIT 2 : Here's an example of apparent no depth sorting in Mathematica, from Silvia :

Plot3D[x+y,{x,-1,1},{y,-1,1},AxesOrigin->{0,0,0},Mesh->None,Boxed->False]

While moving around that plane, you'll notice that the axis, ticks and labels are always shown "on front". They don't display any "depth sorting" of their elements. This is what I would like to achieve for curves.

There's also a strong advantage in getting no depth sorting of curves : when exporting a Mathematica 3D curve to a PDF file, and open the file with another vectorial application, you'll get the curve made of lots of small bits. The whole curve is then very hard to edit in a proper way. Without depth sorting of the curve, the curve would be of a single piece. This is highly desirable for exportation to another vectorial app.


EDIT 3 : Compare the output from the code above, with the output from the same code with the default thickness of curves (code below). The depth sorting is still there, but it is useless since it is not visible from this output :

curve1[t_] := ParametricPlot3D[
{Sin[3 Pi s], Cos[5 Pi s^2], Cos[3 Pi s] Sin[3 Pi s]},
{s, 0.001, t},PlotStyle -> Red]

curve2[t_] := ParametricPlot3D[
{1.3 Sin[7 Pi s], 0.5 Cos[2 Pi s], 0.4 Sin[6 Pi s^2]},
{s, 0.001, t},PlotStyle -> Blue]

Manipulate[
    Show[
    {curve1[t], curve2[t]},
    PlotRange -> {{-1.5, 1.5}, {-1.5, 1.5}, {-1.5, 1.5}},
    Boxed -> True, Axes -> True, AxesOrigin -> {0, 0, 0},
    SphericalRegion -> True,
    Method -> {"RotationControl" -> "Globe"},
    ImageSize -> 600
    ], {{t, 1, "t"}, 0, 12, 1}]

Just to emphasize it : Depth sorting is not visible on thin curves and is thus useless. If a curve is very complicated, depth sorting may have a significant impact on some hardware, and it is desirable to turn it off.

Cham
  • 4,093
  • 21
  • 36
  • 3
    So, um, what is "depth sorting"? – Jason B. Mar 11 '16 at 15:46
  • depth sorting is the ordering of elements in 3D space. One in front of the other, as seen by the observer. This is a standard concept in 3D modeling, games, etc... Mathematica clearly do it too (by default), if you watch closely its 3D output. It is necessary when there are surfaces. – Cham Mar 11 '16 at 15:47
  • 1
    huh, I guess that is the whole idea of 3D rendering... you might be better off with less points on your curves... on my machine, your example renders quite zippy... – Yves Klett Mar 11 '16 at 15:51
  • I'll edit the question to add my last comment. – Cham Mar 11 '16 at 15:51
  • No ! Less points will give a terrible output. This has nothing to do with depth sorting. – Cham Mar 11 '16 at 15:52
  • So what would it be like without depth sorting? As it is, you have two curves and the one closest to you occludes the ones behind you. It seems to me that is necessary for it to be a 3D view – Jason B. Mar 11 '16 at 15:52
  • No, curves without depth sorting will still be 3D (obviously!). But the curves will seem to intersect at some places, depending on the observer view point. Without depth sorting, the curves will not be sorted one in front of the other. This would be obvious if the curves are made very thick, but depth sorting will not be noticeable if the curves are thin. – Cham Mar 11 '16 at 15:56
  • You can take a look at the RenderingOptions (esp. under "Graphics3DRenderingEngine") in the Options inspector. Also, antialiasing can slow rendering down. – Yves Klett Mar 11 '16 at 15:56
  • Antialiasing has an impact too. Is there a way to make it ControlActive ? And also maybe the depth sorting ? – Cham Mar 11 '16 at 15:57
  • I don't see a lag for render... The performance bottleneck seems to be the generating process when t gets larg. – Silvia Mar 11 '16 at 15:58
  • Are you sure Mathematica even uses depth sorting? I'd have guessed it just uses the z-buffer any recent graphics hardware has in-built. The only reason to still use depth sorting is to render transparency correctly, IIRC. – Niki Estner Mar 11 '16 at 15:59
  • @Silvia, Yes, "t" has an impact too, obviously, since the curves are getting longer and more complicated. And don't forget the MWE above is just a toy. It is NOT my real Manipulate box, which is drawing more complicated curves. The lag from large "t" is in part caused by the depth sorting ! – Cham Mar 11 '16 at 15:59
  • @nikie, yes Mathematica is doing real depth sorting of curves. Just add some large thickness to the curves (I'll edit my MWE), and it becomes very obvious. – Cham Mar 11 '16 at 16:00
  • So maybe I'm just a simple caveman lawyer, but I still don't get it. How would, for example, this look without depth sorting? It would be just the blue curve rendered on top of the red curve? That would give the impression that the blue curve is closer to the observer. – Jason B. Mar 11 '16 at 16:02
  • @JasonB, yes, that's the whole point. Without the depth sorting of curve elements, both curves will look like "superposed" one on top of the other (depending on their declaration in the code, I guess). With a single thin curve, this wouldn't be a problem. However, when you rotate your point of view, you still be able to see the 3D shape of each curve. – Cham Mar 11 '16 at 16:06
  • you can project to and draw in 2D quite easily, but I doubt this will be more efficient, esp. if you want a dynamic viewpoint. Can you show a MWE which actually lags badly? If you are on a slow system, there is only so much one can do. Also make sure you have hardware acceleration activated. – Yves Klett Mar 11 '16 at 16:08
  • @YvesKlett, no no ! No 2D ! I need the 3D view of my curve. It's just a single curve that bend and twist in a complicated way in 3D. The depth sorting of all its small elements is what have an high impact on performances (and also its antialiasing). – Cham Mar 11 '16 at 16:11
  • 4
    @Cham, please remain civil, this means not yelling at those who are trying to understand and help (your repeated use of exclamation points is what I'm talking about here) – Jason B. Mar 11 '16 at 16:14
  • @JasonB, Me yelling ? How ? The caps used for ON/OFF was (I believed) the correct way of writing these terms in English. Apparently, I was wrong. Sorry about it. – Cham Mar 11 '16 at 16:41
  • @Cham, I was talking about the exclamation points, which generally indicate shouting. – Jason B. Mar 11 '16 at 16:43
  • @Cham no worries! Everything this side of two exclamation marks is fine with me. Three exclamation marks now, this way madness lurks . – Yves Klett Mar 11 '16 at 18:07
  • Why the negative score for that question ? I believe the question is clear and appropriate. To me, this is a very important topic. – Cham Mar 11 '16 at 21:19
  • 5
    Cham, I am not the one who down-voted. However I am wondering what an image without depth sorting, or Z buffering, would look like. Perhaps others are also. Second, I do not find it apparent that Z-buffering is a significant performance issue on modern hardware; what reference or direct evidence can you give to support your assertion that it is? – Mr.Wizard Mar 11 '16 at 21:55
  • @Mr.Wizard. My strong experience with Celestia (an open source OpenGL astronomy software) have shown to me that depth sorting is a complicated issue, especially with sprites (transparent or translucid objects). Because of the strong hit on frame rate, Celestia don't do any depth sorting on sprites. It can do it on thickless curves (Celestia curves are one pixel of thickness only). The curve I'm drawing with Mathematica are complicated and Mathematica has to calculate lots of useless depth sorting (my curves have a small thickness). – Cham Mar 11 '16 at 22:02
  • @Mr.Wizard, a picture of thin curves (and only thin curves) would look almost exactly the same as a picture with depth sorting. You don't see the depth sorting on thin curves ! – Cham Mar 11 '16 at 22:07
  • 1
    Taking into consideration the qualification of most people discussing with (and eager to help) you, I would argue that you simply have not explained sufficiently what you mean by depth buffering, and have not provided a useful example to show a) how it looks like b) how it actually might impact performance and c) what you actually want to achieve by implementing that (performance? appearance?). As mentioned much earlier, your example does note exhibit any prohibitive lag. So essentially, your question is unclear (at least to me), notwithstanding the comment frenzy. – Yves Klett Mar 12 '16 at 09:27
  • 2
    Regarding "Mathematica has to calculate lots of useless depth sorting", I think the calculation is not done by Mathematica, but forwarded to OpenGL or DirectX engine of the host OS. – Silvia Mar 12 '16 at 11:55
  • 5
    @Cham: Sorry for the car analogy, but that's like saying "my experience with my car has shown me that an automatic transmission wastes a lot of fuel. My plane uses too much fuel, so I'd like to turn of it's automatic transmission". Unless two systems use the same underlying algorithms and similar implementations, you can't transfer performance assumptions like that. – Niki Estner Mar 12 '16 at 12:32

2 Answers2

9

I don't think Mathematica is using depth sorting to decide surface visibility. My reasoning is that a geometry like this:

Graphics3D[
 Table[
  {
   AbsoluteThickness[10],
   If[OddQ[u/(60 °)], Red, Blue],
   Line[{{Cos[u], Sin[u], -.1}, {Cos[u + 80 °], 
      Sin[u + 80 °], +.1}}]
   }, {u, 0, 360 °, 60 °}], Boxed -> False]

enter image description here

would be displayed incorrectly if the red and blue lines were sorted, then simply drawn in that order.

I strongly believe what is really going on here is called Z-buffering. In a nutshell, it means the GPU stores a depth value for every pixel, and when it renders a new pixel it compares the new pixel's depth with the old depth value, only rendering pixels with lower depth (closer to the viewer). Almost any graphics hardware available today (even tablets and cell phones) support this in hardware, so there is virtually no performance cost.


From the documentation:

The 3D renderer uses two different methods of sorting polygons. For graphics scenes that include no transparency, a hardware-accelerated depth buffer is used. Otherwise, the renderer uses a binary space partition tree to split and sort polygons from any viewpoint. The BSP tree is slower to create and is not hardware accelerated, but it provides the most general ability to support polygons.

So, if you want to avoid the performance hit, don't use transparency.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Niki Estner
  • 36,101
  • 3
  • 92
  • 152
  • Comments are not for extended discussion; this conversation has been moved to chat. – Mr.Wizard Mar 11 '16 at 20:46
  • Even if there is no transparency (I don't use it), the hardware may have difficulties in sorting out all the pieces. I know that depth sorting is actually a performance hit problem, when the scene is very complex. So I need to know the solution for my problem. And why the negative score for this question ? – Cham Mar 11 '16 at 21:23
  • 2
    My guess: because people are honestly trying to help you solve your performance problem and you just argue in response. For example: you seem to think that depth buffering is very expensive. Depth buffering requires one extra comparison per pixel, so there is some cost. But that cost only depends on the number of pixels drawn. So if you simplify your geometry (by using fewer PlotPoints), and performance gets better, depth buffering isn't your problem. But when someone suggested that in a comment, you just shouted "no! Thats not what i want". It seems you dont want to accept help... – Niki Estner Mar 11 '16 at 22:27
  • @nikie, I don't want to argue again and again. But apparently people don't understand what is depth sorting, and that it does have a performance cost in some complexe scenes (for which I do need lots of points. I don't have a choice here). So projecting to 2D, reducing the number of points, etc, are not solution to my problem. I do need to turn off the depth sorting. Apparently, there is no option in MMA to do this. This is deceiving. – Cham Mar 11 '16 at 23:00
  • @Cham You can ask WRI support to confirm, or on a Wolfram Community too. – Kuba Mar 12 '16 at 07:01
  • 2
    Yes @Cham, nobody understands what depth sorting is, not even Wikipedia. – Niki Estner Mar 12 '16 at 11:06
  • @nikie. About the "answer" above, here's what MMA is doing : it splits the lines into smaller bits, and use depth sorting for each bit, like what is shown on this picture (this was exported from MMA and opened with a vectorial app) : http://s17.postimg.org/ordvmdfzz/test.jpg. – Cham Mar 12 '16 at 13:36
  • @Cham: As the documentation quote says, it only does that when you turn on transparency, and when it can't use hardware acceleration. If you render on screen, without transparency, it won't do that. – Niki Estner Mar 12 '16 at 13:57
  • @nikie, then why does it splits the lines like shown on my previous picture ? I don't use transparency. – Cham Mar 12 '16 at 14:02
  • @Cham: Because it can't use hardware acceleration when exporting to a vector format. So it has to fall back to BSP tree rendering. – Niki Estner Mar 12 '16 at 14:39
  • @nikie, this isn't convincing. In other 3D curves I made before, without curves overlap, the curve isn't splited after exportation. When there are several parts overlaping, the curves are splited. So it appears to me that MMA do depth sorting, even for exportation. – Cham Mar 12 '16 at 14:50
  • @Cham: It uses depth sorting only for exporting, and when you use transparency. – Niki Estner Mar 12 '16 at 14:55
  • 1
    @nikie, is there a way to turn off depth sorting for exportation ? – Cham Mar 12 '16 at 14:58
  • 1
    @Cham: If there is, it's not documented. – Niki Estner Mar 12 '16 at 15:41
  • @nikie, thanks. This is what I expected. There's probably no commands for this, very sadly. – Cham Mar 12 '16 at 15:51
  • nikie and @Cham what part of "Comments are not for extended discussion" was ambiguous? I provided a venue for extended chat; please use it. – Mr.Wizard Mar 13 '16 at 18:34
3

My QA provides a method to convert Graphics3D to a 2D projection in Graphics. Of course, there is no depth-sorting or z-buffering taking place in 2d graphics.

Using the wrapper function from the link:

Manipulate[
 wrapper[Show[{curve1[t], curve2[t]}, 
   PlotRange -> {{-1.5, 1.5}, {-1.5, 1.5}, {-1.5, 1.5}}, 
   ImageSize -> 600], {-3, 3, 3}, {0, 0, 1}, {0, 0, 1}], {t, 1, 12}]

at high values of t the scene becomes very sluggish, much more so than just the Manipulate from edit 3 of OP. I think it can be concluded, that z-buffering gives a negligible performance hit. In fact, it is dwarfed by the computation time needed to project the lines onto a 2D plane using my top-level implementation.

My wrapper function does, however, prevent the issue mentioned in edit 2: Lines exported from Graphics are not depth-sorted and come out as single, grouped elements.

The answer to this question is

OP's assumption is wrong. z-buffering does not noticeably affect performance

LLlAMnYP
  • 11,486
  • 26
  • 65