Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 cygnusfear · Oct 23, 2012 at 01:12 PM · camerashadersrendertextureimage effectsoverlay

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.

Comment
Add comment · Show 5
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 FWCorey · Oct 23, 2012 at 01:16 PM 0
Share

Are you using layers to filter what each camera is rendering?

avatar image cygnusfear · Oct 23, 2012 at 01:33 PM 0
Share

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'.

avatar image cygnusfear · Oct 23, 2012 at 04:09 PM 0
Share

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. //

  1. $$anonymous$$ain Camera - Various Effects (depth: -1, skybox)

  2. 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?

avatar image pixelmixer · Oct 23, 2012 at 04:27 PM 0
Share

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.

avatar image cygnusfear · Oct 23, 2012 at 04:32 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

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 cygnusfear · Oct 23, 2012 at 05:54 PM 0
Share

Yeah, I guess my question is basically how I should go about overlaying the texture onto another camera without using OnGui() to draw it.

avatar image
1

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'.

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 Studiomaurer · Feb 09, 2017 at 11:11 AM 0
Share

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.

avatar image
1
Wiki

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;
         }
 
     }
 
 }
 
Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Best way to access RenderTexture immediately after camera render 0 Answers

Using custom RenderTexture in ImageEffects. 1 Answer

Render Splitscreen to RenderTexture 0 Answers

Disabling AA Changes how SS Effects Work 1 Answer

RenderTexture behaviour inconsistent to Cameras, Rendering two times to a RenderTexture respecting depth, Rendering object visibility 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges