How can I find two vertex indexes per face on a mesh to create a secondary alignment vector

I've had troubles in the past with instance spinning when using "Align Euler to Vector" to align instances to face normals on deforming meshes. I have a setup that eliminates all spinning problems by aligning one Euler to the face normal (Z) and then also locking a second axis (X or Y) by aligning to a vector created from two of the vertices that make up the face. There is one major shortcoming of my setup that I need help with - currently I am manually stating the vertex indexes that form this second vector (nodes highlighted green). I want the vertex indexes to be automatically ascertained by somehow sampling the faces. I have a feeling I need to use the Corner-of-Face node but just can't figure it out.

Here is the blend file with the setup.

Take a look at the above blender file and you will see that on the single triangle the "spike" object is aligned to the face normal AND one the face edge between vertex 0 and vertex 1 - perfect! On the icosphere however things go wrong as they are all trying to align to verts 0 and 1 rather than two vertexes from each of the faces that spawn an instance. Automatically using the correct vertex indexes will provide a golden solution to instance spinning on deformed meshes.

If you just want to stabilize them so you don't really care about which edge of the face they're on they align with, you can simply Sample Nearest edge for its index, then Sample Index for that edge's directional vector using the two Positions from Edge Vertices:

Update: Stabilize for animation

To stabilize the rotations when the mesh is deformed, instead of referencing the nearest edge dynamically, we can do as you've suspected we should, and employ the positions of certain two face corners instead. The Corners of Face node with its Sort Index set to $$0$$ can get us the first corner. Using Offset Corner in Face set to Offset: $$1$$ we can select the very next one. We can now subtract the positions of these two corners and capture that information as an attribute on the Face domain to use later in the nodetree for alignment:

Left: Align to nearest edge Right: Align to the first two face corners

With more complex geometry and deformations, even when you stabilize the two axes, the third one can still randomly switch between one direction and its opposite, resulting in unwanted movement. To stabilize the third axis, you can finally align it to the Cross Product of the two previous ones (a third vector that's perpendicular to both):

Left: Two axes stabilized. Third sometimes switching sides. Right: All three axes aligned

• Thanks for the input. Because the mesh deforms with time the "nearest" edge changes which results in the instances changing alignment to different edges (at least I think that's what is happening) - which isn't desirable. Have a go scrubbing through the timeline so that the animated deformation occurs and you'll see the effect. Feels like you're way closer than me though... any way you can adapt your setup? Jan 9 at 15:16
• @DavidM Sorry about being hasty with my answer, I missed the animation issue. Updated the answer with another solution. Jan 9 at 17:33
• Perfect!!! Rock solid instancing off faces - been after this since I started Blender. Thank you so much. It actually solves an earlier question I posted a while back here: [link] (blender.stackexchange.com/questions/278372/…) If you want to pop over there and reference this answer I can mark it as the answer there too - don't want to take any credit from you. :) Jan 9 at 19:38
• @DavidM Glad to be of help. I think duplicate questions are supposed to be closed, so I'm gonna do that. The "closed" banner will have a link to this one so we don't have to copy paste the same answer. Cheers. Jan 9 at 19:51
• @DavidM Noticed that they can still flip orientation if you don't align all three axes. Updated the answer with a third alignment setup. Jan 9 at 20:29