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 Hylus · Sep 18, 2019 at 03:22 PM · post processingrender texturecapturepost effectscapturescreen

How to capture screenshot with post process effect?

Im using Edge detection post process effect (https://docs.unity3d.com/540/Documentation/Manual/script-EdgeDetectEffectNormals.html) Script is attached to my camera and wondering how to capture screen with this effect. What I expect: alt text What i have: alt text

My outlines are renderer via shader and everything looks great at runtime. The problem is how to apply post effect to my rendertexture.

 private Rect rect;
 private RenderTexture renderTexture;
 private Texture2D screenShot;
 ImageFormat format = ImageFormat.PPM; 
 
 
 private void CreateScreenShoot()
     {
         camera.targetTexture = renderTexture;
         camera.Render();
 
         // read pixels will read from the currently active render texture so make our offscreen 
         // render texture active and then read the pixels
         RenderTexture.active = renderTexture;
         screenShot.ReadPixels(rect, 0, 0);
 
         // reset active camera texture and render texture
         camera.targetTexture = null;
         RenderTexture.active = null;
 
         DataStorageBase.CreateDirectory(DataStorageBase.MiniaturesDataPath);
         string filename = Path.Combine(DataStorageBase.MiniaturesDataPath, DataStorageBase.ChosenImage + DataStorageBase.MiniaturesSavingExtension);
       //   string filename = Path.Combine(DataStorageBase.MiniaturesDataPath, DataStorageBase.ChosenImage + ".png");   
 
         SaveTexture(filename,screenShot, format);
     }
 
 private static void SaveTexture(string filename, Texture2D screenShot, ImageFormat format)
     {
         // pull in our file header/data bytes for the specified image format (has to be done from main thread)
         byte[] fileHeader = null;
         byte[] fileData = null;
         if (format == ImageFormat.RAW)
         {
             fileData = screenShot.GetRawTextureData();
         }
         else if (format == ImageFormat.PNG)
         {
             fileData = screenShot.EncodeToPNG();
         }
         else if (format == ImageFormat.JPG)
         {
             fileData = screenShot.EncodeToJPG();
         }
         else // ppm
         {
             // create a file header for ppm formatted file
             string headerStr = string.Format("P6\n{0} {1}\n255\n", screenShot.width, screenShot.height);
             fileHeader = System.Text.Encoding.ASCII.GetBytes(headerStr);
             fileData = screenShot.GetRawTextureData();
         }
 
         Debug.Log(filename);
 
         // create new thread to save the image to file (only operation that can be done in background)
         new System.Threading.Thread(() =>
                                     {
                                         // create file and write optional header with image bytes
                                         var f = System.IO.File.Create(filename);
                                         if (fileHeader != null)
                                             f.Write(fileHeader, 0, fileHeader.Length);
                                         f.Write(fileData, 0, fileData.Length);
                                         f.Close();
                                     }).Start();
 
 
     }

q1.png (26.4 kB)
q2.png (2.6 kB)
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 Glurth · Sep 18, 2019 at 03:38 PM 0
Share

shot it the dark here.. but in the docs for camera.Render():

 You are not able to call the Render function from a camera that is currently rendering. If you wish to do this create a copy of the camera, and make it match the original one using CopyFrom.

avatar image Hylus Glurth · Sep 19, 2019 at 06:53 AM 0
Share

Thanks for reply. Well camera script is disabled and i call camera.render to enable it. I forgot to mention that i have two cameras. First is main camera which render everything, and second one which render only shapes (like in picture above) and this camera is enable only when i want to capture a screenshot.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Hylus · Sep 19, 2019 at 08:58 AM

I find origin problem. Camera.render doesn't invoke OnRenderImage in my post effect script. Does exist any solution to invoke and correctly execute OnRenderImage after calling camera.render in the same frame? Because users couldn't see any changes in camera.

Comment
Add comment · Show 3 · 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 Glurth · Sep 19, 2019 at 04:01 PM 0
Share

OnRenderImage is used to generate post processing effects(according to docs), so kinda makes sense it's not invoked inside the post-processing stuff. Perhaps OnPostRender() would work better for this part?

avatar image Hylus Glurth · Sep 24, 2019 at 07:54 AM 0
Share

please explain what do you mean. Should i move some part of my code to onPostRender?

avatar image Glurth Hylus · Oct 09, 2019 at 03:25 PM 0
Share

Yes, that's what I was suggesting- but honestly, it's just a guess.

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

111 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

Related Questions

Skybox and color correction with steamVR 0 Answers

Changing Post process V2 with C# 0 Answers

Running an OnRenderImage effect before the post processing stack (v2) 1 Answer

Unity Camera Capture for Android to video file 4 Answers

Is there any way to apply different textures to a plane on front and back? 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