KSP-Conformal-Decals/Source/ConformalDecals/Text/TextRenderer.cs

68 lines
2.2 KiB
C#
Raw Normal View History

using System;
2020-06-19 08:01:46 +00:00
using System.Collections;
using TMPro;
using UnityEngine;
2020-06-19 08:01:46 +00:00
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();
}
2020-06-19 08:01:46 +00:00
return _instance;
}
}
2020-06-19 08:01:46 +00:00
public const TextureFormat TextTextureFormat = TextureFormat.Alpha8;
public const RenderTextureFormat TextRenderTextureFormat = RenderTextureFormat.R8;
2020-06-19 08:01:46 +00:00
private const string BlitShader = "ConformalDecals/TMP_Blit";
private const int MaxTextureSize = 4096;
2020-06-19 08:01:46 +00:00
private static TextRenderer _instance;
2020-06-19 08:01:46 +00:00
private bool _isSetup;
private TextMeshPro _tmp;
private GameObject _cameraObject;
private Camera _camera;
private Material _blitMaterial;
2020-06-19 08:01:46 +00:00
private void Start() {
if (_instance._isSetup) {
Debug.Log("[ConformalDecals] Duplicate TextRenderer created???");
}
2020-06-19 08:01:46 +00:00
Debug.Log("[ConformalDecals] Creating TextRenderer Object");
_instance = this;
DontDestroyOnLoad(gameObject);
}
2020-06-19 08:01:46 +00:00
public void Setup() {
if (_isSetup) return;
2020-06-19 08:01:46 +00:00
Debug.Log("[ConformalDecals] Setting Up TextRenderer Object");
2020-06-19 08:01:46 +00:00
_tmp = gameObject.AddComponent<TextMeshPro>();
_tmp.renderer.enabled = false; // dont automatically render
2020-06-19 08:01:46 +00:00
_cameraObject = new GameObject("ConformalDecals text camera");
_cameraObject.transform.parent = transform;
_cameraObject.transform.SetPositionAndRotation(Vector3.back, Quaternion.identity);
2020-06-19 08:01:46 +00:00
_camera = _cameraObject.AddComponent<Camera>();
_camera.enabled = false; // dont automatically render
_camera.orthographic = true;
_camera.depthTextureMode = DepthTextureMode.None;
_camera.nearClipPlane = 0.1f;
_camera.farClipPlane = 2f;
_isSetup = true;
2020-06-19 08:01:46 +00:00
_blitMaterial = new Material(Shabby.Shabby.FindShader(BlitShader));
}
}
}