- Home /
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.
I use just the Distant camera and everything renders as you'd expect.
I add Environment camera ontop of that and now the Skybox doesn't render, but there is DoF.
I then add the SmallEnemy camera...skybox doesn't render, DoF gone, CC is ok though
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
Still having no luck with this, has anyone encountered this problem as well?
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...
Your answer
Follow this Question
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