- Home /
Can't draw renderTexture to screen
I've been trying to make a pixelated effect, by simply rendering the camera to a small renderTexture, and then drawing that to the screen scaled up. I have not been able to get this to work, however. The screen inside the editor is completely blank. This is the only custom component attached to the first person character controller. Any ideas?
using UnityEngine;
using System.Collections;
public class ClampRenderRes : MonoBehaviour {
RenderTexture screenTexture;
// Use this for initialization
void Start () {
screenTexture = new RenderTexture(200, 200, 24);
screenTexture.Create();
}
// Update is called once per frame
void Update () {
}
void OnPreRender()
{
Camera cam = this.GetComponent<Camera>();
if (!screenTexture.IsCreated())
{
screenTexture = new RenderTexture(200, 200, 24,RenderTextureFormat.ARGB32);
screenTexture.Create();
}
cam.targetTexture = screenTexture;
}
void OnPostRender()
{
Graphics.DrawTexture(new Rect(0,0,200,200),screenTexture);
}
}
Comment
Answer by tanoshimi · Aug 12, 2015 at 05:53 AM
Use OnRenderImage() to modify the image drawn to the screen, not OnPostRender().
Something like (untested):
using System;
using UnityEngine;
public class ClampRenderRes : UnityStandardAssets.ImageEffects.ImageEffectBase
{
public int horizontalResolution = 320;
public int verticalResolution = 240;
// Called by camera to apply image effect
void OnRenderImage (RenderTexture source, RenderTexture destination)
{
RenderTexture scaled = RenderTexture.GetTemporary (horizontalResolution, verticalResolution);
Graphics.Blit (source, scaled, material);
Graphics.Blit (scaled, destination);
RenderTexture.ReleaseTemporary (scaled);
}
}
Chuck that script onto your camera.
Hmm... That doesn't seem to work. It does actually display though, just at the normal resolution. It also does this when I build and run.