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 Sylafrs · Mar 13, 2017 at 09:44 PM · androidtexturepluginnative plugin

How to get pixels from an external texture?

Hello,

I wanted to know how to get pixels from an external texture (plugin : EasyMovieTexture).

Currently, I'm using this function :

 private Texture2D GetReadableTexture(Texture2D tex)
 {
     if (myTexture2D == null)
         myTexture2D = new Texture2D(tex.width, tex.height);

     // https://support.unity3d.com/hc/en-us/articles/206486626-How-can-I-get-pixels-from-unreadable-textures-
     RenderTexture tmp = RenderTexture.GetTemporary(
         tex.width,
         tex.height,
         0,
         RenderTextureFormat.Default,
         RenderTextureReadWrite.Linear);

     Graphics.Blit(tex, tmp);
     RenderTexture previous = RenderTexture.active;
     RenderTexture.active = tmp;

     myTexture2D.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0);
     //myTexture2D.Apply(); // no need

     RenderTexture.active = previous;
     RenderTexture.ReleaseTemporary(tmp);

     return myTexture2D;
 }

It returns a readable texture from the external texture. It works. But it is quite slow, and creates a lot of garbage with the GetPixels() that will be done after.

Is there a way to get the texture pixel for Editor/Android, we assume that we are using OpenGL. (has someone done it?) Is it possible without creating a native plugin (tried it, crashes everytime ^^')

My final goal is to get the average color of the image (video frame).

Thanks in advance,

Sylafrs

Comment
Add comment · Show 3
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 Sylafrs · Mar 14, 2017 at 11:21 AM 0
Share

Since my goal is to get the average of the pixels, resizing the wanted texture into a small one will create an average (I think it's using an histogram). Since I don't want a histogram result, but a real average, Resizing the tmp and myTexture2D to 10x10 textures will give a small array (len=100) in which we can perform the average.

Perfs. are way better, but still, I know that I can do better... just can't manage to do it right now :-°

avatar image kuailemario · Apr 07, 2020 at 10:41 AM 0
Share

I know this is a very old problem. Reusing a Texture2D memory every time without re-applying will help reduce garbage, but I would love to know a better solution.

avatar image kuailemario kuailemario · Apr 09, 2020 at 08:25 AM 0
Share

I tested it on the development board, and it took 52.3ms to execute such a conversion function once, which means I could not even guarantee an image of 20 frames per second (50ms per second). If I can directly read the external texture, it will be of great help!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by lgarczyn · Mar 14, 2017 at 06:21 PM

Scaling down simply uses bilinear interpolation, so you will get the average of the 4 middle pixels, quite bad.

If you're dealing with the same video, the highest perf you'll ever get is to do any slow algorithm, store the result in a color array / texture, and use that as your data.

If inaccuracy is okay, just do a getpixels and sample 500 semi-random pixels on the map, that's how I did it in the Flash days.

If you want both accuracy and dynamic videos, this guy did something that works: http://stackoverflow.com/questions/20726556/xna-and-hlsl-sample-entire-texture-for-luminosity-average

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

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

136 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

Related Questions

How to access an Android native plugin in unity ? 0 Answers

Referencing a DLL in C# 0 Answers

GetNativeTexturePtr is not working in iOS plugin 1 Answer

Pass camera stream from unity and android plugin 1 Answer

Android native plugin question 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