Here are some numbers when hitting one plane and three spheres…
intersectRay = 5 fps
rayMeshGridIntersect multiple objects = 10 fps
rayMeshGridIntersect single attached object = 12 fps
I think that I’m going with the latter unless any one can tell me there are reasons not to. It is using the method where I snapShot all the objects, attach them together and then build the grid and then delete the object.
I just increased the face count in the plane object to about 10k faces and rayMeshGridIntersect is now 10 times faster.
The issue that I have run into now is with animated geo. I don’t think that I really need it but trying to update the voxel grids are killing it. Could be cause by script controllers that are calling it to often how ever. I might be able to get around this but has any one managed to make this work in other cases?
I did some speed tests but don’t have the results with me right now. I can get back to you on that if you want.
In general, I found MeshProjIntersect more reliable to use. I also found that RayMeshGridIntersect can produce funky results depending on the grid size used.
Watch out for memory leaks, though!
– MartinB
I have a memory leak with it and needed to add a gc() to the function that builds the data grid. A gc light:true didn’t work by the way and niether did using free()
Hi Paul, I found your thread while searching for info on RayMeshGridIntersect. I’m interested in how you went about “attaching them together” as you said here
SnapShotAsMesh all the nodes and attach them together. Create a mesh using mesh mesh:$theAttachedMeshObjects, create and build the rayMeshGrid and then delete the $theAttachedMeshObjects.
The best I’ve been able to come up with is this. I’m sure there’s a less long-winded way to do it.
clearlistener()
validObjects = #()
for obj in objects where superclassof obj == GeometryClass and obj.castShadows == on and obj.ishidden == false do
(
append validObjects obj
)
validMeshes = #()
for validObj in validObjects do
(
tempMesh = snapshotasmesh validObj
append validMeshes (mesh mesh:tempMesh)
)
for i = 2 to validMeshes.count do
(
meshop.attach validMeshes[1] validMeshes[i]
)
validMeshes[1].name = "AttachedMesh"
Cheers,
Cg.
Just something else on this topic that I’ve just figured out. I haven’t been able to get the correct worldspace results of the ray hitpoint out of rayMeshGridIntersect when trying to use barycoords. I don’t know if I was doing it worng, or it’s still broken like I’ve been reading about in other posts about RMGI in earlier versions of max. So I figured out another way to get the worldspace of the hitpoint that seems to work so far. Appologies if this is obvious, but I thought I’d share anyway incase it helps someone out there as there seems to be a bit of confusion and mystery surrounding rayMeshGridIntersect.
I make a unit vector by diving the original ray direction by the total length of the ray (distance between the start and end positions of the ray).
I then use rayMeshGridIntersect.getHitDist to figure out how far from the start position of the ray it intersected the mesh and multiplied that by the unit vector and then added that to the startpoint of the ray to get the actual worldspace of the hitpoint. It’s actually less code than going down the bary path.
When you think about it, it’s really just using 2 co-ordinates (start and end position) and a ratio between those 2 points (comparing the distance of the whole ray and the distance to the hitpoint), whereas barycentric is using 3 co-ordinates (the verts of the face) and the ratio between those 3 points (the bary coords). So it makes sense to me that it would be quicker.
Anyone have any thoughts on this?
Oh and thanks to Bobo as it was his advanced maxscript DVD that clued me in on unit vectors and bary coords in the first place. Anyone that is serious about maxscript really should buy Bobo’s DVDs.
Cg.
Hi Chris, I would have to look back at what I’m doing but it is similar. I’m just doing it all in one loop instead of three. I think what I did was to create an empty mess and then loop through all the objects that are to be part of the ground plane and snapShot and attach them all in one go. Don’t know how much faster it is but I would think a bit.
As for using the barycoords I have not looked at using that. I’m just getting the world coords and not having an issue with that.
“I make a unit vector by diving the original ray direction by the total length of the ray (distance between the start and end positions of the ray).”
You can also use
normalize (theVector)
Hi Paul, thanks kindly for the reply.
When you say:
As for using the barycoords I have not looked at using that. I’m just getting the world coords and not having an issue with that.
How did you go about that? I can’t see an option with RayMeshGridIntersect to be able to get the world coords directly.
Cheers,
Cg.