mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
Fix fitting algorithm for text rendering
This commit is contained in:
parent
dbb3281e95
commit
2994da628b
@ -18,8 +18,9 @@ public class TextRenderTest : MonoBehaviour {
|
|||||||
public Material _targetMaterial;
|
public Material _targetMaterial;
|
||||||
|
|
||||||
public RenderTexture renderTex;
|
public RenderTexture renderTex;
|
||||||
private float pixelDensity = 5;
|
private float pixelDensity = 8;
|
||||||
private int MaxTextureSize = 4096;
|
private int MaxTextureSize = 4096;
|
||||||
|
private static readonly int Decal = Shader.PropertyToID("_Decal");
|
||||||
|
|
||||||
public const TextureFormat TextTextureFormat = TextureFormat.RG16;
|
public const TextureFormat TextTextureFormat = TextureFormat.RG16;
|
||||||
public const RenderTextureFormat TextRenderTextureFormat = RenderTextureFormat.R8;
|
public const RenderTextureFormat TextRenderTextureFormat = RenderTextureFormat.R8;
|
||||||
@ -68,6 +69,8 @@ public class TextRenderTest : MonoBehaviour {
|
|||||||
|
|
||||||
var sizeRatio = Mathf.Min(widthRatio, heightRatio);
|
var sizeRatio = Mathf.Min(widthRatio, heightRatio);
|
||||||
|
|
||||||
|
Debug.Log(sizeRatio);
|
||||||
|
|
||||||
int scaledHeight = (int) (sizeRatio * height);
|
int scaledHeight = (int) (sizeRatio * height);
|
||||||
int scaledWidth = (int) (sizeRatio * width);
|
int scaledWidth = (int) (sizeRatio * width);
|
||||||
|
|
||||||
@ -79,14 +82,14 @@ public class TextRenderTest : MonoBehaviour {
|
|||||||
|
|
||||||
_cameraObject.transform.localPosition = new Vector3(bounds.center.x, bounds.center.y, -1);
|
_cameraObject.transform.localPosition = new Vector3(bounds.center.x, bounds.center.y, -1);
|
||||||
|
|
||||||
var halfHeight = scaledHeight / pixelDensity / 2;
|
var halfHeight = heightPoT / pixelDensity / 2 / sizeRatio;
|
||||||
var halfWidth = scaledWidth / pixelDensity / 2;
|
var halfWidth = widthPoT / pixelDensity / 2 / sizeRatio;
|
||||||
var matrix = Matrix4x4.Ortho(bounds.center.x - halfWidth, bounds.center.x + halfWidth,
|
var matrix = Matrix4x4.Ortho(bounds.center.x - halfWidth, bounds.center.x + halfWidth,
|
||||||
bounds.center.y - halfHeight, bounds.center.y + halfHeight, -1, 1);
|
bounds.center.y - halfHeight, bounds.center.y + halfHeight, -1, 1);
|
||||||
|
|
||||||
// setup texture
|
// setup texture
|
||||||
var texture = new Texture2D(widthPoT, heightPoT, TextTextureFormat, true);
|
var texture = new Texture2D(widthPoT, heightPoT, TextTextureFormat, true);
|
||||||
_targetMaterial.mainTexture = texture;
|
_targetMaterial.SetTexture(Decal, texture);
|
||||||
|
|
||||||
|
|
||||||
// setup render texture
|
// setup render texture
|
||||||
|
@ -61,29 +61,30 @@ namespace ConformalDecals.Text {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void RenderText(DecalText text, out Texture2D texture, out Rect window) {
|
public void RenderText(DecalText text, out Texture2D texture, out Rect window) {
|
||||||
// Setup TMP object for rendering
|
// SETUP TMP OBJECT FOR RENDERING
|
||||||
_tmp.text = text.FormattedText;
|
_tmp.text = text.FormattedText;
|
||||||
_tmp.font = text.Font.fontAsset;
|
_tmp.font = text.Font.fontAsset;
|
||||||
_tmp.fontStyle = text.Style.FontStyle | text.Font.fontStyle;
|
_tmp.fontStyle = text.Style.FontStyle | text.Font.fontStyle;
|
||||||
_tmp.lineSpacing = text.Style.LineSpacing;
|
_tmp.lineSpacing = text.Style.LineSpacing;
|
||||||
_tmp.characterSpacing = text.Style.CharacterSpacing;
|
_tmp.characterSpacing = text.Style.CharacterSpacing;
|
||||||
|
|
||||||
|
_tmp.extraPadding = true;
|
||||||
_tmp.enableKerning = true;
|
_tmp.enableKerning = true;
|
||||||
_tmp.enableWordWrapping = false;
|
_tmp.enableWordWrapping = false;
|
||||||
_tmp.overflowMode = TextOverflowModes.Overflow;
|
_tmp.overflowMode = TextOverflowModes.Overflow;
|
||||||
_tmp.alignment = TextAlignmentOptions.Center | TextAlignmentOptions.Baseline;
|
_tmp.alignment = TextAlignmentOptions.Center | TextAlignmentOptions.Baseline;
|
||||||
_tmp.fontSize = FontSize;
|
_tmp.fontSize = FontSize;
|
||||||
|
|
||||||
// Setup blit material
|
// SETUP BLIT MATERIAL
|
||||||
_blitMaterial.SetTexture(PropertyIDs._MainTex, text.Font.fontAsset.atlas);
|
_blitMaterial.SetTexture(PropertyIDs._MainTex, text.Font.fontAsset.atlas);
|
||||||
|
|
||||||
// Generate Mesh
|
// GENERATE MESH
|
||||||
_tmp.ForceMeshUpdate();
|
_tmp.ForceMeshUpdate();
|
||||||
var mesh = _tmp.mesh;
|
var mesh = _tmp.mesh;
|
||||||
mesh.RecalculateBounds();
|
mesh.RecalculateBounds();
|
||||||
var bounds = mesh.bounds;
|
var bounds = mesh.bounds;
|
||||||
|
|
||||||
// Calculate Sizes
|
// CALCULATE SIZES
|
||||||
var size = bounds.size * PixelDensity;
|
var size = bounds.size * PixelDensity;
|
||||||
|
|
||||||
var textureSize = new Vector2Int {
|
var textureSize = new Vector2Int {
|
||||||
@ -91,24 +92,28 @@ namespace ConformalDecals.Text {
|
|||||||
y = Mathf.NextPowerOfTwo((int) size.y)
|
y = Mathf.NextPowerOfTwo((int) size.y)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// make sure texture isnt too big, scale it down if it is
|
||||||
|
// this is just so you dont crash the game by pasting in the entire script of The Bee Movie
|
||||||
if (textureSize.x > MaxTextureSize) {
|
if (textureSize.x > MaxTextureSize) {
|
||||||
textureSize.x /= textureSize.x / MaxTextureSize;
|
|
||||||
textureSize.y /= textureSize.x / MaxTextureSize;
|
textureSize.y /= textureSize.x / MaxTextureSize;
|
||||||
|
textureSize.x = MaxTextureSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (textureSize.y > MaxTextureSize) {
|
if (textureSize.y > MaxTextureSize) {
|
||||||
textureSize.x /= textureSize.y / MaxTextureSize;
|
textureSize.x /= textureSize.y / MaxTextureSize;
|
||||||
textureSize.y /= textureSize.y / MaxTextureSize;
|
textureSize.y = MaxTextureSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// scale up everything to fit the texture for maximum usage
|
||||||
float sizeRatio = Mathf.Min(textureSize.x / size.x, textureSize.y, size.y);
|
float sizeRatio = Mathf.Min(textureSize.x / size.x, textureSize.y, size.y);
|
||||||
|
|
||||||
|
// calculate where in the texture the used area actually is
|
||||||
window = new Rect {
|
window = new Rect {
|
||||||
size = size * sizeRatio,
|
size = size * sizeRatio,
|
||||||
center = (Vector2) textureSize / 2
|
center = (Vector2) textureSize / 2
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get Texture
|
// GET TEXTURE
|
||||||
if (_lastTexture != null) {
|
if (_lastTexture != null) {
|
||||||
texture = _lastTexture;
|
texture = _lastTexture;
|
||||||
texture.Resize(textureSize.x, textureSize.y, TextTextureFormat, false);
|
texture.Resize(textureSize.x, textureSize.y, TextTextureFormat, false);
|
||||||
@ -118,16 +123,16 @@ namespace ConformalDecals.Text {
|
|||||||
texture = new Texture2D(textureSize.x, textureSize.y, TextTextureFormat, false);
|
texture = new Texture2D(textureSize.x, textureSize.y, TextTextureFormat, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate Projection Matrix
|
// GENERATE PROJECTION MATRIX
|
||||||
var halfSize = window.size / PixelDensity / 2;
|
var halfSize = (Vector2) textureSize / PixelDensity / 2 / sizeRatio;
|
||||||
var matrix = Matrix4x4.Ortho(bounds.center.x - halfSize.x, bounds.center.x + halfSize.x,
|
var matrix = Matrix4x4.Ortho(bounds.center.x - halfSize.x, bounds.center.x + halfSize.x,
|
||||||
bounds.center.y - halfSize.y, bounds.center.y + halfSize.y, -1, 1);
|
bounds.center.y - halfSize.y, bounds.center.y + halfSize.y, -1, 1);
|
||||||
|
|
||||||
// Get Rendertex
|
// GET RENDERTEX
|
||||||
var renderTex = RenderTexture.GetTemporary(textureSize.x, textureSize.y, 0, TextRenderTextureFormat, RenderTextureReadWrite.Linear, 1);
|
var renderTex = RenderTexture.GetTemporary(textureSize.x, textureSize.y, 0, TextRenderTextureFormat, RenderTextureReadWrite.Linear, 1);
|
||||||
renderTex.autoGenerateMips = false;
|
renderTex.autoGenerateMips = false;
|
||||||
|
|
||||||
// Render
|
// RENDER
|
||||||
Graphics.SetRenderTarget(renderTex);
|
Graphics.SetRenderTarget(renderTex);
|
||||||
GL.PushMatrix();
|
GL.PushMatrix();
|
||||||
GL.LoadProjectionMatrix(matrix);
|
GL.LoadProjectionMatrix(matrix);
|
||||||
@ -135,11 +140,12 @@ namespace ConformalDecals.Text {
|
|||||||
Graphics.DrawMeshNow(mesh, Matrix4x4.identity);
|
Graphics.DrawMeshNow(mesh, Matrix4x4.identity);
|
||||||
GL.PopMatrix();
|
GL.PopMatrix();
|
||||||
|
|
||||||
// Copy texture back into RAM
|
// COPY TEXTURE BACK INTO RAM
|
||||||
RenderTexture.active = renderTex;
|
RenderTexture.active = renderTex;
|
||||||
texture.ReadPixels(new Rect(0, 0, textureSize.x, textureSize.y), 0, 0, false);
|
texture.ReadPixels(new Rect(0, 0, textureSize.x, textureSize.y), 0, 0, false);
|
||||||
texture.Apply();
|
texture.Apply();
|
||||||
|
|
||||||
|
// RELEASE RENDERTEX
|
||||||
RenderTexture.ReleaseTemporary(renderTex);
|
RenderTexture.ReleaseTemporary(renderTex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user