ScriptSpot

Forum topic · General Scripting

How to check if an object (point 3) is within camera's FOV?

By kaczorefx · 2009-05-26

Description

I want to check if the object is visible through a given camera.
Preferably if I could manually set a new FOV angle just for the test.

I found a way by doing a semi rendering from the viewport converting point3 data to x,y coordinates in view space, but this requires the camera to use the actual viewport :(

I'm also thinking of building a cone that would mimic the camera cone and check ray intersections with it, but I have to give the cone a height and make sure it encompases the entire scene :(

Does anyone know of a simpler way?

Comments (1)

here is some code for checking if objects are within the view

hedphelym · 2011-09-22

Please note that it uses pivot.
you should make it use boundingbox instead, to make it more accurate.

fn isPointInFrustrum pointPos = (
thePos = pointPos * viewport.getTM()
screen_origin = mapScreenToView [0,0] (thePos.z) [renderWidth,renderHeight]
end_screen = mapScreenToView [renderWidth,renderHeight] thePos.z [renderWidth,renderHeight]
world_size = screen_origin-end_screen
x_aspect = renderWidth/(abs world_size.x)
y_aspect = renderHeight/(abs world_size.y)
screen_coords = point2 (x_aspect*(thePos.x-screen_origin.x)) (-(y_aspect*(thePos.y-screen_origin.y)))
(((screen_coords.x >= 0) AND (screen_coords.x < renderWidth)) AND ((screen_coords.y >= 0) AND (screen_coords.y < renderHeight)))
)

TheObjects = objects as Array
FoundObjects = #()

for i = 1 to TheObjects.count do
(
if (isPointInFrustrum (TheObjects[i].pos))do
(
appendIfUnique FoundObjects TheObjects[i]
)
)

for o = 1 to FoundObjects.count do
(
clearSelection()
select FoundObjects
)