TextRenderer cleanup

feature-better-tweakables
Andrew Cassidy 4 years ago
parent 2d299f99f3
commit 5faa26ae95

@ -1,5 +1,4 @@
using System; using System;
using System.Reflection;
using System.Collections.Generic; using System.Collections.Generic;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
@ -38,6 +37,7 @@ namespace ConformalDecals.Text {
private static readonly Dictionary<DecalText, TextRenderOutput> RenderCache = new Dictionary<DecalText, TextRenderOutput>(); private static readonly Dictionary<DecalText, TextRenderOutput> RenderCache = new Dictionary<DecalText, TextRenderOutput>();
private static readonly Queue<TextRenderJob> RenderJobs = new Queue<TextRenderJob>(); private static readonly Queue<TextRenderJob> RenderJobs = new Queue<TextRenderJob>();
// Update text using the job queue
public static TextRenderJob UpdateText(DecalText oldText, DecalText newText, UnityAction<TextRenderOutput> renderFinishedCallback) { public static TextRenderJob UpdateText(DecalText oldText, DecalText newText, UnityAction<TextRenderOutput> renderFinishedCallback) {
if (newText == null) throw new ArgumentNullException(nameof(newText)); if (newText == null) throw new ArgumentNullException(nameof(newText));
@ -46,6 +46,7 @@ namespace ConformalDecals.Text {
return job; return job;
} }
// Update text immediately without using job queue
public static TextRenderOutput UpdateTextNow(DecalText oldText, DecalText newText) { public static TextRenderOutput UpdateTextNow(DecalText oldText, DecalText newText) {
if (newText == null) throw new ArgumentNullException(nameof(newText)); if (newText == null) throw new ArgumentNullException(nameof(newText));
@ -74,16 +75,6 @@ namespace ConformalDecals.Text {
DontDestroyOnLoad(gameObject); DontDestroyOnLoad(gameObject);
} }
private void Update() {
// TODO: ASYNC RENDERING
// bool renderNeeded;
// do {
// if (RenderJobs.Count == 0) return;
// var nextJob = RenderJobs.Dequeue();
// RunJob(nextJob, out renderNeeded);
// } while (!renderNeeded);
}
private void Setup() { private void Setup() {
if (_isSetup) return; if (_isSetup) return;
@ -98,6 +89,7 @@ namespace ConformalDecals.Text {
_isSetup = true; _isSetup = true;
} }
// Run a text render job
private TextRenderOutput RunJob(TextRenderJob job, out bool renderNeeded) { private TextRenderOutput RunJob(TextRenderJob job, out bool renderNeeded) {
if (!job.Needed) { if (!job.Needed) {
renderNeeded = false; renderNeeded = false;
@ -131,32 +123,27 @@ namespace ConformalDecals.Text {
// now that all old references are handled, begin rendering the new output // now that all old references are handled, begin rendering the new output
if (RenderCache.TryGetValue(job.NewText, out var cachedRender)) { if (RenderCache.TryGetValue(job.NewText, out var renderOutput)) {
Debug.Log("Using Cached Render Output");
Debug.Log($"Finished Text Rendering Job. queue depth = {RenderJobs.Count}, cache size = {RenderCache.Count}");
cachedRender.UserCount++;
job.Finish(cachedRender);
renderNeeded = false; renderNeeded = false;
return cachedRender;
} }
else {
renderNeeded = true;
var output = RenderText(job.NewText, texture); renderOutput = RenderText(job.NewText, texture);
output.UserCount++; RenderCache.Add(job.NewText, renderOutput);
RenderCache.Add(job.NewText, output); }
job.Finish(output); renderOutput.UserCount++;
Debug.Log($"Finished Text Rendering Job. queue depth = {RenderJobs.Count}, cache size = {RenderCache.Count}");
renderNeeded = true; job.Finish(renderOutput);
return output; return renderOutput;
} }
// Render a piece of text to a given texture
public TextRenderOutput RenderText(DecalText text, Texture2D texture) { public TextRenderOutput RenderText(DecalText text, Texture2D texture) {
if (text == null) throw new ArgumentNullException(nameof(text)); if (text == null) throw new ArgumentNullException(nameof(text));
if (_tmp == null) throw new InvalidOperationException("TextMeshPro object not yet created."); if (_tmp == null) throw new InvalidOperationException("TextMeshPro object not yet created.");
Debug.Log($"[ConformalDecals] rendering text '{text.Text}' in {text.Font.Name}");
// SETUP TMP OBJECT FOR RENDERING // SETUP TMP OBJECT FOR RENDERING
_tmp.text = text.FormattedText; _tmp.text = text.FormattedText;
_tmp.font = text.Font.FontAsset; _tmp.font = text.Font.FontAsset;
@ -181,7 +168,6 @@ namespace ConformalDecals.Text {
var bounds = new Bounds(); var bounds = new Bounds();
Debug.Log($"meshFilter count: {meshFilters.Length}");
// SETUP MATERIALS AND BOUNDS // SETUP MATERIALS AND BOUNDS
for (int i = 0; i < meshFilters.Length; i++) { for (int i = 0; i < meshFilters.Length; i++) {
var renderer = meshFilters[i].gameObject.GetComponent<MeshRenderer>(); var renderer = meshFilters[i].gameObject.GetComponent<MeshRenderer>();
@ -215,8 +201,6 @@ namespace ConformalDecals.Text {
return new TextRenderOutput(Texture2D.blackTexture, Rect.zero); return new TextRenderOutput(Texture2D.blackTexture, Rect.zero);
} }
Debug.Log($"Texture size: {textureSize}");
// make sure texture isnt too big, scale it down if it is // make sure texture isnt too big, scale it down if it is
// this is just so you dont crash the game by pasting in the entire script of The Bee Movie // this is just so you dont crash the game by pasting in the entire script of The Bee Movie
if (textureSize.x > MaxTextureSize) { if (textureSize.x > MaxTextureSize) {
@ -238,8 +222,6 @@ namespace ConformalDecals.Text {
center = (Vector2) textureSize / 2 center = (Vector2) textureSize / 2
}; };
Debug.Log($"Window size: {window.size}");
// SETUP TEXTURE // SETUP TEXTURE
if (texture == null) { if (texture == null) {
texture = new Texture2D(textureSize.x, textureSize.y, TextTextureFormat, true); texture = new Texture2D(textureSize.x, textureSize.y, TextTextureFormat, true);

Loading…
Cancel
Save