private static void Lerp(EditableBoneWeight first, EditableBoneWeight second, ref EditableBoneWeight result, float t) { result.Clear(); foreach (BoneWeightChannel channel in first) { if (!channel.enabled) { continue; } var weight = channel.weight * (1f - t); if (weight > 0f) { result.AddChannel(channel.boneIndex, weight, true); } } foreach (BoneWeightChannel channel in second) { if (!channel.enabled) { continue; } var weight = channel.weight * t; if (weight > 0f) { result.AddChannel(channel.boneIndex, weight, true); } } result.UnifyChannelsWithSameBoneIndex(); result.Clamp(4); if (result.Sum() > 1f) { result.Normalize(); } result.FilterChannels(0f); }
public static void SmoothWeights(BoneWeight[] boneWeightIn, IList <int> indices, int boneCount, int iterations, out BoneWeight[] boneWeightOut) { Debug.Assert(boneWeightIn != null); boneWeightOut = new BoneWeight[boneWeightIn.Length]; PrepareTempBuffers(boneWeightIn.Length, boneCount); for (int i = 0; i < boneWeightIn.Length; ++i) { s_BoneWeight.SetFromBoneWeight(boneWeightIn[i]); for (var j = 0; j < s_BoneWeight.Count; ++j) { if (s_BoneWeight[j].enabled) { m_DataInTemp[i, s_BoneWeight[j].boneIndex] = s_BoneWeight[j].weight; } } } for (var i = 0; i < iterations; ++i) { SmoothPerVertexData(indices, m_DataInTemp, m_DataOutTemp); } for (var i = 0; i < boneWeightIn.Length; ++i) { s_BoneWeight.Clear(); for (var j = 0; j < boneCount; ++j) { var weight = m_DataOutTemp[i, j]; var boneIndex = weight > 0f ? j : 0; s_BoneWeight.AddChannel(boneIndex, weight, weight > 0); } s_BoneWeight.Clamp(4); s_BoneWeight.Normalize(); boneWeightOut[i] = s_BoneWeight.ToBoneWeight(false); } }
public void CalculateWeights(IWeightsGenerator weightsGenerator, ISelection <int> selection, float filterTolerance) { Debug.Assert(spriteMeshData != null); Vector2[] controlPoints; Edge[] bones; int[] pins; GetControlPoints(out controlPoints, out bones, out pins); Vector2[] vertices = new Vector2[spriteMeshData.vertexCount]; for (int i = 0; i < spriteMeshData.vertexCount; ++i) { vertices[i] = spriteMeshData.GetPosition(i); } BoneWeight[] boneWeights = weightsGenerator.Calculate(vertices, spriteMeshData.edges.ToArray(), controlPoints, bones, pins); Debug.Assert(boneWeights.Length == spriteMeshData.vertexCount); for (int i = 0; i < spriteMeshData.vertexCount; ++i) { if (selection == null || (selection.Count == 0 || selection.Contains(i))) { EditableBoneWeight editableBoneWeight = EditableBoneWeightUtility.CreateFromBoneWeight(boneWeights[i]); if (filterTolerance > 0f) { editableBoneWeight.FilterChannels(filterTolerance); editableBoneWeight.Normalize(); } spriteMeshData.SetWeight(i, editableBoneWeight); } } }