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.

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

First of all if you read the [documentation of GetPixels][1] 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][2] 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 *== 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();
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.
[1]: Unity - Scripting API: Texture2D.GetPixels
[2]: How to convert pixel/UV coordinates to World Space? - Questions & Answers - Unity Discussions