Notifications
Clear all

[Closed] Raycast from camera

Hi! I want to raycast from camera to some object face/polygon and see, if I get any other faces.polygons colliding along the way. What is the most performat way to do it you can advice? There are going to be many faces, also I’ll need to check from many angles.

Basically, I want just to remove all polygons, which are unvisible to camera from any camera position this way.

Thanks!

4 Replies

Try this – it is what you need, I made it for myself. Works only with Editable Poly, but may be enchanted for Editable Mesh.

  1. select view (orthogonal,perspective,camera)
  2. execute “DeleteBackFaces type:#Ortho” for orthogonal projection
  3. or “DeleteBackFaces type:#Perspective” for something else
  4. enjoy!
ang = 90

fn DeleteBackFaces type:#Ortho = (    
    getFaceN = polyop.getFaceNormal      
	getFaceC = polyop.getFaceCenter 	
    undo on (
		
	if type == #Ortho then (	
		dir = -(Inverse(GetViewTM())).row3   
		
		for obj in geometry do (
			if obj.ishidden == false then (                
				
				f_count = getnumfaces obj
				del_faces = #{}
				del_faces.count = f_count
				
				for i = 1 to f_count do (
					n = getFaceN obj i
					if acos(dot n dir) < ang then del_faces[i] = true
					)    
				polyop.deleteFaces obj del_faces delIsoVerts: true                
				) --if obj.ishidden == false
			) --for obj in geometry
		) -- if type == #Ortho
		
	else if type == #Perspective then (
		v_pos = (Inverse(GetViewTM())).row4
		for obj in geometry do (
			if obj.ishidden == false then (                
				
				f_count = getnumfaces obj
				del_faces = #{}
				del_faces.count = f_count
				  
				for i = 1 to f_count do (
					n = getFaceN obj i
					f_pos = getFaceC obj i
					dir = normalize (f_pos - v_pos)
					if acos(dot n dir) < ang then del_faces[i] = true
					)    
				polyop.deleteFaces obj del_faces delIsoVerts: true                
				) --if obj.ishidden == false
			) --for obj in geometry
		) -- if type == #P		
		
    ) --undo
    )

-- DeleteBackFaces type:#Ortho
--DeleteBackFaces type:#Perspective

I decided to start small, here:

//Get direction vector between camera and on of the vertecies
newVec = man.mesh.verts[1].position – cam.position
ff = normalize newVec

//Raycast
exampleray = ray camera.position ff
result = intersectRay $ exampleray

and result is undefined. I figured out, that it is because vertex position is local. But no matter what, I can’t find how to get in world-space. getVert should do it, but it is not 0_0 Am I missing something?

To get world space vertex position try this (for editable poly object)

vertexPosition = in coordsys world polyop.GetVert polyObj vertIndex

Thank you for the answer, everyone. I’ve got it all working.