WarpTextExample.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace TMPro.Examples
  4. {
  5. public class WarpTextExample : MonoBehaviour
  6. {
  7. private TMP_Text m_TextComponent;
  8. public AnimationCurve VertexCurve = new AnimationCurve(new Keyframe(0, 0), new Keyframe(0.25f, 2.0f), new Keyframe(0.5f, 0), new Keyframe(0.75f, 2.0f), new Keyframe(1, 0f));
  9. public float AngleMultiplier = 1.0f;
  10. public float SpeedMultiplier = 1.0f;
  11. public float CurveScale = 1.0f;
  12. void Awake()
  13. {
  14. m_TextComponent = gameObject.GetComponent<TMP_Text>();
  15. }
  16. void Start()
  17. {
  18. StartCoroutine(WarpText());
  19. }
  20. private AnimationCurve CopyAnimationCurve(AnimationCurve curve)
  21. {
  22. AnimationCurve newCurve = new AnimationCurve();
  23. newCurve.keys = curve.keys;
  24. return newCurve;
  25. }
  26. /// <summary>
  27. /// Method to curve text along a Unity animation curve.
  28. /// </summary>
  29. /// <param name="textComponent"></param>
  30. /// <returns></returns>
  31. IEnumerator WarpText()
  32. {
  33. VertexCurve.preWrapMode = WrapMode.Clamp;
  34. VertexCurve.postWrapMode = WrapMode.Clamp;
  35. //Mesh mesh = m_TextComponent.textInfo.meshInfo[0].mesh;
  36. Vector3[] vertices;
  37. Matrix4x4 matrix;
  38. m_TextComponent.havePropertiesChanged = true; // Need to force the TextMeshPro Object to be updated.
  39. CurveScale *= 10;
  40. float old_CurveScale = CurveScale;
  41. AnimationCurve old_curve = CopyAnimationCurve(VertexCurve);
  42. while (true)
  43. {
  44. if (!m_TextComponent.havePropertiesChanged && old_CurveScale == CurveScale && old_curve.keys[1].value == VertexCurve.keys[1].value)
  45. {
  46. yield return null;
  47. continue;
  48. }
  49. old_CurveScale = CurveScale;
  50. old_curve = CopyAnimationCurve(VertexCurve);
  51. m_TextComponent.ForceMeshUpdate(); // Generate the mesh and populate the textInfo with data we can use and manipulate.
  52. TMP_TextInfo textInfo = m_TextComponent.textInfo;
  53. int characterCount = textInfo.characterCount;
  54. if (characterCount == 0) continue;
  55. //vertices = textInfo.meshInfo[0].vertices;
  56. //int lastVertexIndex = textInfo.characterInfo[characterCount - 1].vertexIndex;
  57. float boundsMinX = m_TextComponent.bounds.min.x; //textInfo.meshInfo[0].mesh.bounds.min.x;
  58. float boundsMaxX = m_TextComponent.bounds.max.x; //textInfo.meshInfo[0].mesh.bounds.max.x;
  59. for (int i = 0; i < characterCount; i++)
  60. {
  61. if (!textInfo.characterInfo[i].isVisible)
  62. continue;
  63. int vertexIndex = textInfo.characterInfo[i].vertexIndex;
  64. // Get the index of the mesh used by this character.
  65. int materialIndex = textInfo.characterInfo[i].materialReferenceIndex;
  66. vertices = textInfo.meshInfo[materialIndex].vertices;
  67. // Compute the baseline mid point for each character
  68. Vector3 offsetToMidBaseline = new Vector2((vertices[vertexIndex + 0].x + vertices[vertexIndex + 2].x) / 2, textInfo.characterInfo[i].baseLine);
  69. //float offsetY = VertexCurve.Evaluate((float)i / characterCount + loopCount / 50f); // Random.Range(-0.25f, 0.25f);
  70. // Apply offset to adjust our pivot point.
  71. vertices[vertexIndex + 0] += -offsetToMidBaseline;
  72. vertices[vertexIndex + 1] += -offsetToMidBaseline;
  73. vertices[vertexIndex + 2] += -offsetToMidBaseline;
  74. vertices[vertexIndex + 3] += -offsetToMidBaseline;
  75. // Compute the angle of rotation for each character based on the animation curve
  76. float x0 = (offsetToMidBaseline.x - boundsMinX) / (boundsMaxX - boundsMinX); // Character's position relative to the bounds of the mesh.
  77. float x1 = x0 + 0.0001f;
  78. float y0 = VertexCurve.Evaluate(x0) * CurveScale;
  79. float y1 = VertexCurve.Evaluate(x1) * CurveScale;
  80. Vector3 horizontal = new Vector3(1, 0, 0);
  81. //Vector3 normal = new Vector3(-(y1 - y0), (x1 * (boundsMaxX - boundsMinX) + boundsMinX) - offsetToMidBaseline.x, 0);
  82. Vector3 tangent = new Vector3(x1 * (boundsMaxX - boundsMinX) + boundsMinX, y1) - new Vector3(offsetToMidBaseline.x, y0);
  83. float dot = Mathf.Acos(Vector3.Dot(horizontal, tangent.normalized)) * 57.2957795f;
  84. Vector3 cross = Vector3.Cross(horizontal, tangent);
  85. float angle = cross.z > 0 ? dot : 360 - dot;
  86. matrix = Matrix4x4.TRS(new Vector3(0, y0, 0), Quaternion.Euler(0, 0, angle), Vector3.one);
  87. vertices[vertexIndex + 0] = matrix.MultiplyPoint3x4(vertices[vertexIndex + 0]);
  88. vertices[vertexIndex + 1] = matrix.MultiplyPoint3x4(vertices[vertexIndex + 1]);
  89. vertices[vertexIndex + 2] = matrix.MultiplyPoint3x4(vertices[vertexIndex + 2]);
  90. vertices[vertexIndex + 3] = matrix.MultiplyPoint3x4(vertices[vertexIndex + 3]);
  91. vertices[vertexIndex + 0] += offsetToMidBaseline;
  92. vertices[vertexIndex + 1] += offsetToMidBaseline;
  93. vertices[vertexIndex + 2] += offsetToMidBaseline;
  94. vertices[vertexIndex + 3] += offsetToMidBaseline;
  95. }
  96. // Upload the mesh with the revised information
  97. m_TextComponent.UpdateVertexData();
  98. yield return new WaitForSeconds(0.025f);
  99. }
  100. }
  101. }
  102. }