Compare commits

...

2 Commits

Author SHA1 Message Date
dadf38acd5 go back to using temporary rendertexs 2020-12-16 01:28:57 -08:00
f42e0d78d6 Don't reuse textures, and don't keep them in RAM
Hopefully fixes #28
2020-12-16 01:11:32 -08:00
2 changed files with 9 additions and 16 deletions

View File

@ -123,7 +123,6 @@ namespace ConformalDecals.Text {
job.Start(); job.Start();
Texture2D texture = null;
if (job.OldText != null && RenderCache.TryGetValue(job.OldText, out var oldRender)) { if (job.OldText != null && RenderCache.TryGetValue(job.OldText, out var oldRender)) {
// old output still exists // old output still exists
@ -132,7 +131,7 @@ namespace ConformalDecals.Text {
if (oldRender.UserCount <= 0) { if (oldRender.UserCount <= 0) {
// this is the only usage of this output, so we are free to re-render into the texture // this is the only usage of this output, so we are free to re-render into the texture
texture = oldRender.Texture; Destroy(oldRender.Texture);
RenderCache.Remove(job.OldText); RenderCache.Remove(job.OldText);
} }
} }
@ -145,7 +144,7 @@ namespace ConformalDecals.Text {
else { else {
renderNeeded = true; renderNeeded = true;
renderOutput = RenderText(job.NewText, texture); renderOutput = RenderText(job.NewText);
RenderCache.Add(job.NewText, renderOutput); RenderCache.Add(job.NewText, renderOutput);
} }
@ -156,7 +155,7 @@ namespace ConformalDecals.Text {
} }
/// Render a piece of text to a given texture /// Render a piece of text to a given texture
public TextRenderOutput RenderText(DecalText text, Texture2D texture) { public TextRenderOutput RenderText(DecalText text) {
if (text == null) throw new ArgumentNullException(nameof(text)); if (text == null) throw new ArgumentNullException(nameof(text));
if (_tmp == null) throw new InvalidOperationException("TextMeshPro object not yet created."); if (_tmp == null) throw new InvalidOperationException("TextMeshPro object not yet created.");
@ -209,7 +208,7 @@ namespace ConformalDecals.Text {
var size = bounds.size * PixelDensity; var size = bounds.size * PixelDensity;
size.x = Mathf.Max(size.x, 0.1f); size.x = Mathf.Max(size.x, 0.1f);
size.y = Mathf.Max(size.y, 0.1f); size.y = Mathf.Max(size.y, 0.1f);
var textureSize = new Vector2Int { var textureSize = new Vector2Int {
x = Mathf.NextPowerOfTwo((int) size.x), x = Mathf.NextPowerOfTwo((int) size.x),
y = Mathf.NextPowerOfTwo((int) size.y) y = Mathf.NextPowerOfTwo((int) size.y)
@ -242,12 +241,7 @@ namespace ConformalDecals.Text {
}; };
// SETUP TEXTURE // SETUP TEXTURE
if (texture == null) { var texture = new Texture2D(textureSize.x, textureSize.y, textTextureFormat, true);
texture = new Texture2D(textureSize.x, textureSize.y, textTextureFormat, true);
}
else if (texture.width != textureSize.x || texture.height != textureSize.y || texture.format != textTextureFormat) {
texture.Resize(textureSize.x, textureSize.y, textTextureFormat, true);
}
// GENERATE PROJECTION MATRIX // GENERATE PROJECTION MATRIX
var halfSize = (Vector2) textureSize / PixelDensity / 2 / sizeRatio; var halfSize = (Vector2) textureSize / PixelDensity / 2 / sizeRatio;
@ -255,7 +249,7 @@ namespace ConformalDecals.Text {
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 = new RenderTexture(textureSize.x, textureSize.y, 0, textRenderTextureFormat, RenderTextureReadWrite.Linear) {autoGenerateMips = false}; var renderTex = RenderTexture.GetTemporary(textureSize.x, textureSize.y, 0, textRenderTextureFormat, RenderTextureReadWrite.Linear);
// RENDER // RENDER
Graphics.SetRenderTarget(renderTex); Graphics.SetRenderTarget(renderTex);
@ -275,14 +269,13 @@ namespace ConformalDecals.Text {
var prevRT = RenderTexture.active; var prevRT = RenderTexture.active;
RenderTexture.active = renderTex; RenderTexture.active = renderTex;
texture.ReadPixels(new Rect(0, 0, textureSize.x, textureSize.y), 0, 0, true); texture.ReadPixels(new Rect(0, 0, textureSize.x, textureSize.y), 0, 0, true);
texture.Apply(); texture.Apply(false, true);
RenderTexture.active = prevRT; RenderTexture.active = prevRT;
GL.PopMatrix(); GL.PopMatrix();
// RELEASE RENDERTEX // RELEASE RENDERTEX
renderTex.Release(); RenderTexture.ReleaseTemporary(renderTex);
RenderTexture.Destroy(renderTex);
// CLEAR SUBMESHES // CLEAR SUBMESHES
_tmp.text = ""; _tmp.text = "";