• 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
2
Question by cftcimert · Mar 04, 2021 at 07:42 PM · texturepainting

Show the percentage of painted surface on the wall

I want to show the percentage of painted surface on the wall. Here is the code I used to paint the wall.

 private void MyPaintOn(Vector2 textureCoord, float[,] splashTexture, Color targetColor)
     {
         if (m_isEnabled)
         {
             lock (m_lockFlag)
             {
                 Stopwatch sw = new Stopwatch();
                 sw.Start();
                 int reqnx = splashTexture.GetLength(0);
                 int reqny = splashTexture.GetLength(1);
                 int reqX = (int)(textureCoord.x * textureSize) - (reqnx / 2);
                 int reqY = (int)(textureCoord.y * textureSize) - (reqny / 2);
                 int right = m_texture.width - 1;
                 int bottom = m_texture.height - 1;
 
                 int x = IntMax(reqX, 0);
                 int y = IntMax(reqY, 0);
                 int nx = IntMin(x + reqnx, right) - x;
                 int ny = IntMin(y + reqny, bottom) - y;
 
                 Color[] pixels = m_texture.GetPixels(x, y, nx, ny);
 
                 int counter = 0;
                 for (int i = 0; i < nx; ++i)
                 {
                     for (int j = 0; j < ny; ++j)
                     {
                         float currAlpha = splashTexture[i, j];
                         if (currAlpha == 1)
                             pixels[counter] = targetColor;
                         else
                         {
                             Color currColor = pixels[counter];
                             // resulting color is an addition of splash texture to the texture based on alpha
                             Color newColor = Color.Lerp(currColor, targetColor, currAlpha);
                             // but resulting alpha is a sum of alphas (adding transparent color should not make base color more transparent)
                             newColor.a = pixels[counter].a + currAlpha;
                             pixels[counter] = newColor;
                         }
                         counter++;
                     }

                     m_texture.SetPixels(x, y, nx, ny, pixels);
                     m_texture.Apply();
                     sw.Stop();
                 }
             }
         }
     }



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 andrew-lukasik · Mar 05, 2021 at 02:31 PM 0
Share

btw. remove that lock mate, it encapsulates exclusively main-thread code there (`m_texture.`).

avatar image pietrohenrique71186 · May 08, 2021 at 04:12 AM 0
Share

102 784


1 Reply

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

Answer by andrew-lukasik · Mar 05, 2021 at 12:52 PM


You can calculate color fill and overall similarity values by treating colors as n-dimensional vectors inside a unit cube (or equivalent). This enables you to express color differences with a single numerical value - distance between them.


 static float CalculateFill ( Color[] colors , Color reference , float tolerance )
 {
     Vector3 target = new Vector3{ x=reference.r , y=reference.g , z=reference.b };
     int numHits = 0;
     const float sqrt_3 = 1.73205080757f;
     for( int i=0 ; i<colors.Length ; i++ )
     {
         Vector3 next = new Vector3{ x=colors[i].r , y=colors[i].g , z=colors[i].b };
         float mag = Vector3.Magnitude( target - next ) / sqrt_3;
         numHits += mag<=tolerance ? 1 : 0;
     }
     return (float)numHits / (float)colors.Length;;
 }
 static float CalculateSimilarity ( Color[] colors , Color reference )
 {
     Vector3 target = new Vector3{ x=reference.r , y=reference.g , z=reference.b };
     float accu = 0;
     const float sqrt_3 = 1.73205080757f;
     for( int i=0 ; i<colors.Length ; i++ )
     {
         Vector3 next = new Vector3{ x=colors[i].r , y=colors[i].g , z=colors[i].b };
         accu += Vector3.Magnitude( target - next ) / sqrt_3;
     }
     return 1f - ( (float)accu / (float)colors.Length );
 }

I'm sure you can simplify this further to meet your project requirements. Optimizing this for runtime-use is whole another topic.


Here is a full source for an editor tool to check how well it works. Some test results:


test3 test4 test 1 test 2

Comment
Add comment · Show 3 · 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 ensareray99 · May 07, 2021 at 11:25 AM 1
Share

Hi andrew, Can you explain what is the importance of sqrt 3 in this equation? I couldn't find an answer on google.

avatar image andrew-lukasik ensareray99 · May 07, 2021 at 11:56 AM 0
Share

Hi. Sure, sqrt_3 represents diagonal of a unit cube. Removing it will introduce a variable margin of error as it is used to normalize the results.

Google may not help much as this method is something I came up with here on the spot.

avatar image ensareray99 andrew-lukasik · May 07, 2021 at 02:53 PM 0
Share

Thanks, I got it now! This doesn't fell like you came up on spot, it's working nicely! Btw, For me 0.5 was satisfactory for tolerance value.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

144 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

Related Questions

Change terrain size without changing painting texture 0 Answers

reversing object scale is ruining the texture coordinates ? 0 Answers

Is there a way to paint a texture onto a mesh like in UE4? 1 Answer

Coloring textures in Unity3d 1 Answer

Improve terrain texture painting 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