From 2994da628bcd28265ac57210111ddd26150f9bd4 Mon Sep 17 00:00:00 2001 From: drewcassidy Date: Sat, 25 Jul 2020 00:47:36 -0700 Subject: [PATCH] Fix fitting algorithm for text rendering --- Assets/Scripts/TextRenderTest.cs | 15 ++++++----- Source/ConformalDecals/Text/TextRenderer.cs | 30 ++++++++++++--------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/Assets/Scripts/TextRenderTest.cs b/Assets/Scripts/TextRenderTest.cs index d283282..6e2fc85 100644 --- a/Assets/Scripts/TextRenderTest.cs +++ b/Assets/Scripts/TextRenderTest.cs @@ -17,9 +17,10 @@ public class TextRenderTest : MonoBehaviour { public Material _targetMaterial; - public RenderTexture renderTex; - private float pixelDensity = 5; - private int MaxTextureSize = 4096; + public RenderTexture renderTex; + private float pixelDensity = 8; + private int MaxTextureSize = 4096; + private static readonly int Decal = Shader.PropertyToID("_Decal"); public const TextureFormat TextTextureFormat = TextureFormat.RG16; public const RenderTextureFormat TextRenderTextureFormat = RenderTextureFormat.R8; @@ -68,6 +69,8 @@ public class TextRenderTest : MonoBehaviour { var sizeRatio = Mathf.Min(widthRatio, heightRatio); + Debug.Log(sizeRatio); + int scaledHeight = (int) (sizeRatio * height); int scaledWidth = (int) (sizeRatio * width); @@ -79,14 +82,14 @@ public class TextRenderTest : MonoBehaviour { _cameraObject.transform.localPosition = new Vector3(bounds.center.x, bounds.center.y, -1); - var halfHeight = scaledHeight / pixelDensity / 2; - var halfWidth = scaledWidth / pixelDensity / 2; + var halfHeight = heightPoT / pixelDensity / 2 / sizeRatio; + var halfWidth = widthPoT / pixelDensity / 2 / sizeRatio; var matrix = Matrix4x4.Ortho(bounds.center.x - halfWidth, bounds.center.x + halfWidth, bounds.center.y - halfHeight, bounds.center.y + halfHeight, -1, 1); // setup texture var texture = new Texture2D(widthPoT, heightPoT, TextTextureFormat, true); - _targetMaterial.mainTexture = texture; + _targetMaterial.SetTexture(Decal, texture); // setup render texture diff --git a/Source/ConformalDecals/Text/TextRenderer.cs b/Source/ConformalDecals/Text/TextRenderer.cs index e717f65..5c46bbb 100644 --- a/Source/ConformalDecals/Text/TextRenderer.cs +++ b/Source/ConformalDecals/Text/TextRenderer.cs @@ -61,29 +61,30 @@ namespace ConformalDecals.Text { } public void RenderText(DecalText text, out Texture2D texture, out Rect window) { - // Setup TMP object for rendering + // SETUP TMP OBJECT FOR RENDERING _tmp.text = text.FormattedText; _tmp.font = text.Font.fontAsset; _tmp.fontStyle = text.Style.FontStyle | text.Font.fontStyle; _tmp.lineSpacing = text.Style.LineSpacing; _tmp.characterSpacing = text.Style.CharacterSpacing; + _tmp.extraPadding = true; _tmp.enableKerning = true; _tmp.enableWordWrapping = false; _tmp.overflowMode = TextOverflowModes.Overflow; _tmp.alignment = TextAlignmentOptions.Center | TextAlignmentOptions.Baseline; _tmp.fontSize = FontSize; - // Setup blit material + // SETUP BLIT MATERIAL _blitMaterial.SetTexture(PropertyIDs._MainTex, text.Font.fontAsset.atlas); - // Generate Mesh + // GENERATE MESH _tmp.ForceMeshUpdate(); var mesh = _tmp.mesh; mesh.RecalculateBounds(); var bounds = mesh.bounds; - // Calculate Sizes + // CALCULATE SIZES var size = bounds.size * PixelDensity; var textureSize = new Vector2Int { @@ -91,24 +92,28 @@ namespace ConformalDecals.Text { y = Mathf.NextPowerOfTwo((int) size.y) }; + // 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 if (textureSize.x > MaxTextureSize) { - textureSize.x /= textureSize.x / MaxTextureSize; textureSize.y /= textureSize.x / MaxTextureSize; + textureSize.x = MaxTextureSize; } if (textureSize.y > MaxTextureSize) { textureSize.x /= textureSize.y / MaxTextureSize; - textureSize.y /= textureSize.y / MaxTextureSize; + textureSize.y = MaxTextureSize; } + // scale up everything to fit the texture for maximum usage float sizeRatio = Mathf.Min(textureSize.x / size.x, textureSize.y, size.y); + // calculate where in the texture the used area actually is window = new Rect { size = size * sizeRatio, center = (Vector2) textureSize / 2 }; - // Get Texture + // GET TEXTURE if (_lastTexture != null) { texture = _lastTexture; texture.Resize(textureSize.x, textureSize.y, TextTextureFormat, false); @@ -118,16 +123,16 @@ namespace ConformalDecals.Text { texture = new Texture2D(textureSize.x, textureSize.y, TextTextureFormat, false); } - // Generate Projection Matrix - var halfSize = window.size / PixelDensity / 2; + // GENERATE PROJECTION MATRIX + var halfSize = (Vector2) textureSize / PixelDensity / 2 / sizeRatio; var matrix = Matrix4x4.Ortho(bounds.center.x - halfSize.x, bounds.center.x + halfSize.x, bounds.center.y - halfSize.y, bounds.center.y + halfSize.y, -1, 1); - // Get Rendertex + // GET RENDERTEX var renderTex = RenderTexture.GetTemporary(textureSize.x, textureSize.y, 0, TextRenderTextureFormat, RenderTextureReadWrite.Linear, 1); renderTex.autoGenerateMips = false; - // Render + // RENDER Graphics.SetRenderTarget(renderTex); GL.PushMatrix(); GL.LoadProjectionMatrix(matrix); @@ -135,11 +140,12 @@ namespace ConformalDecals.Text { Graphics.DrawMeshNow(mesh, Matrix4x4.identity); GL.PopMatrix(); - // Copy texture back into RAM + // COPY TEXTURE BACK INTO RAM RenderTexture.active = renderTex; texture.ReadPixels(new Rect(0, 0, textureSize.x, textureSize.y), 0, 0, false); texture.Apply(); + // RELEASE RENDERTEX RenderTexture.ReleaseTemporary(renderTex); } }