- Home /
RenderTextures and FullScreen Quads
Hey all,
Trying to write a small script that when attached to a camera will allow me to control the size of the rendered screen (so that I can render to a smaller texture, then scale that up to the screen size for performance).
EDIT: The new code bellow currently renders the camera, and uses the OnRenderImage() to draw it to a fullscreen quad (through to another render texture). Both output image files display expected results, the scene as it should be, and an inverted colour version.
using UnityEngine;
using System.Collections;
public class RenderBufferControl : BaseObject
{
private Material mat;
private RenderTexture rTex;
private Camera _cam;
protected override void CustomAwake ()
{
_cam = GetComponent<Camera>();
mat = new Material (
"Shader \"Hidden/Invert\" {" +
"SubShader {" +
" Pass {" +
" ZTest Always Cull Off ZWrite Off" +
" SetTexture [_RenderTex] { combine one-texture }" +
" }" +
"}" +
"}"
);
rTex = new RenderTexture(1024, 1024, 24);
_cam.targetTexture = rTex;
}
void OnRenderImage (RenderTexture source, RenderTexture dest)
{
RenderTexture.active = dest;
source.SetGlobalShaderProperty ("_RenderTex");
DrawFullScreenQuad(mat);
// Output source and destination to images to confirm their contents.
Util.SaveImage(source, "Source.png");
Util.SaveImage(dest, "Dest.png");
}
public static void DrawFullScreenQuad(Material material)
{
GL.PushMatrix();
GL.LoadOrtho();
int i = 0;
while (i < material.passCount)
{
material.SetPass(i);
GL.Begin(GL.QUADS);
GL.Color(Color.white);
GL.TexCoord2(0, 0);
GL.Vertex3(0, 0, 0.1f);
GL.TexCoord2(1, 0);
GL.Vertex3(1, 0, 0.1f);
GL.TexCoord2(1, 1);
GL.Vertex3(1, 1, 0.1f);
GL.TexCoord2(0, 1);
GL.Vertex3(0, 1, 0.1f);
GL.End();
++i;
}
GL.PopMatrix();
}
}
The only issue now is that I need to be drawing the fullscreen quad to the actual screen, not just to another render texture. I'm not entirely sure how to do this, could anyone suggest a way of achieving this?
EDIT: Currently I've managed to get it working by setting the source render texture in OnRenderImage() as the texture for a material on a plane set in front of an orthographic camera that is rendered after the main camera. this seems to work, but I'd still like to know if this is the best way to do it, or if there are any other methods to use.
Woops nnot sure how I missed that one, i've added it to the post.
Answer by Owen-Reynolds · Dec 06, 2012 at 11:07 PM
Ummm ... maybe try a 2nd quad with flipped verts (it says culling is off, but it always sneaks some in)? And put Z as an Inspector var to check other vals? (final Z should be negative? But not sure what LoadOrtho is doing.)
Tried reversing the vert order, still no dice :(. I also just checked that the render texture was getting the screen by saving it to an image, and besides a slightly warped perspective and dimmer lighting that usual it seems to be rendering the camera view just fine.
Answer by DuFFOwNz · Nov 09, 2016 at 05:46 AM
Maybe Graphics.Blit is what you're looking for. You can draw a fullscreen quad on the game view by setting the dest RenderTexture to null like:
Graphics.Blit(aTexture, null as RenderTexture, material);
Your answer
Follow this Question
Related Questions
Easy way to render a texture, pixel perfectly, with a shader, and get the result? 0 Answers
RenderTexture to capture an area bigger than Screen on IOS 0 Answers
behavior of Screen object differs in Unity 3 1 Answer
Just installed Unity, cannot see menu bar 1 Answer
Toggle full screen double click issue 2 Answers