diff --git a/Source/ConformalDecals/ModuleConformalDecal.cs b/Source/ConformalDecals/ModuleConformalDecal.cs index 7e9fb39..8f4ea68 100644 --- a/Source/ConformalDecals/ModuleConformalDecal.cs +++ b/Source/ConformalDecals/ModuleConformalDecal.cs @@ -246,7 +246,7 @@ namespace ConformalDecals { _boundsRenderer = decalProjectorTransform.GetComponent(); - UpdateMaterials(); + //UpdateMaterials(); // handle tweakables if (HighLogic.LoadedSceneIsEditor) { @@ -456,7 +456,7 @@ namespace ConformalDecals { } } - protected void UpdateMaterials() { + protected virtual void UpdateMaterials() { materialProperties.UpdateMaterials(); materialProperties.SetOpacity(opacity); materialProperties.SetCutoff(cutoff); diff --git a/Source/ConformalDecals/ModuleConformalText.cs b/Source/ConformalDecals/ModuleConformalText.cs index 018e204..1b42611 100644 --- a/Source/ConformalDecals/ModuleConformalText.cs +++ b/Source/ConformalDecals/ModuleConformalText.cs @@ -1,6 +1,6 @@ +using ConformalDecals.MaterialProperties; using ConformalDecals.Text; using ConformalDecals.UI; -using ConformalDecals.Util; using TMPro; using UnityEngine; @@ -68,6 +68,9 @@ namespace ConformalDecals { private ColorPickerController _fillColorPickerController; private ColorPickerController _outlineColorPickerCOntroller; + private TextRenderJob _currentJob; + private DecalText _currentText; + public override void OnLoad(ConfigNode node) { base.OnLoad(node); OnAfterDeserialize(); @@ -81,53 +84,14 @@ namespace ConformalDecals { public override void OnStart(StartState state) { base.OnStart(state); - _font = DecalConfig.GetFont(fontName); - _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; - } + UpdateTextRecursive(); } public void OnTextUpdate(string newText, DecalFont newFont, DecalTextStyle newStyle) { text = newText; _font = newFont; _style = newStyle; + UpdateTextRecursive(); } public void OnFillColorUpdate(Color rgb, Util.ColorHSV hsv) { @@ -166,5 +130,62 @@ namespace ConformalDecals { _font = DecalConfig.GetFont(fontName); _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(); + 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("DECAL_FILL").value = fillEnabled; + materialProperties.AddOrGetProperty("DECAL_OUTLINE").value = outlineEnabled; + if (fillEnabled) { + materialProperties.AddOrGetProperty("_DecalColor").color = fillColor; + } + + if (outlineEnabled) { + materialProperties.AddOrGetProperty("_OutlineColor").color = outlineColor; + materialProperties.AddOrGetProperty("_OutlineWidth").value = outlineWidth; + } + + base.UpdateMaterials(); + } } } \ No newline at end of file diff --git a/Source/ConformalDecals/Text/TextRenderer.cs b/Source/ConformalDecals/Text/TextRenderer.cs index d07d882..7f0e76d 100644 --- a/Source/ConformalDecals/Text/TextRenderer.cs +++ b/Source/ConformalDecals/Text/TextRenderer.cs @@ -37,17 +37,19 @@ namespace ConformalDecals.Text { private static readonly Dictionary RenderCache = new Dictionary(); private static readonly Queue RenderJobs = new Queue(); - public static TextRenderJob RenderText(DecalText text, UnityAction renderFinishedCallback) { - var job = new TextRenderJob(text, renderFinishedCallback); - RenderJobs.Enqueue(job); - return job; - } - public static TextRenderJob UpdateText(DecalText oldText, DecalText newText, UnityAction renderFinishedCallback) { + if (newText == null) throw new ArgumentNullException(nameof(newText)); + var job = new TextRenderJob(oldText, newText, renderFinishedCallback); RenderJobs.Enqueue(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) { if (RenderCache.TryGetValue(text, out var renderedText)) { @@ -59,6 +61,8 @@ namespace ConformalDecals.Text { } } + + private void Start() { if (_instance != null) { Debug.Log("[ConformalDecals] Duplicate TextRenderer created???"); @@ -72,9 +76,9 @@ namespace ConformalDecals.Text { private void Update() { bool renderNeeded; do { - if (RenderJobs.Count <= 0) return; + if (RenderJobs.Count == 0) return; var nextJob = RenderJobs.Dequeue(); - renderNeeded = nextJob.Needed && RunJob(nextJob); + RunJob(nextJob, out renderNeeded); } while (!renderNeeded); } @@ -93,7 +97,12 @@ namespace ConformalDecals.Text { _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}"); job.Start(); @@ -123,7 +132,9 @@ namespace ConformalDecals.Text { Debug.Log($"Finished Text Rendering Job. queue depth = {RenderJobs.Count}, cache size = {RenderCache.Count}"); cachedRender.UserCount++; - return false; + job.Finish(cachedRender); + renderNeeded = false; + return cachedRender; } var output = RenderText(job.NewText, texture); @@ -131,7 +142,8 @@ namespace ConformalDecals.Text { job.Finish(output); 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) {