Play around more with text rendering
parent
2c8773ce61
commit
0a77ef57b7
@ -0,0 +1,51 @@
|
|||||||
|
Shader "ConformalDecals/TMP_Blit"
|
||||||
|
{
|
||||||
|
Properties
|
||||||
|
{
|
||||||
|
_MainTex("_MainTex (RGB spec(A))", 2D) = "white" {}
|
||||||
|
|
||||||
|
}
|
||||||
|
SubShader
|
||||||
|
{
|
||||||
|
Tags { "Queue" = "Transparent" }
|
||||||
|
Cull Off
|
||||||
|
ZWrite Off
|
||||||
|
|
||||||
|
Pass
|
||||||
|
{
|
||||||
|
Blend One One
|
||||||
|
|
||||||
|
CGPROGRAM
|
||||||
|
#pragma vertex vert
|
||||||
|
#pragma fragment frag
|
||||||
|
|
||||||
|
sampler2D _MainTex;
|
||||||
|
|
||||||
|
#include "UnityCG.cginc"
|
||||||
|
#include "Lighting.cginc"
|
||||||
|
#include "AutoLight.cginc"
|
||||||
|
|
||||||
|
struct v2f {
|
||||||
|
float4 pos : SV_POSITION;
|
||||||
|
float2 uv : TEXCOORD0;
|
||||||
|
};
|
||||||
|
|
||||||
|
v2f vert(float4 vertex : POSITION, float2 uv : TEXCOORD0) {
|
||||||
|
v2f o;
|
||||||
|
o.pos = UnityObjectToClipPos(vertex);
|
||||||
|
o.uv = uv;
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
fixed4 frag (v2f i) : SV_Target {
|
||||||
|
fixed4 c = 0;
|
||||||
|
|
||||||
|
c.r = tex2D(_MainTex,(i.uv)).a;
|
||||||
|
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
ENDCG
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,146 +1,112 @@
|
|||||||
using System;
|
using System;
|
||||||
using ConformalDecals.Util;
|
using System.Collections;
|
||||||
using TMPro;
|
using TMPro;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.Rendering;
|
||||||
|
|
||||||
|
namespace ConformalDecals.Text {
|
||||||
|
[KSPAddon(KSPAddon.Startup.FlightAndEditor, true)]
|
||||||
|
public class TextRenderer : MonoBehaviour {
|
||||||
|
public static TextRenderer Instance {
|
||||||
|
get {
|
||||||
|
if (!_instance._isSetup) {
|
||||||
|
_instance.Setup();
|
||||||
|
}
|
||||||
|
|
||||||
namespace ConformalDecals {
|
return _instance;
|
||||||
public class TextRenderer {
|
}
|
||||||
private struct GlyphInfo {
|
|
||||||
public TMP_Glyph glyph;
|
|
||||||
public Vector2Int size;
|
|
||||||
public Vector2Int position;
|
|
||||||
public int fontIndex;
|
|
||||||
public bool needsResample;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private struct FontInfo {
|
public const TextureFormat TextTextureFormat = TextureFormat.Alpha8;
|
||||||
public TMP_FontAsset font;
|
public const RenderTextureFormat TextRenderTextureFormat = RenderTextureFormat.R8;
|
||||||
public Texture2D fontAtlas;
|
|
||||||
public Color32[] fontAtlasColors;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Texture2D RenderToTexture(TMP_FontAsset font, string text) {
|
private const string BlitShader = "ConformalDecals/TMP_Blit";
|
||||||
Debug.Log($"Rendering text: {text}");
|
private const int MaxTextureSize = 4096;
|
||||||
var charArray = text.ToCharArray();
|
|
||||||
var glyphInfoArray = new GlyphInfo[charArray.Length];
|
|
||||||
var fontInfoArray = new FontInfo[charArray.Length];
|
|
||||||
|
|
||||||
var baseScale = font.fontInfo.Scale;
|
private static TextRenderer _instance;
|
||||||
|
|
||||||
var padding = (int) font.fontInfo.Padding;
|
private bool _isSetup;
|
||||||
var ascender = (int) font.fontInfo.Ascender;
|
private TextMeshPro _tmp;
|
||||||
var descender = (int) font.fontInfo.Descender;
|
private GameObject _cameraObject;
|
||||||
var baseline = (int) baseScale * (descender + padding);
|
private Camera _camera;
|
||||||
Debug.Log($"baseline: {baseline}");
|
private Material _blitMaterial;
|
||||||
Debug.Log($"ascender: {ascender}");
|
|
||||||
Debug.Log($"descender: {descender}");
|
|
||||||
Debug.Log($"baseScale: {baseScale}");
|
|
||||||
|
|
||||||
fontInfoArray[0].font = font;
|
private void Start() {
|
||||||
|
if (_instance._isSetup) {
|
||||||
|
Debug.Log("[ConformalDecals] Duplicate TextRenderer created???");
|
||||||
|
}
|
||||||
|
|
||||||
int xAdvance = 0;
|
Debug.Log("[ConformalDecals] Creating TextRenderer Object");
|
||||||
for (var i = 0; i < charArray.Length; i++) {
|
_instance = this;
|
||||||
|
DontDestroyOnLoad(gameObject);
|
||||||
|
}
|
||||||
|
|
||||||
var glyphFont = TMP_FontUtilities.SearchForGlyph(font, charArray[i], out var glyph);
|
public void Setup() {
|
||||||
|
if (_isSetup) return;
|
||||||
|
|
||||||
if (glyphFont == font) {
|
|
||||||
glyphInfoArray[i].fontIndex = 0;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
for (int f = 1; i < charArray.Length; i++) {
|
|
||||||
if (fontInfoArray[f].font == null) {
|
|
||||||
fontInfoArray[f].font = glyphFont;
|
|
||||||
glyphInfoArray[i].fontIndex = f;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fontInfoArray[f].font == glyphFont) {
|
|
||||||
glyphInfoArray[i].fontIndex = f;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Debug.Log($"getting font info for character: '{charArray[i]}'");
|
Debug.Log("[ConformalDecals] Setting Up TextRenderer Object");
|
||||||
Debug.Log($"character font: {glyphFont.name}");
|
|
||||||
|
|
||||||
glyphInfoArray[i].glyph = glyph;
|
_tmp = gameObject.AddComponent<TextMeshPro>();
|
||||||
glyphInfoArray[i].needsResample = false;
|
_tmp.renderer.enabled = false; // dont automatically render
|
||||||
|
|
||||||
float elementScale = glyph.scale;
|
_cameraObject = new GameObject("ConformalDecals text camera");
|
||||||
|
_cameraObject.transform.parent = transform;
|
||||||
|
_cameraObject.transform.SetPositionAndRotation(Vector3.back, Quaternion.identity);
|
||||||
|
|
||||||
if (glyphFont == font) {
|
_camera = _cameraObject.AddComponent<Camera>();
|
||||||
if (!Mathf.Approximately(elementScale, 1)) {
|
_camera.enabled = false; // dont automatically render
|
||||||
glyphInfoArray[i].needsResample = true;
|
_camera.orthographic = true;
|
||||||
}
|
_camera.depthTextureMode = DepthTextureMode.None;
|
||||||
|
_camera.nearClipPlane = 0.1f;
|
||||||
|
_camera.farClipPlane = 2f;
|
||||||
|
_isSetup = true;
|
||||||
|
|
||||||
elementScale *= baseScale;
|
_blitMaterial = new Material(Shabby.Shabby.FindShader(BlitShader));
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
var fontScale = glyphFont.fontInfo.Scale / glyphFont.fontInfo.PointSize;
|
|
||||||
if (!Mathf.Approximately(fontScale, baseScale)) {
|
|
||||||
glyphInfoArray[i].needsResample = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
elementScale *= fontScale;
|
public Texture2D RenderToTexture(Texture2D texture2D, TMP_FontAsset font, string text, float fontSize, float pixelDensity) {
|
||||||
}
|
// generate text mesh
|
||||||
|
_tmp.SetText(text);
|
||||||
|
_tmp.font = font;
|
||||||
|
_tmp.fontSize = fontSize;
|
||||||
|
_tmp.ForceMeshUpdate();
|
||||||
|
|
||||||
Debug.Log($"character scale: {glyphFont.fontInfo.Scale / glyphFont.fontInfo.PointSize}");
|
// calculate camera and texture size
|
||||||
Debug.Log($"character needs resampling: {glyphInfoArray[i].needsResample}");
|
var mesh = _tmp.mesh;
|
||||||
|
var bounds = mesh.bounds;
|
||||||
|
|
||||||
glyphInfoArray[i].size.x = (int) ((glyph.width + (padding * 2)) * elementScale);
|
var width = Mathf.NextPowerOfTwo((int) (bounds.size.x * pixelDensity));
|
||||||
glyphInfoArray[i].size.y = (int) ((glyph.height + (padding * 2)) * elementScale);
|
var height = Mathf.NextPowerOfTwo((int) (bounds.size.y * pixelDensity));
|
||||||
glyphInfoArray[i].position.x = (int) ((xAdvance + glyph.xOffset - padding) * elementScale);
|
|
||||||
glyphInfoArray[i].position.y = (int) ((baseline + glyph.yOffset - padding) * elementScale);
|
|
||||||
|
|
||||||
Debug.Log($"character size: {glyphInfoArray[i].size}");
|
|
||||||
Debug.Log($"character position: {glyphInfoArray[i].position}");
|
|
||||||
}
|
|
||||||
|
|
||||||
// calculate texture bounds
|
_camera.orthographicSize = height / pixelDensity / 2;
|
||||||
int xOffset = glyphInfoArray[0].position.x;
|
_camera.aspect = (float) width / height;
|
||||||
var textureWidth = (glyphInfoArray[charArray.Length - 1].position.x + glyphInfoArray[charArray.Length - 1].size.x) - xOffset;
|
|
||||||
var textureHeight = (int) baseScale * (ascender + descender + padding * 2);
|
|
||||||
|
|
||||||
// ensure texture sizes are powers of 2
|
_cameraObject.transform.localPosition = new Vector3(bounds.center.x, bounds.center.y, -1);
|
||||||
textureWidth = Mathf.NextPowerOfTwo(textureWidth);
|
|
||||||
textureHeight = Mathf.NextPowerOfTwo(textureHeight);
|
|
||||||
Debug.Log($"texture is {textureWidth} x {textureHeight}");
|
|
||||||
|
|
||||||
var texture = new Texture2D(textureWidth, textureHeight, TextureFormat.Alpha8, true);
|
width = Mathf.Min(width, MaxTextureSize);
|
||||||
|
height = Mathf.Max(height, MaxTextureSize);
|
||||||
|
|
||||||
var colors = new Color32[textureWidth * textureHeight];
|
// setup render texture
|
||||||
|
var renderTex = RenderTexture.GetTemporary(width, height, 0, TextRenderTextureFormat, RenderTextureReadWrite.Linear, 1);
|
||||||
|
_camera.targetTexture = renderTex;
|
||||||
|
|
||||||
for (var i = 0; i < fontInfoArray.Length; i++) {
|
// setup material
|
||||||
if (fontInfoArray[i].font == null) break;
|
_blitMaterial.SetTexture(PropertyIDs._MainTex, font.atlas);
|
||||||
fontInfoArray[i].fontAtlas = fontInfoArray[i].font.atlas;
|
_blitMaterial.SetPass(0);
|
||||||
fontInfoArray[i].fontAtlasColors = fontInfoArray[i].fontAtlas.GetPixels32();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < charArray.Length; i++) {
|
// draw the mesh
|
||||||
var glyphInfo = glyphInfoArray[i];
|
Graphics.DrawMeshNow(mesh, _tmp.renderer.localToWorldMatrix);
|
||||||
var glyph = glyphInfo.glyph;
|
|
||||||
var fontInfo = fontInfoArray[glyphInfo.fontIndex];
|
|
||||||
|
|
||||||
var srcPos = new Vector2Int((int) glyph.x, (int) glyph.y);
|
|
||||||
var dstPos = glyphInfo.position;
|
|
||||||
dstPos.x += xOffset;
|
|
||||||
var dstSize = glyphInfo.size;
|
|
||||||
|
|
||||||
Debug.Log($"rendering character number {i}");
|
|
||||||
|
|
||||||
if (glyphInfo.needsResample) {
|
|
||||||
var srcSize = new Vector2(glyph.width, glyph.height);
|
|
||||||
TextureUtils.BlitRectBilinearAlpha(fontInfo.fontAtlas, srcPos, srcSize, texture, colors, dstPos, dstSize, TextureUtils.BlitMode.Add);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
TextureUtils.BlitRectAlpha(fontInfo.fontAtlas, fontInfo.fontAtlasColors, srcPos, texture, colors, dstPos, dstSize, TextureUtils.BlitMode.Add);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
texture.Apply(true);
|
var request = AsyncGPUReadback.Request(renderTex, 0, TextTextureFormat);
|
||||||
|
|
||||||
return texture;
|
request.WaitForCompletion();
|
||||||
|
|
||||||
|
if (request.hasError) {
|
||||||
|
throw new Exception("[ConformalDecals] Error encountered trying to request render texture data from the GPU!");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
namespace ConformalDecals.Text {
|
||||||
|
public struct TextSettings {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue