Non-async text rendering integration

This commit is contained in:
Andrew Cassidy 2020-07-27 16:16:15 -07:00
parent f3bda0feb6
commit 8d73541a42
No known key found for this signature in database
GPG Key ID: 963017B38FD477A1
3 changed files with 88 additions and 55 deletions

View File

@ -246,7 +246,7 @@ namespace ConformalDecals {
_boundsRenderer = decalProjectorTransform.GetComponent<MeshRenderer>(); _boundsRenderer = decalProjectorTransform.GetComponent<MeshRenderer>();
UpdateMaterials(); //UpdateMaterials();
// handle tweakables // handle tweakables
if (HighLogic.LoadedSceneIsEditor) { if (HighLogic.LoadedSceneIsEditor) {
@ -456,7 +456,7 @@ namespace ConformalDecals {
} }
} }
protected void UpdateMaterials() { protected virtual void UpdateMaterials() {
materialProperties.UpdateMaterials(); materialProperties.UpdateMaterials();
materialProperties.SetOpacity(opacity); materialProperties.SetOpacity(opacity);
materialProperties.SetCutoff(cutoff); materialProperties.SetCutoff(cutoff);

View File

@ -1,6 +1,6 @@
using ConformalDecals.MaterialProperties;
using ConformalDecals.Text; using ConformalDecals.Text;
using ConformalDecals.UI; using ConformalDecals.UI;
using ConformalDecals.Util;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
@ -68,6 +68,9 @@ namespace ConformalDecals {
private ColorPickerController _fillColorPickerController; private ColorPickerController _fillColorPickerController;
private ColorPickerController _outlineColorPickerCOntroller; private ColorPickerController _outlineColorPickerCOntroller;
private TextRenderJob _currentJob;
private DecalText _currentText;
public override void OnLoad(ConfigNode node) { public override void OnLoad(ConfigNode node) {
base.OnLoad(node); base.OnLoad(node);
OnAfterDeserialize(); OnAfterDeserialize();
@ -81,53 +84,14 @@ namespace ConformalDecals {
public override void OnStart(StartState state) { public override void OnStart(StartState state) {
base.OnStart(state); base.OnStart(state);
_font = DecalConfig.GetFont(fontName); UpdateTextRecursive();
_style = new DecalTextStyle();
// handle tweakables
if (HighLogic.LoadedSceneIsEditor) {
GameEvents.onEditorPartEvent.Add(OnEditorEvent);
}
//TextRenderer.Instance.RenderText(decalText, out var texture, out var window);
//materialProperties.AddOrGetTextureProperty("_Decal", true).Texture = texture;
UpdateMaterials();
UpdateScale();
}
public override void OnDestroy() {
base.OnDestroy();
this.Log("OnDestroy");
if (HighLogic.LoadedSceneIsEditor) {
GameEvents.onEditorPartEvent.Remove(OnEditorEvent);
}
}
public void OnPartDeleted() {
this.Log("OnPartDeleted");
}
public void OnPartSymmetryDeleted() {
this.Log("OnPartSymmetryDeleted");
}
protected new void OnEditorEvent(ConstructionEventType eventType, Part eventPart) {
if (eventPart != part) return;
switch (eventType) {
case ConstructionEventType.PartSymmetryDeleted:
OnPartSymmetryDeleted();
break;
case ConstructionEventType.PartDeleted:
OnPartDeleted();
break;
}
} }
public void OnTextUpdate(string newText, DecalFont newFont, DecalTextStyle newStyle) { public void OnTextUpdate(string newText, DecalFont newFont, DecalTextStyle newStyle) {
text = newText; text = newText;
_font = newFont; _font = newFont;
_style = newStyle; _style = newStyle;
UpdateTextRecursive();
} }
public void OnFillColorUpdate(Color rgb, Util.ColorHSV hsv) { public void OnFillColorUpdate(Color rgb, Util.ColorHSV hsv) {
@ -166,5 +130,62 @@ namespace ConformalDecals {
_font = DecalConfig.GetFont(fontName); _font = DecalConfig.GetFont(fontName);
_style = new DecalTextStyle((FontStyles) style, vertical, lineSpacing, characterSpacing); _style = new DecalTextStyle((FontStyles) style, vertical, lineSpacing, characterSpacing);
} }
public override void OnDestroy() {
TextRenderer.UnregisterText(_currentText);
base.OnDestroy();
}
private void UpdateTextRecursive() {
UpdateText();
foreach (var counterpart in part.symmetryCounterparts) {
var decal = counterpart.GetComponent<ModuleConformalText>();
decal.text = text;
decal._font = _font;
decal._style = _style;
decal._currentJob = _currentJob;
decal._currentText = _currentText;
decal.UpdateText();
}
}
private void UpdateText() {
// Render text
var newText = new DecalText(text, _font, _style);
var output = TextRenderer.UpdateTextNow(_currentText, newText);
_currentText = newText;
UpdateTexture(output);
// TODO: ASYNC RENDERING
// var newText = new DecalText(text, _font, _style);
// _currentJob = TextRenderer.UpdateText(_currentText, newText, UpdateTexture);
// _currentText = newText;
}
public void UpdateTexture(TextRenderOutput output) {
var textureProperty = materialProperties.AddOrGetTextureProperty("_Decal", true);
textureProperty.Texture = output.Texture;
textureProperty.SetTile(output.Window);
UpdateMaterials();
}
protected override void UpdateMaterials() {
materialProperties.AddOrGetProperty<MaterialKeywordProperty>("DECAL_FILL").value = fillEnabled;
materialProperties.AddOrGetProperty<MaterialKeywordProperty>("DECAL_OUTLINE").value = outlineEnabled;
if (fillEnabled) {
materialProperties.AddOrGetProperty<MaterialColorProperty>("_DecalColor").color = fillColor;
}
if (outlineEnabled) {
materialProperties.AddOrGetProperty<MaterialColorProperty>("_OutlineColor").color = outlineColor;
materialProperties.AddOrGetProperty<MaterialFloatProperty>("_OutlineWidth").value = outlineWidth;
}
base.UpdateMaterials();
}
} }
} }

View File

@ -37,17 +37,19 @@ 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>();
public static TextRenderJob RenderText(DecalText text, UnityAction<TextRenderOutput> renderFinishedCallback) {
var job = new TextRenderJob(text, renderFinishedCallback);
RenderJobs.Enqueue(job);
return job;
}
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));
var job = new TextRenderJob(oldText, newText, renderFinishedCallback); var job = new TextRenderJob(oldText, newText, renderFinishedCallback);
RenderJobs.Enqueue(job); RenderJobs.Enqueue(job);
return job; return job;
} }
public static TextRenderOutput UpdateTextNow(DecalText oldText, DecalText newText) {
if (newText == null) throw new ArgumentNullException(nameof(newText));
return Instance.RunJob(new TextRenderJob(oldText, newText, null), out _);
}
public static void UnregisterText(DecalText text) { public static void UnregisterText(DecalText text) {
if (RenderCache.TryGetValue(text, out var renderedText)) { if (RenderCache.TryGetValue(text, out var renderedText)) {
@ -59,6 +61,8 @@ namespace ConformalDecals.Text {
} }
} }
private void Start() { private void Start() {
if (_instance != null) { if (_instance != null) {
Debug.Log("[ConformalDecals] Duplicate TextRenderer created???"); Debug.Log("[ConformalDecals] Duplicate TextRenderer created???");
@ -72,9 +76,9 @@ namespace ConformalDecals.Text {
private void Update() { private void Update() {
bool renderNeeded; bool renderNeeded;
do { do {
if (RenderJobs.Count <= 0) return; if (RenderJobs.Count == 0) return;
var nextJob = RenderJobs.Dequeue(); var nextJob = RenderJobs.Dequeue();
renderNeeded = nextJob.Needed && RunJob(nextJob); RunJob(nextJob, out renderNeeded);
} while (!renderNeeded); } while (!renderNeeded);
} }
@ -93,7 +97,12 @@ namespace ConformalDecals.Text {
_isSetup = true; _isSetup = true;
} }
private bool RunJob(TextRenderJob job) { private TextRenderOutput RunJob(TextRenderJob job, out bool renderNeeded) {
if (!job.Needed) {
renderNeeded = false;
return null;
}
Debug.Log($"Starting Text Rendering Job. queue depth = {RenderJobs.Count}, cache size = {RenderCache.Count}"); Debug.Log($"Starting Text Rendering Job. queue depth = {RenderJobs.Count}, cache size = {RenderCache.Count}");
job.Start(); job.Start();
@ -123,7 +132,9 @@ namespace ConformalDecals.Text {
Debug.Log($"Finished Text Rendering Job. queue depth = {RenderJobs.Count}, cache size = {RenderCache.Count}"); Debug.Log($"Finished Text Rendering Job. queue depth = {RenderJobs.Count}, cache size = {RenderCache.Count}");
cachedRender.UserCount++; cachedRender.UserCount++;
return false; job.Finish(cachedRender);
renderNeeded = false;
return cachedRender;
} }
var output = RenderText(job.NewText, texture); var output = RenderText(job.NewText, texture);
@ -131,7 +142,8 @@ namespace ConformalDecals.Text {
job.Finish(output); job.Finish(output);
Debug.Log($"Finished Text Rendering Job. queue depth = {RenderJobs.Count}, cache size = {RenderCache.Count}"); Debug.Log($"Finished Text Rendering Job. queue depth = {RenderJobs.Count}, cache size = {RenderCache.Count}");
return true; renderNeeded = true;
return output;
} }
public TextRenderOutput RenderText(DecalText text, Texture2D texture) { public TextRenderOutput RenderText(DecalText text, Texture2D texture) {