Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Laguna_Seca · Sep 22, 2021 at 12:55 PM · texture2ddrawinggetpixelsetpixels32

Drawing performance issues. Color32.op_lmplicit() very slow

I try to paint by texture2d and get very poor performance, especially when trying to use a brush texture

 public void PenBrush(Vector2 world_point)
     {
         Vector2 pixelPos = WorldToPixelCoordinates(world_point);
         currentColors = drawableTexture.GetPixels32();
         if (previousDragPosition == Vector2.zero)
             MarkPixelsToColour(pixelPos, PenWidth, PenColour);
         else
             ColourBetween(previousDragPosition, pixelPos, PenWidth, PenColour);
         ApplyMarkedPixelChanges();
         previousDragPosition = pixelPos;
     }

 public void MarkPixelsToColour(Vector2 center_pixel, int pen_thickness, Color color_of_pen)
     {
         
         int center_x = (int)center_pixel.x;
         int center_y = (int)center_pixel.y;
 
         for (int x = center_x - pen_thickness; x <= center_x + pen_thickness; x++)
         {
             if (x >= drawableSpriteWidth || x < 0)
                 continue;
             for (int y = center_y - pen_thickness; y <= center_y + pen_thickness; y++)
                 MarkPixelToChange(x, y, color_of_pen);
         }
 
         /*
         int center_x = (int)center_pixel.x;
         int center_y = (int)center_pixel.y;
         int drawableSpriteWidth = drawableSpriteWidth;
         pen_thickness = BrushTexture.width;
         int x = pen_thickness;
        
         while (x > 0)
         {
             x--;
             int y = pen_thickness;
             while (y > 0)
             {
                 y--;
                 if (x >= (int)drawableSprite.rect.width || x < 0)
                     continue;
                 Color brushColor = BrushTexture.GetPixel(x, y);
                 int array_pos = (y + center_y) * drawableSpriteWidth + (x + center_x);
 
                 Color _endcolor = new Color();
                 _endcolor.r = color_of_pen.r;
                 _endcolor.g = color_of_pen.g;
                 _endcolor.b = color_of_pen.b;
                 _endcolor.a = 1;
                 currentColors[array_pos] = Color.Lerp(currentColors[array_pos], _endcolor, brushColor.a > .9f ? 100 : brushColor.a / 5);
 
             }
         }
         */

 public void MarkPixelToChange(int x, int y, Color color)
     {
         int array_pos = y * drawableSpriteWidth + x;
         if (array_pos > currentColors.Length || array_pos < 0)
             return;
         currentColors[array_pos] = color;
     }
 
 
     public void ApplyMarkedPixelChanges()
     {
         drawableTexture.SetPixels32(currentColors);
         drawableTexture.Apply();
     }


alt text

How can I improve my performance?

3333.png (32.6 kB)
Comment
Add comment · Show 1
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 oscarAbraham · Sep 22, 2021 at 09:23 PM 0
Share

Where does PenColour come from? My first suggestion would be to make it a Color32, and, in general convert or your Color fields and Color parameters to Color32. Could you do that? If you can't, you could convert color_of_pen to Color32 outside of the loop in MarkPixelsToColour and pass a Color32 to MarkPixelToChange.

EDIT
That code seems very prone to get exponentially slow as things scale. If the pen size doubles width and height, and the distance doubles too, you'd be drawing 8 times the amount of pixels. A way to handle it could be a coroutine; you draw a certain amount of pixels, and if there are still pixels to be drawn, you yield, then continue in the next frame. Another idea would be to use Burst and Jobs, setting multiple values in a collection can be a lot faster that way.

Here's an example of painting a texture with Burst and Jobs: https://github.com/Unity-Technologies/UnityTextureAccessApiExamples

0 Replies

· Add your reply
  • Sort: 

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

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

Texture manipulation 0 Answers

Get Colors from RenderTexture faster! 1 Answer

calling Texture2DArray.SetPixels32 results in an "Invalid texture format: 12" error 1 Answer

Turning every color in an image to many GameObjects ? 0 Answers

Changing dinamically a texture 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