VertexShakeA.cs 6.7 KB

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