using System.Collections; using System.Collections.Generic; using UnityEngine; using Cyber.Console; namespace Cyber.Util { /// /// Renders textures that have text written on them. /// public class TextTextureRenderer : MonoBehaviour { private static TextTextureRenderer Singleton; private static Dictionary Cache = new Dictionary(); /// /// The camera the text is rendered with. /// public Camera Camera; /// /// The text that is rendered on screen. /// public TextMesh Text; /// /// Sets the singleton. /// public TextTextureRenderer() { Singleton = this; } private Texture2D RenderText(TextTextureProperties text) { float Scale = 1.0f / text.Width; Text.text = text.Text.Replace("\\n", "\n"); Text.fontSize = text.FontSize; Text.characterSize = text.FontSize * Scale * 0.5f; RenderTexture TextTexture = RenderTexture.GetTemporary(text.Width, text.Height); RenderTexture OldRT = Camera.targetTexture; Camera.targetTexture = TextTexture; Camera.backgroundColor = text.Background; Camera.transform.localPosition = new Vector3(-text.OffsetX * Scale, text.OffsetY * Scale); Camera.Render(); Camera.targetTexture = OldRT; OldRT = RenderTexture.active; Texture2D Result = new Texture2D(text.Width, text.Height); RenderTexture.active = TextTexture; Result.ReadPixels(new Rect(0, 0, Result.width, Result.height), 0, 0); Result.Apply(); RenderTexture.active = OldRT; RenderTexture.ReleaseTemporary(TextTexture); return Result; } /// /// Renders a texture and caches it so it can be used as a texture /// for a material (like a computer screen for example). /// /// The text. /// Text. /// Whether the texture should be rendered /// again even if it's cached. public static Texture2D GetText(TextTextureProperties text, bool forceRender = false) { string Hash = CreateHash(text); if (forceRender || !Cache.ContainsKey(Hash)) { Cache[Hash] = Singleton.RenderText(text); } return Cache[Hash]; } private static string CreateHash(TextTextureProperties text) { return text.Text + "," + text.OffsetX + "," + text.OffsetY + "," + text.Width + "," + text.Height + "," + text.FontSize + "," + text.Background.r + "," + text.Background.g + "," + text.Background.b; } } }