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.

174 lines
6.9 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. namespace TMPro.Examples
  4. {
  5. public class VertexJitter : 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. /// <summary>
  13. /// Structure to hold pre-computed animation data.
  14. /// </summary>
  15. private struct VertexAnim
  16. {
  17. public float angleRange;
  18. public float angle;
  19. public float speed;
  20. }
  21. void Awake()
  22. {
  23. m_TextComponent = GetComponent<TMP_Text>();
  24. }
  25. void OnEnable()
  26. {
  27. // Subscribe to event fired when text object has been regenerated.
  28. TMPro_EventManager.TEXT_CHANGED_EVENT.Add(ON_TEXT_CHANGED);
  29. }
  30. void OnDisable()
  31. {
  32. TMPro_EventManager.TEXT_CHANGED_EVENT.Remove(ON_TEXT_CHANGED);
  33. }
  34. void Start()
  35. {
  36. StartCoroutine(AnimateVertexColors());
  37. }
  38. void ON_TEXT_CHANGED(Object obj)
  39. {
  40. if (obj == m_TextComponent)
  41. hasTextChanged = true;
  42. }
  43. /// <summary>
  44. /// Method to animate vertex colors of a TMP Text object.
  45. /// </summary>
  46. /// <returns></returns>
  47. IEnumerator AnimateVertexColors()
  48. {
  49. // 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.
  50. // Alternatively, we could yield and wait until the end of the frame when the text object will be generated.
  51. m_TextComponent.ForceMeshUpdate();
  52. TMP_TextInfo textInfo = m_TextComponent.textInfo;
  53. Matrix4x4 matrix;
  54. int loopCount = 0;
  55. hasTextChanged = true;
  56. // Create an Array which contains pre-computed Angle Ranges and Speeds for a bunch of characters.
  57. VertexAnim[] vertexAnim = new VertexAnim[1024];
  58. for (int i = 0; i < 1024; i++)
  59. {
  60. vertexAnim[i].angleRange = Random.Range(10f, 25f);
  61. vertexAnim[i].speed = Random.Range(1f, 3f);
  62. }
  63. // Cache the vertex data of the text object as the Jitter FX is applied to the original position of the characters.
  64. TMP_MeshInfo[] cachedMeshInfo = textInfo.CopyMeshInfoVertexData();
  65. while (true)
  66. {
  67. // Get new copy of vertex data if the text has changed.
  68. if (hasTextChanged)
  69. {
  70. // Update the copy of the vertex data for the text object.
  71. cachedMeshInfo = textInfo.CopyMeshInfoVertexData();
  72. hasTextChanged = false;
  73. }
  74. int characterCount = textInfo.characterCount;
  75. // If No Characters then just yield and wait for some text to be added
  76. if (characterCount == 0)
  77. {
  78. yield return new WaitForSeconds(0.25f);
  79. continue;
  80. }
  81. for (int i = 0; i < characterCount; i++)
  82. {
  83. TMP_CharacterInfo charInfo = textInfo.characterInfo[i];
  84. // Skip characters that are not visible and thus have no geometry to manipulate.
  85. if (!charInfo.isVisible)
  86. continue;
  87. // Retrieve the pre-computed animation data for the given character.
  88. VertexAnim vertAnim = vertexAnim[i];
  89. // Get the index of the material used by the current character.
  90. int materialIndex = textInfo.characterInfo[i].materialReferenceIndex;
  91. // Get the index of the first vertex used by this text element.
  92. int vertexIndex = textInfo.characterInfo[i].vertexIndex;
  93. // Get the cached vertices of the mesh used by this text element (character or sprite).
  94. Vector3[] sourceVertices = cachedMeshInfo[materialIndex].vertices;
  95. // Determine the center point of each character at the baseline.
  96. //Vector2 charMidBasline = new Vector2((sourceVertices[vertexIndex + 0].x + sourceVertices[vertexIndex + 2].x) / 2, charInfo.baseLine);
  97. // Determine the center point of each character.
  98. Vector2 charMidBasline = (sourceVertices[vertexIndex + 0] + sourceVertices[vertexIndex + 2]) / 2;
  99. // Need to translate all 4 vertices of each quad to aligned with middle of character / baseline.
  100. // This is needed so the matrix TRS is applied at the origin for each character.
  101. Vector3 offset = charMidBasline;
  102. Vector3[] destinationVertices = textInfo.meshInfo[materialIndex].vertices;
  103. destinationVertices[vertexIndex + 0] = sourceVertices[vertexIndex + 0] - offset;
  104. destinationVertices[vertexIndex + 1] = sourceVertices[vertexIndex + 1] - offset;
  105. destinationVertices[vertexIndex + 2] = sourceVertices[vertexIndex + 2] - offset;
  106. destinationVertices[vertexIndex + 3] = sourceVertices[vertexIndex + 3] - offset;
  107. vertAnim.angle = Mathf.SmoothStep(-vertAnim.angleRange, vertAnim.angleRange, Mathf.PingPong(loopCount / 25f * vertAnim.speed, 1f));
  108. Vector3 jitterOffset = new Vector3(Random.Range(-.25f, .25f), Random.Range(-.25f, .25f), 0);
  109. matrix = Matrix4x4.TRS(jitterOffset * CurveScale, Quaternion.Euler(0, 0, Random.Range(-5f, 5f) * AngleMultiplier), Vector3.one);
  110. destinationVertices[vertexIndex + 0] = matrix.MultiplyPoint3x4(destinationVertices[vertexIndex + 0]);
  111. destinationVertices[vertexIndex + 1] = matrix.MultiplyPoint3x4(destinationVertices[vertexIndex + 1]);
  112. destinationVertices[vertexIndex + 2] = matrix.MultiplyPoint3x4(destinationVertices[vertexIndex + 2]);
  113. destinationVertices[vertexIndex + 3] = matrix.MultiplyPoint3x4(destinationVertices[vertexIndex + 3]);
  114. destinationVertices[vertexIndex + 0] += offset;
  115. destinationVertices[vertexIndex + 1] += offset;
  116. destinationVertices[vertexIndex + 2] += offset;
  117. destinationVertices[vertexIndex + 3] += offset;
  118. vertexAnim[i] = vertAnim;
  119. }
  120. // Push changes into meshes
  121. for (int i = 0; i < textInfo.meshInfo.Length; i++)
  122. {
  123. textInfo.meshInfo[i].mesh.vertices = textInfo.meshInfo[i].vertices;
  124. m_TextComponent.UpdateGeometry(textInfo.meshInfo[i].mesh, i);
  125. }
  126. loopCount += 1;
  127. yield return new WaitForSeconds(0.1f);
  128. }
  129. }
  130. }
  131. }