# How to calculate nearest bones, weights and bindposes programmatically?

Hey guys,

I'm trying to implement mesh rigging algorithm inside the Unity.

For testing, I'm using Ethan's mesh. The algorithm looks something like:

1. For each bone, find all related bones (parents and children)
2. For each vertex in mesh, find nearest bone in hierarchy
3. Then, find nearest four (or less) related bones
4. Calculate weights inversely proportional to the distance from vertex
5. Calculate bindposes

This is how mesh looks like with 1 bone quality set in SkinnedMeshRenderer:
[145988-2019-09-10-20h28-46.png*|145988]
And this is with 4 bone quality:
[145989-2019-09-10-20h29-43.png*|145989]

I was working on it for a quite while and don't know how to fix this problem.

Here's the code I am using:

``````public class RiggingHelper : MonoBehaviour
{
private GameObject rootBoneObject;
private Transform[] bones;
private Mesh mesh;
private float boneDistance;
private int boneIndex;
private int vertexCount;
private BoneWeight[] boneWeights;
private Matrix4x4[] bindposes;
private int[] boneIndicies;
private Vector3[] vertices;
private Vector3[] bonesPositions;

private bool finished = false;
private bool calcWeightsFinished = false;

private Vector3 weightBonesVertex;
private Vector3[] weightBonesPositions;
private float[] weightBonesWeights;
private int[] weightBonesIndices;

private List<Vector3>[] relatedBonesPositions;
private List<int>[] relatedBonesIndices;

public void SkinMesh(SkinnedMeshRenderer meshRenderer, GameObject skeleton)
{
rootBoneObject = skeleton;
List<Transform> bonesList = new List<Transform>();
foreach (var bone in skeleton.GetComponentsInChildren<Transform>())
{
if (bone.parent != null)
}
bones = bonesList.ToArray();

mesh = meshRenderer.sharedMesh;
vertexCount = mesh.vertexCount;
meshRenderer.bones = bones;
boneWeights = new BoneWeight[vertexCount];
boneIndicies = new int[vertexCount];
vertices = mesh.vertices;
bonesPositions = new Vector3[bones.Length];

for (int i = 0; i < bones.Length; i++)
{
bonesPositions _= bones*.position;*_
``````

}

StartCoroutine(SkinMeshCoroutine());
}

private IEnumerator SkinMeshCoroutine()
{
while (!finished)
yield return null;

FindRelatedBones();

finished = false;
while (!finished)
yield return null;

// Create bindposes
bindposes = new Matrix4x4[bones.Length];
for (int i = 0; i < bindposes.Length; i++)
{
if (bones*.parent != null)*
bindposes = bones_.worldToLocalMatrix * bones*.parent.localToWorldMatrix;
else_

bindposes _= bones.worldToLocalMatrix;
}*_

mesh.boneWeights = boneWeights;
mesh.bindposes = bindposes;
}

private void FindNearestBones(object token)
{
// For each of the vertices, find nearest bone
for (int i = 0; i < vertexCount; i++)
{
Vector3 vertex = vertices*;*
boneIndex = 0;
boneDistance = Vector3.Distance(bonesPositions[0], vertex);
for (int j = 1; j < bones.Length; j++)
{
float newDistance = Vector3.Distance(bonesPositions[j], vertex);
if (newDistance < boneDistance)
{
boneIndex = j;
boneDistance = newDistance;
}
}
boneIndicies = boneIndex;
}
finished = true;
}

private void FindRelatedBones()
{
relatedBonesPositions = new List[bones.Length];
relatedBonesIndices = new List[bones.Length];

// Find all related bones, including children and parents
for (int i = 0; i < bones.Length; i++)
{
Transform startBone = bones*;*
relatedBonesPositions = new List();
relatedBonesIndices = new List();

foreach (var child in startBone.GetComponentsInChildren())
{
if (child.gameObject.GetInstanceID() != startBone.gameObject.GetInstanceID())
{
for (int j = 0; j < bones.Length; j++)
{
if (bones[j].gameObject.GetInstanceID() == child.gameObject.GetInstanceID())
{
break;
}
}
}
}
foreach (var parent in startBone.GetComponentsInParent())
{
if (parent.gameObject.GetInstanceID() != startBone.gameObject.GetInstanceID() &&
parent.gameObject.GetInstanceID() != startBone.parent.gameObject.GetInstanceID())
{
for (int j = 0; j < bones.Length; j++)
{
if (bones[j].gameObject.GetInstanceID() == parent.gameObject.GetInstanceID())
{
break;
}
}
}
}
}
}

{
for (int i = 0; i < boneIndicies.Length; i++)
{
weightBonesVertex = vertices*;*
weightBonesPositions = relatedBonesPositions[boneIndicies*].ToArray();*
weightBonesIndices = relatedBonesIndices[boneIndicies*].ToArray();*
calcWeightsFinished = false;
GetBonesWithWages();

// Assign bone indicies and weights
for (int j = 0; j < weightBonesWeights.Length; j++)
{
switch (j)
{
case 0:
boneWeights*.boneIndex0 = weightBonesIndices[j];*
boneWeights*.weight0 = weightBonesWeights[j];*
break;
case 1:
boneWeights*.boneIndex1 = weightBonesIndices[j];*
boneWeights*.weight1 = weightBonesWeights[j];*
break;
case 2:
boneWeights*.boneIndex2 = weightBonesIndices[j];*
boneWeights*.weight2 = weightBonesWeights[j];*
break;
case 3:
boneWeights*.boneIndex3 = weightBonesIndices[j];*
boneWeights*.weight3 = weightBonesWeights[j];*
break;
default:
break;
}
}
}
finished = true;
}

private void GetBonesWithWages()
{
float[] distances = new float[weightBonesPositions.Length];

for (int i = 0; i < distances.Length; i++)
distances = Vector3.Distance(weightBonesVertex, weightBonesPositions*);*

// Sort distances ascending
for (int j = distances.Length - 1; j > 0; j–)
{
for (int i = 0; i < j; i++)
{
if (distances > distances[i + 1])
{
// Sort distance, position and index
float tmpDist = distances*;*
distances = distances[i + 1];
distances[i + 1] = tmpDist;

Vector3 tmpPos = weightBonesPositions*;*
weightBonesPositions = weightBonesPositions[i + 1];
weightBonesPositions[i + 1] = tmpPos;

int tmpIndex = weightBonesIndices*;*
weightBonesIndices = weightBonesIndices[i + 1];
weightBonesIndices[i + 1] = tmpIndex;
}
}
}

// Take up to 4 distances, calc their weights and find corresponding indices
float distanceSum = 0f;
int index = 0;

for (int i = 0; i < 4; i++)
{
if (i < distances.Length)
{
distanceSum += distances*;*
index++;
}
else
break;
}

weightBonesWeights = new float[index];
for (int i = 0; i < 4; i++)
{
if (i < distances.Length)
{
weightBonesWeights = distances / distanceSum;
}
else
break;
}
Array.Reverse(weightBonesWeights);

calcWeightsFinished = true;
}
}

*
*

Hi
did you fix it