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
2
Question by david.gavilan · May 21, 2013 at 10:37 AM · texture2drendertexturescreenshot

Call camera.Render() twice: RenderTexture contents differs from ReadPixels()

I am trying to render something to a texture in 2 phases. I've created a camera, and in phase (A) I render the contents that some other camera (A) is seeing. After loading some object, in phase (B) I render the contents that another camera (B) is seeing. So basically, I move my capture camera around, looking at what other cameras are looking at, and compositing the results of 2 Render() calls in one RenderTexture.

When I debug the RenderTexture in the Editor, the result seems perfectly fine. But when I call ReadPixels to put the RenderTexture inside a Texture2D, the result is a texture that only contains the results of the first render (A). How can this be possible? Any ideas? I've tried waiting until the end of the frame, waiting several frames, etc. but nothing worked...

     mCamera.CopyFrom(Camera.mainCamera);
     mCamera.clearFlags = CameraClearFlags.SolidColor;
     
     //Assign camera to temporary render target
     RenderTexture tTempRT = RenderTexture.GetTemporary(w, h, 16, RenderTextureFormat.RGB565);
     mCamera.targetTexture = tTempRT;
     //Set the temporary render target to the active one.
     RenderTexture.active = mCamera.targetTexture;
     // (A) Then render it.
     mCamera.Render();
     RenderTexture.active = null; // go back to main context
     // Load something else and wait
     LoadSomething();
     while (!mIsReady) {
         yield return null;
     }
     // look at the new stuff we loaded from other camera
     mCamera.CopyFrom(mSomeOtherCamera);
     // Don't clear, because we'll render the frame on top
     mCamera.clearFlags = CameraClearFlags.Nothing;
     //Set the temporary render target to the active one.
     mCamera.targetTexture = tTempRT;
     RenderTexture.active = mCamera.targetTexture;
     // (B) Render the new stuff on top of (A)
     mCamera.Render();
     // at this point, if we wait, and check the RenderTexture in the editor, it contains both (A) and (B)
     //for (int i = 1; i < 1000000000; i++) yield return null;

     // But if we do this, tImage contains only (A), not (B) :(
     RenderTexture.active = mCamera.targetTexture;
     Texture2D tImage = new Texture2D(w, h,TextureFormat.RGB24, false);
     tImage.ReadPixels(new Rect(0, 0, mWidth, mHeight), 0, 0);
     tImage.Apply();
     
     mCamera.targetTexture = null; //Free the camera from render target
     RenderTexture.ReleaseTemporary(tTempRT);
     RenderTexture.active = null; // go back to main context
     
     someCallback(tImage);


Edit: * actually, I start the process with

      mCamera.enabled = false

  • but in LoadSomething() I set mCamera.enabled = true, that's why I see the thing (B) rendered in the RenderTexture...

  • If I keep mCamera.enabled = false all the way, I only see the first render (A) in the RenderTexture... the same as the ReadPixels is reading.... I don't understand :(

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by david.gavilan · May 27, 2013 at 02:03 AM

Ok, there was some mistake that was not detectable just with the code above. Sorry... The problem was the timing of the 2nd capture:

     LoadSomething();
     while (!mIsReady) {
        yield return null;
     }

This LoadSomething() loads some graphic (FYI, a 2D object rendered using LWF library), and OnPostLoad() it tells us that it finished loading. The problem was that I needed to wait one frame more until the object calls its Render once, where it initializes its transformation matrices.

When I was debugging in the editor, because I was pausing with that busy loop, of course I could see the object, because it was getting updated. But when I was trying to capture immediately, it didn't appear just yet... That's all. orz

So, to solve the problem, I manually called the exec of that object once OnPostLoad(). End of the story. Sorry about the fuss. I hope the code above is useful anyway.

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 MrPhil · Dec 13, 2013 at 06:27 PM 0
Share

I bet you'd find this worked even better:

yield return new WaitForEndOfFrame();

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

RenderTexture to Texture2D 4 Answers

Texture2D saves as PNG correctly but doesn't display properly in RawImage 1 Answer

Taking Non Power of Two Resolution Screen Shots 0 Answers

Converting a RenderTexture to a Texture2D for use in a shader 2 Answers

Only grey (205) when reading pixels from a RenderTexture to a Texture2D 1 Answer


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