Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

The demo script "VertexJitter" has a public variable "SpeedMultiplier" that doesn't do anything?

Discussion in 'UGUI & TextMesh Pro' started by negative_zero, Sep 28, 2019.

  1. negative_zero

    negative_zero

    Joined:
    Nov 14, 2013
    Posts:
    24
    So I'm looking at the "Animating Vertex Attributes" demo scene in TMP that runs off a script called "VertexJitter" that has 3 public variables: AngleMultiplier, SpeedMultiplier, and CurveScale. The other two are used in the script and when you change them there is an obvious change. However, SpeedMultiplier never actually appears anywhere else in the script and doesn't actually change anything when I change the numbers.

    Is this a mistake? Was it SUPPOSED to be used somewhere?!

    I notice also that the main coroutine in in VertexJitter is called AnimateVertexColors even though the script has nothing to do with animating colors (that's done in a different script, VertexColorCycler) so maybe it was just a reworked script and they forgot to take SpeedMultiplier out? (It doesn't appear in the VertexColorCycler either though.)

    Here is the script in question:

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

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    A few of the example scripts are re-cycled from other examples and as such they contain all the goodies and warts ;)

    When I have time or if we have any volunteers, those scripts could certainly be cleaned up. Some of these scripts have also been modified by users overtime to add more functionality so it could be cool to also include some of that as well.