[Closed] getting explicit normal of a surface with a ray intersection?
Hey,
So i have some models with custom normals applied. The Nodes in question are Editable Polys, and they have custom normals that I added using an Edit Normal modifier and then collapsed.
so what im trying to do is spawn an object on the surface of the node. Im currently doing this with a raytrace from the mouse position, which grabs the intersection point and surface normal with an intersectRay like so:
mouseray = mapScreenToWorldRay mouse.pos
sectray = intersectRay $ mouseray
obj = box()
obj.pos = sectray.pos; obj.dir = sectray.dir
I also wanted to be able to place the object perfectly level with the face that is being clicked, so it doesnt clip through the face on rounder objects, so I did this:
local sg = #()
mouseray = mapScreenToWorldRay mouse.pos
for f = 1 to polyop.getNumFaces $ do (append sg (polyop.getFaceSmoothGroup $ f); polyop.setFaceSmoothGroup $ f 0)
sectray = intersectRay $ mouseray
for f = 1 to polyop.getNumFaces $ do (polyop.setFaceSmoothGroup $ f sg[f])
obj = box()
obj.pos = sectray.pos; obj.dir = sectray.dir
Basically it stores all smoothing groups of the object in an array, sets them all to zero to removing smoothing, does the ray intersection to get just the face’s normal, and then reapplies the smoothing.
Kind of hacky but worked well enough.
The primary functionality I actually need though, which I have not been able to work out, is to get the explicit normal of the face from the ray intersection. Currently the intersectRay seems to ignore custom normals, and just uses the normals from the smoothing groups, even though they are not actually visible on the model.
there are no explicit normal of a face,
this normals are vertex-normals
to get your result, you do rayintersect which gives you the face index and Barycentric coordinates
with that index, get the normal-face of the poly, they have same index as the mesh faces
on that “normal-poly” you have then the normals at each vertex of the face
then do the Barycentric coordinate-calculations to get the normal at your intersection point
Is there a way to do that on a polymesh?
I played around with the intersectRayEx method but i couldnt get it to work on an editable poly, and indeed the documents say it only works on mesh nodes.
Im not sure if there are any other methods for getting barycentric coordinates.
The intersectRay method seems to just return a single vector but no face ID or coordinates.
I realize that editable polys dont actually have seperate face IDs for the triangles, so im not sure in what format the coordinates could be given in, but I dont think coverting the mesh to an editable mesh is a viable option for this script im making. (unless thers a faster and more intuitive way to do it than the convertTo method)
i’m not a scripter, so dunno…
with c++ you can do, so it must be possible by script too, just have no idea about scripting
wait for some script-guru to reply
you can use MeshProjIntersect or RayMeshGridIntersect
the idea is the same but methods are slightly different. (see mxs help)
do intersection
get hit face and its bary (both values are corresponding to current object mesh, so it’s trimesh data)
get face verts
using meshop.getFaceRNormals (the best you can get with mxs) get three normals for a specified face
using bary coordinates get a normal in the point of intersection
Both of those only work on editable meshes though right?
Is there some way i could temporary convert an ePoly to a eMesh. Im trying to use convertTo but it takes like a second to load and kinda makes the script unusable.
Ok I worked around the issue by cloning the canvas mesh before raycasting and converting it to editable Mesh.
Now the problem is that the command meshop.getFaceRNormals doesnt actually return the custom normals, it just returns what the normal would be according to the face positions and smoothing groups.
I also tried getNormals in an attempt to get the normals straight from the vertex, but that doesnt return the explicit normal either.
The only way to work with Specified Normals in MXS is by using the Edit Normals modifier.