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 mroveli · Sep 21, 2016 at 07:00 PM · texturecolorworld spacegetpixels

Get color coordinate from the texture and convert to world space

Hello Guys!

On the plane I have texture. My goal is to loop through the whole texture, grab the coordinates by colors and then use these coordinates as vertices to construct mesh.

I know how to grab colors from the texture

   Color [] colorArray = mainText.GetPixels();

This gives me raw data about colors in array.

My questions is: How to grab pixel coordinates of every color (I guess these coordinates will be color position on texture, right?) and then convert this coordinates to world space to use them as vertices.

If I am spouting nonsense don't hurt me. I will show myself out.

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 Osama_haashir · Mar 21, 2018 at 10:11 AM 0
Share

hey @mroveli, Did you find the answer of this?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Mar 21, 2018 at 12:18 PM

First of all if you read the documentation of GetPixels it returns a flattended array which is laid out left to right and bottom to top. So it exactly matches the usual UV layout. If you have the index of the color you want you can just do:

 int index; // your index in the colors array.
 
 int width = mainTex.width;
 Vector2 uv = new Vector2(index % width, index / width);

uv will be your normalized texture coordinate of that pixel Though note that it will return the left-bottom corner of that pixel. If you want to get the center of the pixel add half the "pixel size" which is new Vector2(0.5f / mainTex.width, 0.5f / mainTex.height).


Once you have the texture coordinate you can find the triangle(s) that contain this UV coordinate. As i said in this question over here a certain area of a texture doesn't need to be mapped to any geometry at all or could be mapped several times. However if you use a simple plane / quad mesh that is mapped to the whole texture you should get only one result.


Note that this mapping does not take any shader manipulations into account. So texture offset or scaling or any other fancy shader manipulation will be ignored. We assume that the texture coordinates of the mesh are used as provided.


Next you just iterate through all triangles of your mesh, use the vertex UV coordinates of the 3 corners and calculate the barycentric coordinate of our pixel UV coordinate within the triangle. With "InTriangle" you can test if the provided position is actually mapped inside that triangle. If it is we can use the barycentric coordinate to get the local space position of our desired point. Once we have the localspace position just convert it to worldspace using transform.TransformPoint() and you're done.

In a nutshell:

 public static Vector3 GetPixelWorldPos(Transform aObj, Color aCol)
 {
     var rend = aObj.GetComponent<Renderer>();
     var mainTex = rend.mainTexture;
     int w = mainTex.width;
     int h = mainTex.height;
     
     // find color in texture
     Color[] colors = mainTex.GetPixels();
     int index = -1;
     for(int i =0; i < colors.Length; i++)
     {
         if (colors[i] == aCol)
         {
             index = i;
             break;
         }
     }
     if (index == -1)
         return Vector3.zero;
     
     // Find triangle and get local pos
     var point = new Vector2(index % w, index / w) - new Vector2(0.5f/w, 0.5f/h);
     var mf = aObj.GetComponent<MeshFilter>();
     var mesh = mf.sharedMesh;
     var uvs = mesh.uv;
     var verts = mesh.vertices;
     var tris = mesh.triangles;
     for(int i = 0; i < tris.Length; i+=3)
     {
         var uv0 = uvs[i + 0];
         var uv1 = uvs[i + 1];
         var uv2 = uvs[i + 2];
         var bary = GetBarycentric(uv0, uv1, uv2, point);
         if (InTriangle(bary))
         {
             Vector3 localPos = verts[i + 0] * bary.x + verts[i + 1] * bary.y + verts[i+2] * bary.z;
             // Transform to world pos and return
             return aObj.TransformPoint(localPos);
         }
     }
     return Vector3.zero;
 }


Note that this method does simply return (0,0,0) if:

  • the exact color isn't found

  • the found pixel isn't mapped to any triangle.


Also keep in mind that this method does only search for the first color that matches the reference color and it only returns the point in the first triangle that is contains the specified UV point.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

I can't see a reason for this Array.Reverse() error 3 Answers

Color specific sections of a texture 1 Answer

Texture Takes Main Color of Material 1 Answer

2 Problems: with colors of textures and skybox. 2 Answers

Multiple Texture Colours 2 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