8
$\begingroup$

(This is a follow-up question to the answer found here.)

I would like to define a clip plane dynamically from the current ViewPoint of my Graphics3D. Based on the solution found in the link above, I would like to do something like this (where the ClipPlanes option depends on the ViewPoint variable vp):

DynamicModule[
   {vp},
   {vp} = Options[Graphics3D, ViewPoint][[All, 2]];
   Graphics3D[{FaceForm[Red, Blue], Sphere[]}, Axes -> True, ViewPoint -> Dynamic[vp], 
     ClipPlanes -> {{Sequence@@vp,0}}, ClipPlanesStyle -> Directive[Opacity[.2], Blue]
   ]
]

Unfortunately this doesn't work, and I couldn't figure out how to do it.

$\endgroup$
8
$\begingroup$

I think this may be the behavior you desire. I am borrowing Kuba's modified ClipPanes specification. One should not need RawBoxes here I think but it does serve the purpose to get our Dynamic expression into the Front End box form.

DynamicModule[{vp},
  {vp} = Options[Graphics3D, ViewPoint][[All, 2]];
  Graphics3D[{FaceForm[Red, Blue], Sphere[]},
    Axes -> True,
    ViewPoint -> Dynamic[vp],
    ClipPlanes -> RawBoxes @ Dynamic @ {{Sequence @@ Cross[vp, {0, 0, 1}], 1}}, 
    ClipPlanesStyle -> Directive[Opacity[.2], Blue]
  ]
]

enter image description here

In Evaluation leak from Dynamic in Button's action John Fultz writes with fair authority:

Front end options, which includes all box options, can take Dynamic heads. That basically means that the FE will compute the value of the Dynamic and use it for the option. And that it will be updated whenever a Dynamic dependency updates.

Since:

  1. Graphic3D expression is transformed into a Graphics3DBox (as may be viewed with menu command Cell > Show Expression)
  2. ClipPlanes remains as an option therein

it stands to reason that this must work if we can keep our Dynamic option value unmolested, and RawBoxes serves to do that.

$\endgroup$
  • 1
    $\begingroup$ Uff, at least I was right! Nice trick. $\endgroup$ – Kuba Jun 15 '16 at 16:31
  • $\begingroup$ @Kuba Thanks for adding the animation. This is a perfect example of a time where I wish the collaborative answer feature I have lobbied for had been implemented, but since it has not been and I see no benefit in this case to dueling answers I would be willing to delete mine and merge its useful content into yours. Shall I? $\endgroup$ – Mr.Wizard Jun 16 '16 at 7:46
  • 1
    $\begingroup$ Somehow the clipping, doesn't work with this solution if I change the clip plane option to ClipPlanes -> RawBoxes@Dynamic@{{Sequence @@ (vp), 0}}. What I'm trying to achieve is to always hide the back of my rendered object. $\endgroup$ – Konstantin Jun 16 '16 at 16:23
  • 1
    $\begingroup$ @Konstantin probably FE thinks it can handle it and then it can't. Let's put there something tougher so it has not choice ;) RawBoxes@Dynamic@{Append[vp, 1]} - seems to work. p.s. don't quote my attempt to explain that :) $\endgroup$ – Kuba Jun 17 '16 at 6:22
  • 1
    $\begingroup$ @Mr.Wizard RawBoxes is not needed in V11.1, at least here. $\endgroup$ – Kuba Apr 19 '17 at 11:56
3
$\begingroup$

Skip that and go upvote Mr.Wizard's answer :-)


I don't know much about rendering of Graphics3D by FrontEnd, but I'd expect ClipPlanes to accept Dynamic setting. GraphicsBox3D stays the same after changing it so it seems it is a FrontEnd which handles it anyway.

It is not the case but we can deal with that problem.

  • we need Dynamic @ Graphics3d
  • we need to send info about ViewPoint changes different way than Graphics3D is returned to not choke things. That's why the outer Dynamic is a-Synchronous

Not perfect but don't know how to make it better

(changed ClipPlanes to make them more visible)

DynamicModule[{vp}, 
 {vp} = Options[Graphics3D, ViewPoint][[All, 2]];
 Dynamic[
  Graphics3D[{FaceForm[Red, Blue], Sphere[]}, Axes -> True, 
   ViewPoint -> Dynamic[vp], 
   ClipPlanes -> {{Sequence @@ Cross[vp, {0, 0, 1}], 1}}, 
   ClipPlanesStyle -> Directive[Opacity[.2], Blue],
   SphericalRegion -> True],
  SynchronousUpdating -> False
  ]]

enter image description here

$\endgroup$
  • $\begingroup$ Thanks that is exactly what I needed! $\endgroup$ – Konstantin Jun 15 '16 at 16:15
  • 1
    $\begingroup$ @Konstantin As a rule it is best not to Accept an answer so quickly. It may discourage others form looking at and working on your problem, and sometimes even better answers are in store if you let them happen. :^) $\endgroup$ – Mr.Wizard Jun 15 '16 at 16:24
  • $\begingroup$ Kuba, you have my vote of course, but I think it is merely a bug stopping this from working directly as-is. RawBoxes seems sufficient to bypass this. Do you concur? $\endgroup$ – Mr.Wizard Jun 15 '16 at 16:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.