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.

MeshPreviewCache.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. namespace UnityEditor.U2D.Animation
  4. {
  5. internal class MeshPreviewCache : SkinningObject
  6. {
  7. [SerializeField]
  8. SpriteCache m_Sprite;
  9. [SerializeField]
  10. Mesh m_Mesh;
  11. [SerializeField]
  12. Mesh m_DefaultMesh;
  13. List<Vector3> m_SkinnedVertices = new List<Vector3>();
  14. List<Vector3> m_Vertices = new List<Vector3>();
  15. List<BoneWeight> m_Weights = new List<BoneWeight>();
  16. List<Vector2> m_TexCoords = new List<Vector2>();
  17. List<Color> m_Colors = new List<Color>();
  18. List<Matrix4x4> m_SkinningMatrices = new List<Matrix4x4>();
  19. bool m_MeshDirty;
  20. bool m_VerticesDirty;
  21. bool m_SkinningDirty;
  22. bool m_WeightsDirty;
  23. bool m_IndicesDirty;
  24. bool m_ColorsDirty;
  25. bool m_EnableSkinning;
  26. public SpriteCache sprite
  27. {
  28. get => m_Sprite;
  29. set
  30. {
  31. m_Sprite = value;
  32. InitializeDefaultMesh();
  33. SetMeshDirty();
  34. }
  35. }
  36. public Mesh mesh => m_Mesh;
  37. public Mesh defaultMesh => m_DefaultMesh;
  38. public bool enableSkinning
  39. {
  40. get => m_EnableSkinning;
  41. set
  42. {
  43. if (m_EnableSkinning != value)
  44. {
  45. m_EnableSkinning = value;
  46. SetSkinningDirty();
  47. }
  48. }
  49. }
  50. public bool canSkin => CanSkin();
  51. public List<Vector3> vertices
  52. {
  53. get
  54. {
  55. if (enableSkinning && canSkin)
  56. return m_SkinnedVertices;
  57. return m_Vertices;
  58. }
  59. }
  60. bool CanSkin()
  61. {
  62. if (m_Vertices.Count == 0 || m_Vertices.Count != m_Weights.Count)
  63. return false;
  64. var bones = sprite.GetBonesFromMode();
  65. Debug.Assert(bones != null);
  66. if (bones.Length == 0)
  67. return false;
  68. foreach (var weight in m_Weights)
  69. {
  70. if (weight.boneIndex0 < 0 || weight.boneIndex0 >= bones.Length ||
  71. weight.boneIndex1 < 0 || weight.boneIndex1 >= bones.Length ||
  72. weight.boneIndex2 < 0 || weight.boneIndex2 >= bones.Length ||
  73. weight.boneIndex3 < 0 || weight.boneIndex3 >= bones.Length)
  74. return false;
  75. }
  76. return true;
  77. }
  78. internal override void OnCreate()
  79. {
  80. m_Mesh = CreateMesh();
  81. m_DefaultMesh = CreateMesh();
  82. }
  83. internal override void OnDestroy()
  84. {
  85. DestroyImmediate(m_Mesh);
  86. DestroyImmediate(m_DefaultMesh);
  87. }
  88. static Mesh CreateMesh()
  89. {
  90. var mesh = new Mesh();
  91. mesh.MarkDynamic();
  92. mesh.hideFlags = HideFlags.DontSave;
  93. return mesh;
  94. }
  95. void InitializeDefaultMesh()
  96. {
  97. Debug.Assert(sprite != null);
  98. Debug.Assert(m_DefaultMesh != null);
  99. var meshCache = sprite.GetMesh();
  100. Debug.Assert(meshCache != null);
  101. meshCache.textureDataProvider.GetTextureActualWidthAndHeight(out var width, out var height);
  102. var uvScale = new Vector2(1f / width, 1f / height);
  103. Vector3 position = sprite.textureRect.position;
  104. var size = sprite.textureRect.size;
  105. var defaultVerts = new List<Vector3>()
  106. {
  107. Vector3.zero,
  108. new Vector3(0f, size.y, 0f),
  109. new Vector3(size.x, 0f, 0f),
  110. size,
  111. };
  112. var uvs = new List<Vector2>()
  113. {
  114. Vector3.Scale(defaultVerts[0] + position, uvScale),
  115. Vector3.Scale(defaultVerts[1] + position, uvScale),
  116. Vector3.Scale(defaultVerts[2] + position, uvScale),
  117. Vector3.Scale(defaultVerts[3] + position, uvScale),
  118. };
  119. m_DefaultMesh.SetVertices(defaultVerts);
  120. m_DefaultMesh.SetUVs(0, uvs);
  121. m_DefaultMesh.SetColors(new List<Color>
  122. {
  123. Color.black,
  124. Color.black,
  125. Color.black,
  126. Color.black
  127. });
  128. m_DefaultMesh.SetIndices(new int[]
  129. {
  130. 0, 1, 3, 0, 3, 2
  131. },
  132. MeshTopology.Triangles, 0);
  133. m_DefaultMesh.UploadMeshData(false);
  134. }
  135. public void SetMeshDirty()
  136. {
  137. m_MeshDirty = true;
  138. }
  139. public void SetSkinningDirty()
  140. {
  141. m_SkinningDirty = true;
  142. }
  143. public void SetWeightsDirty()
  144. {
  145. m_WeightsDirty = true;
  146. }
  147. public void SetColorsDirty()
  148. {
  149. m_ColorsDirty = true;
  150. }
  151. public void Prepare()
  152. {
  153. var meshChanged = false;
  154. var meshCache = sprite.GetMesh();
  155. Debug.Assert(meshCache != null);
  156. m_MeshDirty |= m_Vertices.Count != meshCache.vertices.Length;
  157. if (m_MeshDirty)
  158. {
  159. m_Mesh.Clear();
  160. m_VerticesDirty = true;
  161. m_WeightsDirty = true;
  162. m_IndicesDirty = true;
  163. m_SkinningDirty = true;
  164. m_MeshDirty = false;
  165. }
  166. if (m_VerticesDirty)
  167. {
  168. m_Vertices.Clear();
  169. m_TexCoords.Clear();
  170. meshCache.textureDataProvider.GetTextureActualWidthAndHeight(out var width, out var height);
  171. var uvScale = new Vector2(1f / width, 1f / height);
  172. foreach (var vertex in meshCache.vertices)
  173. {
  174. m_Vertices.Add(vertex);
  175. m_TexCoords.Add(Vector2.Scale(vertex + sprite.textureRect.position, uvScale));
  176. }
  177. m_Mesh.SetVertices(m_Vertices);
  178. m_Mesh.SetUVs(0, m_TexCoords);
  179. meshChanged = true;
  180. m_VerticesDirty = false;
  181. }
  182. if (m_WeightsDirty)
  183. {
  184. m_Weights.Clear();
  185. for (var i = 0; i < meshCache.vertexWeights.Length; ++i)
  186. {
  187. var weight = meshCache.vertexWeights[i];
  188. m_Weights.Add(weight.ToBoneWeight(true));
  189. }
  190. SetColorsDirty();
  191. meshChanged = true;
  192. m_WeightsDirty = false;
  193. }
  194. if (m_ColorsDirty)
  195. {
  196. PrepareColors();
  197. m_Mesh.SetColors(m_Colors);
  198. meshChanged = true;
  199. m_ColorsDirty = false;
  200. }
  201. if (m_IndicesDirty)
  202. {
  203. m_Mesh.SetTriangles(meshCache.indices, 0);
  204. meshChanged = true;
  205. m_IndicesDirty = false;
  206. }
  207. if (m_SkinningDirty)
  208. {
  209. if (enableSkinning && canSkin)
  210. {
  211. SkinVertices();
  212. m_Mesh.SetVertices(m_SkinnedVertices);
  213. meshChanged = true;
  214. }
  215. m_SkinningDirty = false;
  216. }
  217. if (meshChanged)
  218. {
  219. m_Mesh.UploadMeshData(false);
  220. m_Mesh.RecalculateBounds();
  221. skinningCache.events.meshPreviewChanged.Invoke(this);
  222. }
  223. }
  224. void PrepareColors()
  225. {
  226. var bones = sprite.GetBonesFromMode();
  227. Debug.Assert(bones != null);
  228. m_Colors.Clear();
  229. for (var i = 0; i < m_Weights.Count; ++i)
  230. {
  231. var boneWeight = m_Weights[i];
  232. var weightSum = 0f;
  233. var color = Color.black;
  234. for (var j = 0; j < 4; ++j)
  235. {
  236. var boneIndex = boneWeight.GetBoneIndex(j);
  237. var weight = boneWeight.GetWeight(j);
  238. if (boneIndex >= 0 && boneIndex < bones.Length)
  239. color += bones[boneIndex].bindPoseColor * weight;
  240. weightSum += weight;
  241. }
  242. color.a = 1f;
  243. m_Colors.Add(Color.Lerp(Color.black, color, weightSum));
  244. }
  245. }
  246. void SkinVertices()
  247. {
  248. Debug.Assert(canSkin);
  249. Debug.Assert(sprite != null);
  250. var bones = sprite.GetBonesFromMode();
  251. var originMatrix = Matrix4x4.TRS(sprite.pivotRectSpace, Quaternion.identity, Vector3.one);
  252. var originInverseMatrix = originMatrix.inverse;
  253. var spriteMatrix = sprite.GetLocalToWorldMatrixFromMode();
  254. var spriteMatrixInv = spriteMatrix.inverse;
  255. m_SkinnedVertices.Clear();
  256. m_SkinningMatrices.Clear();
  257. for (var i = 0; i < bones.Length; ++i)
  258. m_SkinningMatrices.Add(spriteMatrixInv * originInverseMatrix * bones[i].localToWorldMatrix * bones[i].bindPose.matrix.inverse * spriteMatrix);
  259. for (var i = 0; i < m_Vertices.Count; ++i)
  260. {
  261. var position = m_Vertices[i];
  262. var boneWeight = m_Weights[i];
  263. var weightSum = boneWeight.weight0 + boneWeight.weight1 + boneWeight.weight2 + boneWeight.weight3;
  264. if (weightSum > 0f)
  265. {
  266. var weightSumInv = 1f / weightSum;
  267. var skinnedPosition = m_SkinningMatrices[boneWeight.boneIndex0].MultiplyPoint3x4(position) * boneWeight.weight0 * weightSumInv +
  268. m_SkinningMatrices[boneWeight.boneIndex1].MultiplyPoint3x4(position) * boneWeight.weight1 * weightSumInv +
  269. m_SkinningMatrices[boneWeight.boneIndex2].MultiplyPoint3x4(position) * boneWeight.weight2 * weightSumInv +
  270. m_SkinningMatrices[boneWeight.boneIndex3].MultiplyPoint3x4(position) * boneWeight.weight3 * weightSumInv;
  271. position = Vector3.Lerp(position, skinnedPosition, weightSum);
  272. }
  273. m_SkinnedVertices.Add(position);
  274. }
  275. }
  276. protected override void OnAfterDeserialize()
  277. {
  278. SetMeshDirty();
  279. }
  280. }
  281. }