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.

158 lines
6.7 KiB

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