mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
Play around more with text rendering
This commit is contained in:
90
Assets/Scripts/TextRenderTest.cs
Normal file
90
Assets/Scripts/TextRenderTest.cs
Normal 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);
|
||||
}
|
||||
}
|
51
Assets/Shaders/TMPBlit.shader
Normal file
51
Assets/Shaders/TMPBlit.shader
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user