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:
47
Assets/Scripts/DecalProjectorTest.cs
Normal file
47
Assets/Scripts/DecalProjectorTest.cs
Normal 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);
|
||||
}
|
||||
}
|
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user