Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
avatar image
0
Question by sumit47 · Jul 27, 2018 at 03:02 PM · imagepixelcirclecolourfill

I want to fill a circle with red colour. There is a boundary of black colour and the other region is of white colour.

I am using this script to fill red colour but it is not filling inside the circle , it is filling red colour outside the circle. Any help please...... Thanks in advance.

using UnityEngine; using System.Collections; using System.Collections.Generic;

public static class TextureExtension { public struct Point { public short x; public short y; public Point(short aX, short aY) { x = aX; y = aY; } public Point(int aX, int aY) : this((short)aX, (short)aY) { } }

 public static void FloodFillArea( Texture2D aTex, int aX, int aY, Color aFillColor)
 {
     int w = aTex.width;
     int h = aTex.height;
     Color[] colors = aTex.GetPixels();
     Color refCol = colors[aX + aY * w];
     Queue<Point> nodes = new Queue<Point>();
     nodes.Enqueue(new Point(aX, aY));
     while (nodes.Count > 0)
     {
         Point current = nodes.Dequeue();
         for (int i = current.x; i < w; i++)
         {
             Color C = colors[i + current.y * w];
             if (C != refCol || C == aFillColor)
                 break;
             colors[i + current.y * w] = aFillColor;
             if (current.y + 1 < h)
             {
                 C = colors[i + current.y * w + w];
                 if (C == refCol && C != aFillColor)
                     nodes.Enqueue(new Point(i, current.y + 1));
             }
             if (current.y - 1 >= 0)
             {
                 C = colors[i + current.y * w - w];
                 if (C == refCol && C != aFillColor)
                     nodes.Enqueue(new Point(i, current.y - 1));
             }
         }
         for (int i = current.x - 1; i >= 0; i--)
         {
             Color C = colors[i + current.y * w];
             if (C != refCol || C == aFillColor)
                 break;
             colors[i + current.y * w] = aFillColor;
             if (current.y + 1 < h)
             {
                 C = colors[i + current.y * w + w];
                 if (C == refCol && C != aFillColor)
                     nodes.Enqueue(new Point(i, current.y + 1));
             }
             if (current.y - 1 >= 0)
             {
                 C = colors[i + current.y * w - w];
                 if (C == refCol && C != aFillColor)
                     nodes.Enqueue(new Point(i, current.y - 1));
             }
         }
     }
     aTex.SetPixels(colors);
 }

 public static void FloodFillBorder( Texture2D aTex, int aX, int aY, Color aFillColor, Color aBorderColor)
 {
     int w = aTex.width;
     int h = aTex.height;
     Color[] colors = aTex.GetPixels();
     byte[] checkedPixels = new byte[colors.Length];
     Color refCol = aBorderColor;
     Queue<Point> nodes = new Queue<Point>();
     nodes.Enqueue(new Point(aX, aY));
     while (nodes.Count > 0)
     {
         Point current = nodes.Dequeue();

         for (int i = current.x; i < w; i++)
         {
             if (checkedPixels[i + current.y * w] > 0 || colors[i + current.y * w] == refCol)
                 break;
             colors[i + current.y * w] = aFillColor;
             checkedPixels[i + current.y * w] = 1;
             if (current.y + 1 < h)
             {
                 if (checkedPixels[i + current.y * w + w] == 0 && colors[i + current.y * w + w] != refCol)
                     nodes.Enqueue(new Point(i, current.y + 1));
             }
             if (current.y - 1 >= 0)
             {
                 if (checkedPixels[i + current.y * w - w] == 0 && colors[i + current.y * w - w] != refCol)
                     nodes.Enqueue(new Point(i, current.y - 1));
             }
         }
         for (int i = current.x - 1; i >= 0; i--)
         {
             if (checkedPixels[i + current.y * w] > 0 || colors[i + current.y * w] == refCol)
                 break;
             colors[i + current.y * w] = aFillColor;
             checkedPixels[i + current.y * w] = 1;
             if (current.y + 1 < h)
             {
                 if (checkedPixels[i + current.y * w + w] == 0 && colors[i + current.y * w + w] != refCol)
                     nodes.Enqueue(new Point(i, current.y + 1));
             }
             if (current.y - 1 >= 0)
             {
                 if (checkedPixels[i + current.y * w - w] == 0 && colors[i + current.y * w - w] != refCol)
                     nodes.Enqueue(new Point(i, current.y - 1));
             }
         }
     }
     aTex.SetPixels(colors);
 }

}

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 Hellium · Jul 27, 2018 at 03:12 PM 0
Share

