Notifications
Clear all
[Closed] Maxscript function doesn't update in while loop
Nov 13, 2015 9:20 am
I have this function (which I found here) that checks if verts are in the camera frame. What I want to do is put the function in a while loop and move the camera back, and every time the camera moves it should check if the object is in camera or not. But when I do that, the function in the while loop doesn’t update, it always returns the values from the first check.
fn getVertsInViewport theObject =
(
local theVerts = #() --return array
local theMesh = snapshotasmesh theObject --grab the mesh from top of the stack
local theCount = theMesh.numverts --get the number of vertices
local theTM = viewport.getTM() --get the current view's transformation
local screen_width = renderWidth --get the current render height
local screen_height = renderHeight --get the current render width
for v = 1 to theCount do --loop through all vertices
(
local thePos = (getVert theMesh v) * theTM --transform vertex in view space
--get the world location of the upper left corner of the camera view at the depth of the vertex
local screen_origin = mapScreenToView [0,0] (thePos.z) [screen_width,screen_height]
--get the bottom right corner at the vertex depth
local end_screen = mapScreenToView [screen_width,screen_height] (thePos.z) [screen_width,screen_height]
--calculate the world size based on the two corners
local world_size = screen_origin-end_screen
--calculate the X and Y aspect factors
local x_aspect = screen_width/(abs world_size.x)
local y_aspect = screen_height/(abs world_size.y)
--calculate the screen coordinates using all the above data:
local screen_coords = point2 (x_aspect*(thePos.x-screen_origin.x)) (-(y_aspect*(thePos.y-screen_origin.y)))
--if the vertex is outside of the screen (negative or higher than the render size), collect it
if screen_coords.x <= 0 or screen_coords.y <= 0 or screen_coords.x > screen_width or screen_coords.y > screen_height then
append theVerts v
)--end v loop
delete theMesh --release the memory used by the TriMesh
theVerts --return the collected vertices
)--end fn verts in viewport
maksimum = 3
counter = 1
while counter < maksimum do
(
move $Camera001 [0,-550,0]
cameraOK = getVertsInViewport $
print (counter as string +" "+ "camera_pos = "+ $Camera001.pos.y as string +" : " +(cameraOK as string))
counter += 1
)
1 Reply
Nov 13, 2015 9:20 am
first of all as i can see reading the code the function your use detects verts in a viewport. but you need it in camera view. which is different in general case.
you have to take into account ‘render rectangle’ and it’s aspect ratio.
also i would use gw.hTransPoint to get a vertex position in viewport coordinates (there must be a lot of examples on this forum)