GenerateWeightsTool.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using UnityEditor.U2D.Layout;
  3. using UnityEngine;
  4. namespace UnityEditor.U2D.Animation
  5. {
  6. internal class GenerateWeightsTool : MeshToolWrapper
  7. {
  8. private const float kWeightTolerance = 0.01f;
  9. private SpriteMeshDataController m_SpriteMeshDataController = new SpriteMeshDataController();
  10. private IWeightsGenerator m_WeightGenerator;
  11. private GenerateWeightsPanel m_GenerateWeightsPanel;
  12. internal override void OnCreate()
  13. {
  14. m_WeightGenerator = new BoundedBiharmonicWeightsGenerator();
  15. }
  16. public override void Initialize(LayoutOverlay layout)
  17. {
  18. base.Initialize(layout);
  19. m_GenerateWeightsPanel = GenerateWeightsPanel.GenerateFromUXML();
  20. layout.rightOverlay.Add(m_GenerateWeightsPanel);
  21. BindElements();
  22. m_GenerateWeightsPanel.SetHiddenFromLayout(true);
  23. }
  24. private void BindElements()
  25. {
  26. Debug.Assert(m_GenerateWeightsPanel != null);
  27. m_GenerateWeightsPanel.onGenerateWeights += () =>
  28. {
  29. HandleWeights(GenerateWeights, TextContent.generateWeights);
  30. };
  31. m_GenerateWeightsPanel.onNormalizeWeights += () =>
  32. {
  33. HandleWeights(NormalizeWeights, TextContent.normalizeWeights);
  34. };
  35. m_GenerateWeightsPanel.onClearWeights += () =>
  36. {
  37. HandleWeights(ClearWeights, TextContent.clearWeights);
  38. };
  39. }
  40. protected override void OnActivate()
  41. {
  42. base.OnActivate();
  43. m_GenerateWeightsPanel.SetHiddenFromLayout(false);
  44. skinningCache.events.skinningModeChanged.AddListener(OnModeChanged);
  45. skinningCache.events.selectedSpriteChanged.AddListener(OnSpriteSelectionChanged);
  46. m_GenerateWeightsPanel.Update(skinningCache.mode == SkinningMode.Character);
  47. OnSpriteSelectionChanged(skinningCache.selectedSprite);
  48. }
  49. protected override void OnDeactivate()
  50. {
  51. base.OnDeactivate();
  52. skinningCache.events.skinningModeChanged.RemoveListener(OnModeChanged);
  53. skinningCache.events.selectedSpriteChanged.RemoveListener(OnSpriteSelectionChanged);
  54. m_GenerateWeightsPanel.SetHiddenFromLayout(true);
  55. }
  56. void OnModeChanged(SkinningMode mode)
  57. {
  58. m_GenerateWeightsPanel.Update(mode == SkinningMode.Character);
  59. }
  60. void OnSpriteSelectionChanged(SpriteCache sprite)
  61. {
  62. m_GenerateWeightsPanel.generateButtonText = sprite != null ? TextContent.generate : TextContent.generateAll;
  63. }
  64. private void HandleWeights(Action<SpriteCache> action, string undoName)
  65. {
  66. using (skinningCache.UndoScope(undoName))
  67. {
  68. var selectedSprite = skinningCache.selectedSprite;
  69. if (selectedSprite != null)
  70. HandleWeightsForSprite(selectedSprite, action);
  71. else
  72. {
  73. var sprites = skinningCache.GetSprites();
  74. foreach (var sprite in sprites)
  75. {
  76. if (sprite.IsVisible())
  77. HandleWeightsForSprite(sprite, action);
  78. }
  79. }
  80. }
  81. }
  82. private void HandleWeightsForSprite(SpriteCache sprite, Action<SpriteCache> action)
  83. {
  84. Debug.Assert(sprite != null);
  85. using (new DefaultPoseScope(skinningCache.GetEffectiveSkeleton(sprite)))
  86. {
  87. action(sprite);
  88. }
  89. skinningCache.events.meshChanged.Invoke(sprite.GetMesh());
  90. }
  91. private void GenerateWeights(SpriteCache sprite)
  92. {
  93. using (m_GenerateWeightsPanel.associateBones ? new AssociateBonesScope(sprite) : null)
  94. {
  95. m_SpriteMeshDataController.spriteMeshData = sprite.GetMesh();
  96. m_SpriteMeshDataController.CalculateWeights(m_WeightGenerator, skinningCache.vertexSelection, kWeightTolerance);
  97. m_SpriteMeshDataController.SortTrianglesByDepth();
  98. }
  99. }
  100. private void NormalizeWeights(SpriteCache sprite)
  101. {
  102. m_SpriteMeshDataController.spriteMeshData = sprite.GetMesh();
  103. m_SpriteMeshDataController.NormalizeWeights(skinningCache.vertexSelection);
  104. m_SpriteMeshDataController.SortTrianglesByDepth();
  105. }
  106. private void ClearWeights(SpriteCache sprite)
  107. {
  108. m_SpriteMeshDataController.spriteMeshData = sprite.GetMesh();
  109. m_SpriteMeshDataController.ClearWeights(skinningCache.vertexSelection);
  110. }
  111. protected override void OnGUI()
  112. {
  113. m_MeshPreviewBehaviour.showWeightMap = true;
  114. m_MeshPreviewBehaviour.overlaySelected = true;
  115. skeletonMode = SkeletonMode.EditPose;
  116. meshMode = SpriteMeshViewMode.EditGeometry;
  117. disableMeshEditor = true;
  118. skeletonTool.skeletonStyle = SkeletonStyles.WeightMap;
  119. DoSkeletonGUI();
  120. DoMeshGUI();
  121. }
  122. }
  123. }