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 KyleKindaKnows · May 29, 2018 at 12:11 AM · optimizationperformance optimizationsetpixelssetpixelsetpixels32

Performance issue. Optimize or change SetPixel to allow for painting specific areas of a texture.

Hey, I'm having a performance issue where I am having moving game objects painting a texture as they move. Because I'm using texture splatting in shaderforge they are only painting in a single color (red) and the shader paints the actual texture.

Texture Splatting


The performance issue is that unity runs out of memory after (X) amount of seconds of 'painting' and it crashs. I've been looking into SetPixels & SetPixels32 which seem to be more efficient, but I can't figure out how to use them to paint specific pixel areas.

     private void PaintAround(Texture2D tex, Vector2 pixelLocation, Color color, Vector2 mySize)
     {
         float widthRadius = ((mySize.x / 2) / objWidth) * tex.width;
         float heightRadius = ((mySize.y / 2) / objHeight) * tex.height;
     
         ////Sets each individual pixel around the object to be a color
         for (int x = (int)pixelLocation.x - (int)widthRadius; x < (int)pixelLocation.x + (int)widthRadius; x++)
         {
             for (int y = (int)pixelLocation.y - (int)heightRadius; y < (int)pixelLocation.y + (int)heightRadius; y++)
             {
                 tex.SetPixel(-y, x, color);
             }
         }
 
         tex.Apply();
     }

Anyone got any ideas?

ant-img.png (238.2 kB)
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 KyleKindaKnows · May 30, 2018 at 01:13 AM 0
Share

I figured out a solution to the crashing (it was something else, but still related). I am still looking to improve performance with SetPixels/SetPixels32 though.

avatar image hexagonius · May 30, 2018 at 05:13 AM 0
Share

collect the pixels you want to write and write them with SetPixels (plural) in one go.

avatar image KyleKindaKnows · Jun 18, 2018 at 11:30 PM 0
Share

Do I add each pixel into a Vector 2 array and then SetPixels to that array? Could you give an example as I haven't been able to get it to work yet.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Jun 19, 2018 at 12:53 AM

The straight forward solution is simply this:

 private void PaintAround(Texture2D tex, Vector2 pixelLocation, Color32 color, Vector2 mySize)
 {
     float widthRadius = ((mySize.x / 2) / objWidth) * tex.width;
     float heightRadius = ((mySize.y / 2) / objHeight) * tex.height;
     
     Color32[] colors = tex.GetPixels32();
     int startX = Mathf.Clamp((int)pixelLocation.x - (int)widthRadius, 0, tex.width-1);
     int endX = Mathf.Clamp((int)pixelLocation.x + (int)widthRadius, 0, tex.width-1);
     int startY = Mathf.Clamp((int)pixelLocation.y - (int)heightRadius, 0, tex.height-1);
     int endY = Mathf.Clamp((int)pixelLocation.y + (int)heightRadius, 0, tex.height-1);
     
     for (int y = startY; y < endY y++)
     {
         int yOff = y * tex.width;
         for (int x = startX; x < endX x++)
         {
             colors[yOff + x] = color;
         }
     }
     tex.SetPixels32(colors);
     tex.Apply();
 }


Note that this solutions actually expects the pixel position to be in texture space. A texture's origin is the lower left corner. Your original line of code:

 tex.SetPixel(-y, x, color);

Doesn't make much sense as you flipped x and y but you calculated them with the width and height of the texture. The proper clamping of each position is important as the colors array is a linear flattened array. If you would go beyond the right most pixel you would continue from the left side on the next line. Just as an example a 4x4 texture looks like this:

 12 13 14 15
  8  9 10 11
  4  5  6  7
  0  1  2  3



Though for performance reasons you may want to avoid calling GetPixels32 all the time. Since the texture doesn't change in between your calls you could just get the color array once in Start and just store it in a class member variable. Just change the values you want to change in the array and call SetPixels32 afterwards.


We can't get more into detail since we don't know how you actually use the texture or where and how you determine your "pixelLocation"

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

94 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

Related Questions

Unity Best Practices - Cached Components 1 Answer

Use SetPixels, but dont set unvisible Pixels 1 Answer

SetPixels32 called with invalid number if pixels in the array 1 Answer

Optimize mesh for runtime performance 0 Answers

Punching holes through a texture and making them regenerate back again in an efficient way 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