I’m trying to do some simple line/text drawing to a RenderTexture during certain frames of my game, and chose to go down the GL immediate mode route via GL.Begin(…) / GL.Vertex3(…). Everything works as expected in the Editor, and in development builds, but for some reason release builds seem to behave at least slightly differently.
In Release, it seems my entire projection matrix is shifted like ~10% up and left. It also seems to be mucking with text rendering but I’m having a hard time figuring out quite what’s going on there, and I seem to have at least found a workaround for that.
I’ve built a standalone test case like this:
private void DrawTest() {
// Matrix4x4 orthoProjection = Matrix4x4.Ortho(0, 1, 0, 1, -100, 100);
Matrix4x4 orthoProjection = Matrix4x4.Ortho(0, renderTexture.width, 0, renderTexture.height, -100, 100);
// Weird camera fix posted elsewhere for flickering, doesn't seem to change anything on or off?
if (Camera.current != null) {
orthoProjection *= Camera.current.worldToCameraMatrix.inverse;
}
RenderTexture prev = RenderTexture.active;
RenderTexture.active = renderTexture;
Shader shader = Shader.Find("Hidden/Internal-Colored");
Material lineMaterial = new Material(shader);
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
// Turn on alpha blending
lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
// Turn backface culling off
lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
// Turn off depth writes
lineMaterial.SetInt("_ZWrite", 0);
lineMaterial.SetPass(0);
GL.PushMatrix();
GL.LoadProjectionMatrix(orthoProjection);
GL.Begin(GL.LINE_STRIP);
GL.Clear(true, true, Color.red);
GL.Color(new Color(0, 1, 0, 0.5f));
GL.Vertex3(0, 0, 0);
GL.Vertex3(renderTexture.width, renderTexture.height, 0);
GL.End();
TextPosition = new Vector3(renderTexture.width / 2, renderTexture.height / 2, 0);
text.ForceMeshUpdate(true, true);
float aspect = (float)renderTexture.width / renderTexture.height;
float size = TextSize;
Vector3 scale = new Vector3(size, size * aspect, 1);
Matrix4x4 matrix = Matrix4x4.TRS(TextPosition, Quaternion.identity, scale);
Material tmpMat = text.fontSharedMaterial;
bool canRender = tmpMat.SetPass(0);
if (canRender) {
Graphics.DrawMeshNow(text.mesh, matrix);
}
GL.PopMatrix();
GL.invertCulling = false;
RenderTexture.active = prev;
}
In this config things mostly work. Dev/Editor builds draw a diagonal line as expected and text renders to the center. Release builds, everything is shifted down and right like 10%.
I was calling this from within Update(). I’ve tried moving that to all of the RenderPipelineManager.endContextRendering events and the like, but there’s no difference.
I also was originally using the first projection matrix listed that was 0-1 to do things by percentages, but for some reason that one ends up really messed up with text and I’m having trouble even locating what’s going on with it. I had it working in Edit/Dev builds but release everything just totally disappears.
I suspect this is something to do with the GL stack or something but I can’t find anyway around this except to try to just re-offset the matrix backwards, but I have no idea why it’s only happening in release and if this behavior will stay consistent.
I’m considering moving to CommandBuffers instead, but I’m not convinced I won’t have the same trouble there.