No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MeshCache.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor.U2D.Sprites;
  4. using UnityEngine;
  5. namespace UnityEditor.U2D.Animation
  6. {
  7. [Serializable]
  8. internal class MeshCache : BaseSpriteMeshData
  9. {
  10. [SerializeField]
  11. List<BoneCache> m_Bones = new List<BoneCache>();
  12. [SerializeField]
  13. SpriteCache m_Sprite;
  14. public override string spriteName => sprite.name;
  15. public override int boneCount => m_Bones.Count;
  16. public override Rect frame => sprite.textureRect;
  17. public ITextureDataProvider textureDataProvider { get; set; }
  18. public SpriteCache sprite
  19. {
  20. get => m_Sprite;
  21. set => m_Sprite = value;
  22. }
  23. public BoneCache[] bones
  24. {
  25. get => m_Bones.ToArray();
  26. set => SetBones(value);
  27. }
  28. public override SpriteBoneData GetBoneData(int index)
  29. {
  30. var worldToLocalMatrix = sprite.worldToLocalMatrix;
  31. //We expect m_Bones to contain character's bones references if character exists. Sprite's skeleton bones otherwise.
  32. if (sprite.skinningCache.hasCharacter)
  33. worldToLocalMatrix = sprite.GetCharacterPart().worldToLocalMatrix;
  34. SpriteBoneData spriteBoneData;
  35. var bone = m_Bones[index];
  36. if (bone == null)
  37. spriteBoneData = new SpriteBoneData();
  38. else
  39. {
  40. spriteBoneData = new SpriteBoneData()
  41. {
  42. parentId = bone.parentBone == null ? -1 : m_Bones.IndexOf(bone.parentBone),
  43. localPosition = bone.localPosition,
  44. localRotation = bone.localRotation,
  45. position = worldToLocalMatrix.MultiplyPoint3x4(bone.position),
  46. endPosition = worldToLocalMatrix.MultiplyPoint3x4(bone.endPosition),
  47. depth = bone.depth,
  48. length = bone.localLength
  49. };
  50. }
  51. return spriteBoneData;
  52. }
  53. public override float GetBoneDepth(int index)
  54. {
  55. return m_Bones[index].depth;
  56. }
  57. public bool ContainsBone(BoneCache bone)
  58. {
  59. return m_Bones.Contains(bone);
  60. }
  61. public void SetCompatibleBoneSet(BoneCache[] boneCache)
  62. {
  63. m_Bones = new List<BoneCache>(boneCache);
  64. }
  65. void SetBones(BoneCache[] boneCache)
  66. {
  67. FixWeights(boneCache);
  68. SetCompatibleBoneSet(boneCache);
  69. }
  70. void FixWeights(BoneCache[] newBones)
  71. {
  72. var newBonesList = new List<BoneCache>(newBones);
  73. var indexMap = new Dictionary<int, int>();
  74. for (var i = 0; i < m_Bones.Count; ++i)
  75. {
  76. var bone = m_Bones[i];
  77. var newIndex = newBonesList.IndexOf(bone);
  78. if (newIndex != -1)
  79. indexMap.Add(i, newIndex);
  80. }
  81. for (var i = 0; i < vertexWeights.Length; ++i)
  82. {
  83. var boneWeight = vertexWeights[i];
  84. for (var m = 0; m < boneWeight.Count; ++m)
  85. {
  86. var boneRemoved = indexMap.TryGetValue(boneWeight[m].boneIndex, out var newIndex) == false;
  87. if (boneRemoved)
  88. {
  89. boneWeight[m].weight = 0f;
  90. boneWeight[m].enabled = false;
  91. }
  92. boneWeight[m].boneIndex = newIndex;
  93. if (boneRemoved)
  94. boneWeight.CompensateOtherChannels(m);
  95. }
  96. boneWeight.UnifyChannelsWithSameBoneIndex();
  97. vertexWeights[i] = boneWeight;
  98. }
  99. }
  100. }
  101. }