• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Kuura · Jan 03, 2016 at 09:13 AM · shaderrendertexturerender textureclearblit

Prevent Render Texture clearing

In a nutshell, How can I prevent RenderTextures from being cleared after every frame?

I have a script that writes high intensity values to a render texture and I want to do an image effect with this over multiple frames while constantly drawing more to it; kinda like a camera with no clear flags. The problem with this is that Unity is damn dedicated to clear the RenderTexture on every frame with the camera content. Is it possible to prevent this somehow or make a workaround?

Here's my simple script for reference.

Unity 5.3.1p1

 using UnityEngine;
 
 [RequireComponent(typeof(Camera))]
 public class TemporalTest : MonoBehaviour {
 
     public Shader temporalTestShader;
     public RenderTexture rt;
     public Material temporalMaterial;
 
     void Start () {
         rt = new RenderTexture(Screen.width, Screen.height, 0);
         temporalMaterial = new Material(temporalTestShader);
     }
 
     void OnRenderImage(RenderTexture source, RenderTexture destination) {

         // rt gets filled correctly here
         // but it's cleared every frame for no reason causing nuclear headaches

         temporalMaterial.SetTexture("_TemporalMap", rt);
         Graphics.Blit(source, rt, temporalMaterial, 0);
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Kuura · Jan 04, 2016 at 01:25 PM

Ok, after hours of debugging I found out that it was the Blit function that is clearing the rendertexture and not unity in between OnRenderImage calls.

The problem was that you can't do

 mat.SetTexture("something", sometex);
 Blit(A, B, mat); // Add A to "something" in shader and save to some result B

and expect it to write the result nicely to B. Instead you have to use temporary rendertextures and do

 mat.SetTexture("something", sometex);
 RenderTexture temp = RenderTexture.GetTemporary(...);
 Blit(A, temp, mat); // Add A to "something", save to temp
 Blit(temp, B); // Copy temp to some result texture B
 RenderTexture.Release(temp);

I don't know why it has to be done this way or why can't I use B straight as a result and so on but at least it works.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TocaSimon · Dec 05, 2019 at 09:57 AM 0
Share

Thank you!

We had this exact problem. We're usually are developing on OSX but we had a $$anonymous$$m member joining on Windows and a feature related to this did not work at all. Your solution solved everything. You are my hero for the day!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to alter/modify a RenderTexture with a shader,How can I modify/"post process" a RenderTexture using a shader 0 Answers

How to use Graphics.Blit with custom input textures 3 Answers

In Unity 2D how can I make a shader aware of the Z coordinate (depth) of the object it is about to draw over? 1 Answer

dynamic hole in layer / texture / camera 0 Answers

Blit isn't writing to render texture 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges