Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 samqweqwe · Oct 19, 2014 at 07:12 PM · texturetexture2dloopefficiencygetpixel

Find a colour in a Texture2D

I am trying to find a colour that is found on a texture but the method that i have used takes too much time.

 string ColourString = colour.ToString ();
         for (int i = 0; i < ColourTex2D.height; i++)
         {
             for (int j = 0; j < ColourTex2D.width; j++)
             {
                 Color TempColour = ColourTex2D.GetPixel(j, i);
                 if (TempColour.ToString() == ColourString)
                 {
                     CrosshairPos = new Vector2(((float)j/ColourTex2D.width) * ColourSelectSize.x, ColourSelectSize.y - ((float)i/ColourTex2D.height) * ColourSelectSize.y);
                     i=ColourTex2D.height;
                     j=ColourTex2D.width;
                 }
             }
         }

Is there a more effient way of achieving this?

Comment
Add comment
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

1 Reply

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

Answer by robertbu · Oct 19, 2014 at 07:52 PM

I'm not sure what you are trying to do with this code. The is likely a better approach than what you are doing to accomplish what you want. But to answer your specific question, you can use Texture2D.GetPixels32() or Textur2D.GetPixels() in start to get the color of all the pixel data. Then you can directly access the pixel data without a GetPixel() call for each position in the texture. GetPixels32() and GetPixels() return a 1D array of the 2D pixel data.

Here is a script that uses GetPixels32(). Attached to a object with texture, clicking on that object will output the color at the cursor position.

 using UnityEngine;
 using System.Collections;
 
 public class GetColor : MonoBehaviour {
 
     public Texture2D tex;
     private Color32[] pixels;
     private float width, height; 
 
     void Start () {
         Texture2D tex = (Texture2D)renderer.material.mainTexture;
         pixels = tex.GetPixels32();
         width = tex.width;
         height = tex.height;
     }
     
     void Update () {
         
         RaycastHit hit ;
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
         if (Input.GetMouseButton(0) && collider.Raycast(ray, out hit, Mathf.Infinity)) {
             Vector3 coord = hit.textureCoord;
             int w  = (int)(coord.x * width);
             int h  = (int)(coord.y * height);
             
             Debug.Log (pixels[h * (int)width + w]);
         }
     }
 }

Note that your use of strings for comparisons is also of concern for performance in your code.

Comment
Add comment · Show 2 · 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 samqweqwe · Oct 19, 2014 at 08:51 PM 0
Share

Thanks, i used strings because nothing else would work. I tried:

 if (Colour1 == Colour2)
 
 //for r, g and b
 if((int)(Colour1.r * 1000) == (int)(Colour2.r * 1000))
 
 //for r, g and b
 if ($$anonymous$$athf.Approximately(Colour1.r, Colour2.r)) 

is the problem that they are Color not Color32?

avatar image robertbu · Oct 19, 2014 at 11:02 PM 0
Share

I'm not sure of your code, but if you want to compare with '==', they need to be of the same type. You can cast one from another. Say you had a variable color of type Color and a variable color32 of type Color32, you can do:

if (color == (Color)color32)

Note when comparing colors, unless you are absolutely sure of the values, consider looking at the distance between the two colors and considering them a match if the distance is below a threshold.

 float ColorSqrDistance(Color c1, Color c2) {
     return ((c2.r - c1.r) * (c2.r - c1.r) + (c2.b - c1.b) * (c2.b - c1.b) + (c2.g - c1.g) * (c2.g - c1.g));
 }

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

27 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

Related Questions

Texture2D GetPixel returns the same value everywhere 2 Answers

How can I efficiently determine that certain parts of a texture have a specific color? 1 Answer

Update Texture type to GUI during runtime 0 Answers

Converting a RenderTexture to a Texture2D for use in a shader 2 Answers

Loop trough list of textures and chek mouse position over 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