VertexShakeB.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace TMPro.Examples
  4. {
  5. public class VertexShakeB : MonoBehaviour
  6. {
  7. public float AngleMultiplier = 1.0f;
  8. public float SpeedMultiplier = 1.0f;
  9. public float CurveScale = 1.0f;
  10. private TMP_Text m_TextComponent;
  11. private bool hasTextChanged;
  12. void Awake()
  13. {
  14. m_TextComponent = GetComponent<TMP_Text>();
  15. }
  16. void OnEnable()
  17. {
  18. // Subscribe to event fired when text object has been regenerated.
  19. TMPro_EventManager.TEXT_CHANGED_EVENT.Add(ON_TEXT_CHANGED);
  20. }
  21. void OnDisable()
  22. {
  23. TMPro_EventManager.TEXT_CHANGED_EVENT.Remove(ON_TEXT_CHANGED);
  24. }
  25. void Start()
  26. {
  27. StartCoroutine(AnimateVertexColors());
  28. }
  29. void ON_TEXT_CHANGED(Object obj)
  30. {
  31. if (obj = m_TextComponent)
  32. hasTextChanged = true;
  33. }
  34. /// <summary>
  35. /// Method to animate vertex colors of a TMP Text object.
  36. /// </summary>
  37. /// <returns></returns>
  38. IEnumerator AnimateVertexColors()
  39. {
  40. // We force an update of the text object since it would only be updated at the end of the frame. Ie. before this code is executed on the first frame.
  41. // Alternatively, we could yield and wait until the end of the frame when the text object will be generated.
  42. m_TextComponent.ForceMeshUpdate();
  43. TMP_TextInfo textInfo = m_TextComponent.textInfo;
  44. Matrix4x4 matrix;
  45. Vector3[][] copyOfVertices = new Vector3[0][];
  46. hasTextChanged = true;
  47. while (true)
  48. {
  49. // Allocate new vertices
  50. if (hasTextChanged)
  51. {
  52. if (copyOfVertices.Length < textInfo.meshInfo.Length)
  53. copyOfVertices = new Vector3[textInfo.meshInfo.Length][];
  54. for (int i = 0; i < textInfo.meshInfo.Length; i++)
  55. {
  56. int length = textInfo.meshInfo[i].vertices.Length;
  57. copyOfVertices[i] = new Vector3[length];
  58. }
  59. hasTextChanged = false;
  60. }
  61. int characterCount = textInfo.characterCount;
  62. // If No Characters then just yield and wait for some text to be added
  63. if (characterCount == 0)
  64. {
  65. yield return new WaitForSeconds(0.25f);
  66. continue;
  67. }
  68. int lineCount = textInfo.lineCount;
  69. // Iterate through each line of the text.
  70. for (int i = 0; i < lineCount; i++)
  71. {
  72. int first = textInfo.lineInfo[i].firstCharacterIndex;
  73. int last = textInfo.lineInfo[i].lastCharacterIndex;
  74. // Determine the center of each line
  75. Vector3 centerOfLine = (textInfo.characterInfo[first].bottomLeft + textInfo.characterInfo[last].topRight) / 2;
  76. Quaternion rotation = Quaternion.Euler(0, 0, Random.Range(-0.25f, 0.25f));
  77. // Iterate through each character of the line.
  78. for (int j = first; j <= last; j++)
  79. {
  80. // Skip characters that are not visible and thus have no geometry to manipulate.
  81. if (!textInfo.characterInfo[j].isVisible)
  82. continue;
  83. // Get the index of the material used by the current character.
  84. int materialIndex = textInfo.characterInfo[j].materialReferenceIndex;
  85. // Get the index of the first vertex used by this text element.
  86. int vertexIndex = textInfo.characterInfo[j].vertexIndex;
  87. // Get the vertices of the mesh used by this text element (character or sprite).
  88. Vector3[] sourceVertices = textInfo.meshInfo[materialIndex].vertices;
  89. // Determine the center point of each character at the baseline.
  90. Vector3 charCenter = (sourceVertices[vertexIndex + 0] + sourceVertices[vertexIndex + 2]) / 2;
  91. // Need to translate all 4 vertices of each quad to aligned with center of character.
  92. // This is needed so the matrix TRS is applied at the origin for each character.
  93. copyOfVertices[materialIndex][vertexIndex + 0] = sourceVertices[vertexIndex + 0] - charCenter;
  94. copyOfVertices[materialIndex][vertexIndex + 1] = sourceVertices[vertexIndex + 1] - charCenter;
  95. copyOfVertices[materialIndex][vertexIndex + 2] = sourceVertices[vertexIndex + 2] - charCenter;
  96. copyOfVertices[materialIndex][vertexIndex + 3] = sourceVertices[vertexIndex + 3] - charCenter;
  97. // Determine the random scale change for each character.
  98. float randomScale = Random.Range(0.95f, 1.05f);
  99. // Setup the matrix for the scale change.
  100. matrix = Matrix4x4.TRS(Vector3.one, Quaternion.identity, Vector3.one * randomScale);
  101. // Apply the scale change relative to the center of each character.
  102. copyOfVertices[materialIndex][vertexIndex + 0] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 0]);
  103. copyOfVertices[materialIndex][vertexIndex + 1] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 1]);
  104. copyOfVertices[materialIndex][vertexIndex + 2] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 2]);
  105. copyOfVertices[materialIndex][vertexIndex + 3] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 3]);
  106. // Revert the translation change.
  107. copyOfVertices[materialIndex][vertexIndex + 0] += charCenter;
  108. copyOfVertices[materialIndex][vertexIndex + 1] += charCenter;
  109. copyOfVertices[materialIndex][vertexIndex + 2] += charCenter;
  110. copyOfVertices[materialIndex][vertexIndex + 3] += charCenter;
  111. // Need to translate all 4 vertices of each quad to aligned with the center of the line.
  112. // This is needed so the matrix TRS is applied from the center of the line.
  113. copyOfVertices[materialIndex][vertexIndex + 0] -= centerOfLine;
  114. copyOfVertices[materialIndex][vertexIndex + 1] -= centerOfLine;
  115. copyOfVertices[materialIndex][vertexIndex + 2] -= centerOfLine;
  116. copyOfVertices[materialIndex][vertexIndex + 3] -= centerOfLine;
  117. // Setup the matrix rotation.
  118. matrix = Matrix4x4.TRS(Vector3.one, rotation, Vector3.one);
  119. // Apply the matrix TRS to the individual characters relative to the center of the current line.
  120. copyOfVertices[materialIndex][vertexIndex + 0] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 0]);
  121. copyOfVertices[materialIndex][vertexIndex + 1] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 1]);
  122. copyOfVertices[materialIndex][vertexIndex + 2] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 2]);
  123. copyOfVertices[materialIndex][vertexIndex + 3] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 3]);
  124. // Revert the translation change.
  125. copyOfVertices[materialIndex][vertexIndex + 0] += centerOfLine;
  126. copyOfVertices[materialIndex][vertexIndex + 1] += centerOfLine;
  127. copyOfVertices[materialIndex][vertexIndex + 2] += centerOfLine;
  128. copyOfVertices[materialIndex][vertexIndex + 3] += centerOfLine;
  129. }
  130. }
  131. // Push changes into meshes
  132. for (int i = 0; i < textInfo.meshInfo.Length; i++)
  133. {
  134. textInfo.meshInfo[i].mesh.vertices = copyOfVertices[i];
  135. m_TextComponent.UpdateGeometry(textInfo.meshInfo[i].mesh, i);
  136. }
  137. yield return new WaitForSeconds(0.1f);
  138. }
  139. }
  140. }
  141. }