Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to capture a camera at power-of-two size in unity free

Discussion in 'Scripting' started by FuzzyQuills, Jun 19, 2014.

  1. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Hi.

    I need to be able to use GL.Viewport to make the camera smaller, then capture the tex at that size, then be able to stretch the viewport back to normal screen size. How could I go about doing this? I am currently trying this, but it nly captures the bottom-left corner... :(
     
  2. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    And i want the captured tex to be at the correct aspect.
     
  3. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    Example (using Unity Pro feature):
    Code (CSharp):
    1.         void LateUpdate ()
    2.         {
    3.                 if (Input.GetKeyDown (KeyCode.S)) {
    4.                         int rw = (int)Mathf.Round (camera.pixelWidth / 4);
    5.                         int rh = (int)Mathf.Round (camera.pixelHeight / 4);
    6.                         RenderTexture rt = new RenderTexture (rw, rh, 24);
    7.                         camera.targetTexture = rt;
    8.                         Texture2D ss = new Texture2D (rw, rh, TextureFormat.RGB24, false);
    9.                         camera.Render ();
    10.                         RenderTexture.active = rt;
    11.                         ss.ReadPixels (new Rect (0, 0, rw, rh), 0, 0);
    12.                         camera.targetTexture = null;
    13.                         RenderTexture.active = null;
    14.                         Destroy (rt);
    15.                         byte[] bytes = ss.EncodeToPNG ();
    16.                         string filename = "screenshot.png";
    17.                         System.IO.File.WriteAllBytes (filename, bytes);
    18.                         Debug.Log (string.Format ("Took screenshot to: {0}", filename));
    19.                 }
    20.         }
     
  4. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Oh, and i forgot to mention, I don't have Unity pro... But i can still use this, so thanks for the code! :)
     
  5. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    This code doesn't make it power-of-two size...
     
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    Surely you can manage that yourself?
     
  7. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    Of course. If you want power of two and save aspect ratio you have to set fixed size you like and make all empty space transparent.

    Say if you render on the screen 1280 x 720 then render 512 x 512 and make top 224 rows of pixels transparent.
     
  8. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    I did mean this:
    basically, perhaps the camera is like this:
    BloomScreen.png
    I want it to be like this:
    BloomScreen - POT.png
    That way, I can capture the screen at power-of-two size. understand now? I know my question wasn't very obvious, but if you know something about the above, please let me know! Also note how the apsect ratio isn't lost, it is just squished together, like a powr-of-two rendertexture in Unity Pro
     
  9. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    Are you asking for help, or do you just want us to do it for you?

    Have you tried playing around with the supplied code? Changing it to be a power of two size isn't a big deal - find where the size is specified and change it to be what you want it to be.
     
  10. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    No, no. I am asking how to do it. and I can easily make any texture power-of-two size, the real problem is trying to capture the WHOLE viewport at that size without chopping off the edges or capturing the bottom-left corner... :(

    thanks for the fast rply BTW! :) I also tried the TextureScale script on the wiki, but that is dead slow for realtime performance...
     
  11. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    Right, so the issue is that you can't use the RenderTexture part?
     
  12. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Yes, I have Unity Free. In unity pro, it is possible to render into a POT texture, right?

    In free version, we have ReadPixels, which captures a viewport as a rect. the workaround would be to squish the aspect ratio so that it is preserved in the POT texture, but all my attempts fail with the bottom-left corner problem. (and that measn that the viewport is STILL at NPOT size!)
     
  13. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    Have you had a look at the documentation for the camera's aspect property?
     
  14. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    I had no idea about the aspect property. If you mean setting the apsect ratio manually, this won't work, as I need the POT texture to hold, say 16:9 aspect, so that when I draw to the screen, the 16:9 aspect lines up with the camera.

    I will have a look at the aspect property...

    EDIT: Ok, this worked for me, thank you very much! :)
     
  15. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Ok, I now have the aspect working. now how would I go about adjusting the aspect so that I can squish the image to preserve it? Thank you for your help so far!

    EDIT: got that fixed, but I have a problem: resetting the aspect after capturing the screen results in the apsect being lost, help!
     
    Last edited: Jun 26, 2014
  16. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    Why mess around with changing the settings on one single camera? Could you set up a second, dormant camera just for the purpose of capturing and leave your normal one alone? Why mess with something you don't need to mess with?
     
  17. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Strangely, if i remove the aspect ratio reset, the camera capturees properly, only problem is, the camera is not stretched. (which is intentional, this is what readpixels is supposed to capture!)
     
  18. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    And there's that, but i was sure a second camera ruined the framerate... But still you never know... unless you try! may give this a go later, as I have an exam after lunch.
     
  19. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    That's why it should be dormant. You don't want it rendering every frame, just the ones you want to capture.

    Just merely existing won't ruin your framerate.
     
  20. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Ok, I got everything working. again thanks for your help! :)

    If you wish to find out the reason why I am attempting this, click the link in my signature for more info. if you wish also, I can put you in the credits for helping capture screen at power-of-two size.