...
--Snaps the mesh and all of its verts to the nearest integer values
fn SnapMeshAndVertsToNearestInteger oNode = (
oNode.position = roundNearestIntegerVec3 oNode.position
for v = 1 to oNode.numverts do (
local vert = getvert oNode v
vert = roundNearestIntegerVec3 vert
setvert oNode v vert
)
)
Your Next Step
If the second test failed, then you know you have more work to do in getting the pivot behind all faces. In addition to that, you also need to check that a ray drawn from the pivot outward can only ever hit one face. A simple test from pivot to each vert will tell you that. Gmax has some ray tracing functions, but they're inherently broken, failing when a backface is struck first. This issue may or may not be fixed in higher versions of Max. For this issue, there is some code I will share with a finished product at a later date. That code has a fully functional ray tracing function with a full report of the faces struck, including their details like normals and face indices.
Once you determine which faces are still offending, you can either move the pivot further, or in the case of overlapping faces or concave faces for which there is no simple fix, detach those faces to another mesh.
The code I will share simply takes all faces that fail the second code test above and puts them in another mesh, repeating the process until there are no more reductions to do.
An optional finished script instead crawls the pivot around through a network of lines through face normals trying to locate a position that will work within about 3x the bounding box size of the mesh. This works well for really big objects with somewhat complex shapes, but still requires split hulls for concave and overlapping forms.
I know these scripts work because they've worked wonders on the Crumbling Crypt set I started building in 2021. Hopefully I can simplify them to work for you too.