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 TruffelsAndOranges · Aug 26, 2015 at 06:51 PM · camerasample

Use RenderTexture to sample pixels from camera?

I have a camera. How do I sample pixels from this camera? What call do I make to the camera to get a texture of what the camera is going to render?

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

2 Replies

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

Answer by _Gkxd · Aug 26, 2015 at 07:27 PM

I did actually have to do this in one of my projects, where I used some off-screen rendering to detect mouse clicks on non-standard objects (cubic bezier curves to be precise). As far as I know, there is no way of directly accessing the data inside a RenderTexture, since the data is stored on the GPU. You have to transfer it to a Texture2D in order to actually sample the texture on the CPU (in scripts).

Here is a relevant code snippet from that project that does this (attached to a camera object):

 public Camera offscreenCamera; // The camera that will do the offscreen rendering  

 // Initialize everything properly in Start/Awake
 private RenderTexture curveSelectTargetTexture; // The target texture
 private Texture2D curveSelectTexture; // The texture we sample from
 private bool shouldDoRendering; // Prevents the process from happening every frame
 
 void OnPostRender() {
     if (shouldDoRendering) {
         // Make sure that the the target texture actually exists
         if (!curveSelectTargetTexture.IsCreated()) {
             curveSelectTargetTexture.Create();
         }
         
         // Set camera to render to our target texture
         offscreenCamera.targetTexture = curveSelectTargetTexture;
         
         /* Offscreen rendering was done here (removed from this code snippet) */
         
         // The following line transfers the data from the RenderTexture to the Texture2D
         curveSelectTexture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);

         Color colorOfPixelAtMouse = curveSelectTexture.GetPixel((int)Input.mousePosition.x, (int)Input.mousePosition.y);
         
         // Some other processing that is irrelevant
     }
 }

Note that the pixels in the Texture2D will be anti-aliased. You can disable this by disabling anti-aliasing in the quality settings, but then your game would not have anti-aliasing. You can also disable anti-aliasing by making the camera use deferred rendering, though I have encountered a bug where anti-aliasing still occurs in orthographic view.

If you are rendering visuals, then this doesn't really matter, but if you are rendering information (which is what I had to do), then you will need to either disable anti-aliasing or filter out garbage information caused by anti-aliasing.

You will probably be most interested in the line that contains GetPixel.

Comment
Add comment · Show 4 · 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 TruffelsAndOranges · Aug 26, 2015 at 07:31 PM 0
Share

Thanks. That's a brilliant example. I'll try it out. :)

avatar image TruffelsAndOranges · Aug 26, 2015 at 08:53 PM 0
Share

Okey so hm that didn't work. Is the RenderTexture even used? I just get 0, 0, 0, 1 as the RGBA color.

avatar image _Gkxd · Aug 26, 2015 at 09:03 PM 0
Share

You can use the following line to see if the Texture2D actually contains anything:

 System.IO.File.WriteAllBytes(Application.dataPath + "/../SomeTestFileName.png", curveSelectTexture.EncodeToPNG());

This will save an image in your project folder whenever it's called. Be sure not to call this every frame, or your program will crash.

$$anonymous$$y guess is that you're not rendering anything. After you set the target texture of the camera, you have to do some rendering so that the target texture actually contains things.

avatar image TruffelsAndOranges · Aug 26, 2015 at 11:34 PM 0
Share

I realized I didn't even need to use a RenderTexture! As I just needed the color (I didn't need to draw it), I could just do this:

 void OnPostRender(){
     cameraTex2D.ReadPixels(new Rect(0, 0, screenWidth, screenHeight), 0, 0);
     Color sampledPixelColor = cameraTex2D.GetPixel(pixelPositionX, pixelPositionY);

     // Do something with sampled color.
 }

Thank you for your help though! :)

avatar image
2

Answer by Thomas-Mountainborn · Aug 26, 2015 at 06:57 PM

Use OnRenderImage.

Comment
Add comment · Show 2 · 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 TruffelsAndOranges · Aug 26, 2015 at 07:07 PM 0
Share

That seems pretty neat but how do I grab a specific pixel from the src RenderTexture?

avatar image Thomas-Mountainborn · Aug 26, 2015 at 07:12 PM 0
Share

@Friduric, http://docs.unity3d.com/ScriptReference/Texture2D.ReadPixels.html

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

27 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

Related Questions

How to make camera position relative to a specific target. 1 Answer

third person shooter player follow the crosshair 1 Answer

Editor Camera Lag 5 Answers

Disabling VR Camera rotation creates ''cinema'' effect in VR device 1 Answer

Switching Between Cameras JavaScript 0 Answers


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