Side note: Do you imagine the pain it would be to take your code up again in 6 months? Do you think this is something you will be able to understand quickly? No? So now, imagine the pain it is for other people to read and understand your code....

The skills of a programmer is not reflected by the complexity of its algorithms, it's reflected by the quality of his / her code. And it's starts with the comments, the na$$anonymous$$g of the variables and functions, and the length of the functions.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by madks13 · Jul 29, 2018 at 02:08 AM

Hmm, i got some time so here, try using these instead :

         /// <summary>
         /// This method will flood fill a texture with a color begining from given coordinates
         /// </summary>
         /// <param name="texture">The texture to apply the flood fill to</param>
         /// <param name="x">The starting pixel's x coordinate</param>
         /// <param name="y">The starting pixel's y coordinate</param>
         /// <param name="fillColor">The color to apply</param>
         /// <param name="borderColor">The border color of the flood fill</param>
         /// <param name="useBorder">Do we fill till borders or not</param>
         public static void FloodFill(Texture2D texture, int x, int y, Color fillColor, Color borderColor, bool useBorder)
         {
             //Out of bounds
             if (x < 0 || x >= texture.width || y < 0 || y >= texture.height)
             {
                 return;
             }
 
             //Set up shared info
             Color[] colors = texture.GetPixels();
             //Reference color changes depending if we want to use a border color or not
             Color refColor = useBorder ? borderColor : colors[x + y * texture.width];
 
             //Call the first iteration
             FloodFillRecursive(colors, texture.width, x, y, refColor, fillColor, useBorder);
 
             //Apply to texture
             texture.SetPixels(colors);
         }
 
         /// <summary>
         /// This methos will flood fill a table of colors recursively
         /// </summary>
         /// <param name="colors">The table containing the colors</param>
         /// <param name="w">The width of the texture</param>
         /// <param name="x">The x coordinate of the current pixel</param>
         /// <param name="y">The y coordinate of the current pixel</param>
         /// <param name="refColor">The reference color</param>
         /// <param name="fill">The color to set the pixel at</param>
         /// <param name="useBorder">Is refColor a border color or not</param>
         private static void FloodFillRecursive(Color[] colors, int w, int x, int y, Color refColor, Color fill, bool useBorder)
         {
             int index = x + y * w;
 
             //Out of range
             if (index < 0 || index > colors.Length)
             {
                 return;
             }
             
             //Get current pixel color
             Color c = colors[index];
 
             //Nothing to do
             if (c == fill || (useBorder ? c == refColor : c != refColor ))
             {
                 return;
             }
 
             //Apply color
             colors[index] = fill;
 
             //Apply same to neighbour pixels
             //Left pixel
             FloodFillRecursive(colors, w, x - 1, y, refColor, fill, useBorder);
             //Right pixel
             FloodFillRecursive(colors, w, x + 1, y, refColor, fill, useBorder);
             //Down pixel
             FloodFillRecursive(colors, w, x, y - 1, refColor, fill, useBorder);
             //Up pixel
             FloodFillRecursive(colors, w, x, y + 1, refColor, fill, useBorder);
 
         }
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 sumit47 · Aug 22, 2018 at 05:48 PM 0
Share

Actually i need pixel coordinates in c# code ,so that my script can start filling color from these coordinates. Could you help for that, how to get pixel coordinates by mouse click on a particular image @madks13

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

89 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

Related Questions

how can i get pixel color from one image then fill one mesh with that color? 0 Answers

How to fill circle in Eric5h5's TextureDrawCircle function 2 Answers

How to change the end of image with type "Filled" 1 Answer

Image Fill Bug 0 Answers

Image custom fillOrigin 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