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 richard_hawkes · Nov 01, 2016 at 10:05 AM · 2dtexture2dsetpixel

Texture2D update every frame - The right method

Hi guys,

Apologies if this has been answered before, but I'm struggling to find exactly what I'm after.

I'm playing around with some algorithms that require me to update each pixel of a texture depending on those around it. So I create a Plane, and add a Texture2D via a script to it. I loop through the pixels on each Update(), do the calculations, then the SetPixel(...). At the end, I call Apply(). This all works... But...

... it's terribly slow! Don't get me wrong, I understand that for a 1024x1024 texture, I'm doing a million pixel updates per frame. I'm just wondering if this is the standard way to do this.

Please understand that I'm not asking for ways to optimize the algorithm. I know that one answer might be "only update pixels that have changed". Unfortunately that kind of optimisation is probably more computationally expensive than a simple pixel update.

So my question is:

What's the standard "Canvas" for 2D pixel manipulation per frame?

If I'm doing it right already, I'll shut up. I'm just wondering if somebody's going to say "No, you fool! Use XXXXX" !!

Thanks in advance Richard

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 TreyH · Nov 01, 2016 at 05:10 PM 1
Share

Yeah, what are you really trying to do here? This sort of thing is typically done by setting all of the pixels at once (see $$anonymous$$inect camera projects), but individually is quite slow.

Taken from another question: http://answers.unity3d.com/questions/266170/for-different-texture-sizes-which-is-faster-setpix.html

 for(int a = 0; a < size*size; a++) {
     colors[a]=Color.white;
 }
 texture.SetPixels(colors);

Create all of your colors ahead of time, then assign them all at once.

avatar image Daemonhahn · Nov 01, 2016 at 05:15 PM 0
Share

To expand on what TreyH is saying, every time you set the pixels ALL of them are reset even if to the same color. So when you call for each pixel individually ins$$anonymous$$d of setting all at once you are setting -a lot- more than millions every frame.

4 Replies

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

Answer by Whitecold · Nov 01, 2016 at 11:20 AM

What do you intend to do that needs per frame updating? You can either use render textures, where a camera renders the texture you want live, or you could use a custom shader and tweak its parameters on each frame.

If you absolutely need to update the texture and have more than a few single pixles, use SetPixels instead of iterating SetPixel, this saves you at least some time.

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

Answer by richard_hawkes · Nov 01, 2016 at 12:54 PM

So I'm doing a reaction diffusion algorithm. So each pixel gets evaluated. For sure, some pixels change a little, but unfortunately it is hard to truly garuntee that change wasn't significant.

Not sure on these render textures. I'll take a look.

However, I do like the idea of SetPixel s (). This is easy enough to do as I already have the array setup, so can just dump it out at the end of the update. I know that calling SetPixel() on a 256x256 image took 35ms, so this could make a large difference.

Thanks for this.

Comment
Add comment · Show 1 · 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 _dns_ · Nov 01, 2016 at 02:33 PM 1
Share

I'll add that using SetPixels32 is also faster (less conversions, better CPU cache usage). I'd try using uncompressed RGBA textures too (closer to the color32 struct + no compression involved before being sent to the GPU)

avatar image
0

Answer by Daemonhahn · Nov 01, 2016 at 05:01 PM

Don't do it every frame and instead limit the check to a couple of times a second, or wrap up inside of a co-routine.

To put it simply though you already answered your own question when you said your doing millions of operations every frame.

So limit how much is done per frame and you will see performance increase

Comment
Add comment · Show 1 · 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 richard_hawkes · Nov 02, 2016 at 08:17 AM 0
Share

O$$anonymous$$, so what I'm getting here is that I'm going about it in the right way from a rendering perspective.

Thanks for your help and assurance.

avatar image
0

Answer by isaac_champ · Nov 02, 2016 at 03:47 PM

If you're familiar with OpenGL/Metal for Android/iOS, you can write the plugin to retrieve the pixel data from render texture and then update the texture which created from you plugin.

This is the best solution we use so far.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Fastest rendering of 2D world with lots of changes to screen every frame? 3 Answers

Set pixels on light cookie texture via script? 0 Answers

Can't create a 2D sprite with Sprite.Create when creating texture in code 1 Answer

2D Texture with shadow square border 1 Answer

Unity 4.3 2d tools. Will there be native 2d shadows and or 2d textured terrain? 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