Notifications
Clear all

[Closed] RayIntersect without an actual mesh

The way I’ve used IntersectRay is like this: intersectRay targetMesh (ray inputPos inputNormal)

But I’m wondering if it’s possible to do IntersectRay without an actual target-mesh? Instead of passing in the actual mesh you would just pass in the faceCenter and faceNormal of the target, is this possible?

5 Replies

How the Max to know the bounds of your face only by knowing the face center and face normal?

The ray may hit the face near any of its edges.

I would retrieve them and pass them in myself within the script.

The reason why I’m asking is that when I’m doing multiple RayIntersects on a mesh, I have to split out the surfaces for each intersection I want to check. It’s not a big problem, but if it’s possible to just pass in this information it would be a bit faster/more efficient.

Do you mean that you have to create scene nodes to use IntersectRay on that part of the mesh?

Yes

You can make or access native mesh and use its IntersectRay method. (for 2018+ versions you can use MCG’s wrappers to do the job )


tea = Teapot()
rotate tea (EulerAngles 15 23 44)
(
	inverse_tm = inverse tea.objecttransform
	tri = snapshotAsMesh tea


	temp_target_mesh = createInstance Editable_mesh
	
	meshop.attach temp_target_mesh tri

	g = (dotNetClass "Autodesk.Max.GlobalInterface").Instance
	IMesh = g.animatable.getanimbyhandle (dotNetObject "System.UIntPtr" (getHandleByAnim temp_target_mesh ))

	fn MakeRayNative pos dir tm =
	(
		-- convert ray from world to local space
		_pos = pos * tm
		_dir = dir * tm
		
		(g.rayvalue.Create (g.point3.create _pos.x _pos.y _pos.z) (g.point3.create _dir.x _dir.y _dir.z)).toRay
	)

	intersection_normal = g.point3.create()
	intersection_distance = 0.0
	
	for i=1 to 10 do
	(
		ray_pos = [ random -10 10, random -10 10, 100 ]
		ray_dir = -z_axis
		_ray = MakeRayNative ray_pos ray_dir inverse_tm


        	-- .<System.Boolean> IntersectRay  <System.Int32>t <Autodesk.Max.IRay>r <System.Single&>at <Autodesk.Max.IPoint3>norm
		has_hit = IMesh.IntersectRay (currenttime as integer) _ray &intersection_distance intersection_normal

		if has_hit do
		(
			point pos:(ray_pos + ray_dir * intersection_distance) centermarker:on cross:off wirecolor:red
		)
	)
	
	free temp_target_mesh
	free tri
)