- Home /
After upgrade to Unity 5, my post effect fails
Hi and thanks for all your help. I'm not sure where to begin with this one.
Basically, inside of an image effect, during the OnRenderImage method, I create a camera, render with special shader, then delete it. This worked in Unity 4.
Can we no longer render a new camera within OnRenderImage of the main camera?
Here is the shader code:
Shader "Lemon Joker/Image Effects/LJ_AOFactor"
{
Properties
{
_AOIntensity ("AO Intensity", Float) = 1.0
}
SubShader
{
Tags { "AOFactorType"="Properties" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float _AOIntensity;
float4 vert(float4 v : POSITION) : SV_POSITION
{
return mul(UNITY_MATRIX_MVP, v);
}
float4 frag() : COLOR
{
return float4(_AOIntensity, 0, 0, 0);
}
ENDCG
}
}
}
here is the OnRenderImage code:
[ImageEffectOpaque]
void OnRenderImage (RenderTexture source, RenderTexture destination)
{
// Adjust size
int height = Screen.height;
int width = Screen.width;
if (destination != null)
{
height = destination.height;
width = destination.width;
}
if (outputTex.width != width || outputTex.height != height)
{
RecreateSurfaces(width, height);
}
var aoFactorTex = RenderTexture.GetTemporary(Screen.width, Screen.height, 16, RenderTextureFormat.RFloat);
RenderAOFactor(aoFactorTex);
Graphics.Blit(source, depthNormalTex, material);
RenderHBAO(outputTex.GetNativeTexturePtr(), Util.MatrixToArray(GetComponent<Camera>().projectionMatrix),
radius, bias, exponent, blurEnable, (int)blurIterations, sharpness, coarseAO, detailAO);
Material blendMat = new Material(Shader.Find("Lemon Joker/Image Effects/BlendAO"));
blendMat.mainTexture = source;
blendMat.SetTexture("_AOTex", outputTex);
blendMat.SetTexture("_AOFactorTex", aoFactorTex);
Graphics.Blit(source, destination, blendMat);
RenderTexture.ReleaseTemporary(aoFactorTex);
}
and here is the code which is called from OnRenderImage:
private void RenderAOFactor(RenderTexture target)
{
var go = new GameObject("", typeof(Camera));
Camera cam = go.GetComponent<Camera>();
cam.CopyFrom(GetComponent<Camera>());
cam.renderingPath = RenderingPath.Forward;
cam.depthTextureMode = DepthTextureMode.None;
cam.backgroundColor = Color.white;
cam.targetTexture = target;
cam.enabled = true;
cam.RenderWithShader(Shader.Find("Lemon Joker/Image Effects/LJ_AOFactor"), "AOFactorType");
DestroyImmediate(go);
}
If I remove RenderAOFactor, everything else works as expected. And here is a video of what the result of this camera render looks like. Its an RFloat texture format.
Then you get this image sometimes.. what is the tire texture doing here? It appears to me as some kind of bad memory pointer somewhere. Thank you for your help! I'll be sure to return the favor.
What happens if you create the gameobject+camera only once? You can still call copyfrom and change all the other settings, and you can set enabled=false to prevent it from drawing as part of the normal pipeline.
Thanks, that made it work perfectly. I don't know exactly why that helped, but I have a feeling it has to do with the init of the camera requiring one game loop. If you add an answer to this post, I'll mark it as correct.
Great, thanks! I'm not sure 100% why either, but I had a hunch along the same lines.
Answer by zach-r-d · Jun 12, 2015 at 02:57 AM
Try creating the gameobject+camera only once. You can still call copyfrom and change all the other settings, and you can set enabled=false to prevent it from drawing as part of the normal pipeline.
Your answer
Follow this Question
Related Questions
Image Effect creation, Wulff Net 0 Answers
Edge Detection script [Legacy Image Effect] - NOT working on mobile (iOS, Android) 0 Answers
Image Effects with 2D sprites 0 Answers
G-Buffers don't exist... 0 Answers
How to make a 2d shader with edge waves 0 Answers