Created
October 28, 2020 16:30
-
-
Save rstecca/df429915089a46c216b88a4f58f728d5 to your computer and use it in GitHub Desktop.
TextMeshPro Warper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Thanks to Kemble Software https://www.youtube.com/channel/UC-n80lsjMCS3OJR8kFrATLg | |
// Nice hack here: https://www.youtube.com/watch?v=FXMqUdP3XcE | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using TMPro; | |
[RequireComponent(typeof(TextMeshPro)), ExecuteInEditMode] | |
public class TextMeshProWarper : MonoBehaviour | |
{ | |
TextMeshPro TMP; | |
[SerializeField] TMP_Text tmpText; | |
[SerializeField] float radius = 1f; | |
[SerializeField] float warp = 1f; | |
[SerializeField] float yScaler = 1f; | |
void Start() | |
{ | |
//tmpText = TMP.GetComponent<TMP_Text>(); | |
} | |
void Update() | |
{ | |
tmpText.ForceMeshUpdate(); | |
var tInfo = tmpText.textInfo; | |
for (int i = 0; i < tInfo.characterCount; i++) { | |
var chInfo = tInfo.characterInfo[i]; | |
if(!chInfo.isVisible) { | |
continue; | |
} | |
var verts = tInfo.meshInfo[chInfo.materialReferenceIndex].vertices; | |
for (int j = 0; j < 4; j++) { | |
var v = verts[chInfo.vertexIndex + j]; | |
// Parabolic | |
//verts[chInfo.vertexIndex + j] = v + (-factor * 0.01f) * new Vector3(v.x * amplitude, 0f, v.x*v.x); | |
// Circular | |
verts[chInfo.vertexIndex + j] = new Vector3(radius * Mathf.Cos(v.x * warp * 0.1f), v.y * yScaler, radius * Mathf.Sin(v.x * warp * 0.1f)); | |
} | |
} | |
for (int i = 0; i < tInfo.meshInfo.Length; i++) { | |
var mInfo = tInfo.meshInfo[i]; | |
mInfo.mesh.vertices = mInfo.vertices; | |
tmpText.UpdateGeometry(mInfo.mesh, i); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Warps TextMeshPro text into a circle. Can act as template for more deformations (Parabolic is left commented as an example).