Hi, I want to draw a full screen background in my game using a script.
According to the docs, I can simply call Graphics.Blit() in the Update function, like this:-
But I can’t get anything to show, is this code correct? Does it have to have a camera component as well? Thanks.
Please note that I want to render the full screen texture BEFORE anything else is rendered, this is not GUI related.
Can anyone help?
Cheers,
Dave
Yep. Has anyone actually used this method?
We had some trouble using Graphics.Blit() in the Update loop as well. For our purposes we could switch to OnGUI/OnRenderImage because it wasn’t a background. Perhaps OnPreRender will work in your case?
Update() is before rendering starts, so how are your clear flags set on your camera? Perhaps whatever you’re blitting is being cleared by the upcoming frame?
I just realised this was the source of our trouble with Graphics.Blit() so thanks for clearing that up 
After spending a few hours with it, I’m amazed it can’t be done.
Graphics.Blit only seems to work when attached to a camera.
I can’t set a sort order for it to render first, because of this.
Blit always obliterates everything on screen, no matter where you put the call.
I’ve tried using the polygon draw out of the image effects stuff with this, but it still didn’t work, I suspect that this is correct way of doing this though, I’m just not understanding it correctly I guess.
void CustomGraphicsBlit(Material fxMaterial)
{
GL.PushMatrix();
GL.LoadOrtho();
// Tried this with many z values, it never appears behind the game objects...
float z = 100.0f;
fxMaterial.SetPass(0);
GL.MultiTexCoord2(0, 0.0f, 1.0f);
GL.Vertex3(0.0f, 0.0f, z); // BL
GL.MultiTexCoord2(0, 1.0f, 1.0f);
GL.Vertex3(1.0f, 0.0f, z); // BR
GL.MultiTexCoord2(0, 1.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, z); // TR
GL.MultiTexCoord2(0, 0.0f, 1.0f);
GL.Vertex3(0.0f, 1.0f, z); // TL
GL.End();
GL.PopMatrix();
}
I can’t believe I’m in this apparently simple, yet impossible to achieve situation!
You could use two cameras and set the depth to specify which should render first. Then use one camera for rendering your background and the other for rendering the rest of the scene. I’m unsure how efficient it would be, though.
I only wanted to draw one quad in a 3D background.
Too much to ask for? 
OK I went with sticking a quad as a child of the camera at the Z distance I wanted, and used the camera stuff to calculate how big it needs to be to render it exactly full screen. It seems to rotate perfectly.
Someone may find it useful, so here it is:-
In the quad script…
void Update()
{
GameObject obj = GameObject.FindGameObjectWithTag("MainCamera");
Camera cam = obj.GetComponent<Camera>();
float size = Mathf.Tan(cam.fieldOfView * Mathf.PI / 360.0f) * 2.0f * transform.localPosition.z;
transform.localScale = new Vector3(size * cam.aspect, size, 1.0f);
}
Dave.