• 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 MOrlando616 · Apr 09, 2014 at 04:39 PM · camerarenderingrendertexture

ClearFlags don't seem to work when I manually call Render() on my cameras.

Hi there,

I'm having a bit of an issue with taking screenshots in our game. Rather than using Application.CaptureScreenshot(), I create a RenderTexture with my own sizing and handle rendering myself (so I have a bit more control over what is in the screenshot...ie I don't add the HUD camera).

So my code looks like this:

 if( Input.GetKeyDown( KeyCode.F12 ) && m_OrderedCameras != null )
 {
      RenderTexture rt = new RenderTexture( m_ResWidth, m_ResHeight, 24 );
      Texture2D screenShot = new Texture2D( m_ResWidth, m_ResHeight, TextureFormat.RGB24, false );
     
      for( int i = 0; i < m_OrderedCameras.Length; ++i )
      {
         if( m_OrderedCameras[ i ] != null )
         {
             m_OrderedCameras[ i ].targetTexture = rt;
             m_OrderedCameras[ i ].Render();
             m_OrderedCameras[ i ].targetTexture = null;
         }
     }
     
     RenderTexture oldRT = RenderTexture.active;
     RenderTexture.active = rt;
     screenShot.ReadPixels( new Rect( 0, 0, (float)m_ResWidth, (float)m_ResHeight ), 0, 0 );
     RenderTexture.active = oldRT;
     Destroy( rt );
     
     string filename = Application.dataPath.Substring( 0, Application.dataPath.LastIndexOf( "/" ) );
     filename = filename.Substring( 0, filename.LastIndexOf( "/" ) );
     System.IO.Directory.CreateDirectory( filename + "/Screenshots" );
     filename += string.Format( "/Screenshots/screen_{0}x{1}_{2}.png", m_ResWidth, m_ResHeight, System.DateTime.Now.ToString( "yyyy-MM-dd_HH-mm-ss" ) );
     
     System.IO.File.WriteAllBytes( filename, screenShot.EncodeToPNG() );
     
     Debug.Log( "Screenshot taken:  " + filename );
    }

The code is pretty straightforward I believe. The problem, however, is that some of my cameras aren't doing what they should be...or at least...what they do during normal rendering.

For instance, I use about 7 cameras during our main loop (in order based on depth):

  • Distant Camera - clears to Skybox, renders distant objects

  • Environment Camera - clears depth only, renders most objects, Post: DoF Scatter

  • SmallEnemy Camera - no clearing, renders small enemies, Post: Color Correction

  • Effects Camera - no clearing, renders particle FX

  • Radar Camera - no clearing, renders radar-related objects

  • Player Camera - no clearing, renders player, Post: Vignette

So, here is a very specific test I ran that clearly outlines the problem and hopefully what's happening for some graphics-savvy person out there.

  1. I use just the Distant camera and everything renders as you'd expect.

  2. I add Environment camera ontop of that and now the Skybox doesn't render, but there is DoF.

  3. I then add the SmallEnemy camera...skybox doesn't render, DoF gone, CC is ok though

  4. I get up to adding the player and now color correction is also gone...

I added some debug text to the OnRenderImage of the Depth of Field and Color Correction post processing scripts and they got hit during the manual rendering. I don't understand why there's no skybox, and I certainly don't understand how a future camera could 'undo' the DoF unless it also rendered the same layers (which none do). I've noticed where the sky doesn't render that in some tests I saw the player again, but upside-down.

In any case, I appreciate all help anyone can provide, this has me stumped.

Thanks!

-Matt

Comment
Add comment · Show 2
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 MOrlando616 · Apr 24, 2014 at 07:15 PM 0
Share

Still having no luck with this, has anyone encountered this problem as well?

avatar image MOrlando616 · May 09, 2014 at 11:23 PM 0
Share

Progress!...sort of. I've narrowed down the problem to the resolution size that I'm trying to render this screenshot at. When I boot up, I set the resolution width and height that I use to match that of the game, but I expose these so we can take tri-monitor screenshots. When I change the resolution, that's when things stop working...

It must have something to do with the allocation of the post-processing effects maybe? This is uncharted territory for me so I'm not quite sure what would be wrong...

0 Replies

· Add your reply
  • Sort: 

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Texture Render - Camera only render changes 0 Answers

How can I set the exact camera render size for render textures? 0 Answers

RenderTexture doesn't get cleared and camera just adds to it 1 Answer

Is there a way to write to Deferred Depth from a command buffer in CameraEvent.AfterImageEffects? 1 Answer

How to get depth texture and render texture from one camera 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