123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- using UnityEngine;
- using System.Collections;
- namespace TMPro.Examples
- {
- public class VertexJitter : MonoBehaviour
- {
- public float AngleMultiplier = 1.0f;
- public float SpeedMultiplier = 1.0f;
- public float CurveScale = 1.0f;
- private TMP_Text m_TextComponent;
- private bool hasTextChanged;
-
-
-
- private struct VertexAnim
- {
- public float angleRange;
- public float angle;
- public float speed;
- }
- void Awake()
- {
- m_TextComponent = GetComponent<TMP_Text>();
- }
- void OnEnable()
- {
-
- TMPro_EventManager.TEXT_CHANGED_EVENT.Add(ON_TEXT_CHANGED);
- }
- void OnDisable()
- {
- TMPro_EventManager.TEXT_CHANGED_EVENT.Remove(ON_TEXT_CHANGED);
- }
- void Start()
- {
- StartCoroutine(AnimateVertexColors());
- }
- void ON_TEXT_CHANGED(Object obj)
- {
- if (obj == m_TextComponent)
- hasTextChanged = true;
- }
-
-
-
-
- IEnumerator AnimateVertexColors()
- {
-
-
- m_TextComponent.ForceMeshUpdate();
- TMP_TextInfo textInfo = m_TextComponent.textInfo;
- Matrix4x4 matrix;
- int loopCount = 0;
- hasTextChanged = true;
-
- VertexAnim[] vertexAnim = new VertexAnim[1024];
- for (int i = 0; i < 1024; i++)
- {
- vertexAnim[i].angleRange = Random.Range(10f, 25f);
- vertexAnim[i].speed = Random.Range(1f, 3f);
- }
-
- TMP_MeshInfo[] cachedMeshInfo = textInfo.CopyMeshInfoVertexData();
- while (true)
- {
-
- if (hasTextChanged)
- {
-
- cachedMeshInfo = textInfo.CopyMeshInfoVertexData();
- hasTextChanged = false;
- }
- int characterCount = textInfo.characterCount;
-
- if (characterCount == 0)
- {
- yield return new WaitForSeconds(0.25f);
- continue;
- }
- for (int i = 0; i < characterCount; i++)
- {
- TMP_CharacterInfo charInfo = textInfo.characterInfo[i];
-
- if (!charInfo.isVisible)
- continue;
-
- VertexAnim vertAnim = vertexAnim[i];
-
- int materialIndex = textInfo.characterInfo[i].materialReferenceIndex;
-
- int vertexIndex = textInfo.characterInfo[i].vertexIndex;
-
- Vector3[] sourceVertices = cachedMeshInfo[materialIndex].vertices;
-
-
-
- Vector2 charMidBasline = (sourceVertices[vertexIndex + 0] + sourceVertices[vertexIndex + 2]) / 2;
-
-
- Vector3 offset = charMidBasline;
- Vector3[] destinationVertices = textInfo.meshInfo[materialIndex].vertices;
- destinationVertices[vertexIndex + 0] = sourceVertices[vertexIndex + 0] - offset;
- destinationVertices[vertexIndex + 1] = sourceVertices[vertexIndex + 1] - offset;
- destinationVertices[vertexIndex + 2] = sourceVertices[vertexIndex + 2] - offset;
- destinationVertices[vertexIndex + 3] = sourceVertices[vertexIndex + 3] - offset;
- vertAnim.angle = Mathf.SmoothStep(-vertAnim.angleRange, vertAnim.angleRange, Mathf.PingPong(loopCount / 25f * vertAnim.speed, 1f));
- Vector3 jitterOffset = new Vector3(Random.Range(-.25f, .25f), Random.Range(-.25f, .25f), 0);
- matrix = Matrix4x4.TRS(jitterOffset * CurveScale, Quaternion.Euler(0, 0, Random.Range(-5f, 5f) * AngleMultiplier), Vector3.one);
- destinationVertices[vertexIndex + 0] = matrix.MultiplyPoint3x4(destinationVertices[vertexIndex + 0]);
- destinationVertices[vertexIndex + 1] = matrix.MultiplyPoint3x4(destinationVertices[vertexIndex + 1]);
- destinationVertices[vertexIndex + 2] = matrix.MultiplyPoint3x4(destinationVertices[vertexIndex + 2]);
- destinationVertices[vertexIndex + 3] = matrix.MultiplyPoint3x4(destinationVertices[vertexIndex + 3]);
- destinationVertices[vertexIndex + 0] += offset;
- destinationVertices[vertexIndex + 1] += offset;
- destinationVertices[vertexIndex + 2] += offset;
- destinationVertices[vertexIndex + 3] += offset;
- vertexAnim[i] = vertAnim;
- }
-
- for (int i = 0; i < textInfo.meshInfo.Length; i++)
- {
- textInfo.meshInfo[i].mesh.vertices = textInfo.meshInfo[i].vertices;
- m_TextComponent.UpdateGeometry(textInfo.meshInfo[i].mesh, i);
- }
- loopCount += 1;
- yield return new WaitForSeconds(0.1f);
- }
- }
- }
- }
|