I have a MXS function that is supposed to create Plane() primitives that align with rectangular quads on EPoly objects and make the Plane match the size of the quad. My problem is determining the the Length/Width of the Plane–I can get the correct values for length and width I cannot figure out how to determine which is actually the length and the width (for example, the value I get for width sometimes belongs to length and vice versa).
Here is the function:
function wallworm_create_plane_from_polygon polynode faceId forceQuad:true power:2 = (
if ww_wwdt_control_mesh == undefined then (
in coordsys world (
if classOf polynode == editable_poly then (
--verts = polyop.getVertsUsingFace polynode #{faceId}
verts = polyop.getFaceVerts polynode faceId
if verts.count == 4 OR forceQuad==false then (
v1 = polyop.getVert polynode verts[1] node:polynode
v2 = polyop.getVert polynode verts[2] node:polynode
v3 = polyop.getVert polynode verts[3] node:polynode
v4 = polyop.getVert polynode verts[4] node:polynode
d1 = distance (v1) (v2)
d2 = distance (v2) (v3)
--segs = ww_wwdt_vmfpowertosegs power
--func not present in sample... assign manually
segs = 4
l = d2
w=d1
faceNormal = in coordsys polynode (polyop.getFaceNormal polynode faceId node:polynode)
local brsh = Plane length:l width:w isSelected:off lengthsegs:1 widthsegs:1 pos:(polyop.getSafeFaceCenter polynode faceId node:polynode)
/*
worldUpVector = [0,0,1]
rightVector = normalize (cross worldUpVector faceNormal)
upVector = normalize ( cross rightVector faceNormal )
theMatrix = matrix3 rightVector upVector faceNormal [0,0,0]
brsh.transform = theMatrix
*/
--Move the new mesh to the center of the face.
brsh.pos = (polyop.getSafeFaceCenter polynode faceId node:polynode)
--Make the new mesh face in the direction of the face
brsh.dir = faceNormal
--make a copy of the new mesh to determine if the verts of the mesh are aligned to verts of the original face.
--This does assume rectangular faces.
tmp = copy brsh
convertToPoly tmp
tmpverts = polyop.getFaceVerts tmp 1
tmpv1 = polyop.getVert tmp tmpverts[1] node:tmp
nverts = #(v1,v2,v3,v4)
format "
###############
Face Verts: % % % %
" v1 v2 v3 v4
format "New Verts: %
" tmpv1
/*
See if the first vert in the tmp mesh aligns with a vert in the original face.
If none, swap the length and width.
*/
if findItem nverts tmpv1 == 0 then (
print "changing l/w"
brsh.length = w
brsh.width = l
)
delete tmp
brsh.lengthsegs = segs
brsh.widthsegs = segs
if polynode.mat != undefined then brsh.mat = polynode.mat
return brsh
) else (
format "The selected face % has % verts!
" faceId verts.count
)
) else (
)
)
)
undefined
)
I have assumed that the following code would determine if the plane dimensions match the face… but it simply fails sometimes:
if findItem nverts tmpv1 == 0 then (
print "changing l/w"
brsh.length = w
brsh.width = l
)
What is odd is that the findItem sometimes (but not always) fails when the values look identical to me… for example, here is a line in the listener from a failure:
###############
Face Verts: [-70,146,128] [-70,146,256] [-70,274,256] [-70,274,128]
New Verts: [-70,274,256]
"changing l/w"
But as you can see, the Point in New Verts [-70,274,256] does exist in the Face Verts… but the findItem function returns 0 (and the script fails).
Maybe I should try another approach altogehter… but I still want to know how that fails.
Question 2:
Also, while discussing this, which of the following two methods is best for the orientation? I always see docs mentioning the first but the second seems to produce the same results:
Method 1:
worldUpVector = [0,0,1]
rightVector = normalize (cross worldUpVector faceNormal)
upVector = normalize ( cross rightVector faceNormal )
theMatrix = matrix3 rightVector upVector faceNormal [0,0,0]
brsh.transform = theMatrix
--Move the new mesh to the center of the face.
brsh.pos = (polyop.getSafeFaceCenter polynode faceId node:polynode)
Method 2:
--Move the new mesh to the center of the face.
brsh.pos = (polyop.getSafeFaceCenter polynode faceId node:polynode)
--Make the new mesh face in the direction of the face
brsh.dir = faceNormal