- Home /
Additively overlay one camera on another
I have two cameras, one GUI camera, another is a main Game camera; the GUI camera (which needs to be a separate camera) uses a Vignetting image effect that should only be applied to the GUI camera. The main Game camera uses various other image effects.
What I'd like to do is additively overlay the GUI camera onto the main camera.
Clearly I cannot use the default 'camera depth' system of Unity, as this will apply the Vignetting effect to the main camera.
Now the problem is that I can't figure out for the life of me where to start; I have a hunch that I need both (or the GUI only) to render to a rendertexture. Then apply that texture (by assistance of a blend one one shader) to the main camera). So where do I intercept all this data, precull? and do I apply all my stuff onto the main camera on postrender?
Maybe I just need a little nudge into the right direction.
Are you using layers to filter what each camera is rendering?
Yes, the main camera won't render the GUI layer; the GUI camera only renders the GUI layer. However due to the way the 'camera depth' in Unity works, any effects rendered at the highest depth are rendered over the full camera buffer.
I want to render the GUI layer separately; and avoid rendering the GUI camera effects on the main camera 'buffer'.
Received a notification about the following answer:
// The way I usually do this is create another camera exclusively for your GUI camera like you mentioned. Then set the GUI Camera to render above the $$anonymous$$ain Camera with the Filter applied by using The Camera depth like you mentioned. Then, the important step is to set the Clear Flags on your GUI camera to either Depth Only, or Don't Clear. This will allow your $$anonymous$$ain camera to show through your GUI Camera. //
$$anonymous$$ain Camera - Various Effects (depth: -1, skybox)
GUI Camera - Vignetting (chromatic aberration shader) (depth: 0, Depth only or Don't clear)
What this does (for me anyway ;) is render the main camera to the screenbuffer (rendering the full game without GUI). Because the GUI camera is set to don't clear or depth only, the GUI layer is rendered on top of that screenbuffer. The image effects are applied post-render, so it takes the whole screenbuffer, and then applies the effect.
What I want, is apply the effect only to the GUI.
From my perspective, this means I render the GUI onto a black / empty canvas (preferrably alpha) and then overlay it onto the image from the $$anonymous$$ain Camera.
I tried rendering the GUI Camera to a texture and putting it on an object in front of the camera, this is a bad solution.
The good solution would consist of taking the rendertexture from the GUI Camera after the $$anonymous$$ain camera is finished, and somehow overlay the rendertexture onto the $$anonymous$$ain camera render. Do I write an additive shader specifically for this and put some code on main camera, or is there another way to do this?
Yeah, sorry about that. I actually deleted my comment after I read through it because I realized what was happening. That is an interesting problem. I'll give it a look and see if I can think of any other solutions.
Ah I see, Unity Answers was showing me a 'one more reply' button that didn't have any effect so I was under the impression it might be bugged ;).
Thank you everyone for your answers; let's hope someone can put me on track.
Answer by DaveA · Oct 23, 2012 at 05:47 PM
Look into using RenderTexture. Render one of those cams to texture and overlay it on the other's view with some transparency or whatever math you need.
Yeah, I guess my question is basically how I should go about overlaying the texture onto another camera without using OnGui() to draw it.
Answer by cygnusfear · Oct 23, 2012 at 05:42 PM
Yes, the main camera won't render the GUI layer; the GUI camera only renders the GUI layer. However due to the way the 'camera depth' in Unity works, any effects rendered at the highest depth are rendered over the full camera buffer.
I want to render the GUI layer separately; and avoid rendering the GUI camera effects on the main camera 'buffer'.
Thank you @cygnusfear. This is the most useful answer to my current problem. Unfortunately I'm not very deep into shader program$$anonymous$$g yet. All I need to do is fade in a Camera that is depth-cleared addititvely to the main camera so objects (whichfor various reasons cannot be faded by shader) will appear on top while the main camera is still visible. So your approach using the postprocess and Graphics.blit should work, but could you please give me a hint which shader/Graphics.blit combination I have to use to get the depth-cleared result? That would be a life-saver right now.
Answer by cygnusfear · Nov 14, 2012 at 03:12 PM
Ok I can answer myself now: this renders the chromatic abberation / vignetting shader to the current camera only; I removed the original checks from PostEffectsBase so this is a hack. Mind you, you have to assign the correct shaders. It also uses a custom material called material that performs the final additive overlay (you can use FX/Flare)
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class GUICamera : MonoBehaviour {
public float intensity = 0.375f;
public float blur = 0.1f;
public float blurSpread = 1.5f;
public float chromaticAberration = 0.2f;
public Shader chromAberrationShader;
private Material chromAberrationMaterial;
public Shader separableBlurShader;
private Material separableBlurMaterial;
public Shader vignetteShader;
private Material vignetteMaterial;
void Awake () {
cam = GetComponent<Camera>();
chromAberrationMaterial = new Material (chromAberrationShader);
chromAberrationMaterial.hideFlags = HideFlags.DontSave;
separableBlurMaterial = new Material (separableBlurShader);
separableBlurMaterial.hideFlags = HideFlags.DontSave;
vignetteMaterial = new Material (vignetteShader);
vignetteMaterial.hideFlags = HideFlags.DontSave;
}
private RenderTexture display;
public Camera cam;
public Material material;
void OnPreRender () {
CleanRenderTextures();
// display = RenderTexture.GetTemporary((int)camera.pixelWidth,
// (int)camera.pixelHeight, 16, RenderTextureFormat.ARGB32);
// cam.targetTexture = display;
cam.backgroundColor = new Color(0,0,0,0);
cam.clearFlags = CameraClearFlags.SolidColor;
}
void OnRenderImage(RenderTexture source, RenderTexture destination) {
// Graphics.Blit (source, destination);
float widthOverHeight = (1.0f * source.width) / (1.0f * source.height);
float oneOverBaseSize = 1.0f / 512.0f;
RenderTexture color = RenderTexture.GetTemporary (source.width, source.height, 0);
RenderTexture color2 = RenderTexture.GetTemporary (source.width, source.height, 0);
RenderTexture halfRezColor = RenderTexture.GetTemporary ((int)(source.width / 2.0), (int)(source.height / 2.0), 0);
RenderTexture quarterRezColor = RenderTexture.GetTemporary ((int)(source.width / 4.0), (int)(source.height / 4.0), 0);
RenderTexture secondQuarterRezColor = RenderTexture.GetTemporary ((int)(source.width / 4.0), (int)(source.height / 4.0), 0);
Graphics.Blit (source, halfRezColor, chromAberrationMaterial, 0);
Graphics.Blit (halfRezColor, quarterRezColor);
for (int it = 0; it < 2; it++ ) {
separableBlurMaterial.SetVector ("offsets", new Vector4 (0.0f, blurSpread * oneOverBaseSize, 0.0f, 0.0f));
Graphics.Blit (quarterRezColor, secondQuarterRezColor, separableBlurMaterial);
separableBlurMaterial.SetVector ("offsets", new Vector4 (blurSpread * oneOverBaseSize / widthOverHeight, 0.0f, 0.0f, 0.0f));
Graphics.Blit (secondQuarterRezColor, quarterRezColor, separableBlurMaterial);
}
vignetteMaterial.SetFloat ("_Intensity", intensity);
vignetteMaterial.SetFloat ("_Blur", blur);
vignetteMaterial.SetTexture ("_VignetteTex", quarterRezColor);
Graphics.Blit (source, color, vignetteMaterial);
// Graphics.Blit (source, halfRezColor, chromAberrationMaterial, 0);
chromAberrationMaterial.SetFloat ("_ChromaticAberration", chromaticAberration);
Graphics.Blit (color, color2, chromAberrationMaterial, 1);
Graphics.Blit (color2, destination, material);
// RenderTexture.active = destination;
// material.SetTexture("_DepthNormal", display);
// ImageEffects.BlitWithMaterial(material, source, destination);
// get shaders to do their shit here
RenderTexture.ReleaseTemporary (quarterRezColor);
RenderTexture.ReleaseTemporary (secondQuarterRezColor);
RenderTexture.ReleaseTemporary (halfRezColor);
RenderTexture.ReleaseTemporary (color);
RenderTexture.ReleaseTemporary (color2);
CleanRenderTextures();
}
void CleanRenderTextures() {
if (display != null) {
RenderTexture.ReleaseTemporary(display);
display = null;
}
}
}