Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Crush on SetBoneWeights() 2019.3.0a11

Discussion in 'Editor & General Support' started by illinar, Aug 12, 2019.

  1. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    621
    I have this code:

    Code (CSharp):
    1. public void LoadMesh()
    2.     {
    3.         _mesh = Instantiate(_meshRenderer.sharedMesh);
    4.         _meshRenderer.sharedMesh = _mesh;
    5.         _mesh.GetVertices(vertices);
    6.         _mesh.GetColors(colors);
    7.         _mesh.GetUVs(0, uvs);
    8.         _mesh.GetBindposes(bindposes);
    9.         weights.AddRange(_mesh.GetAllBoneWeights());
    10.         bonesPerVertex.AddRange(_mesh.GetBonesPerVertex().ToArray());
    11.         bones.AddRange(_meshRenderer.bones);
    12.     }
    Code (CSharp):
    1. private void UpdateMesh()
    2.     {
    3.         _mesh.SetBoneWeights(new NativeArray<byte>(bonesPerVertex.ToArray(), Allocator.Temp), new NativeArray<BoneWeight1>(weights.ToArray(), Allocator.Temp)); //SetBoneWeights(weights);
    4.         _meshRenderer.bones = bones.ToArray();
    5.         _mesh.bindposes = bindposes.ToArray();
    6.     }
    It all works fine until I run this in between:

    Code (CSharp):
    1. private void BindVertices()
    2.     {
    3.         var vertices = _skinnedMesh.vertices;
    4.         var softness = _skinnedMesh.softness;
    5.         var weights = _skinnedMesh.weights;
    6.         var newWeights = new List<BoneWeight1>(vertices.Count * 2);
    7.         var bonesPerVertex = _skinnedMesh.bonesPerVertex;
    8.         var currentWeightIndex = 0;
    9.         for (int i = 0; i < vertices.Count; i++)
    10.         {
    11.             var existingWeightsCount = bonesPerVertex[i];
    12.             for (int ii = 0; ii < existingWeightsCount; ii++)
    13.             {
    14.                 newWeights.Add(weights[currentWeightIndex]);
    15.                 currentWeightIndex++;
    16.             }
    17.             var vertexSoftness = softness[i];
    18.             if (vertexSoftness > 0)
    19.             {
    20.                 var vertex = vertices[i];
    21.                 for (int ii = 0; ii < _softColliders.Count; ii++)
    22.                 {
    23.                     var bonePosition = positions[ii];
    24.                     var distance = Vector3.Distance(vertex, bonePosition);
    25.                     var bindDistance = vertexBindDistance * sizes[ii];
    26.                     if (distance < bindDistance)
    27.                     {
    28.                         var weight = new BoneWeight1();
    29.                         weight.boneIndex = boneIndexes[ii];
    30.                         var t = distance / bindDistance;
    31.                         weight.weight = 0.1f;//Mathf.Lerp(0f, 2f, t);
    32.                         newWeights.Add(weight);
    33.                         bonesPerVertex[i]++;
    34.                     }
    35.                 }
    36.             }
    37.         }
    38.         _skinnedMesh.weights = newWeights;
    39.     }
    Then it crashes every time on _mesh.SetBoneWeights() call.

    Also project no longer loads. Unity process is launched but no window.

    I am also normalizing the weights since they are not 1 per vertex after BindVertices().

    Code (CSharp):
    1.     private void NormalizeWeights()
    2.     {
    3.         for (int i = 0, weightIndex = 0; i < bonesPerVertex.Count; i++)
    4.         {
    5.             var boneCount = bonesPerVertex[i];
    6.             if (boneCount > 1)
    7.             {
    8.                 var totalWeight = 0f;
    9.                 for (int ii = weightIndex; ii < boneCount; ii++)
    10.                 {
    11.                     totalWeight += weights[weightIndex + ii].weight;
    12.                 }
    13.                 float multiplyer = 1 / totalWeight;
    14.                 for (int ii = weightIndex; ii < boneCount; ii++)
    15.                 {
    16.                     var weight = weights[weightIndex + ii];
    17.                     weight.weight *= multiplyer;
    18.                     weights[weightIndex + ii] = weight;
    19.                 }
    20.                 weightIndex += boneCount;
    21.             }
    22.         }
    23.     }
    P.S. The project is totally broken it seems. I will try everything in stable 2019.2

    So I can't make a bug report rn.

    EDIT: restored the project, made a report, linked to this page.
     
    Last edited: Aug 14, 2019