Search Unity

Align TextMesh Pro with Normal

Discussion in 'Scripting' started by xVSpeedy, Feb 15, 2018.

  1. xVSpeedy

    xVSpeedy

    Joined:
    Sep 22, 2014
    Posts:
    12
    Hi! Can someone help me with this rotation?
    I want to align it with the raycast hit.normal rotation..
    fasfasfasf.jpg

    This is my code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. namespace TMPro.Examples
    6. {
    7.  
    8.     public class AlignText: 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.         private bool hasTextChanged;
    17.  
    18.         private struct VertexAnim
    19.         {
    20.             public float angleRange;
    21.             public float angle;
    22.             public float speed;
    23.         }
    24.  
    25.         void Awake()
    26.         {
    27.             m_TextComponent = GetComponent<TMP_Text>();
    28.         }
    29.  
    30.         void OnEnable()
    31.         {
    32.             TMPro_EventManager.TEXT_CHANGED_EVENT.Add(ON_TEXT_CHANGED);
    33.         }
    34.  
    35.         void OnDisable()
    36.         {
    37.             TMPro_EventManager.TEXT_CHANGED_EVENT.Remove(ON_TEXT_CHANGED);
    38.         }
    39.  
    40.  
    41.         void Start()
    42.         {
    43.             StartCoroutine(AnimateVertexColors());
    44.         }
    45.  
    46.  
    47.         void ON_TEXT_CHANGED(Object obj)
    48.         {
    49.             if (obj == m_TextComponent)
    50.                 hasTextChanged = true;
    51.         }
    52.  
    53.         IEnumerator AnimateVertexColors()
    54.         {
    55.             m_TextComponent.ForceMeshUpdate();
    56.  
    57.             TMP_TextInfo textInfo = m_TextComponent.textInfo;
    58.  
    59.             Matrix4x4 matrix;
    60.  
    61.             int loopCount = 0;
    62.             hasTextChanged = true;
    63.  
    64.             VertexAnim[] vertexAnim = new VertexAnim[1024];
    65.             for (int i = 0; i < 1024; i++)
    66.             {
    67.                 vertexAnim[i].angleRange = Random.Range(10f, 25f);
    68.                 vertexAnim[i].speed = Random.Range(1f, 3f);
    69.             }
    70.  
    71.             TMP_MeshInfo[] cachedMeshInfo = textInfo.CopyMeshInfoVertexData();
    72.                 int characterCount = textInfo.characterCount;
    73.  
    74.                 if (characterCount == 0)
    75.                 {
    76.                     yield return new WaitForSeconds(0.25f);
    77.                 }
    78.  
    79.  
    80.                 for (int i = 0; i < characterCount; i++)
    81.                 {
    82.                     TMP_CharacterInfo charInfo = textInfo.characterInfo[i];
    83.  
    84.                     if (!charInfo.isVisible)
    85.                         continue;
    86.  
    87.                     VertexAnim vertAnim = vertexAnim[i];
    88.                     int materialIndex = textInfo.characterInfo[i].materialReferenceIndex;
    89.                     int vertexIndex = textInfo.characterInfo[i].vertexIndex;
    90.                     Vector3[] sourceVertices = cachedMeshInfo[materialIndex].vertices;
    91.                     Vector2 charMidBasline = (sourceVertices[vertexIndex + 0] + sourceVertices[vertexIndex + 2]) / 2;
    92.                     Vector3 offset = charMidBasline;
    93.  
    94.                     Vector3[] destinationVertices = textInfo.meshInfo[materialIndex].vertices;
    95.  
    96.                     RaycastHit hit;
    97.  
    98.                     if (Physics.Raycast(offset, Vector3.forward, out hit))
    99.                         Debug.DrawLine(offset, hit.point, Color.green, 10.0f, false);
    100.                 float offs = -7.5f;
    101.  
    102.                     destinationVertices[vertexIndex + 0].z = hit.point.z + offs;
    103.                     destinationVertices[vertexIndex + 1].z = hit.point.z + offs;
    104.                     destinationVertices[vertexIndex + 2].z = hit.point.z + offs;
    105.                     destinationVertices[vertexIndex + 3].z = hit.point.z + offs;
    106.  
    107.  
    108.             }
    109.                 for (int i = 0; i < textInfo.meshInfo.Length; i++)
    110.                 {
    111.                     textInfo.meshInfo[i].mesh.vertices = textInfo.meshInfo[i].vertices;
    112.                     m_TextComponent.UpdateGeometry(textInfo.meshInfo[i].mesh, i);
    113.                 }
    114.                 loopCount += 1;
    115.                 yield return new WaitForSeconds(0.1f);
    116.             }
    117.    
    118.  
    119.     }
    120. }
     
  2. xVSpeedy

    xVSpeedy

    Joined:
    Sep 22, 2014
    Posts:
    12
    Someone?
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Wait... are you rotating your meshes by changing the vertices?

    Why not just set the rotation portion of the Transform instead. And not bother touching the vertices?

    Then you can just use something like Quaternion.LookRotation to pick a vector looking in the direction of the normal:
    https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html
     
  4. xVSpeedy

    xVSpeedy

    Joined:
    Sep 22, 2014
    Posts:
    12
    Sorry..but I need to do it with Text Mesh component or with TextMesh Pro.. can I manipulate transform for each letters?
     
  5. xVSpeedy

    xVSpeedy

    Joined:
    Sep 22, 2014
    Posts:
    12
    Can someone tell me if it's possible to do that?
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188