SpriteSkinBatch.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #if ENABLE_ANIMATION_COLLECTION && ENABLE_ANIMATION_BURST
  2. #define ENABLE_SPRITESKIN_COMPOSITE
  3. #endif
  4. using System;
  5. using Unity.Collections;
  6. using Unity.Collections.LowLevel.Unsafe;
  7. namespace UnityEngine.U2D.Animation
  8. {
  9. public sealed partial class SpriteSkin : MonoBehaviour
  10. #if ENABLE_SPRITESKIN_COMPOSITE
  11. {
  12. int m_TransformId;
  13. NativeArray<int> m_BoneTransformId;
  14. int m_RootBoneTransformId;
  15. NativeCustomSlice<Vector2> m_SpriteUVs;
  16. NativeCustomSlice<Vector3> m_SpriteVertices;
  17. NativeCustomSlice<Vector4> m_SpriteTangents;
  18. NativeCustomSlice<BoneWeight> m_SpriteBoneWeights;
  19. NativeCustomSlice<Matrix4x4> m_SpriteBindPoses;
  20. NativeCustomSlice<int> m_BoneTransformIdNativeSlice;
  21. bool m_SpriteHasTangents;
  22. int m_SpriteVertexStreamSize;
  23. int m_SpriteVertexCount;
  24. int m_SpriteTangentVertexOffset;
  25. int m_DataIndex = -1;
  26. bool m_BoneCacheUpdateToDate = false;
  27. void OnEnableBatch()
  28. {
  29. m_TransformId = gameObject.transform.GetInstanceID();
  30. UpdateSpriteDeform();
  31. if (m_UseBatching && m_BatchSkinning == false)
  32. {
  33. CacheBoneTransformIds(true);
  34. SpriteSkinComposite.instance.AddSpriteSkin(this);
  35. m_BatchSkinning = true;
  36. }
  37. else
  38. SpriteSkinComposite.instance.AddSpriteSkinForLateUpdate(this);
  39. }
  40. void OnResetBatch()
  41. {
  42. if (m_UseBatching)
  43. {
  44. CacheBoneTransformIds(true);
  45. SpriteSkinComposite.instance.CopyToSpriteSkinData(this);
  46. }
  47. }
  48. void OnDisableBatch()
  49. {
  50. RemoveTransformFromSpriteSkinComposite();
  51. SpriteSkinComposite.instance.RemoveSpriteSkin(this);
  52. SpriteSkinComposite.instance.RemoveSpriteSkinForLateUpdate(this);
  53. m_BatchSkinning = false;
  54. }
  55. internal void UpdateSpriteDeform()
  56. {
  57. if (sprite == null)
  58. {
  59. m_SpriteUVs = NativeCustomSlice<Vector2>.Default();
  60. m_SpriteVertices = NativeCustomSlice<Vector3>.Default();
  61. m_SpriteTangents = NativeCustomSlice<Vector4>.Default();
  62. m_SpriteBoneWeights = NativeCustomSlice<BoneWeight>.Default();
  63. m_SpriteBindPoses = NativeCustomSlice<Matrix4x4>.Default();
  64. m_SpriteHasTangents = false;
  65. m_SpriteVertexStreamSize = 0;
  66. m_SpriteVertexCount = 0;
  67. m_SpriteTangentVertexOffset = 0;
  68. }
  69. else
  70. {
  71. m_SpriteUVs = new NativeCustomSlice<Vector2>(sprite.GetVertexAttribute<Vector2>(UnityEngine.Rendering.VertexAttribute.TexCoord0));
  72. m_SpriteVertices = new NativeCustomSlice<Vector3>(sprite.GetVertexAttribute<Vector3>(UnityEngine.Rendering.VertexAttribute.Position));
  73. m_SpriteTangents = new NativeCustomSlice<Vector4>(sprite.GetVertexAttribute<Vector4>(UnityEngine.Rendering.VertexAttribute.Tangent));
  74. m_SpriteBoneWeights = new NativeCustomSlice<BoneWeight>(sprite.GetVertexAttribute<BoneWeight>(UnityEngine.Rendering.VertexAttribute.BlendWeight));
  75. m_SpriteBindPoses = new NativeCustomSlice<Matrix4x4>(sprite.GetBindPoses());
  76. m_SpriteHasTangents = sprite.HasVertexAttribute(Rendering.VertexAttribute.Tangent);
  77. m_SpriteVertexStreamSize = sprite.GetVertexStreamSize();
  78. m_SpriteVertexCount = sprite.GetVertexCount();
  79. m_SpriteTangentVertexOffset = sprite.GetVertexStreamOffset(Rendering.VertexAttribute.Tangent);
  80. }
  81. SpriteSkinComposite.instance.CopyToSpriteSkinData(this);
  82. }
  83. void CacheBoneTransformIds(bool forceUpdate = false)
  84. {
  85. if (!m_BoneCacheUpdateToDate || forceUpdate)
  86. {
  87. SpriteSkinComposite.instance.RemoveTransformById(m_RootBoneTransformId);
  88. if (rootBone != null)
  89. {
  90. m_RootBoneTransformId = rootBone.GetInstanceID();
  91. if (this.enabled)
  92. SpriteSkinComposite.instance.AddSpriteSkinRootBoneTransform(this);
  93. }
  94. else
  95. m_RootBoneTransformId = 0;
  96. if (boneTransforms != null)
  97. {
  98. int boneCount = 0;
  99. for (int i = 0; i < boneTransforms.Length; ++i)
  100. {
  101. if (boneTransforms[i] != null)
  102. ++boneCount;
  103. }
  104. if (m_BoneTransformId.IsCreated)
  105. {
  106. for (int i = 0; i < m_BoneTransformId.Length; ++i)
  107. SpriteSkinComposite.instance.RemoveTransformById(m_BoneTransformId[i]);
  108. NativeArrayHelpers.ResizeIfNeeded(ref m_BoneTransformId, boneCount);
  109. }
  110. else
  111. {
  112. m_BoneTransformId = new NativeArray<int>(boneCount, Allocator.Persistent);
  113. }
  114. m_BoneTransformIdNativeSlice = new NativeCustomSlice<int>(m_BoneTransformId);
  115. for (int i = 0, j = 0; i < boneTransforms.Length; ++i)
  116. {
  117. if (boneTransforms[i] != null)
  118. {
  119. m_BoneTransformId[j] = boneTransforms[i].GetInstanceID();
  120. ++j;
  121. }
  122. }
  123. if (this.enabled)
  124. {
  125. SpriteSkinComposite.instance.AddSpriteSkinBoneTransform(this);
  126. }
  127. }
  128. else
  129. {
  130. if (m_BoneTransformId.IsCreated)
  131. NativeArrayHelpers.ResizeIfNeeded(ref m_BoneTransformId, 0);
  132. else
  133. m_BoneTransformId = new NativeArray<int>(0, Allocator.Persistent);
  134. }
  135. CacheValidFlag();
  136. m_BoneCacheUpdateToDate = true;
  137. SpriteSkinComposite.instance.CopyToSpriteSkinData(this);
  138. }
  139. }
  140. void UseBatchingBatch()
  141. {
  142. if (!this.enabled)
  143. return;
  144. if (m_UseBatching)
  145. {
  146. m_BatchSkinning = true;
  147. CacheBoneTransformIds();
  148. SpriteSkinComposite.instance.AddSpriteSkin(this);
  149. SpriteSkinComposite.instance.RemoveSpriteSkinForLateUpdate(this);
  150. }
  151. else
  152. {
  153. SpriteSkinComposite.instance.RemoveSpriteSkin(this);
  154. SpriteSkinComposite.instance.AddSpriteSkinForLateUpdate(this);
  155. RemoveTransformFromSpriteSkinComposite();
  156. m_BatchSkinning = false;
  157. }
  158. }
  159. void RemoveTransformFromSpriteSkinComposite()
  160. {
  161. if (m_BoneTransformId.IsCreated)
  162. {
  163. for (int i = 0; i < m_BoneTransformId.Length; ++i)
  164. SpriteSkinComposite.instance.RemoveTransformById(m_BoneTransformId[i]);
  165. m_BoneTransformId.Dispose();
  166. }
  167. SpriteSkinComposite.instance.RemoveTransformById(m_RootBoneTransformId);
  168. m_RootBoneTransformId = -1;
  169. m_BoneCacheUpdateToDate = false;
  170. }
  171. internal void CopyToSpriteSkinData(ref SpriteSkinData data, int spriteSkinIndex)
  172. {
  173. CacheBoneTransformIds();
  174. CacheCurrentSprite();
  175. data.vertices = m_SpriteVertices;
  176. data.boneWeights = m_SpriteBoneWeights;
  177. data.bindPoses = m_SpriteBindPoses;
  178. data.tangents = m_SpriteTangents;
  179. data.hasTangents = m_SpriteHasTangents;
  180. data.spriteVertexStreamSize = m_SpriteVertexStreamSize;
  181. data.spriteVertexCount = m_SpriteVertexCount;
  182. data.tangentVertexOffset = m_SpriteTangentVertexOffset;
  183. data.transformId = m_TransformId;
  184. data.boneTransformId = m_BoneTransformIdNativeSlice;
  185. m_DataIndex = spriteSkinIndex;
  186. }
  187. internal bool NeedUpdateCompositeCache()
  188. {
  189. unsafe
  190. {
  191. var iptr = new IntPtr(sprite.GetVertexAttribute<Vector2>(UnityEngine.Rendering.VertexAttribute.TexCoord0).GetUnsafeReadOnlyPtr());
  192. var rs = m_SpriteUVs.data != iptr;
  193. if (rs)
  194. {
  195. UpdateSpriteDeform();
  196. }
  197. return rs;
  198. }
  199. }
  200. internal bool BatchValidate()
  201. {
  202. CacheBoneTransformIds();
  203. CacheCurrentSprite();
  204. return (m_IsValid && spriteRenderer.enabled && (alwaysUpdate || spriteRenderer.isVisible));
  205. }
  206. void OnBoneTransformChanged()
  207. {
  208. if (this.enabled)
  209. {
  210. CacheBoneTransformIds(true);
  211. }
  212. }
  213. void OnRootBoneTransformChanged()
  214. {
  215. if (this.enabled)
  216. {
  217. CacheBoneTransformIds(true);
  218. }
  219. }
  220. void OnBeforeSerializeBatch()
  221. {}
  222. void OnAfterSerializeBatch()
  223. {
  224. #if UNITY_EDITOR
  225. m_BoneCacheUpdateToDate = false;
  226. #endif
  227. }
  228. }
  229. #else
  230. {
  231. void OnEnableBatch(){}
  232. internal void UpdateSpriteDeform(){}
  233. void OnResetBatch(){}
  234. void UseBatchingBatch(){}
  235. void OnDisableBatch(){}
  236. void OnBoneTransformChanged(){}
  237. void OnRootBoneTransformChanged(){}
  238. void OnBeforeSerializeBatch(){}
  239. void OnAfterSerializeBatch(){}
  240. }
  241. #endif
  242. }