[Closed] How do I find the face of an intersection?
If I have a ray intersecting a particular mesh, what do I need to do in order to find the particular face that the ray intersects?
There are some things in the documentation that seem to address this question using RayMeshGridIntersect and MeshProjIntersect, but I don’t really understand the usage of either. The example script in RayMeshGridIntersect makes my brain hurt, and I don’t understand what it is supposed to be doing. :shrug:
either use IntersectRayEx (which returns additional information, among which the face hit), or use “<rayMeshGridIntersect>.hitFace <int>” to get the int’th hit in a rayMeshGridIntersect.
Below is some code that demonstrates both methods (only evaluate the callback code for the example you want to test.
The example displays the hit face in red in the viewport, while the rest will remain grey.
-- from help file; constructs a ray through the center of the current viewport (perspective/non-ortho camera)
fn getViewDirectionRay = (
local coordSysTM = Inverse(getViewTM())
local viewDir = -coordSysTM.row3
local viewPt = coordSysTM.row4
ray viewPt viewDir
)
-- make a sphere where we can select its faces and have that effect a change in material IDs
-- this will visualize the selected face
mySphere = GeoSphere()
convertToMesh mySphere
addModifier mySphere (Mesh_Select())
max modify mode
select mySphere
subObjectLevel = 3
modPanel.addModToSelection (MaterialModifier())
mySphere.material = MultiMaterial()
mySphere.material.materialList[1].diffuse = (color 255 0 0)
/* ******************************************
EXAMPLE 1:
set up a callback using intersectRayEx
****************************************** */
callbacks.removeScripts id:#test
callbacks.addScript #viewportChange "
myRay = getViewDirectionRay()
myHit = intersectRayEx mySphere myRay
if (myHit != undefined) then (
myHitFace = myHit[2]
setFaceSelection mySphere mySphere.modifiers[#Mesh_Select] #(myHitFace)
)
else (
setFaceSelection mySphere mySphere.modifiers[#Mesh_Select] #()
)
" id:#test
forceCompleteRedraw()
/* ***********************************************
EXAMPLE 2:
set up a callback using rayMeshGridIntersect
*********************************************** */
callbacks.removeScripts id:#test
rMGI = rayMeshGridIntersect()
rMGI.initialize 50
rMGI.addNode mySphere
rMGI.buildGrid()
callbacks.addScript #viewportChange "
myHit = rMGI.intersectray (getViewDirectionRay()).pos (getViewDirectionRay()).dir false
if (myHit != 0) then (
myHitFace = rMGI.getHitFace 1
setFaceSelection mySphere mySphere.modifiers[#Mesh_Select] #(myHitFace)
)
else (
setFaceSelection mySphere mySphere.modifiers[#Mesh_Select] #()
)
" id:#test
forceCompleteRedraw()
( tested in 3ds Max 2009 )