Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by JOKER_LD · Aug 01, 2019 at 03:08 AM · scripting problemrendertexture

Capture image use Camera.Render but get different result between preview in editor and saved image.

Sorry for the complex title. But I really being crazy about this issue for days. I manually call Camera.Render to a RenderTexture, then use texture2D to get the texture and then save it as PNG. But what I get in the saved png file is different in the game view. Specificly, I find that gameobject with certain material looks seem in both editor view and saved png file.

     // Public Properties
     public int maxFrames; // maximum number of frames you want to record in one video
     public int frameRate = 30; // number of frames to capture per second
 
     // The Encoder Thread
     private Thread encoderThread;
 
     // Texture Readback Objects
     private RenderTexture tempRenderTexture;
     private Texture2D tempTexture2D;
 
     // Timing Data
 
     private float captureFrameTime;
     private float lastFrameTime;
     private int frameNumber;
     private int savingFrameNumber;
 
     // Encoder Thread Shared Resources
     private Queue<byte[]> frameQueue;
     private string persistentDataPath;
     private int screenWidth;
     private int screenHeight;
     private bool threadIsProcessing;                //
     private bool terminateThreadWhenDone;           
     private string ffmpegBatchPath;
 
     public Camera testCamera;
     public Camera mainCamera;
 
     private void LateUpdate()
     {
         TakeScreenShot();
     }
 
 void TakeScreenShot()
     {
         // Encode texture into PNG
         //byte[] bytes = imageOverview.EncodeToPNG();
 
         if (frameNumber <= maxFrames)
         {
             // Check if render target size has changed, if so, terminate
             if (testCamera.pixelWidth!= screenWidth || testCamera.pixelHeight != screenHeight)
             {
                 threadIsProcessing = false;
                 this.enabled = false;
                 throw new UnityException("ScreenRecorder render target size has changed!");
             }
 
             // Calculate number of video frames to produce from this game frame
             // Generate 'padding' frames if desired framerate is higher than actual framerate
             float thisFrameTime = Time.time;
             int framesToCapture = ((int)(thisFrameTime / captureFrameTime)) - ((int)(lastFrameTime / captureFrameTime));
 
             // Capture the frame
             if (framesToCapture > 0)
             {
                 tempRenderTexture.Create();
                 testCamera.targetTexture = tempRenderTexture;
                 testCamera.Render();
                 RenderTexture.active = testCamera.targetTexture;
 
                 tempTexture2D.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
                 tempTexture2D.Apply();
 
                 RenderTexture.active = null;
                 tempRenderTexture.Release();
                 //RenderTexture.active = null;
             }
 
             // Add the required number of copies to the queue
             for (int i = 0; i < framesToCapture && frameNumber <= maxFrames; ++i)
             {
                 frameQueue.Enqueue(tempTexture2D.EncodeToPNG());
 
                 frameNumber++;
             }
 
             lastFrameTime = thisFrameTime;
 
         }
         else //keep making screenshots until it reaches the max frame amount
         {
             // Inform thread to terminate when finished processing frames
             terminateThreadWhenDone = true;
 
             // Disable script
             this.enabled = false;
         }
 
     }

alt text

This is what the camera render in editor

And this is the image I get as png

alt text

And there is another strange thing, the character with certain material and the background rendered the same in both editor preview and saved png, but other part of the character which have different materials rendered differently.

editor.jpg (62.9 kB)
frame0.png (325.9 kB)
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

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.

Follow this Question

Answers Answers and Comments

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

Related Questions

Texture2D to RenderTexture 1 Answer

CustomRenderTexture (RFloat) wont initialize in the same frame it is created. 0 Answers

Texture.GetNativeTexturePtr sometime returns 0. 2 Answers

EncodeToJPG() creates gray image after using CopyTexture(). 1 Answer

How to get a camera to render to render texture at a custom aspect ratio. 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