Commit 1444629e authored by BlackAngle233's avatar BlackAngle233
Browse files

12.22 update UI

the font assets is too large, so not be pushed
parent c4fc982a
using UnityEngine;
using System.Collections;
namespace TMPro.Examples
{
public class TextConsoleSimulator : MonoBehaviour
{
private TMP_Text m_TextComponent;
private bool hasTextChanged;
void Awake()
{
m_TextComponent = gameObject.GetComponent<TMP_Text>();
}
void Start()
{
StartCoroutine(RevealCharacters(m_TextComponent));
//StartCoroutine(RevealWords(m_TextComponent));
}
void OnEnable()
{
// Subscribe to event fired when text object has been regenerated.
TMPro_EventManager.TEXT_CHANGED_EVENT.Add(ON_TEXT_CHANGED);
}
void OnDisable()
{
TMPro_EventManager.TEXT_CHANGED_EVENT.Remove(ON_TEXT_CHANGED);
}
// Event received when the text object has changed.
void ON_TEXT_CHANGED(Object obj)
{
hasTextChanged = true;
}
/// <summary>
/// Method revealing the text one character at a time.
/// </summary>
/// <returns></returns>
IEnumerator RevealCharacters(TMP_Text textComponent)
{
textComponent.ForceMeshUpdate();
TMP_TextInfo textInfo = textComponent.textInfo;
int totalVisibleCharacters = textInfo.characterCount; // Get # of Visible Character in text object
int visibleCount = 0;
while (true)
{
if (hasTextChanged)
{
totalVisibleCharacters = textInfo.characterCount; // Update visible character count.
hasTextChanged = false;
}
if (visibleCount > totalVisibleCharacters)
{
yield return new WaitForSeconds(1.0f);
visibleCount = 0;
}
textComponent.maxVisibleCharacters = visibleCount; // How many characters should TextMeshPro display?
visibleCount += 1;
yield return null;
}
}
/// <summary>
/// Method revealing the text one word at a time.
/// </summary>
/// <returns></returns>
IEnumerator RevealWords(TMP_Text textComponent)
{
textComponent.ForceMeshUpdate();
int totalWordCount = textComponent.textInfo.wordCount;
int totalVisibleCharacters = textComponent.textInfo.characterCount; // Get # of Visible Character in text object
int counter = 0;
int currentWord = 0;
int visibleCount = 0;
while (true)
{
currentWord = counter % (totalWordCount + 1);
// Get last character index for the current word.
if (currentWord == 0) // Display no words.
visibleCount = 0;
else if (currentWord < totalWordCount) // Display all other words with the exception of the last one.
visibleCount = textComponent.textInfo.wordInfo[currentWord - 1].lastCharacterIndex + 1;
else if (currentWord == totalWordCount) // Display last word and all remaining characters.
visibleCount = totalVisibleCharacters;
textComponent.maxVisibleCharacters = visibleCount; // How many characters should TextMeshPro display?
// Once the last character has been revealed, wait 1.0 second and start over.
if (visibleCount >= totalVisibleCharacters)
{
yield return new WaitForSeconds(1.0f);
}
counter += 1;
yield return new WaitForSeconds(0.1f);
}
}
}
}
\ No newline at end of file
using UnityEngine;
using System.Collections;
namespace TMPro.Examples
{
public class TextMeshProFloatingText : MonoBehaviour
{
public Font TheFont;
private GameObject m_floatingText;
private TextMeshPro m_textMeshPro;
private TextMesh m_textMesh;
private Transform m_transform;
private Transform m_floatingText_Transform;
private Transform m_cameraTransform;
Vector3 lastPOS = Vector3.zero;
Quaternion lastRotation = Quaternion.identity;
public int SpawnType;
//private int m_frame = 0;
void Awake()
{
m_transform = transform;
m_floatingText = new GameObject(this.name + " floating text");
// Reference to Transform is lost when TMP component is added since it replaces it by a RectTransform.
//m_floatingText_Transform = m_floatingText.transform;
//m_floatingText_Transform.position = m_transform.position + new Vector3(0, 15f, 0);
m_cameraTransform = Camera.main.transform;
}
void Start()
{
if (SpawnType == 0)
{
// TextMesh Pro Implementation
m_textMeshPro = m_floatingText.AddComponent<TextMeshPro>();
m_textMeshPro.rectTransform.sizeDelta = new Vector2(3, 3);
m_floatingText_Transform = m_floatingText.transform;
m_floatingText_Transform.position = m_transform.position + new Vector3(0, 15f, 0);
//m_textMeshPro.fontAsset = Resources.Load("Fonts & Materials/JOKERMAN SDF", typeof(TextMeshProFont)) as TextMeshProFont; // User should only provide a string to the resource.
//m_textMeshPro.fontSharedMaterial = Resources.Load("Fonts & Materials/LiberationSans SDF", typeof(Material)) as Material;
m_textMeshPro.alignment = TextAlignmentOptions.Center;
m_textMeshPro.color = new Color32((byte)Random.Range(0, 255), (byte)Random.Range(0, 255), (byte)Random.Range(0, 255), 255);
m_textMeshPro.fontSize = 24;
//m_textMeshPro.enableExtraPadding = true;
//m_textMeshPro.enableShadows = false;
m_textMeshPro.enableKerning = false;
m_textMeshPro.text = string.Empty;
StartCoroutine(DisplayTextMeshProFloatingText());
}
else if (SpawnType == 1)
{
//Debug.Log("Spawning TextMesh Objects.");
m_floatingText_Transform = m_floatingText.transform;
m_floatingText_Transform.position = m_transform.position + new Vector3(0, 15f, 0);
m_textMesh = m_floatingText.AddComponent<TextMesh>();
m_textMesh.font = Resources.Load<Font>("Fonts/ARIAL");
m_textMesh.GetComponent<Renderer>().sharedMaterial = m_textMesh.font.material;
m_textMesh.color = new Color32((byte)Random.Range(0, 255), (byte)Random.Range(0, 255), (byte)Random.Range(0, 255), 255);
m_textMesh.anchor = TextAnchor.LowerCenter;
m_textMesh.fontSize = 24;
StartCoroutine(DisplayTextMeshFloatingText());
}
else if (SpawnType == 2)
{
}
}
//void Update()
//{
// if (SpawnType == 0)
// {
// m_textMeshPro.SetText("{0}", m_frame);
// }
// else
// {
// m_textMesh.text = m_frame.ToString();
// }
// m_frame = (m_frame + 1) % 1000;
//}
public IEnumerator DisplayTextMeshProFloatingText()
{
float CountDuration = 2.0f; // How long is the countdown alive.
float starting_Count = Random.Range(5f, 20f); // At what number is the counter starting at.
float current_Count = starting_Count;
Vector3 start_pos = m_floatingText_Transform.position;
Color32 start_color = m_textMeshPro.color;
float alpha = 255;
int int_counter = 0;
float fadeDuration = 3 / starting_Count * CountDuration;
while (current_Count > 0)
{
current_Count -= (Time.deltaTime / CountDuration) * starting_Count;
if (current_Count <= 3)
{
//Debug.Log("Fading Counter ... " + current_Count.ToString("f2"));
alpha = Mathf.Clamp(alpha - (Time.deltaTime / fadeDuration) * 255, 0, 255);
}
int_counter = (int)current_Count;
m_textMeshPro.text = int_counter.ToString();
//m_textMeshPro.SetText("{0}", (int)current_Count);
m_textMeshPro.color = new Color32(start_color.r, start_color.g, start_color.b, (byte)alpha);
// Move the floating text upward each update
m_floatingText_Transform.position += new Vector3(0, starting_Count * Time.deltaTime, 0);
// Align floating text perpendicular to Camera.
if (!lastPOS.Compare(m_cameraTransform.position, 1000) || !lastRotation.Compare(m_cameraTransform.rotation, 1000))
{
lastPOS = m_cameraTransform.position;
lastRotation = m_cameraTransform.rotation;
m_floatingText_Transform.rotation = lastRotation;
Vector3 dir = m_transform.position - lastPOS;
m_transform.forward = new Vector3(dir.x, 0, dir.z);
}
yield return new WaitForEndOfFrame();
}
//Debug.Log("Done Counting down.");
yield return new WaitForSeconds(Random.Range(0.1f, 1.0f));
m_floatingText_Transform.position = start_pos;
StartCoroutine(DisplayTextMeshProFloatingText());
}
public IEnumerator DisplayTextMeshFloatingText()
{
float CountDuration = 2.0f; // How long is the countdown alive.
float starting_Count = Random.Range(5f, 20f); // At what number is the counter starting at.
float current_Count = starting_Count;
Vector3 start_pos = m_floatingText_Transform.position;
Color32 start_color = m_textMesh.color;
float alpha = 255;
int int_counter = 0;
float fadeDuration = 3 / starting_Count * CountDuration;
while (current_Count > 0)
{
current_Count -= (Time.deltaTime / CountDuration) * starting_Count;
if (current_Count <= 3)
{
//Debug.Log("Fading Counter ... " + current_Count.ToString("f2"));
alpha = Mathf.Clamp(alpha - (Time.deltaTime / fadeDuration) * 255, 0, 255);
}
int_counter = (int)current_Count;
m_textMesh.text = int_counter.ToString();
//Debug.Log("Current Count:" + current_Count.ToString("f2"));
m_textMesh.color = new Color32(start_color.r, start_color.g, start_color.b, (byte)alpha);
// Move the floating text upward each update
m_floatingText_Transform.position += new Vector3(0, starting_Count * Time.deltaTime, 0);
// Align floating text perpendicular to Camera.
if (!lastPOS.Compare(m_cameraTransform.position, 1000) || !lastRotation.Compare(m_cameraTransform.rotation, 1000))
{
lastPOS = m_cameraTransform.position;
lastRotation = m_cameraTransform.rotation;
m_floatingText_Transform.rotation = lastRotation;
Vector3 dir = m_transform.position - lastPOS;
m_transform.forward = new Vector3(dir.x, 0, dir.z);
}
yield return new WaitForEndOfFrame();
}
//Debug.Log("Done Counting down.");
yield return new WaitForSeconds(Random.Range(0.1f, 1.0f));
m_floatingText_Transform.position = start_pos;
StartCoroutine(DisplayTextMeshFloatingText());
}
}
}
\ No newline at end of file
using UnityEngine;
using System.Collections;
namespace TMPro.Examples
{
public class TextMeshSpawner : MonoBehaviour
{
public int SpawnType = 0;
public int NumberOfNPC = 12;
public Font TheFont;
private TextMeshProFloatingText floatingText_Script;
void Awake()
{
}
void Start()
{
for (int i = 0; i < NumberOfNPC; i++)
{
if (SpawnType == 0)
{
// TextMesh Pro Implementation
//go.transform.localScale = new Vector3(2, 2, 2);
GameObject go = new GameObject(); //"NPC " + i);
go.transform.position = new Vector3(Random.Range(-95f, 95f), 0.5f, Random.Range(-95f, 95f));
//go.transform.position = new Vector3(0, 1.01f, 0);
//go.renderer.castShadows = false;
//go.renderer.receiveShadows = false;
//go.transform.rotation = Quaternion.Euler(0, Random.Range(0, 360), 0);
TextMeshPro textMeshPro = go.AddComponent<TextMeshPro>();
//textMeshPro.FontAsset = Resources.Load("Fonts & Materials/LiberationSans SDF", typeof(TextMeshProFont)) as TextMeshProFont;
//textMeshPro.anchor = AnchorPositions.Bottom;
textMeshPro.fontSize = 96;
textMeshPro.text = "!";
textMeshPro.color = new Color32(255, 255, 0, 255);
//textMeshPro.Text = "!";
// Spawn Floating Text
floatingText_Script = go.AddComponent<TextMeshProFloatingText>();
floatingText_Script.SpawnType = 0;
}
else
{
// TextMesh Implementation
GameObject go = new GameObject(); //"NPC " + i);
go.transform.position = new Vector3(Random.Range(-95f, 95f), 0.5f, Random.Range(-95f, 95f));
//go.transform.position = new Vector3(0, 1.01f, 0);
TextMesh textMesh = go.AddComponent<TextMesh>();
textMesh.GetComponent<Renderer>().sharedMaterial = TheFont.material;
textMesh.font = TheFont;
textMesh.anchor = TextAnchor.LowerCenter;
textMesh.fontSize = 96;
textMesh.color = new Color32(255, 255, 0, 255);
textMesh.text = "!";
// Spawn Floating Text
floatingText_Script = go.AddComponent<TextMeshProFloatingText>();
floatingText_Script.SpawnType = 1;
}
}
}
}
}
using UnityEngine;
using System.Collections;
namespace TMPro.Examples
{
public class VertexColorCycler : MonoBehaviour
{
private TMP_Text m_TextComponent;
void Awake()
{
m_TextComponent = GetComponent<TMP_Text>();
}
void Start()
{
StartCoroutine(AnimateVertexColors());
}
/// <summary>
/// Method to animate vertex colors of a TMP Text object.
/// </summary>
/// <returns></returns>
IEnumerator AnimateVertexColors()
{
// Force the text object to update right away so we can have geometry to modify right from the start.
m_TextComponent.ForceMeshUpdate();
TMP_TextInfo textInfo = m_TextComponent.textInfo;
int currentCharacter = 0;
Color32[] newVertexColors;
Color32 c0 = m_TextComponent.color;
while (true)
{
int characterCount = textInfo.characterCount;
// If No Characters then just yield and wait for some text to be added
if (characterCount == 0)
{
yield return new WaitForSeconds(0.25f);
continue;
}
// Get the index of the material used by the current character.
int materialIndex = textInfo.characterInfo[currentCharacter].materialReferenceIndex;
// Get the vertex colors of the mesh used by this text element (character or sprite).
newVertexColors = textInfo.meshInfo[materialIndex].colors32;
// Get the index of the first vertex used by this text element.
int vertexIndex = textInfo.characterInfo[currentCharacter].vertexIndex;
// Only change the vertex color if the text element is visible.
if (textInfo.characterInfo[currentCharacter].isVisible)
{
c0 = new Color32((byte)Random.Range(0, 255), (byte)Random.Range(0, 255), (byte)Random.Range(0, 255), 255);
newVertexColors[vertexIndex + 0] = c0;
newVertexColors[vertexIndex + 1] = c0;
newVertexColors[vertexIndex + 2] = c0;
newVertexColors[vertexIndex + 3] = c0;
// New function which pushes (all) updated vertex data to the appropriate meshes when using either the Mesh Renderer or CanvasRenderer.
m_TextComponent.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);
// This last process could be done to only update the vertex data that has changed as opposed to all of the vertex data but it would require extra steps and knowing what type of renderer is used.
// These extra steps would be a performance optimization but it is unlikely that such optimization will be necessary.
}
currentCharacter = (currentCharacter + 1) % characterCount;
yield return new WaitForSeconds(0.05f);
}
}
}
}
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;
/// <summary>
/// Structure to hold pre-computed animation data.
/// </summary>
private struct VertexAnim
{
public float angleRange;
public float angle;
public float speed;
}
void Awake()
{
m_TextComponent = GetComponent<TMP_Text>();
}
void OnEnable()
{
// Subscribe to event fired when text object has been regenerated.
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;
}
/// <summary>
/// Method to animate vertex colors of a TMP Text object.
/// </summary>
/// <returns></returns>
IEnumerator AnimateVertexColors()
{
// 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.
// Alternatively, we could yield and wait until the end of the frame when the text object will be generated.
m_TextComponent.ForceMeshUpdate();
TMP_TextInfo textInfo = m_TextComponent.textInfo;
Matrix4x4 matrix;
int loopCount = 0;
hasTextChanged = true;
// Create an Array which contains pre-computed Angle Ranges and Speeds for a bunch of characters.
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);
}
// Cache the vertex data of the text object as the Jitter FX is applied to the original position of the characters.
TMP_MeshInfo[] cachedMeshInfo = textInfo.CopyMeshInfoVertexData();
while (true)
{
// Get new copy of vertex data if the text has changed.
if (hasTextChanged)
{
// Update the copy of the vertex data for the text object.
cachedMeshInfo = textInfo.CopyMeshInfoVertexData();
hasTextChanged = false;
}
int characterCount = textInfo.characterCount;
// If No Characters then just yield and wait for some text to be added
if (characterCount == 0)
{
yield return new WaitForSeconds(0.25f);
continue;
}
for (int i = 0; i < characterCount; i++)
{
TMP_CharacterInfo charInfo = textInfo.characterInfo[i];
// Skip characters that are not visible and thus have no geometry to manipulate.
if (!charInfo.isVisible)
continue;
// Retrieve the pre-computed animation data for the given character.
VertexAnim vertAnim = vertexAnim[i];
// Get the index of the material used by the current character.
int materialIndex = textInfo.characterInfo[i].materialReferenceIndex;
// Get the index of the first vertex used by this text element.
int vertexIndex = textInfo.characterInfo[i].vertexIndex;
// Get the cached vertices of the mesh used by this text element (character or sprite).
Vector3[] sourceVertices = cachedMeshInfo[materialIndex].vertices;
// Determine the center point of each character at the baseline.
//Vector2 charMidBasline = new Vector2((sourceVertices[vertexIndex + 0].x + sourceVertices[vertexIndex + 2].x) / 2, charInfo.baseLine);
// Determine the center point of each character.
Vector2 charMidBasline = (sourceVertices[vertexIndex + 0] + sourceVertices[vertexIndex + 2]) / 2;
// Need to translate all 4 vertices of each quad to aligned with middle of character / baseline.
// This is needed so the matrix TRS is applied at the origin for each character.
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;
}
// Push changes into meshes
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);
}
}
}
}
\ No newline at end of file
using UnityEngine;
using System.Collections;
namespace TMPro.Examples
{
public class VertexShakeA : MonoBehaviour
{
public float AngleMultiplier = 1.0f;
public float SpeedMultiplier = 1.0f;
public float ScaleMultiplier = 1.0f;
public float RotationMultiplier = 1.0f;
private TMP_Text m_TextComponent;
private bool hasTextChanged;
void Awake()
{
m_TextComponent = GetComponent<TMP_Text>();
}
void OnEnable()
{
// Subscribe to event fired when text object has been regenerated.
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;
}
/// <summary>
/// Method to animate vertex colors of a TMP Text object.
/// </summary>
/// <returns></returns>
IEnumerator AnimateVertexColors()
{
// 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.
// Alternatively, we could yield and wait until the end of the frame when the text object will be generated.
m_TextComponent.ForceMeshUpdate();
TMP_TextInfo textInfo = m_TextComponent.textInfo;
Matrix4x4 matrix;
Vector3[][] copyOfVertices = new Vector3[0][];
hasTextChanged = true;
while (true)
{
// Allocate new vertices
if (hasTextChanged)
{
if (copyOfVertices.Length < textInfo.meshInfo.Length)
copyOfVertices = new Vector3[textInfo.meshInfo.Length][];
for (int i = 0; i < textInfo.meshInfo.Length; i++)
{
int length = textInfo.meshInfo[i].vertices.Length;
copyOfVertices[i] = new Vector3[length];
}
hasTextChanged = false;
}
int characterCount = textInfo.characterCount;
// If No Characters then just yield and wait for some text to be added
if (characterCount == 0)
{
yield return new WaitForSeconds(0.25f);
continue;
}
int lineCount = textInfo.lineCount;
// Iterate through each line of the text.
for (int i = 0; i < lineCount; i++)
{
int first = textInfo.lineInfo[i].firstCharacterIndex;
int last = textInfo.lineInfo[i].lastCharacterIndex;
// Determine the center of each line
Vector3 centerOfLine = (textInfo.characterInfo[first].bottomLeft + textInfo.characterInfo[last].topRight) / 2;
Quaternion rotation = Quaternion.Euler(0, 0, Random.Range(-0.25f, 0.25f) * RotationMultiplier);
// Iterate through each character of the line.
for (int j = first; j <= last; j++)
{
// Skip characters that are not visible and thus have no geometry to manipulate.
if (!textInfo.characterInfo[j].isVisible)
continue;
// Get the index of the material used by the current character.
int materialIndex = textInfo.characterInfo[j].materialReferenceIndex;
// Get the index of the first vertex used by this text element.
int vertexIndex = textInfo.characterInfo[j].vertexIndex;
// Get the vertices of the mesh used by this text element (character or sprite).
Vector3[] sourceVertices = textInfo.meshInfo[materialIndex].vertices;
// Need to translate all 4 vertices of each quad to aligned with center of character.
// This is needed so the matrix TRS is applied at the origin for each character.
copyOfVertices[materialIndex][vertexIndex + 0] = sourceVertices[vertexIndex + 0] - centerOfLine;
copyOfVertices[materialIndex][vertexIndex + 1] = sourceVertices[vertexIndex + 1] - centerOfLine;
copyOfVertices[materialIndex][vertexIndex + 2] = sourceVertices[vertexIndex + 2] - centerOfLine;
copyOfVertices[materialIndex][vertexIndex + 3] = sourceVertices[vertexIndex + 3] - centerOfLine;
// Determine the random scale change for each character.
float randomScale = Random.Range(0.995f - 0.001f * ScaleMultiplier, 1.005f + 0.001f * ScaleMultiplier);
// Setup the matrix rotation.
matrix = Matrix4x4.TRS(Vector3.one, rotation, Vector3.one * randomScale);
// Apply the matrix TRS to the individual characters relative to the center of the current line.
copyOfVertices[materialIndex][vertexIndex + 0] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 0]);
copyOfVertices[materialIndex][vertexIndex + 1] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 1]);
copyOfVertices[materialIndex][vertexIndex + 2] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 2]);
copyOfVertices[materialIndex][vertexIndex + 3] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 3]);
// Revert the translation change.
copyOfVertices[materialIndex][vertexIndex + 0] += centerOfLine;
copyOfVertices[materialIndex][vertexIndex + 1] += centerOfLine;
copyOfVertices[materialIndex][vertexIndex + 2] += centerOfLine;
copyOfVertices[materialIndex][vertexIndex + 3] += centerOfLine;
}
}
// Push changes into meshes
for (int i = 0; i < textInfo.meshInfo.Length; i++)
{
textInfo.meshInfo[i].mesh.vertices = copyOfVertices[i];
m_TextComponent.UpdateGeometry(textInfo.meshInfo[i].mesh, i);
}
yield return new WaitForSeconds(0.1f);
}
}
}
}
\ No newline at end of file
using UnityEngine;
using System.Collections;
namespace TMPro.Examples
{
public class VertexShakeB : MonoBehaviour
{
public float AngleMultiplier = 1.0f;
public float SpeedMultiplier = 1.0f;
public float CurveScale = 1.0f;
private TMP_Text m_TextComponent;
private bool hasTextChanged;
void Awake()
{
m_TextComponent = GetComponent<TMP_Text>();
}
void OnEnable()
{
// Subscribe to event fired when text object has been regenerated.
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;
}
/// <summary>
/// Method to animate vertex colors of a TMP Text object.
/// </summary>
/// <returns></returns>
IEnumerator AnimateVertexColors()
{
// 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.
// Alternatively, we could yield and wait until the end of the frame when the text object will be generated.
m_TextComponent.ForceMeshUpdate();
TMP_TextInfo textInfo = m_TextComponent.textInfo;
Matrix4x4 matrix;
Vector3[][] copyOfVertices = new Vector3[0][];
hasTextChanged = true;
while (true)
{
// Allocate new vertices
if (hasTextChanged)
{
if (copyOfVertices.Length < textInfo.meshInfo.Length)
copyOfVertices = new Vector3[textInfo.meshInfo.Length][];
for (int i = 0; i < textInfo.meshInfo.Length; i++)
{
int length = textInfo.meshInfo[i].vertices.Length;
copyOfVertices[i] = new Vector3[length];
}
hasTextChanged = false;
}
int characterCount = textInfo.characterCount;
// If No Characters then just yield and wait for some text to be added
if (characterCount == 0)
{
yield return new WaitForSeconds(0.25f);
continue;
}
int lineCount = textInfo.lineCount;
// Iterate through each line of the text.
for (int i = 0; i < lineCount; i++)
{
int first = textInfo.lineInfo[i].firstCharacterIndex;
int last = textInfo.lineInfo[i].lastCharacterIndex;
// Determine the center of each line
Vector3 centerOfLine = (textInfo.characterInfo[first].bottomLeft + textInfo.characterInfo[last].topRight) / 2;
Quaternion rotation = Quaternion.Euler(0, 0, Random.Range(-0.25f, 0.25f));
// Iterate through each character of the line.
for (int j = first; j <= last; j++)
{
// Skip characters that are not visible and thus have no geometry to manipulate.
if (!textInfo.characterInfo[j].isVisible)
continue;
// Get the index of the material used by the current character.
int materialIndex = textInfo.characterInfo[j].materialReferenceIndex;
// Get the index of the first vertex used by this text element.
int vertexIndex = textInfo.characterInfo[j].vertexIndex;
// Get the vertices of the mesh used by this text element (character or sprite).
Vector3[] sourceVertices = textInfo.meshInfo[materialIndex].vertices;
// Determine the center point of each character at the baseline.
Vector3 charCenter = (sourceVertices[vertexIndex + 0] + sourceVertices[vertexIndex + 2]) / 2;
// Need to translate all 4 vertices of each quad to aligned with center of character.
// This is needed so the matrix TRS is applied at the origin for each character.
copyOfVertices[materialIndex][vertexIndex + 0] = sourceVertices[vertexIndex + 0] - charCenter;
copyOfVertices[materialIndex][vertexIndex + 1] = sourceVertices[vertexIndex + 1] - charCenter;
copyOfVertices[materialIndex][vertexIndex + 2] = sourceVertices[vertexIndex + 2] - charCenter;
copyOfVertices[materialIndex][vertexIndex + 3] = sourceVertices[vertexIndex + 3] - charCenter;
// Determine the random scale change for each character.
float randomScale = Random.Range(0.95f, 1.05f);
// Setup the matrix for the scale change.
matrix = Matrix4x4.TRS(Vector3.one, Quaternion.identity, Vector3.one * randomScale);
// Apply the scale change relative to the center of each character.
copyOfVertices[materialIndex][vertexIndex + 0] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 0]);
copyOfVertices[materialIndex][vertexIndex + 1] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 1]);
copyOfVertices[materialIndex][vertexIndex + 2] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 2]);
copyOfVertices[materialIndex][vertexIndex + 3] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 3]);
// Revert the translation change.
copyOfVertices[materialIndex][vertexIndex + 0] += charCenter;
copyOfVertices[materialIndex][vertexIndex + 1] += charCenter;
copyOfVertices[materialIndex][vertexIndex + 2] += charCenter;
copyOfVertices[materialIndex][vertexIndex + 3] += charCenter;
// Need to translate all 4 vertices of each quad to aligned with the center of the line.
// This is needed so the matrix TRS is applied from the center of the line.
copyOfVertices[materialIndex][vertexIndex + 0] -= centerOfLine;
copyOfVertices[materialIndex][vertexIndex + 1] -= centerOfLine;
copyOfVertices[materialIndex][vertexIndex + 2] -= centerOfLine;
copyOfVertices[materialIndex][vertexIndex + 3] -= centerOfLine;
// Setup the matrix rotation.
matrix = Matrix4x4.TRS(Vector3.one, rotation, Vector3.one);
// Apply the matrix TRS to the individual characters relative to the center of the current line.
copyOfVertices[materialIndex][vertexIndex + 0] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 0]);
copyOfVertices[materialIndex][vertexIndex + 1] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 1]);
copyOfVertices[materialIndex][vertexIndex + 2] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 2]);
copyOfVertices[materialIndex][vertexIndex + 3] = matrix.MultiplyPoint3x4(copyOfVertices[materialIndex][vertexIndex + 3]);
// Revert the translation change.
copyOfVertices[materialIndex][vertexIndex + 0] += centerOfLine;
copyOfVertices[materialIndex][vertexIndex + 1] += centerOfLine;
copyOfVertices[materialIndex][vertexIndex + 2] += centerOfLine;
copyOfVertices[materialIndex][vertexIndex + 3] += centerOfLine;
}
}
// Push changes into meshes
for (int i = 0; i < textInfo.meshInfo.Length; i++)
{
textInfo.meshInfo[i].mesh.vertices = copyOfVertices[i];
m_TextComponent.UpdateGeometry(textInfo.meshInfo[i].mesh, i);
}
yield return new WaitForSeconds(0.1f);
}
}
}
}
\ No newline at end of file
using UnityEngine;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
namespace TMPro.Examples
{
public class VertexZoom : MonoBehaviour
{
public float AngleMultiplier = 1.0f;
public float SpeedMultiplier = 1.0f;
public float CurveScale = 1.0f;
private TMP_Text m_TextComponent;
private bool hasTextChanged;
void Awake()
{
m_TextComponent = GetComponent<TMP_Text>();
}
void OnEnable()
{
// Subscribe to event fired when text object has been regenerated.
TMPro_EventManager.TEXT_CHANGED_EVENT.Add(ON_TEXT_CHANGED);
}
void OnDisable()
{
// UnSubscribe to event fired when text object has been regenerated.
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;
}
/// <summary>
/// Method to animate vertex colors of a TMP Text object.
/// </summary>
/// <returns></returns>
IEnumerator AnimateVertexColors()
{
// 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.
// Alternatively, we could yield and wait until the end of the frame when the text object will be generated.
m_TextComponent.ForceMeshUpdate();
TMP_TextInfo textInfo = m_TextComponent.textInfo;
Matrix4x4 matrix;
TMP_MeshInfo[] cachedMeshInfoVertexData = textInfo.CopyMeshInfoVertexData();
// Allocations for sorting of the modified scales
List<float> modifiedCharScale = new List<float>();
List<int> scaleSortingOrder = new List<int>();
hasTextChanged = true;
while (true)
{
// Allocate new vertices
if (hasTextChanged)
{
// Get updated vertex data
cachedMeshInfoVertexData = textInfo.CopyMeshInfoVertexData();
hasTextChanged = false;
}
int characterCount = textInfo.characterCount;
// If No Characters then just yield and wait for some text to be added
if (characterCount == 0)
{
yield return new WaitForSeconds(0.25f);
continue;
}
// Clear list of character scales
modifiedCharScale.Clear();
scaleSortingOrder.Clear();
for (int i = 0; i < characterCount; i++)
{
TMP_CharacterInfo charInfo = textInfo.characterInfo[i];
// Skip characters that are not visible and thus have no geometry to manipulate.
if (!charInfo.isVisible)
continue;
// Get the index of the material used by the current character.
int materialIndex = textInfo.characterInfo[i].materialReferenceIndex;
// Get the index of the first vertex used by this text element.
int vertexIndex = textInfo.characterInfo[i].vertexIndex;
// Get the cached vertices of the mesh used by this text element (character or sprite).
Vector3[] sourceVertices = cachedMeshInfoVertexData[materialIndex].vertices;
// Determine the center point of each character at the baseline.
//Vector2 charMidBasline = new Vector2((sourceVertices[vertexIndex + 0].x + sourceVertices[vertexIndex + 2].x) / 2, charInfo.baseLine);
// Determine the center point of each character.
Vector2 charMidBasline = (sourceVertices[vertexIndex + 0] + sourceVertices[vertexIndex + 2]) / 2;
// Need to translate all 4 vertices of each quad to aligned with middle of character / baseline.
// This is needed so the matrix TRS is applied at the origin for each character.
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;
//Vector3 jitterOffset = new Vector3(Random.Range(-.25f, .25f), Random.Range(-.25f, .25f), 0);
// Determine the random scale change for each character.
float randomScale = Random.Range(1f, 1.5f);
// Add modified scale and index
modifiedCharScale.Add(randomScale);
scaleSortingOrder.Add(modifiedCharScale.Count - 1);
// Setup the matrix for the scale change.
//matrix = Matrix4x4.TRS(jitterOffset, Quaternion.Euler(0, 0, Random.Range(-5f, 5f)), Vector3.one * randomScale);
matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, Vector3.one * randomScale);
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;
// Restore Source UVS which have been modified by the sorting
Vector2[] sourceUVs0 = cachedMeshInfoVertexData[materialIndex].uvs0;
Vector2[] destinationUVs0 = textInfo.meshInfo[materialIndex].uvs0;
destinationUVs0[vertexIndex + 0] = sourceUVs0[vertexIndex + 0];
destinationUVs0[vertexIndex + 1] = sourceUVs0[vertexIndex + 1];
destinationUVs0[vertexIndex + 2] = sourceUVs0[vertexIndex + 2];
destinationUVs0[vertexIndex + 3] = sourceUVs0[vertexIndex + 3];
// Restore Source Vertex Colors
Color32[] sourceColors32 = cachedMeshInfoVertexData[materialIndex].colors32;
Color32[] destinationColors32 = textInfo.meshInfo[materialIndex].colors32;
destinationColors32[vertexIndex + 0] = sourceColors32[vertexIndex + 0];
destinationColors32[vertexIndex + 1] = sourceColors32[vertexIndex + 1];
destinationColors32[vertexIndex + 2] = sourceColors32[vertexIndex + 2];
destinationColors32[vertexIndex + 3] = sourceColors32[vertexIndex + 3];
}
// Push changes into meshes
for (int i = 0; i < textInfo.meshInfo.Length; i++)
{
//// Sort Quads based modified scale
scaleSortingOrder.Sort((a, b) => modifiedCharScale[a].CompareTo(modifiedCharScale[b]));
textInfo.meshInfo[i].SortGeometry(scaleSortingOrder);
// Updated modified vertex attributes
textInfo.meshInfo[i].mesh.vertices = textInfo.meshInfo[i].vertices;
textInfo.meshInfo[i].mesh.uv = textInfo.meshInfo[i].uvs0;
textInfo.meshInfo[i].mesh.colors32 = textInfo.meshInfo[i].colors32;
m_TextComponent.UpdateGeometry(textInfo.meshInfo[i].mesh, i);
}
yield return new WaitForSeconds(0.1f);
}
}
}
}
\ No newline at end of file
using UnityEngine;
using System.Collections;
namespace TMPro.Examples
{
public class WarpTextExample : MonoBehaviour
{
private TMP_Text m_TextComponent;
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));
public float AngleMultiplier = 1.0f;
public float SpeedMultiplier = 1.0f;
public float CurveScale = 1.0f;
void Awake()
{
m_TextComponent = gameObject.GetComponent<TMP_Text>();
}
void Start()
{
StartCoroutine(WarpText());
}
private AnimationCurve CopyAnimationCurve(AnimationCurve curve)
{
AnimationCurve newCurve = new AnimationCurve();
newCurve.keys = curve.keys;
return newCurve;
}
/// <summary>
/// Method to curve text along a Unity animation curve.
/// </summary>
/// <param name="textComponent"></param>
/// <returns></returns>
IEnumerator WarpText()
{
VertexCurve.preWrapMode = WrapMode.Clamp;
VertexCurve.postWrapMode = WrapMode.Clamp;
//Mesh mesh = m_TextComponent.textInfo.meshInfo[0].mesh;
Vector3[] vertices;
Matrix4x4 matrix;
m_TextComponent.havePropertiesChanged = true; // Need to force the TextMeshPro Object to be updated.
CurveScale *= 10;
float old_CurveScale = CurveScale;
AnimationCurve old_curve = CopyAnimationCurve(VertexCurve);
while (true)
{
if (!m_TextComponent.havePropertiesChanged && old_CurveScale == CurveScale && old_curve.keys[1].value == VertexCurve.keys[1].value)
{
yield return null;
continue;
}
old_CurveScale = CurveScale;
old_curve = CopyAnimationCurve(VertexCurve);
m_TextComponent.ForceMeshUpdate(); // Generate the mesh and populate the textInfo with data we can use and manipulate.
TMP_TextInfo textInfo = m_TextComponent.textInfo;
int characterCount = textInfo.characterCount;
if (characterCount == 0) continue;
//vertices = textInfo.meshInfo[0].vertices;
//int lastVertexIndex = textInfo.characterInfo[characterCount - 1].vertexIndex;
float boundsMinX = m_TextComponent.bounds.min.x; //textInfo.meshInfo[0].mesh.bounds.min.x;
float boundsMaxX = m_TextComponent.bounds.max.x; //textInfo.meshInfo[0].mesh.bounds.max.x;
for (int i = 0; i < characterCount; i++)
{
if (!textInfo.characterInfo[i].isVisible)
continue;
int vertexIndex = textInfo.characterInfo[i].vertexIndex;
// Get the index of the mesh used by this character.
int materialIndex = textInfo.characterInfo[i].materialReferenceIndex;
vertices = textInfo.meshInfo[materialIndex].vertices;
// Compute the baseline mid point for each character
Vector3 offsetToMidBaseline = new Vector2((vertices[vertexIndex + 0].x + vertices[vertexIndex + 2].x) / 2, textInfo.characterInfo[i].baseLine);
//float offsetY = VertexCurve.Evaluate((float)i / characterCount + loopCount / 50f); // Random.Range(-0.25f, 0.25f);
// Apply offset to adjust our pivot point.
vertices[vertexIndex + 0] += -offsetToMidBaseline;
vertices[vertexIndex + 1] += -offsetToMidBaseline;
vertices[vertexIndex + 2] += -offsetToMidBaseline;
vertices[vertexIndex + 3] += -offsetToMidBaseline;
// Compute the angle of rotation for each character based on the animation curve
float x0 = (offsetToMidBaseline.x - boundsMinX) / (boundsMaxX - boundsMinX); // Character's position relative to the bounds of the mesh.
float x1 = x0 + 0.0001f;
float y0 = VertexCurve.Evaluate(x0) * CurveScale;
float y1 = VertexCurve.Evaluate(x1) * CurveScale;
Vector3 horizontal = new Vector3(1, 0, 0);
//Vector3 normal = new Vector3(-(y1 - y0), (x1 * (boundsMaxX - boundsMinX) + boundsMinX) - offsetToMidBaseline.x, 0);
Vector3 tangent = new Vector3(x1 * (boundsMaxX - boundsMinX) + boundsMinX, y1) - new Vector3(offsetToMidBaseline.x, y0);
float dot = Mathf.Acos(Vector3.Dot(horizontal, tangent.normalized)) * 57.2957795f;
Vector3 cross = Vector3.Cross(horizontal, tangent);
float angle = cross.z > 0 ? dot : 360 - dot;
matrix = Matrix4x4.TRS(new Vector3(0, y0, 0), Quaternion.Euler(0, 0, angle), Vector3.one);
vertices[vertexIndex + 0] = matrix.MultiplyPoint3x4(vertices[vertexIndex + 0]);
vertices[vertexIndex + 1] = matrix.MultiplyPoint3x4(vertices[vertexIndex + 1]);
vertices[vertexIndex + 2] = matrix.MultiplyPoint3x4(vertices[vertexIndex + 2]);
vertices[vertexIndex + 3] = matrix.MultiplyPoint3x4(vertices[vertexIndex + 3]);
vertices[vertexIndex + 0] += offsetToMidBaseline;
vertices[vertexIndex + 1] += offsetToMidBaseline;
vertices[vertexIndex + 2] += offsetToMidBaseline;
vertices[vertexIndex + 3] += offsetToMidBaseline;
}
// Upload the mesh with the revised information
m_TextComponent.UpdateVertexData();
yield return new WaitForSeconds(0.025f);
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment