View Full Version : Hiding objects outside camera cone

03 March 2012, 07:56 AM
I use this script ( to create a frustum culling cone for my camera and I use the cone to hide faces of some object like ground with vol select and delete mesh operators.

But what I miss is a script to hide objects outside of the camera cone.

Does anyone know of such a script?

I know forest pack pro and vue Carbon Scatter do it but I would like to do it by maxscript.

03 March 2012, 12:24 PM
Use this to get objects inside the cone, and then invert the selection.

03 March 2012, 02:26 AM
I found this one and it seems to work!

I load it in the RPManager "Before:" script section that way it's executed at every frame.

03 March 2012, 04:42 AM
finally it was not working at everyframe, it just run the script on first frame. to make it run at everyframe at render time I had to use the callback command like this:

here is what it looks like in the before script and after script of RPManager:

before script:

callbacks.removescripts id:#hideNonVis

txt ="TestGeom=$capRock_* ---TESTED GEOMETRY"
txt +="TheCamera=$camC ---TESTED CAMERA"
txt +="DoCullPlane=undefined\n"
txt +="infront=undefined\n"
txt +="GetCameraDirections =undefined\n"
txt +="fn infront TheCullPlanePos TheCullPlaneDir TesObjPosition = if (dot TheCullPlaneDir (normalize(TesObjPosition-TheCullPlanePos)))>=0 then true else false\n"
txt +="fn DoCullPlane Passgeom TheCullPlanePos TheCullPlaneDir = (\n"
txt +="local TestGeom=Passgeom\n"
txt +="local Passgeom=#()\n"
txt +="for testPlane in TestGeom do\n"
txt +="(\n"
txt +="local testTransform=testPlane.objecttransform\n"
txt +="local vis=false\n"
txt +="for p in testPlane.mesh.verts while not vis do vis = infront TheCullPlanePos TheCullPlaneDir (p.pos*testTransform)\n"
txt +="if vis do append Passgeom testPlane\n"
txt +=")\n"
txt +="Passgeom\n"
txt +=")\n"
txt +="fn GetCameraDirections theCamera = (\n"
txt +="local dirs=#()\n"
txt +="local CamTR=TheCamera.objecttransform\n"
txt +="local TheCullPlanePos=theCamera.objecttransform.position\n"
txt +="local HFOV=theCamera.fov/2.0\n"
txt +="local VFOV = atan(tan(HFOV)/((renderWidth as float)/renderHeight*renderPixelAspect))\n"
txt +="local offset=1000\n"
txt +="local w=Offset*(sin(HFOV)/sin(90-HFOV))\n"
txt +="local h=Offset*(sin(VFOV)/sin(90-VFOV))\n"
txt +="local TL=([-w,h,-offset]*CamTR)\n"
txt +="local BR=([w,-h,-offset]*CamTR)\n"
txt +="local TR=([w,h,-offset]*CamTR)\n"
txt +="local BL=([-w,-h,-offset]*CamTR) \n"
txt +="dirs[1]=TheCamera.dir * -1 --front\n"
txt +="dirs[2]=cross (normalize (TL-TheCullPlanePos)) (normalize (TR-TheCullPlanePos)) --top\n"
txt +="dirs[3]=cross (normalize (BR-TheCullPlanePos)) (normalize (BL-TheCullPlanePos)) --bottom\n"
txt +="dirs[4]=cross (normalize (BL-TheCullPlanePos)) (normalize (TL-TheCullPlanePos)) --left\n"
txt +="dirs[5]=cross (normalize (TR-TheCullPlanePos)) (normalize (BR-TheCullPlanePos)) --right\n"
txt +="dirs\n"
txt +=")\n"
txt +="TheCameraPos=theCamera.objecttransform.position\n"
txt +="directions=GetCameraDirections TheCamera\n"
txt +="hide testgeom\n"
txt +="testgeom.wirecolor=black\n"
txt +="for testdir in directions while TestGeom.count >=1 do TestGeom=DoCullPlane TestGeom TheCameraPos TestDir\n"
txt +="testgeom.wirecolor=green\n"
txt +="unhide TestGeom\n"
callbacks.addscript #preRender txt id:#hideNonVis

txt ="unhide TestGeom"
callbacks.addscript #postRender txt id:#hideNonVis

After script:

callbacks.removescripts id:#hideNonVis
unhide $capRock_*

there is also a callback feature in RPManager, but that one just ran the script randomly only on some frames..

03 March 2012, 07:02 PM
The script is working but when I render a test frame, I sometimes get those errors. Does anyone can see why?

-- Unkknown property: "objecttransform" in undefined

And after render: -- No ""unhide"" function for undefined

CGTalk Moderation
03 March 2012, 07:02 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.