- Home /
Question by
Teku-Studios · May 23, 2013 at 03:45 PM ·
shaderrendertexturepostprocess
Render to texture performance problems
Hi there!
We are just doing some cool effects with shaders and post process but there is something wrong for sure the way we are doing it because it's pretty slow (specially at Full Screen or Maximizing the editor window). We suspect could be the way assigning RenderTextures to the camera. Here it is the code in a camera script used:
public void OnPreCull()
{
// Start from nothing
CleanRenderTextures();
// Reference to ppCamera’s camera
Camera cam = GetPPCamera();
// Set up camera
cam.CopyFrom(this.camera);
cam.cullingMask = 1 << 27; //Culling mask for rendering what we need to combine later
cam.depth = 2;
cam.clearFlags = CameraClearFlags.Color;
cam.backgroundColor = new Color(0.2f,0.2f,0.2f,1.0f);
lightTex = RenderTexture.GetTemporary (Screen.width, Screen.height, 16);
//I could use new RenderTexture with Screen dimensions but not sure about using Create method
//(it is called from Cam when render?)
cam.targetTexture = lightTex;
//RenderTexture.active = lightTex; Is this needed?
cam.Render ();
}
/**
* Post Processing magic
* @param source
* @param destination
*/
public void OnRenderImage(RenderTexture source, RenderTexture destination)
{
dimensionCompositeMaterial.SetTexture("_ForeTex", lightTex);
Graphics.Blit (source,destination,compositeMaterial );
CleanRenderTextures();
}
And here it is the compositeMaterial shader code:
Shader "Teku/TestComposite" {
Properties {
_MainTex ("Focus scene texture", 2D) = "white" {}
_ForeTex ("ForeGround texture", 2D) = "white" {}
}
SubShader {
Tags {"Queue"="Transparent+31"}
Pass {
ZWrite Off Lighting Off Cull Off Fog { Mode Off } Blend SrcAlpha OneMinusSrcAlpha
/* This was used to cobine layers as in photoshop but it's incredibly slow
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct data
{
float4 position : POSITION;
float2 uv: TEXCOORD0;
};
struct v2f
{
float4 position : POSITION;
float2 uv : TEXCOORD;
};
sampler2D _Back0Tex;
sampler2D _Back1Tex;
sampler2D _ForeTex;
sampler2D _MainTex;
v2f vert(data v)
{
v2f o;
o.position = mul(UNITY_MATRIX_MVP, v.position);
o.uv = v.uv;
return o;
}
float4 frag(v2f i) : COLOR
{
float4 pa = tex2D(_MainTex, i.uv);
float4 pa = tex2D(_ForeTex, i.uv);
float4 final = pa + pb *(1.0-pa.a); //MainScene + Front
return final;
}
ENDCG
*/
SetTexture [_MainTex] {
Combine texture
}
//Multiply time instead using macros for testing!!
SetTexture [_ForeTex]
{
Combine texture * previous
}
}
}
}
Well, as you can see, we'll try to find if the real problem was in the fragment shader testing SetTexture macros but the performance it's similar. I'm sure the problem is the way we are assigning Rendertextures to the camera or something similar. Sometimes we suffer from flickering problems too.
Thank you very much in advance for your help!!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613102805im_/https://answers.unity.com/themes/thub/images/avi.jpg)