Notifications
Clear all
[Closed] Camera CULLING
Mar 12, 2009 5:05 pm
Hi, maybe its not important, but i did camera culling script and its preaty fast, becouse i fight this problem for a very long time with no sucess here is the solution. i hope that it will be helpfull for somebody.
Idea is very simple, i just pass all geometry to test is it in front of the camera and 4 Frustrum planes (camera piramid) so its a 5 passes and every pass eliminates part of geometry for the next step.
-- THIS IS A LITTLE SCRIPT THAT HIDES ALL INVISIBLE OBJECTS FROM CAMERA
---
TestGeom=$Plane* ---TESTED GEOMETRY
TheCamera=$camera01 ---TESTED CAMERA
----
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------FUNCTIONS
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
DoCullPlane=undefined
infront=undefined
GetCameraDirections =undefined
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
fn infront TheCullPlanePos TheCullPlaneDir TesObjPosition = if (dot TheCullPlaneDir (normalize(TesObjPosition-TheCullPlanePos)))>=0 then true else false
fn DoCullPlane Passgeom TheCullPlanePos TheCullPlaneDir = (
local TestGeom=Passgeom
local Passgeom=#()
for testPlane in TestGeom do
(
local testTransform=testPlane.objecttransform
local vis=false
for p in testPlane.mesh.verts while not vis do vis = infront TheCullPlanePos TheCullPlaneDir (p.pos*testTransform)
if vis do append Passgeom testPlane
)
Passgeom
)
fn GetCameraDirections theCamera = (
local dirs=#()
local CamTR=TheCamera.objecttransform
local TheCullPlanePos=theCamera.objecttransform.position
local HFOV=theCamera.fov/2.0
local VFOV = atan(tan(HFOV)/((renderWidth as float)/renderHeight*renderPixelAspect))
local offset=1000
local w=Offset*(sin(HFOV)/sin(90-HFOV))
local h=Offset*(sin(VFOV)/sin(90-VFOV))
local TL=([-w,h,-offset]*CamTR)
local BR=([w,-h,-offset]*CamTR)
local TR=([w,h,-offset]*CamTR)
local BL=([-w,-h,-offset]*CamTR)
dirs[1]=TheCamera.dir * -1 --front
dirs[2]=cross (normalize (TL-TheCullPlanePos)) (normalize (TR-TheCullPlanePos)) --top
dirs[3]=cross (normalize (BR-TheCullPlanePos)) (normalize (BL-TheCullPlanePos)) --bottom
dirs[4]=cross (normalize (BL-TheCullPlanePos)) (normalize (TL-TheCullPlanePos)) --left
dirs[5]=cross (normalize (TR-TheCullPlanePos)) (normalize (BR-TheCullPlanePos)) --right
dirs
)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------END FUNCTIONS
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TheCameraPos=theCamera.objecttransform.position
directions=GetCameraDirections TheCamera
hide testgeom
testgeom.wirecolor=black
for testdir in directions while TestGeom.count >=1 do TestGeom=DoCullPlane TestGeom TheCameraPos TestDir
testgeom.wirecolor=green
unhide TestGeom