Global Game Jam 2022
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.

191 lines
8.3 KiB

  1. using UnityEngine;
  2. using System.Linq;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace TMPro.Examples
  6. {
  7. public class VertexZoom : MonoBehaviour
  8. {
  9. public float AngleMultiplier = 1.0f;
  10. public float SpeedMultiplier = 1.0f;
  11. public float CurveScale = 1.0f;
  12. private TMP_Text m_TextComponent;
  13. private bool hasTextChanged;
  14. void Awake()
  15. {
  16. m_TextComponent = GetComponent<TMP_Text>();
  17. }
  18. void OnEnable()
  19. {
  20. // Subscribe to event fired when text object has been regenerated.
  21. TMPro_EventManager.TEXT_CHANGED_EVENT.Add(ON_TEXT_CHANGED);
  22. }
  23. void OnDisable()
  24. {
  25. // UnSubscribe to event fired when text object has been regenerated.
  26. TMPro_EventManager.TEXT_CHANGED_EVENT.Remove(ON_TEXT_CHANGED);
  27. }
  28. void Start()
  29. {
  30. StartCoroutine(AnimateVertexColors());
  31. }
  32. void ON_TEXT_CHANGED(Object obj)
  33. {
  34. if (obj == m_TextComponent)
  35. hasTextChanged = true;
  36. }
  37. /// <summary>
  38. /// Method to animate vertex colors of a TMP Text object.
  39. /// </summary>
  40. /// <returns></returns>
  41. IEnumerator AnimateVertexColors()
  42. {
  43. // 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.
  44. // Alternatively, we could yield and wait until the end of the frame when the text object will be generated.
  45. m_TextComponent.ForceMeshUpdate();
  46. TMP_TextInfo textInfo = m_TextComponent.textInfo;
  47. Matrix4x4 matrix;
  48. TMP_MeshInfo[] cachedMeshInfoVertexData = textInfo.CopyMeshInfoVertexData();
  49. // Allocations for sorting of the modified scales
  50. List<float> modifiedCharScale = new List<float>();
  51. List<int> scaleSortingOrder = new List<int>();
  52. hasTextChanged = true;
  53. while (true)
  54. {
  55. // Allocate new vertices
  56. if (hasTextChanged)
  57. {
  58. // Get updated vertex data
  59. cachedMeshInfoVertexData = textInfo.CopyMeshInfoVertexData();
  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. // Clear list of character scales
  70. modifiedCharScale.Clear();
  71. scaleSortingOrder.Clear();
  72. for (int i = 0; i < characterCount; i++)
  73. {
  74. TMP_CharacterInfo charInfo = textInfo.characterInfo[i];
  75. // Skip characters that are not visible and thus have no geometry to manipulate.
  76. if (!charInfo.isVisible)
  77. continue;
  78. // Get the index of the material used by the current character.
  79. int materialIndex = textInfo.characterInfo[i].materialReferenceIndex;
  80. // Get the index of the first vertex used by this text element.
  81. int vertexIndex = textInfo.characterInfo[i].vertexIndex;
  82. // Get the cached vertices of the mesh used by this text element (character or sprite).
  83. Vector3[] sourceVertices = cachedMeshInfoVertexData[materialIndex].vertices;
  84. // Determine the center point of each character at the baseline.
  85. //Vector2 charMidBasline = new Vector2((sourceVertices[vertexIndex + 0].x + sourceVertices[vertexIndex + 2].x) / 2, charInfo.baseLine);
  86. // Determine the center point of each character.
  87. Vector2 charMidBasline = (sourceVertices[vertexIndex + 0] + sourceVertices[vertexIndex + 2]) / 2;
  88. // Need to translate all 4 vertices of each quad to aligned with middle of character / baseline.
  89. // This is needed so the matrix TRS is applied at the origin for each character.
  90. Vector3 offset = charMidBasline;
  91. Vector3[] destinationVertices = textInfo.meshInfo[materialIndex].vertices;
  92. destinationVertices[vertexIndex + 0] = sourceVertices[vertexIndex + 0] - offset;
  93. destinationVertices[vertexIndex + 1] = sourceVertices[vertexIndex + 1] - offset;
  94. destinationVertices[vertexIndex + 2] = sourceVertices[vertexIndex + 2] - offset;
  95. destinationVertices[vertexIndex + 3] = sourceVertices[vertexIndex + 3] - offset;
  96. //Vector3 jitterOffset = new Vector3(Random.Range(-.25f, .25f), Random.Range(-.25f, .25f), 0);
  97. // Determine the random scale change for each character.
  98. float randomScale = Random.Range(1f, 1.5f);
  99. // Add modified scale and index
  100. modifiedCharScale.Add(randomScale);
  101. scaleSortingOrder.Add(modifiedCharScale.Count - 1);
  102. // Setup the matrix for the scale change.
  103. //matrix = Matrix4x4.TRS(jitterOffset, Quaternion.Euler(0, 0, Random.Range(-5f, 5f)), Vector3.one * randomScale);
  104. matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, Vector3.one * randomScale);
  105. destinationVertices[vertexIndex + 0] = matrix.MultiplyPoint3x4(destinationVertices[vertexIndex + 0]);
  106. destinationVertices[vertexIndex + 1] = matrix.MultiplyPoint3x4(destinationVertices[vertexIndex + 1]);
  107. destinationVertices[vertexIndex + 2] = matrix.MultiplyPoint3x4(destinationVertices[vertexIndex + 2]);
  108. destinationVertices[vertexIndex + 3] = matrix.MultiplyPoint3x4(destinationVertices[vertexIndex + 3]);
  109. destinationVertices[vertexIndex + 0] += offset;
  110. destinationVertices[vertexIndex + 1] += offset;
  111. destinationVertices[vertexIndex + 2] += offset;
  112. destinationVertices[vertexIndex + 3] += offset;
  113. // Restore Source UVS which have been modified by the sorting
  114. Vector2[] sourceUVs0 = cachedMeshInfoVertexData[materialIndex].uvs0;
  115. Vector2[] destinationUVs0 = textInfo.meshInfo[materialIndex].uvs0;
  116. destinationUVs0[vertexIndex + 0] = sourceUVs0[vertexIndex + 0];
  117. destinationUVs0[vertexIndex + 1] = sourceUVs0[vertexIndex + 1];
  118. destinationUVs0[vertexIndex + 2] = sourceUVs0[vertexIndex + 2];
  119. destinationUVs0[vertexIndex + 3] = sourceUVs0[vertexIndex + 3];
  120. // Restore Source Vertex Colors
  121. Color32[] sourceColors32 = cachedMeshInfoVertexData[materialIndex].colors32;
  122. Color32[] destinationColors32 = textInfo.meshInfo[materialIndex].colors32;
  123. destinationColors32[vertexIndex + 0] = sourceColors32[vertexIndex + 0];
  124. destinationColors32[vertexIndex + 1] = sourceColors32[vertexIndex + 1];
  125. destinationColors32[vertexIndex + 2] = sourceColors32[vertexIndex + 2];
  126. destinationColors32[vertexIndex + 3] = sourceColors32[vertexIndex + 3];
  127. }
  128. // Push changes into meshes
  129. for (int i = 0; i < textInfo.meshInfo.Length; i++)
  130. {
  131. //// Sort Quads based modified scale
  132. scaleSortingOrder.Sort((a, b) => modifiedCharScale[a].CompareTo(modifiedCharScale[b]));
  133. textInfo.meshInfo[i].SortGeometry(scaleSortingOrder);
  134. // Updated modified vertex attributes
  135. textInfo.meshInfo[i].mesh.vertices = textInfo.meshInfo[i].vertices;
  136. textInfo.meshInfo[i].mesh.uv = textInfo.meshInfo[i].uvs0;
  137. textInfo.meshInfo[i].mesh.colors32 = textInfo.meshInfo[i].colors32;
  138. m_TextComponent.UpdateGeometry(textInfo.meshInfo[i].mesh, i);
  139. }
  140. yield return new WaitForSeconds(0.1f);
  141. }
  142. }
  143. }
  144. }