TextMeshProMotionAnimator.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #if LITMOTION_SUPPORT_TMP
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Runtime.CompilerServices;
  5. using UnityEngine;
  6. using TMPro;
  7. using LitMotion.Collections;
  8. #if UNITY_EDITOR
  9. using UnityEditor;
  10. #endif
  11. namespace LitMotion.Extensions
  12. {
  13. // TODO: optimization
  14. /// <summary>
  15. /// Wrapper class for animating individual characters in TextMeshPro.
  16. /// </summary>
  17. internal sealed class TextMeshProMotionAnimator
  18. {
  19. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
  20. static void Init()
  21. {
  22. #if UNITY_EDITOR
  23. var domainReloadDisabled = EditorSettings.enterPlayModeOptionsEnabled && EditorSettings.enterPlayModeOptions.HasFlag(EnterPlayModeOptions.DisableDomainReload);
  24. if (!domainReloadDisabled && initialized) return;
  25. #else
  26. if (initialized) return;
  27. #endif
  28. PlayerLoopHelper.OnUpdate += UpdateAnimators;
  29. initialized = true;
  30. }
  31. #if UNITY_EDITOR
  32. [InitializeOnLoadMethod]
  33. static void InitEditor()
  34. {
  35. EditorApplication.update += UpdateAnimatorsEditor;
  36. }
  37. #endif
  38. static bool initialized;
  39. static TextMeshProMotionAnimator rootNode;
  40. internal static TextMeshProMotionAnimator Get(TMP_Text text)
  41. {
  42. if (textToAnimator.TryGetValue(text, out var animator))
  43. {
  44. animator.RemoveCompletedMotions();
  45. if (animator.motionHandleList.Length == 0) animator.Reset();
  46. return animator;
  47. }
  48. // get or create animator
  49. animator = rootNode ?? new();
  50. rootNode = animator.nextNode;
  51. animator.nextNode = null;
  52. // set target
  53. animator.target = text;
  54. animator.Reset();
  55. // add to array
  56. if (tail == animators.Length)
  57. {
  58. Array.Resize(ref animators, tail * 2);
  59. }
  60. animators[tail] = animator;
  61. tail++;
  62. // add to dictionary
  63. textToAnimator.Add(text, animator);
  64. return animator;
  65. }
  66. internal static void Return(TextMeshProMotionAnimator animator)
  67. {
  68. animator.nextNode = rootNode;
  69. rootNode = animator;
  70. textToAnimator.Remove(animator.target);
  71. animator.target = null;
  72. }
  73. static readonly Dictionary<TMP_Text, TextMeshProMotionAnimator> textToAnimator = new();
  74. static TextMeshProMotionAnimator[] animators = new TextMeshProMotionAnimator[8];
  75. static int tail;
  76. static void UpdateAnimators()
  77. {
  78. var j = tail - 1;
  79. for (int i = 0; i < animators.Length; i++)
  80. {
  81. var animator = animators[i];
  82. if (animator != null)
  83. {
  84. if (!animator.TryUpdate())
  85. {
  86. Return(animator);
  87. animators[i] = null;
  88. }
  89. else
  90. {
  91. continue;
  92. }
  93. }
  94. while (i < j)
  95. {
  96. var fromTail = animators[j];
  97. if (fromTail != null)
  98. {
  99. if (!fromTail.TryUpdate())
  100. {
  101. Return(fromTail);
  102. animators[j] = null;
  103. j--;
  104. continue;
  105. }
  106. else
  107. {
  108. animators[i] = fromTail;
  109. animators[j] = null;
  110. j--;
  111. goto NEXT_LOOP;
  112. }
  113. }
  114. else
  115. {
  116. j--;
  117. }
  118. }
  119. tail = i;
  120. break;
  121. NEXT_LOOP:
  122. continue;
  123. }
  124. }
  125. #if UNITY_EDITOR
  126. static void UpdateAnimatorsEditor()
  127. {
  128. if (EditorApplication.isPlayingOrWillChangePlaymode || EditorApplication.isCompiling || EditorApplication.isUpdating)
  129. {
  130. return;
  131. }
  132. UpdateAnimators();
  133. }
  134. #endif
  135. internal struct CharInfo
  136. {
  137. public Vector3 position;
  138. public Vector3 scale;
  139. public Quaternion rotation;
  140. public Color color;
  141. }
  142. public TextMeshProMotionAnimator()
  143. {
  144. charInfoArray = new CharInfo[32];
  145. for (int i = 0; i < charInfoArray.Length; i++)
  146. {
  147. charInfoArray[i].color = Color.white;
  148. charInfoArray[i].rotation = Quaternion.identity;
  149. charInfoArray[i].scale = Vector3.one;
  150. charInfoArray[i].position = Vector3.zero;
  151. }
  152. }
  153. TMP_Text target;
  154. internal CharInfo[] charInfoArray;
  155. internal FastListCore<MotionHandle> motionHandleList;
  156. TextMeshProMotionAnimator nextNode;
  157. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  158. public void EnsureCapacity(int length)
  159. {
  160. var prevLength = charInfoArray.Length;
  161. if (length > prevLength)
  162. {
  163. Array.Resize(ref charInfoArray, length);
  164. if (length > prevLength)
  165. {
  166. for (int i = prevLength; i < length; i++)
  167. {
  168. charInfoArray[i].color = new(target.color.r, target.color.g, target.color.b, target.color.a);
  169. charInfoArray[i].rotation = Quaternion.identity;
  170. charInfoArray[i].scale = Vector3.one;
  171. charInfoArray[i].position = Vector3.zero;
  172. }
  173. }
  174. }
  175. }
  176. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  177. public void Update()
  178. {
  179. TryUpdate();
  180. }
  181. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  182. public void SetCharColor(int charIndex, Color color)
  183. {
  184. charInfoArray[charIndex].color = color;
  185. }
  186. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  187. public void SetCharPosition(int charIndex, Vector3 position)
  188. {
  189. charInfoArray[charIndex].position = position;
  190. }
  191. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  192. public void SetCharRotation(int charIndex, Quaternion rotation)
  193. {
  194. charInfoArray[charIndex].rotation = rotation;
  195. }
  196. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  197. public void SetCharScale(int charIndex, Vector3 scale)
  198. {
  199. charInfoArray[charIndex].scale = scale;
  200. }
  201. public void Reset()
  202. {
  203. for (int i = 0; i < charInfoArray.Length; i++)
  204. {
  205. charInfoArray[i].color = new(target.color.r, target.color.g, target.color.b, target.color.a);
  206. charInfoArray[i].rotation = Quaternion.identity;
  207. charInfoArray[i].scale = Vector3.one;
  208. charInfoArray[i].position = Vector3.zero;
  209. }
  210. }
  211. void RemoveCompletedMotions()
  212. {
  213. for (int i = 0; i < motionHandleList.Length; i++)
  214. {
  215. if (!motionHandleList[i].IsActive())
  216. {
  217. motionHandleList.RemoveAtSwapback(i);
  218. i--;
  219. }
  220. }
  221. }
  222. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  223. bool TryUpdate()
  224. {
  225. if (target == null) return false;
  226. RemoveCompletedMotions();
  227. if (motionHandleList.Length == 0) return false;
  228. target.ForceMeshUpdate();
  229. var textInfo = target.textInfo;
  230. EnsureCapacity(textInfo.characterCount);
  231. for (int i = 0; i < textInfo.characterCount; i++)
  232. {
  233. ref var charInfo = ref textInfo.characterInfo[i];
  234. if (!charInfo.isVisible) continue;
  235. var materialIndex = charInfo.materialReferenceIndex;
  236. var vertexIndex = charInfo.vertexIndex;
  237. ref var colors = ref textInfo.meshInfo[materialIndex].colors32;
  238. ref var motionCharInfo = ref charInfoArray[i];
  239. var charColor = motionCharInfo.color;
  240. for (int n = 0; n < 4; n++)
  241. {
  242. colors[vertexIndex + n] = charColor;
  243. }
  244. var verts = textInfo.meshInfo[materialIndex].vertices;
  245. var center = (verts[vertexIndex] + verts[vertexIndex + 2]) * 0.5f;
  246. var charRotation = motionCharInfo.rotation;
  247. var charScale = motionCharInfo.scale;
  248. var charOffset = motionCharInfo.position;
  249. for (int n = 0; n < 4; n++)
  250. {
  251. var vert = verts[vertexIndex + n];
  252. var dir = vert - center;
  253. verts[vertexIndex + n] = center +
  254. charRotation * new Vector3(dir.x * charScale.x, dir.y * charScale.y, dir.z * charScale.z) +
  255. charOffset;
  256. }
  257. }
  258. for (int i = 0; i < textInfo.materialCount; i++)
  259. {
  260. if (textInfo.meshInfo[i].mesh == null) continue;
  261. textInfo.meshInfo[i].mesh.colors32 = textInfo.meshInfo[i].colors32;
  262. textInfo.meshInfo[i].mesh.vertices = textInfo.meshInfo[i].vertices;
  263. target.UpdateGeometry(textInfo.meshInfo[i].mesh, i);
  264. }
  265. return true;
  266. }
  267. }
  268. }
  269. #endif