public static void GetMultiEditChannelData(this SpriteMeshData spriteMeshData, ISelection selection, int channelIndex, out bool channelEnabled, out BoneWeightData boneWeightData, out bool isChannelEnabledMixed, out bool isBoneIndexMixed, out bool isWeightMixed) { if (selection == null) { throw new ArgumentNullException("selection is null"); } bool first = true; channelEnabled = false; boneWeightData = new BoneWeightData(); isChannelEnabledMixed = false; isBoneIndexMixed = false; isWeightMixed = false; foreach (int i in selection) { EditableBoneWeight editableBoneWeight = spriteMeshData.vertices[i].editableBoneWeight; BoneWeightData otherBoneWeightData = editableBoneWeight.GetBoneWeightData(channelIndex); if (first) { channelEnabled = editableBoneWeight.IsChannelEnabled(channelIndex); boneWeightData.boneIndex = otherBoneWeightData.boneIndex; boneWeightData.weight = otherBoneWeightData.weight; first = false; } else { if (channelEnabled != editableBoneWeight.IsChannelEnabled(channelIndex)) { isChannelEnabledMixed = true; channelEnabled = false; } if (boneWeightData.boneIndex != otherBoneWeightData.boneIndex) { isBoneIndexMixed = true; boneWeightData.boneIndex = -1; } if (boneWeightData.weight != otherBoneWeightData.weight) { isWeightMixed = true; boneWeightData.weight = 0f; } } } }
public static void UnifyChannelsWithSameBoneIndex(this EditableBoneWeight editableBoneWeight) { int channelCount = editableBoneWeight.GetChannelCount(); for (int i = 0; i < channelCount; ++i) { BoneWeightData data = editableBoneWeight.GetBoneWeightData(i); if (!editableBoneWeight.IsChannelEnabled(i)) { continue; } bool weightChanged = false; for (int j = i + 1; j < channelCount; ++j) { BoneWeightData otherData = editableBoneWeight.GetBoneWeightData(j); if (otherData.boneIndex == data.boneIndex) { weightChanged = true; data.weight += otherData.weight; editableBoneWeight.EnableChannel(j, false); } } if (weightChanged) { editableBoneWeight.SetBoneWeightData(i, data); editableBoneWeight.CompensateOtherChannels(i); } } }
public static void CompensateOtherChannels(this EditableBoneWeight editableBoneWeight, int masterChannelIndex) { ValidateChannels(editableBoneWeight); int validChannelCount = 0; float sum = 0f; for (int i = 0; i < editableBoneWeight.GetChannelCount(); ++i) { if (i != masterChannelIndex && editableBoneWeight.IsChannelEnabled(i)) { BoneWeightData data = editableBoneWeight.GetBoneWeightData(i); sum += data.weight; ++validChannelCount; } } if (validChannelCount == 0) { return; } BoneWeightData channelData = editableBoneWeight.GetBoneWeightData(masterChannelIndex); float targetSum = 1f - channelData.weight; for (int i = 0; i < editableBoneWeight.GetChannelCount(); ++i) { if (i != masterChannelIndex && editableBoneWeight.IsChannelEnabled(i)) { BoneWeightData data = editableBoneWeight.GetBoneWeightData(i); if (sum == 0f) { data.weight = targetSum / validChannelCount; } else { data.weight = data.weight * targetSum / sum; } editableBoneWeight.SetBoneWeightData(i, data); } } }
public static void ValidateChannels(this EditableBoneWeight editableBoneWeight) { for (int channelIndex = 0; channelIndex < editableBoneWeight.GetChannelCount(); ++channelIndex) { BoneWeightData data = editableBoneWeight.GetBoneWeightData(channelIndex); if (!editableBoneWeight.IsChannelEnabled(channelIndex)) { data.weight = 0f; } data.weight = Mathf.Clamp01(data.weight); editableBoneWeight.SetBoneWeightData(channelIndex, data); } }
public static float GetWeightSum(this EditableBoneWeight editableBoneWeight) { float sum = 0f; for (int i = 0; i < editableBoneWeight.GetChannelCount(); ++i) { if (editableBoneWeight.IsChannelEnabled(i)) { BoneWeightData data = editableBoneWeight.GetBoneWeightData(i); sum += data.weight; } } return(sum); }
public static int GetChannelFromBoneIndex(this EditableBoneWeight editableBoneWeight, int boneIndex) { for (int i = 0; i < editableBoneWeight.GetChannelCount(); ++i) { if (editableBoneWeight.IsChannelEnabled(i)) { BoneWeightData data = editableBoneWeight.GetBoneWeightData(i); if (data.boneIndex == boneIndex) { return(i); } } } return(-1); }
public static void SmoothWeights(BoneWeight[] boneWeightIn, IList <int> indices, int boneCount, BoneWeight[] boneWeightOut) { Debug.Assert(boneWeightIn != null); Debug.Assert(boneWeightOut != null); Debug.Assert(boneWeightIn != boneWeightOut); Debug.Assert(boneWeightIn.Length == boneWeightOut.Length); PrepareTempBuffers(boneWeightIn.Length, boneCount); EditableBoneWeight editableBoneWeight = new EditableBoneWeight(); for (int i = 0; i < boneWeightIn.Length; ++i) { editableBoneWeight.SetFromBoneWeight(boneWeightIn[i]); for (int channelIndex = 0; channelIndex < editableBoneWeight.GetChannelCount(); ++channelIndex) { if (editableBoneWeight.IsChannelEnabled(channelIndex)) { BoneWeightData boneWeightData = editableBoneWeight.GetBoneWeightData(channelIndex); m_DataInTemp[i, boneWeightData.boneIndex] = boneWeightData.weight; } } } SmoothPerVertexData(indices, m_DataInTemp, m_DataOutTemp); for (int i = 0; i < boneWeightIn.Length; ++i) { editableBoneWeight.Clear(); for (int boneIndex = 0; boneIndex < boneCount; ++boneIndex) { float weight = m_DataOutTemp[i, boneIndex]; int boneIndex2 = weight > 0f ? boneIndex : 0; editableBoneWeight.AddChannel(new BoneWeightData(boneIndex2, weight), weight > 0); } editableBoneWeight.ClampChannels(4); editableBoneWeight.NormalizeChannels(); boneWeightOut[i] = editableBoneWeight.ToBoneWeight(false); } }
public static void NormalizeChannels(this EditableBoneWeight editableBoneWeight) { ValidateChannels(editableBoneWeight); float sum = editableBoneWeight.GetWeightSum(); if (sum == 0f || sum == 1f) { return; } for (int i = 0; i < editableBoneWeight.GetChannelCount(); ++i) { if (editableBoneWeight.IsChannelEnabled(i)) { BoneWeightData data = editableBoneWeight.GetBoneWeightData(i); data.weight = data.weight / sum; editableBoneWeight.SetBoneWeightData(i, data); } } }