Is there a way to crop a texture having a list of triangles which represent the part to be kept?
For instance, I have a rectangular texture and I want to obtain a texture shaped by the triangle list I have.

up vote 2 down vote accepted

I don't think you can have non-rectangular texture. But you could get a bounding box for the vertices and crop the texture with it. Just read the pixels inside the bounding box and write them to a new texture instance.

Vector2i min = triangleslist[0]; // Assuming the list is integer Vector2 list
Vector2i max = triangleslist[0]; // and the vertex positions are in pixels

foreach(var p in triangleslist) {
    if(p.x < min.x)
        min.x = p.x;

    if(p.x > max.x)
        max.x = p.x;

    if(p.y < min.y)
        min.y = p.y;

    if(p.y > max.y)
        max.y = p.y;
}

var size = max - min;
var texture = new Texture2D(size.x, size.y, TextureFormat.ARGB32, false);

for(int x = 0; x < size.x; x++) {
    for(int y = 0; y < size.y; y++) {
        texture.SetPixel(x, y, originalTexture.GetPixel(x + min.x, y + min.y);
    }
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.