Intermidiate – 02 working with vectors and matrices

This small scripts gets all pivot points for all geometry in the scene.. selects all that are in front of the camera “c”, you need to defined the camera…

It then selects all objects that are inside the view… remember that it checks for the PIVOT point not faces, so there might still be a part of a object visible but not included..


--a small function that calculates the screen position in X/Y pixels of a 3D pos 
fn PixelPos inputPoint3D  =
	screen_width = 0
	screen_height = 0
	screen_width=RenderWidth as float
	screen_height=RenderHeight as float

	--viewport.setTM ViewTransform
	thePos = inputPoint3D * viewport.getTM()
	screen_origin = mapScreenToView [0,0] (thePos.z) [screen_width,screen_height]
	end_screen = mapScreenToView [screen_width,screen_height] (thePos.z) [screen_width , screen_height]
	world_size = screen_origin-end_screen
	x_aspect =screen_width/(abs world_size.x)
	y_aspect =screen_height/(abs world_size.y)

	screen_coords = point2 (x_aspect*(thePos.x-screen_origin.x)) (-(y_aspect*(thePos.y-screen_origin.y)))

	return screen_coords

--a small function that rounds values.. handy sometimes
fn roundFloat d pre:0.1 =
	d = (d as float)/pre
	v = if (d - (v1 = floor d)) > ((v2 = ceil d) - d) then v2 else v1 

--define the camera
c = $camera001
--this is only useful if you intend to use it inside a scriptcontroller.. which is fun when testing.
--dependson = $camera001

objs = for o in geometry where (in coordsys c o.pos ).z <= 0.0 collect o

for o in objs do
	screenPos = (PixelPos  o.pos )
	if screenPos.x >= 0.0 and screenPos.x <= renderwidth and screenPos.y >= 0.0 and screenPos.y <= renderHeight then (
	--print out all objects that has their pivot point inside the camera view
		format "ScreenX:%\tScreenY:%\t%\n" screenPos.x screenPos.y
		o.wirecolor = green
	) else (
		o.wirecolor = yellow

--color all objects behind the camera normal red..
objsInv = for o in geometry where (o.pos * (inverse c.transform)).z >= 0.0 collect o
for o in objsInv do o.wirecolor = red