Play around more with text rendering

This commit is contained in:
2020-06-19 01:01:46 -07:00
parent 2c8773ce61
commit 0a77ef57b7
8 changed files with 237 additions and 119 deletions

View File

@ -0,0 +1,90 @@
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Rendering;
public class TextRenderTest : MonoBehaviour {
//[InspectorButton("go")] public bool button;
public Camera _camera;
public GameObject _cameraObject;
public TextMeshPro _tmp;
public Material _blitMaterial;
public Material _targetMaterial;
public RenderTexture renderTex;
private float pixelDensity = 36;
private int MaxTextureSize = 4096;
public const TextureFormat TextTextureFormat = TextureFormat.RG16;
public const RenderTextureFormat TextRenderTextureFormat = RenderTextureFormat.R8;
// Start is called before the first frame update
void Start() {
Debug.Log("starting...");
StartCoroutine(OnRender());
}
// Update is called once per frame
void Update() {
}
private void go() {
Debug.Log("starting...");
}
private IEnumerator OnRender() {
Debug.Log("starting...2");
// calculate camera and texture size
_tmp.ForceMeshUpdate();
var mesh = _tmp.mesh;
mesh.RecalculateBounds();
var bounds = mesh.bounds;
Debug.Log(bounds.size);
var width = Mathf.NextPowerOfTwo((int) (bounds.size.x * pixelDensity));
var height = Mathf.NextPowerOfTwo((int) (bounds.size.y * pixelDensity));
Debug.Log($"width = {width}");
Debug.Log($"height = {height}");
_camera.orthographicSize = height / pixelDensity / 2;
_camera.aspect = (float) width / height;
_cameraObject.transform.localPosition = new Vector3(bounds.center.x, bounds.center.y, -1);
width = Mathf.Min(width, MaxTextureSize);
height = Mathf.Min(height, MaxTextureSize);
// setup texture
var texture = new Texture2D(width, height, TextTextureFormat, true);
_targetMaterial.mainTexture = texture;
// setup render texture
renderTex = RenderTexture.GetTemporary(width, height, 0, TextRenderTextureFormat, RenderTextureReadWrite.Linear, 1);
renderTex.autoGenerateMips = true;
_camera.targetTexture = renderTex;
// setup material
_blitMaterial.mainTexture = _tmp.font.atlas;
// draw the mesh
Graphics.DrawMesh(mesh, _tmp.renderer.localToWorldMatrix, _blitMaterial, 0, _camera, 0);
_camera.Render();
yield return null;
RenderTexture.active = renderTex;
texture.ReadPixels(new Rect(0, 0, width, height), 0, 0, true);
texture.Apply(false, true);
RenderTexture.ReleaseTemporary(renderTex);
}
}

View File

@ -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
}
}
}