mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
Fix text rendering again, but break bold text again
This commit is contained in:
parent
53f8e74836
commit
ae351c021f
Binary file not shown.
@ -78,7 +78,7 @@ namespace ConformalDecals {
|
|||||||
private ColorPickerController _outlineColorPickerController;
|
private ColorPickerController _outlineColorPickerController;
|
||||||
|
|
||||||
private MaterialTextureProperty _decalTextureProperty;
|
private MaterialTextureProperty _decalTextureProperty;
|
||||||
private MaterialFloatProperty _decalTextWeightProperty;
|
private MaterialFloatProperty _decalTextWeightProperty;
|
||||||
|
|
||||||
private MaterialKeywordProperty _fillEnabledProperty;
|
private MaterialKeywordProperty _fillEnabledProperty;
|
||||||
private MaterialColorProperty _fillColorProperty;
|
private MaterialColorProperty _fillColorProperty;
|
||||||
@ -93,7 +93,7 @@ namespace ConformalDecals {
|
|||||||
public override void OnLoad(ConfigNode node) {
|
public override void OnLoad(ConfigNode node) {
|
||||||
base.OnLoad(node);
|
base.OnLoad(node);
|
||||||
OnAfterDeserialize();
|
OnAfterDeserialize();
|
||||||
|
|
||||||
UpdateTextRecursive();
|
UpdateTextRecursive();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,13 +104,13 @@ namespace ConformalDecals {
|
|||||||
|
|
||||||
public override void OnStart(StartState state) {
|
public override void OnStart(StartState state) {
|
||||||
base.OnStart(state);
|
base.OnStart(state);
|
||||||
|
|
||||||
UpdateTextRecursive();
|
UpdateTextRecursive();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnAwake() {
|
public override void OnAwake() {
|
||||||
base.OnAwake();
|
base.OnAwake();
|
||||||
|
|
||||||
_decalTextureProperty = materialProperties.AddOrGetTextureProperty("_Decal", true);
|
_decalTextureProperty = materialProperties.AddOrGetTextureProperty("_Decal", true);
|
||||||
_decalTextWeightProperty = materialProperties.AddOrGetProperty<MaterialFloatProperty>("_Weight");
|
_decalTextWeightProperty = materialProperties.AddOrGetProperty<MaterialFloatProperty>("_Weight");
|
||||||
|
|
||||||
@ -121,7 +121,7 @@ namespace ConformalDecals {
|
|||||||
_outlineColorProperty = materialProperties.AddOrGetProperty<MaterialColorProperty>("_OutlineColor");
|
_outlineColorProperty = materialProperties.AddOrGetProperty<MaterialColorProperty>("_OutlineColor");
|
||||||
_outlineWidthProperty = materialProperties.AddOrGetProperty<MaterialFloatProperty>("_OutlineWidth");
|
_outlineWidthProperty = materialProperties.AddOrGetProperty<MaterialFloatProperty>("_OutlineWidth");
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
@ -152,30 +152,28 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void OnFillToggle(BaseField field, object obj) {
|
public void OnFillToggle(BaseField field, object obj) {
|
||||||
if (!fillEnabled && !outlineEnabled) {
|
// fill and outline cant both be disabled
|
||||||
outlineEnabled = true;
|
outlineEnabled = outlineEnabled || (!outlineEnabled && !fillEnabled);
|
||||||
OnOutlineToggle(field, obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
UpdateTweakables();
|
UpdateTweakables();
|
||||||
|
|
||||||
foreach (var counterpart in part.symmetryCounterparts) {
|
foreach (var counterpart in part.symmetryCounterparts) {
|
||||||
var decal = counterpart.GetComponent<ModuleConformalText>();
|
var decal = counterpart.GetComponent<ModuleConformalText>();
|
||||||
decal.fillEnabled = fillEnabled;
|
decal.fillEnabled = fillEnabled;
|
||||||
|
decal.outlineEnabled = outlineEnabled;
|
||||||
decal.UpdateTweakables();
|
decal.UpdateTweakables();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnOutlineToggle(BaseField field, object obj) {
|
public void OnOutlineToggle(BaseField field, object obj) {
|
||||||
if (!fillEnabled && !outlineEnabled) {
|
// fill and outline cant both be disabled
|
||||||
fillEnabled = true;
|
fillEnabled = fillEnabled || (!fillEnabled && !outlineEnabled);
|
||||||
OnFillToggle(field, obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
UpdateTweakables();
|
UpdateTweakables();
|
||||||
|
|
||||||
foreach (var counterpart in part.symmetryCounterparts) {
|
foreach (var counterpart in part.symmetryCounterparts) {
|
||||||
var decal = counterpart.GetComponent<ModuleConformalText>();
|
var decal = counterpart.GetComponent<ModuleConformalText>();
|
||||||
|
decal.fillEnabled = fillEnabled;
|
||||||
decal.outlineEnabled = outlineEnabled;
|
decal.outlineEnabled = outlineEnabled;
|
||||||
decal.UpdateTweakables();
|
decal.UpdateTweakables();
|
||||||
}
|
}
|
||||||
@ -206,7 +204,7 @@ namespace ConformalDecals {
|
|||||||
|
|
||||||
public override void OnDestroy() {
|
public override void OnDestroy() {
|
||||||
if (_currentText != null) TextRenderer.UnregisterText(_currentText);
|
if (_currentText != null) TextRenderer.UnregisterText(_currentText);
|
||||||
|
|
||||||
base.OnDestroy();
|
base.OnDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -215,7 +213,7 @@ namespace ConformalDecals {
|
|||||||
if (_textEntryController != null) _textEntryController.OnClose();
|
if (_textEntryController != null) _textEntryController.OnClose();
|
||||||
if (_fillColorPickerController != null) _fillColorPickerController.OnClose();
|
if (_fillColorPickerController != null) _fillColorPickerController.OnClose();
|
||||||
if (_outlineColorPickerController != null) _outlineColorPickerController.OnClose();
|
if (_outlineColorPickerController != null) _outlineColorPickerController.OnClose();
|
||||||
|
|
||||||
base.OnDetach();
|
base.OnDetach();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -251,8 +249,7 @@ namespace ConformalDecals {
|
|||||||
public void UpdateTexture(TextRenderOutput output) {
|
public void UpdateTexture(TextRenderOutput output) {
|
||||||
_decalTextureProperty.Texture = output.Texture;
|
_decalTextureProperty.Texture = output.Texture;
|
||||||
_decalTextureProperty.SetTile(output.Window);
|
_decalTextureProperty.SetTile(output.Window);
|
||||||
_decalTextWeightProperty.value = output.Weight;
|
|
||||||
|
|
||||||
UpdateMaterials();
|
UpdateMaterials();
|
||||||
UpdateScale();
|
UpdateScale();
|
||||||
}
|
}
|
||||||
@ -269,22 +266,17 @@ namespace ConformalDecals {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected override void UpdateTweakables() {
|
protected override void UpdateTweakables() {
|
||||||
Debug.Log($"Fields is null: {Fields == null}");
|
|
||||||
Debug.Log($"Actions is null: {Actions == null}");
|
|
||||||
var fillEnabledField = Fields[nameof(fillEnabled)];
|
var fillEnabledField = Fields[nameof(fillEnabled)];
|
||||||
var fillColorAction = Actions["SetFillColor"];
|
var fillColorEvent = Events["SetFillColor"];
|
||||||
|
|
||||||
var outlineEnabledField = Fields[nameof(outlineEnabled)];
|
var outlineEnabledField = Fields[nameof(outlineEnabled)];
|
||||||
var outlineWidthField = Fields[nameof(outlineWidth)];
|
var outlineWidthField = Fields[nameof(outlineWidth)];
|
||||||
var outlineColorAction = Actions["SetOutlineColor"];
|
var outlineColorEvent = Events["SetOutlineColor"];
|
||||||
|
|
||||||
Debug.Log($"outlineColorAction is null: {outlineColorAction == null}");
|
|
||||||
|
|
||||||
// fillColorAction.activeEditor = fillEnabled;
|
fillColorEvent.guiActiveEditor = fillEnabled;
|
||||||
// outlineWidthField.guiActiveEditor = outlineEnabled;
|
outlineWidthField.guiActiveEditor = outlineEnabled;
|
||||||
// outlineColorAction.activeEditor = outlineEnabled;
|
outlineColorEvent.guiActiveEditor = outlineEnabled;
|
||||||
|
|
||||||
Debug.Log("Fart");
|
|
||||||
((UI_Toggle) fillEnabledField.uiControlEditor).onFieldChanged = OnFillToggle;
|
((UI_Toggle) fillEnabledField.uiControlEditor).onFieldChanged = OnFillToggle;
|
||||||
((UI_Toggle) outlineEnabledField.uiControlEditor).onFieldChanged = OnOutlineToggle;
|
((UI_Toggle) outlineEnabledField.uiControlEditor).onFieldChanged = OnOutlineToggle;
|
||||||
((UI_FloatRange) outlineWidthField.uiControlEditor).onFieldChanged = OnOutlineWidthUpdate;
|
((UI_FloatRange) outlineWidthField.uiControlEditor).onFieldChanged = OnOutlineWidthUpdate;
|
||||||
|
@ -21,6 +21,7 @@ namespace ConformalDecals.Text {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public DecalText(string text, DecalFont font, DecalTextStyle style) {
|
public DecalText(string text, DecalFont font, DecalTextStyle style) {
|
||||||
|
if (font == null) throw new ArgumentNullException(nameof(font));
|
||||||
Text = text;
|
Text = text;
|
||||||
Font = font;
|
Font = font;
|
||||||
Style = style;
|
Style = style;
|
||||||
|
@ -6,14 +6,11 @@ namespace ConformalDecals.Text {
|
|||||||
|
|
||||||
public Rect Window { get; private set; }
|
public Rect Window { get; private set; }
|
||||||
|
|
||||||
public float Weight { get; private set; }
|
|
||||||
|
|
||||||
public int UserCount { get; set; }
|
public int UserCount { get; set; }
|
||||||
|
|
||||||
public TextRenderOutput(Texture2D texture, Rect window, float weight) {
|
public TextRenderOutput(Texture2D texture, Rect window) {
|
||||||
Texture = texture;
|
Texture = texture;
|
||||||
Window = window;
|
Window = window;
|
||||||
Weight = weight;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using TMPro;
|
using TMPro;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
@ -32,7 +33,7 @@ namespace ConformalDecals.Text {
|
|||||||
|
|
||||||
private bool _isSetup;
|
private bool _isSetup;
|
||||||
private TextMeshPro _tmp;
|
private TextMeshPro _tmp;
|
||||||
private Material _blitMaterial;
|
private Shader _blitShader;
|
||||||
|
|
||||||
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>();
|
||||||
@ -44,10 +45,10 @@ namespace ConformalDecals.Text {
|
|||||||
RenderJobs.Enqueue(job);
|
RenderJobs.Enqueue(job);
|
||||||
return job;
|
return job;
|
||||||
}
|
}
|
||||||
|
|
||||||
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));
|
||||||
|
|
||||||
return Instance.RunJob(new TextRenderJob(oldText, newText, null), out _);
|
return Instance.RunJob(new TextRenderJob(oldText, newText, null), out _);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,7 +92,7 @@ namespace ConformalDecals.Text {
|
|||||||
|
|
||||||
var shader = Shabby.Shabby.FindShader(BlitShader);
|
var shader = Shabby.Shabby.FindShader(BlitShader);
|
||||||
if (shader == null) Debug.LogError($"[ConformalDecals] could not find text blit shader named '{shader}'");
|
if (shader == null) Debug.LogError($"[ConformalDecals] could not find text blit shader named '{shader}'");
|
||||||
_blitMaterial = new Material(Shabby.Shabby.FindShader(BlitShader));
|
_blitShader = Shabby.Shabby.FindShader(BlitShader);
|
||||||
|
|
||||||
_isSetup = true;
|
_isSetup = true;
|
||||||
}
|
}
|
||||||
@ -146,6 +147,11 @@ namespace ConformalDecals.Text {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public TextRenderOutput RenderText(DecalText text, Texture2D texture) {
|
public TextRenderOutput RenderText(DecalText text, Texture2D texture) {
|
||||||
|
if (text == null) throw new ArgumentNullException(nameof(text));
|
||||||
|
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;
|
||||||
@ -157,33 +163,54 @@ namespace ConformalDecals.Text {
|
|||||||
_tmp.enableKerning = true;
|
_tmp.enableKerning = true;
|
||||||
_tmp.enableWordWrapping = false;
|
_tmp.enableWordWrapping = false;
|
||||||
_tmp.overflowMode = TextOverflowModes.Overflow;
|
_tmp.overflowMode = TextOverflowModes.Overflow;
|
||||||
_tmp.alignment = TextAlignmentOptions.Center | TextAlignmentOptions.Baseline;
|
_tmp.alignment = TextAlignmentOptions.Center;
|
||||||
_tmp.fontSize = FontSize;
|
_tmp.fontSize = FontSize;
|
||||||
|
|
||||||
// CALCULATE FONT WEIGHT
|
|
||||||
|
|
||||||
float weight = 0;
|
|
||||||
if (text.Style.Bold && text.Font.FontAsset.fontWeights[7].regularTypeface == null) {
|
|
||||||
weight = text.Font.FontAsset.boldStyle;
|
|
||||||
}
|
|
||||||
|
|
||||||
// SETUP BLIT MATERIAL
|
|
||||||
_blitMaterial.SetTexture(PropertyIDs._MainTex, text.Font.FontAsset.atlas);
|
|
||||||
|
|
||||||
// GENERATE MESH
|
// GENERATE MESH
|
||||||
_tmp.ForceMeshUpdate();
|
_tmp.ForceMeshUpdate();
|
||||||
var mesh = _tmp.mesh;
|
|
||||||
mesh.RecalculateBounds();
|
var meshFilters = gameObject.GetComponentsInChildren<MeshFilter>();
|
||||||
var bounds = mesh.bounds;
|
var meshes = new Mesh[meshFilters.Length];
|
||||||
|
var materials = new Material[meshFilters.Length];
|
||||||
|
|
||||||
|
var bounds = new Bounds();
|
||||||
|
|
||||||
|
Debug.Log($"meshFilter count: {meshFilters.Length}");
|
||||||
|
// SETUP MATERIALS AND BOUNDS
|
||||||
|
for (int i = 0; i < meshFilters.Length; i++) {
|
||||||
|
var renderer = meshFilters[i].gameObject.GetComponent<MeshRenderer>();
|
||||||
|
|
||||||
|
meshes[i] = meshFilters[i].mesh;
|
||||||
|
meshes[i].RecalculateBounds();
|
||||||
|
|
||||||
|
materials[i] = Instantiate(renderer.material);
|
||||||
|
materials[i].shader = _blitShader;
|
||||||
|
|
||||||
|
if (renderer == null) throw new FormatException($"Object {meshFilters[i].gameObject.name} has filter but no renderer");
|
||||||
|
if (meshes[i] == null) throw new FormatException($"Object {meshFilters[i].gameObject.name} has a null mesh");
|
||||||
|
|
||||||
|
if (i == 0) {
|
||||||
|
bounds = meshes[i].bounds;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
bounds.Encapsulate(meshes[i].bounds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// CALCULATE SIZES
|
// CALCULATE SIZES
|
||||||
var size = bounds.size * PixelDensity;
|
var size = bounds.size * PixelDensity;
|
||||||
|
|
||||||
var textureSize = new Vector2Int {
|
var textureSize = new Vector2Int {
|
||||||
x = Mathf.NextPowerOfTwo((int) size.x),
|
x = Mathf.NextPowerOfTwo((int) size.x),
|
||||||
y = Mathf.NextPowerOfTwo((int) size.y)
|
y = Mathf.NextPowerOfTwo((int) size.y)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (textureSize.x == 0 || textureSize.y == 0) {
|
||||||
|
Debug.LogWarning("[ConformalDecals] No text present or error in texture size calculation. Aborting.");
|
||||||
|
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) {
|
||||||
@ -204,9 +231,8 @@ namespace ConformalDecals.Text {
|
|||||||
size = size * sizeRatio,
|
size = size * sizeRatio,
|
||||||
center = (Vector2) textureSize / 2
|
center = (Vector2) textureSize / 2
|
||||||
};
|
};
|
||||||
|
|
||||||
Debug.Log($"Window size: {window.size}");
|
Debug.Log($"Window size: {window.size}");
|
||||||
Debug.Log($"Texture size: {textureSize}");
|
|
||||||
|
|
||||||
// SETUP TEXTURE
|
// SETUP TEXTURE
|
||||||
if (texture == null) {
|
if (texture == null) {
|
||||||
@ -230,8 +256,14 @@ namespace ConformalDecals.Text {
|
|||||||
GL.PushMatrix();
|
GL.PushMatrix();
|
||||||
GL.LoadProjectionMatrix(matrix);
|
GL.LoadProjectionMatrix(matrix);
|
||||||
GL.Clear(false, true, Color.black);
|
GL.Clear(false, true, Color.black);
|
||||||
_blitMaterial.SetPass(0);
|
|
||||||
Graphics.DrawMeshNow(mesh, Matrix4x4.identity);
|
for (var i = 0; i < meshes.Length; i++) {
|
||||||
|
if (meshes[i].vertexCount >= 3) {
|
||||||
|
materials[i].SetPass(0);
|
||||||
|
Graphics.DrawMeshNow(meshes[i], Matrix4x4.identity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
GL.PopMatrix();
|
GL.PopMatrix();
|
||||||
|
|
||||||
// COPY TEXTURE BACK INTO RAM
|
// COPY TEXTURE BACK INTO RAM
|
||||||
@ -242,7 +274,12 @@ namespace ConformalDecals.Text {
|
|||||||
// RELEASE RENDERTEX
|
// RELEASE RENDERTEX
|
||||||
RenderTexture.ReleaseTemporary(renderTex);
|
RenderTexture.ReleaseTemporary(renderTex);
|
||||||
|
|
||||||
return new TextRenderOutput(texture, window, weight);
|
// CLEAR SUBMESHES
|
||||||
|
for (int i = 0; i < transform.childCount; i++) {
|
||||||
|
Destroy(transform.GetChild(i).gameObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new TextRenderOutput(texture, window);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user