# Dynamic ClipPlanes calculated from current ViewPoint

(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.

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]
]
]


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.

• Uff, at least I was right! Nice trick. – Kuba Jun 15 '16 at 16:31
• @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? – Mr.Wizard Jun 16 '16 at 7:46
• 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. – Konstantin Jun 16 '16 at 16:23
• @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 :) – Kuba Jun 17 '16 at 6:22
• @Mr.Wizard RawBoxes is not needed in V11.1, at least here. – Kuba Apr 19 '17 at 11:56

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
]]


• Thanks that is exactly what I needed! – Konstantin Jun 15 '16 at 16:15
• @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. :^) – Mr.Wizard Jun 15 '16 at 16:24
• 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? – Mr.Wizard Jun 15 '16 at 16:30