PDA

View Full Version : Align to vertex?


ivanisavich
04-23-2012, 11:49 PM
I have to meshes with an equal number of vertices.

Mesh1 is a deforming mesh. All of the vertices in Mesh2 are offset from Mesh1 by an arbitrary position.

I want to be able to deform Mesh1, then run a script and have all the vertices of Mesh2 re-align themselves to the vertices of Mesh1, maintaining their original position offset.

So far I have two scripts. This script saves the original offset of the vertices of Mesh2:


verts = #()

for g in 1 to mesh2.numverts do
(
v = (getvert mesh2 g) - (getvert mesh1 g)
append verts v
)


Then, after deforming Mesh1, I run this script to re-align the verts of Mesh2 to Mesh1:


for g in 1 to mesh2.numverts do
(
mesh1V = (getvert mesh1 g)
mesh1N = (getnormal mesh1 g)
mesh1M = (matrixfromnormal mesh1N)

setvert mesh2 g ( mesh1V +(verts[g]*mesh1M))
)
update mesh2


This seems to work when the deformations are small. But when the deformations are large, the align no longer works and the mesh2 vertices do not end up in the right place.

Anyone have any ideas how I can achieve correct deformations? I'm basically trying to recreate what the SkinWrap modifier does.

TheOnlyAaron
04-24-2012, 04:02 AM
I don't know what is causing your problem, but could you do some ghetto macro of making a morpher modifier and pulling the new target, dialing it up to 100% and then collapsing it back to mesh?

That probably wouldn't work for animation however.

ivanisavich
04-24-2012, 02:19 PM
Hey Aaron,

Well I need to do this on a few thousand verts per frame....it's part of a modifier I'm making, so a hacky solution won't work unfortunately :(

denisT
04-24-2012, 02:42 PM
I have to meshes with an equal number of vertices.

Mesh1 is a deforming mesh. All of the vertices in Mesh2 are offset from Mesh1 by an arbitrary position.

I want to be able to deform Mesh1, then run a script and have all the vertices of Mesh2 re-align themselves to the vertices of Mesh1, maintaining their original position offset.

So far I have two scripts. This script saves the original offset of the vertices of Mesh2:


verts = #()

for g in 1 to mesh2.numverts do
(
v = (getvert mesh2 g) - (getvert mesh1 g)
append verts v
)


Anyone have any ideas how I can achieve correct deformations? I'm basically trying to recreate what the SkinWrap modifier does.

you are storing the offset of these two meshes in world space (difference of correspondent vertex positions). i see another way that can work better.
try to store the difference in "deformed vertex" space. the rule of making the space might be different but lets say do it as (pseudocode):

vertoffset = (getvert mesh1 vertIndex) * (inverse (matrixfromnormal ((getvert mesh2 vertIndex) - mesh.center)))
vert_repos = vertoffset * (matrixfromnormal ((getvert mesh2 vertIndex) - mesh.center))

of course the method is not perfect but I hope it makes sense.

Eugenio
04-24-2012, 03:44 PM
In don't think it's needed to convert between spaces Denis. But who knows, you are always right in the end... :D

Here is my try:

for g in 1 to mesh2.numverts do
(
mesh1V = (getvert mesh1 g)
mesh1N = (getnormal mesh1 g)
offset = mesh1V - (mesh1N * length(verts[g]))

setvert mesh2 g offset
)
update mesh2As you can see I'm not even using matrices because the positions are directly set in world space anyway...

Hope this helps,
Eugenio

ivanisavich
04-25-2012, 07:06 PM
I got it working.

The key was to transform the vertices relative to a matrix for each target point constructed in the following way:


t = getvert mesh1 vertexIndex
zvec = getnormal mesh1 vertexIndex

--the following code gets a vert connected to our target vert by a shared edge
edgeList = (meshop.getEdgesUsingVert mesh1 vertexIndex) as array
edge1 = (meshop.getVertsUsingEdge mesh1 edgeList[1]) as array
vInx = edge1[1]
if (vInx == g) then vInx = edge1[2]

pole = getVert mesh1 vInx
pole = normalize (pole - t)
xvec = normalize (cross zvec pole)
yvec = normalize (cross zvec xvec)

m3 = matrix3 xvec yvec zvec [0,0,0]

3ak
04-25-2012, 07:38 PM
why do you need vertex normal at all?
store (obj2.vertex.pos - obj1.vertex.pos) and then just add it to new obj1.vertex.pos
am i missing something?

ivanisavich
04-25-2012, 09:18 PM
why do you need vertex normal at all?
store (obj2.vertex.pos - obj1.vertex.pos) and then just add it to new obj1.vertex.pos
am i missing something?

Yes, what you're forgetting is that if the target mesh mesh rotates, the position offset will not be correct unless multiplied by a proper transform matrix....and the transform matrix we want must be relative to each target vert if the surface of the target object is deforming.

denisT
04-26-2012, 02:55 AM
I got it working.

The key was to transform the vertices relative to a matrix for each target point constructed in the following way:

that's exactly what i said... find a space of the relative position. it might be any. just it has to be relative, not absolute.

ivanisavich
04-26-2012, 03:11 AM
that's exactly what i said... find a space of the relative position. it might be any. just it has to be relative, not absolute.

Yea, your tip pointed me in the right direction. Only reason why your example didn't work on the first try, is because the surface of the mesh is deforming (it's a skinned mesh with bones), so pointing it to the center won't work. Pointing it to an adjacent vertex solves the problem.

denisT
04-26-2012, 03:26 AM
Yea, your tip pointed me in the right direction. Only reason why your example didn't work on the first try, is because the surface of the mesh is deforming (it's a skinned mesh with bones), so pointing it to the center won't work. Pointing it to an adjacent vertex solves the problem.
i was absolutely sure that my point has been wrong. :)

denisT
04-26-2012, 03:31 AM
the only thing what i tried to do was push you to a relative coordinates. it worked. :)

Eugenio
04-26-2012, 04:35 AM
Mesh1 is a deforming mesh.Oh boy, somehow I completely overlooked this part at the very beggining. And this changes everything. Shame on me... :blush:

And that's a shame twice because some days ago I had problems to stick particles to a deforming volume, since Lock/Bond leaves empty spaces if the deformations are too strong. Then I got thinking about how to stick the particles, and I was somewhat on your lines but never tried anything because I figured out I would probably end up with something similar to Lock/Bond result. Anyway in the end we got thinking particles and there it worked very well, evenly distributed particles all the time. I wonder how they do that.

And just out of curiosity, your meshes have the same number of vertices and topology, right? And why did you have to do it through maxscript and not skinwrap?

ivanisavich
04-26-2012, 06:14 AM
And just out of curiosity, your meshes have the same number of vertices and topology, right? And why did you have to do it through maxscript and not skinwrap?

Yup...the mesh has the same number of vertices.

I need to do it through code because it's part of a plugin that deforms a mesh over time...there's actually not two meshes....just one being deformed over a series of frames. I only explained my example with two meshes to make it easier to understand :)

Basically I calculate a deformation on a mesh, and then I want to apply that deformation to the mesh as it animates...while maintaining the deforming mesh underneath as the base. In order to properly apply the deformation to the deforming mesh, I needed to know how to calculate a proper vertex offset between the base verts and the deformed verts.

CGTalk Moderation
04-26-2012, 06:14 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.