I have created a new project to test this behavior (code below): this simple project assigns a render texture to a camera, renders the camera, then performs the GL drawing. The resultant RenderTexture is then displayed using a simple GUI.DrawTexture.
This used to work fine, but now I’m seeing some odd results of the GL drawing functions. I have found two workaround, that I dont like, while testing:
- I’m able to eliminate this issue, by
recreating a new Render texture each
time I do the rendering, rather than
reusing the one I have. - I’m also able to eliminate the
issue by setting up a scene
Monobehavior object to call the
GL drawing functions in OnRenderObject.
I was hoping to be able to call the GL drawing function manually, rather than having to create a scene object for it to be called OnRenderObject (not so hard in this sample project, but a real pain in my real project). Obviously, I would also rather not have to create a new RenderTexture every frame.
Below are two screen shots, one working right, the other with the new issue. Edit: ALSO, when using a replacement shader, which I am in my main project, the OnRenderObject() function is not called.
To create a scene similar to the sample images just create a cube in the scene, and add this sample monobehavior to it. Then assign a material (I used unlit/color shader for the lines material). And drag over the main camera, onto the rendertTextureCaputreCamera field. Hit play, and THEN rotate the cube object to see the lines mess-up.
Can you suggest any other solutions?
using UnityEngine;
public class GLdraw : MonoBehaviour {
public Material axisMaterial;
public Camera rendertTextureCaputreCamera;
RenderTexture caputuredRenderTexture=null;
void Update () {
RenderTexture originalCameraTex = rendertTextureCaputreCamera.targetTexture;
RenderTexture originalActiveTex = RenderTexture.active;
if (caputuredRenderTexture == null) //remark out this line to create a new render texture each time, and resolve the issue.
{
if (caputuredRenderTexture != null)
Object.Destroy(caputuredRenderTexture);
caputuredRenderTexture = new RenderTexture(200, 200, 16, RenderTextureFormat.ARGB32);
}
rendertTextureCaputreCamera.targetTexture = caputuredRenderTexture;
RenderTexture.active = caputuredRenderTexture;
rendertTextureCaputreCamera.Render();
DrawAxis(rendertTextureCaputreCamera); //doing GL drawing in here
RenderTexture.active= originalActiveTex;
rendertTextureCaputreCamera.targetTexture = originalCameraTex;
}
private void OnGUI()
{
if (caputuredRenderTexture != null)
GUI.DrawTexture(new Rect(100, 100, 200, 200), caputuredRenderTexture);//changng scalemode has no effect.
}
private void OnRenderObject()
{
//DrawAxis(Camera.current); //this version, drawn in the current camera, AND the render texture camera, ALWAYS loosk fine.
}
protected void DrawAxis(Camera cam)
{
axisMaterial.SetPass(0);
GL.PushMatrix();
Matrix4x4 projection = GL.GetGPUProjectionMatrix(cam.projectionMatrix, false);
GL.LoadProjectionMatrix(projection);
GL.LoadIdentity();
GL.MultMatrix(cam.worldToCameraMatrix * transform.localToWorldMatrix);
GL.Begin(GL.LINES);
float axisLength = 10;
GL.Color(Color.red);
GL.Vertex3(0f, axisLength, 0f);
GL.Vertex3(0f, -axisLength, 0f);
GL.Color(Color.green);
GL.Vertex3(axisLength, 0f, 0f);
GL.Vertex3(-axisLength, 0f, 0f);
GL.Color(Color.blue);
GL.Vertex3(0f, 0f, axisLength);
GL.Vertex3(0f, 0f, -axisLength);
GL.End();
GL.PopMatrix();
}
}