|
|
@ -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>();
|
|
|
@ -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
|
|
|
|
// GENERATE MESH
|
|
|
|
|
|
|
|
_tmp.ForceMeshUpdate();
|
|
|
|
|
|
|
|
|
|
|
|
float weight = 0;
|
|
|
|
var meshFilters = gameObject.GetComponentsInChildren<MeshFilter>();
|
|
|
|
if (text.Style.Bold && text.Font.FontAsset.fontWeights[7].regularTypeface == null) {
|
|
|
|
var meshes = new Mesh[meshFilters.Length];
|
|
|
|
weight = text.Font.FontAsset.boldStyle;
|
|
|
|
var materials = new Material[meshFilters.Length];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// SETUP BLIT MATERIAL
|
|
|
|
var bounds = new Bounds();
|
|
|
|
_blitMaterial.SetTexture(PropertyIDs._MainTex, text.Font.FontAsset.atlas);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GENERATE MESH
|
|
|
|
Debug.Log($"meshFilter count: {meshFilters.Length}");
|
|
|
|
_tmp.ForceMeshUpdate();
|
|
|
|
// SETUP MATERIALS AND BOUNDS
|
|
|
|
var mesh = _tmp.mesh;
|
|
|
|
for (int i = 0; i < meshFilters.Length; i++) {
|
|
|
|
mesh.RecalculateBounds();
|
|
|
|
var renderer = meshFilters[i].gameObject.GetComponent<MeshRenderer>();
|
|
|
|
var bounds = mesh.bounds;
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
@ -206,7 +233,6 @@ namespace ConformalDecals.Text {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|