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,47 @@
using UnityEngine;
[ExecuteInEditMode]
public class DecalProjectorTest : MonoBehaviour
{
public GameObject target = null;
public Material targetMaterial = null;
public MeshRenderer targetRenderer;
public float aspectRatio = 1.0f;
public float size = 1.0f;
public float factor = 1.0f;
private Matrix4x4 _projectionMatrix;
private Matrix4x4 _OrthoMatrix;
private int _matrixID;
private int _normalID;
public int _tangentID;
// Start is called before the first frame update
void Awake()
{
_projectionMatrix = Matrix4x4.identity;
_matrixID = Shader.PropertyToID("_ProjectionMatrix");
_normalID = Shader.PropertyToID("_DecalNormal");
_tangentID= Shader.PropertyToID("_DecalTangent");
targetRenderer = target.GetComponent<MeshRenderer>();
}
// Update is called once per frame
void Update()
{
Vector3 pos =new Vector3( 0.5f ,0.5f, 0);
Vector3 scale = new Vector3(1 / size, 1 / (aspectRatio * size), 1);
_OrthoMatrix.SetTRS(pos, Quaternion.identity, scale);
//Debug.Log(_OrthoMatrix);
var targetToProjector = transform.worldToLocalMatrix * targetRenderer.localToWorldMatrix;
var projectorToTarget = targetRenderer.worldToLocalMatrix * transform.localToWorldMatrix;
_projectionMatrix = _OrthoMatrix * targetToProjector;
targetMaterial.SetMatrix(_matrixID, _projectionMatrix);
targetMaterial.SetVector(_normalID, projectorToTarget.MultiplyVector(Vector3.back).normalized);
targetMaterial.SetVector(_tangentID, projectorToTarget.MultiplyVector(Vector3.right).normalized);
}
}

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);
}
}