MeshPreviewTool.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace UnityEditor.U2D.Animation
  5. {
  6. internal class MeshPreviewTool : BaseTool
  7. {
  8. [SerializeField]
  9. private Material m_Material;
  10. private List<SpriteCache> m_Sprites;
  11. private IMeshPreviewBehaviour m_DefaultPreviewBehaviour = new DefaultPreviewBehaviour();
  12. public IMeshPreviewBehaviour previewBehaviourOverride { get; set; }
  13. public override IMeshPreviewBehaviour previewBehaviour
  14. {
  15. get
  16. {
  17. if (previewBehaviourOverride != null)
  18. return previewBehaviourOverride;
  19. return m_DefaultPreviewBehaviour;
  20. }
  21. }
  22. internal override void OnCreate()
  23. {
  24. m_Material = new Material(Shader.Find("Hidden/SkinningModule-GUITextureClip"));
  25. m_Material.hideFlags = HideFlags.DontSave;
  26. }
  27. internal override void OnDestroy()
  28. {
  29. base.OnDestroy();
  30. Debug.Assert(m_Material != null);
  31. DestroyImmediate(m_Material);
  32. }
  33. protected override void OnActivate()
  34. {
  35. m_Sprites = new List<SpriteCache>(skinningCache.GetSprites());
  36. DirtyMeshesAll();
  37. skinningCache.events.meshChanged.AddListener(MeshChanged);
  38. skinningCache.events.characterPartChanged.AddListener(CharacterPartChanged);
  39. skinningCache.events.skeletonPreviewPoseChanged.AddListener(SkeletonChanged);
  40. skinningCache.events.skeletonBindPoseChanged.AddListener(SkeletonChanged);
  41. skinningCache.events.skinningModeChanged.AddListener(SkinningModuleModeChanged);
  42. }
  43. protected override void OnDeactivate()
  44. {
  45. skinningCache.events.meshChanged.RemoveListener(MeshChanged);
  46. skinningCache.events.skeletonPreviewPoseChanged.RemoveListener(SkeletonChanged);
  47. skinningCache.events.skeletonBindPoseChanged.RemoveListener(SkeletonChanged);
  48. skinningCache.events.skinningModeChanged.RemoveListener(SkinningModuleModeChanged);
  49. }
  50. protected override void OnGUI()
  51. {
  52. Prepare();
  53. if (Event.current.type == EventType.Repaint)
  54. {
  55. //DrawDefaultSpriteMeshes();
  56. DrawSpriteMeshes();
  57. }
  58. }
  59. public void DrawOverlay()
  60. {
  61. if (Event.current.type != EventType.Repaint)
  62. return;
  63. if (skinningCache.mode == SkinningMode.SpriteSheet)
  64. {
  65. foreach (var sprite in m_Sprites)
  66. {
  67. if (previewBehaviour.Overlay(sprite))
  68. DrawSpriteMesh(sprite);
  69. }
  70. }
  71. else
  72. {
  73. var character = skinningCache.character;
  74. Debug.Assert(character != null);
  75. var parts = character.parts;
  76. foreach (var part in parts)
  77. {
  78. if (part.isVisible && previewBehaviour.Overlay(part.sprite))
  79. DrawSpriteMesh(part.sprite);
  80. }
  81. }
  82. }
  83. public void OverlayWireframe()
  84. {
  85. if (Event.current.type != EventType.Repaint)
  86. return;
  87. foreach (var sprite in m_Sprites)
  88. if (previewBehaviour.OverlayWireframe(sprite))
  89. DrawWireframe(sprite);
  90. }
  91. private void CharacterPartChanged(CharacterPartCache characterPart)
  92. {
  93. var meshPreview = characterPart.sprite.GetMeshPreview();
  94. Debug.Assert(meshPreview != null);
  95. meshPreview.SetSkinningDirty();
  96. }
  97. private void MeshChanged(MeshCache mesh)
  98. {
  99. var meshPreview = mesh.sprite.GetMeshPreview();
  100. Debug.Assert(meshPreview != null);
  101. meshPreview.SetMeshDirty();
  102. }
  103. private void SkeletonChanged(SkeletonCache skeleton)
  104. {
  105. DirtySkinningAll();
  106. }
  107. private void SkinningModuleModeChanged(SkinningMode mode)
  108. {
  109. DirtyMeshesAll();
  110. }
  111. private void DirtyMeshesAll()
  112. {
  113. foreach (var sprite in m_Sprites)
  114. {
  115. var meshPreview = sprite.GetMeshPreview();
  116. if (meshPreview != null)
  117. meshPreview.SetMeshDirty();
  118. }
  119. }
  120. private void DirtySkinningAll()
  121. {
  122. foreach (var sprite in m_Sprites)
  123. {
  124. var meshPreview = sprite.GetMeshPreview();
  125. Debug.Assert(meshPreview != null);
  126. meshPreview.SetSkinningDirty();
  127. }
  128. }
  129. private void Prepare()
  130. {
  131. foreach (var sprite in m_Sprites)
  132. {
  133. var meshPreview = sprite.GetMeshPreview();
  134. Debug.Assert(meshPreview != null);
  135. meshPreview.enableSkinning = true;
  136. meshPreview.Prepare();
  137. }
  138. }
  139. private void DrawDefaultSpriteMeshes()
  140. {
  141. Debug.Assert(Event.current.type == EventType.Repaint);
  142. if (skinningCache.mode == SkinningMode.SpriteSheet)
  143. {
  144. foreach (var sprite in m_Sprites)
  145. DrawDefaultSpriteMesh(sprite);
  146. }
  147. else
  148. {
  149. var character = skinningCache.character;
  150. Debug.Assert(character != null);
  151. var parts = character.parts;
  152. foreach (var part in parts)
  153. {
  154. if (part.isVisible)
  155. DrawDefaultSpriteMesh(part.sprite);
  156. }
  157. }
  158. }
  159. private void DrawDefaultSpriteMesh(SpriteCache sprite)
  160. {
  161. Debug.Assert(m_Material != null);
  162. var meshPreview = sprite.GetMeshPreview();
  163. var meshCache = sprite.GetMesh();
  164. var skeleton = skinningCache.GetEffectiveSkeleton(sprite);
  165. Debug.Assert(meshPreview != null);
  166. if (meshPreview.canSkin == false || skeleton.isPosePreview == false)
  167. {
  168. m_Material.mainTexture = meshCache.textureDataProvider.texture;
  169. m_Material.SetFloat("_Opacity", 1f);
  170. m_Material.SetFloat("_VertexColorBlend", 0f);
  171. m_Material.color = new Color(1f, 1f, 1f, 1f);
  172. DrawingUtility.DrawMesh(meshPreview.defaultMesh, m_Material, sprite.GetLocalToWorldMatrixFromMode());
  173. }
  174. }
  175. private void DrawSpriteMeshes()
  176. {
  177. Debug.Assert(Event.current.type == EventType.Repaint);
  178. if (skinningCache.mode == SkinningMode.SpriteSheet)
  179. {
  180. foreach (var sprite in m_Sprites)
  181. {
  182. if (previewBehaviour.Overlay(sprite))
  183. continue;
  184. DrawSpriteMesh(sprite);
  185. }
  186. }
  187. else
  188. {
  189. var character = skinningCache.character;
  190. Debug.Assert(character != null);
  191. var parts = character.parts;
  192. var selected = skinningCache.selectedSprite;
  193. var selectedVisible = false;
  194. foreach (var part in parts)
  195. {
  196. if (previewBehaviour.Overlay(part.sprite))
  197. continue;
  198. if (part.sprite == selected)
  199. selectedVisible = part.isVisible;
  200. else if (part.isVisible)
  201. DrawSpriteMesh(part.sprite);
  202. }
  203. if (selectedVisible && selected != null)
  204. DrawSpriteMesh(selected);
  205. }
  206. }
  207. private void DrawSpriteMesh(SpriteCache sprite)
  208. {
  209. var weightMapOpacity = previewBehaviour.GetWeightMapOpacity(sprite);
  210. DrawSpriteMesh(sprite, weightMapOpacity);
  211. if (previewBehaviour.DrawWireframe(sprite))
  212. DrawWireframe(sprite);
  213. }
  214. private void DrawSpriteMesh(SpriteCache sprite, float weightMapOpacity)
  215. {
  216. Debug.Assert(m_Material != null);
  217. var meshPreview = sprite.GetMeshPreview();
  218. var meshCache = sprite.GetMesh();
  219. Debug.Assert(meshPreview != null);
  220. if (meshPreview.mesh == null || meshPreview.mesh.vertexCount == 0)
  221. {
  222. DrawDefaultSpriteMesh(sprite);
  223. }
  224. else
  225. {
  226. m_Material.mainTexture = meshCache.textureDataProvider.texture;
  227. m_Material.SetFloat("_Opacity", 1f);
  228. m_Material.SetFloat("_VertexColorBlend", weightMapOpacity);
  229. m_Material.color = Color.white;
  230. DrawingUtility.DrawMesh(meshPreview.mesh, m_Material, sprite.GetLocalToWorldMatrixFromMode());
  231. }
  232. }
  233. private void DrawSelectedSpriteWeightMap()
  234. {
  235. var selectedSprite = skinningCache.selectedSprite;
  236. if (selectedSprite != null)
  237. {
  238. var opacity = GetWeightOpacityFromCurrentTool();
  239. if (opacity > 0f)
  240. DrawSpriteMesh(selectedSprite, opacity);
  241. }
  242. }
  243. private float GetWeightOpacityFromCurrentTool()
  244. {
  245. return IsWeightTool() ? VisibilityToolSettings.meshOpacity : 0f;
  246. }
  247. private bool IsWeightTool()
  248. {
  249. var currentTool = skinningCache.selectedTool;
  250. if (currentTool == skinningCache.GetTool(Tools.WeightSlider) ||
  251. currentTool == skinningCache.GetTool(Tools.WeightBrush) ||
  252. currentTool == skinningCache.GetTool(Tools.BoneInfluence) ||
  253. currentTool == skinningCache.GetTool(Tools.GenerateWeights))
  254. return true;
  255. return false;
  256. }
  257. private void DrawWireframe(SpriteCache sprite)
  258. {
  259. Debug.Assert(Event.current.type == EventType.Repaint);
  260. Debug.Assert(sprite != null);
  261. var meshPreview = sprite.GetMeshPreview();
  262. Debug.Assert(meshPreview != null);
  263. m_Material.mainTexture = null;
  264. m_Material.SetFloat("_Opacity", 0.35f);
  265. m_Material.SetFloat("_VertexColorBlend", 0f);
  266. m_Material.color = Color.white;
  267. GL.wireframe = true;
  268. DrawingUtility.DrawMesh(meshPreview.mesh, m_Material, sprite.GetLocalToWorldMatrixFromMode());
  269. GL.wireframe = false;
  270. }
  271. }
  272. }