# Coloring an area defined by a few transforms? (2D)

What would be the way to proceed to color a certain area defined by transforms in 2D?

Say I want to color the square (0, 5, 0) (5, 5, 0) (5, 0, 0) and (0, 0, 0), is there a possible way to create a polygon sprite at runtime?

In my example it’s a square, but it could be any polygon really, hence I can’t just use something prebuilt.

I’m known to not make any sense, if it’s unclear, let me know!

Any ideas/tips would be much appreciated!

EDIT: I’m reading up on generating meshes at runtime, it looks fairly complex and I want to make sure I’m not headed the wrong way or overdoing this before I get started. If I could get any input, it’d be great!

I don’t think runtime mesh generation is difficult. You jsut need to take a look at the API for the mesh class to see how it works.

1. Add three Vector3s to a List. (Vertex positions)
2. Add the ints 0, 1 and 2 to another List. (vertex indexes indicating the vertices that make up the triangle)
3. Add three Vector2 to yet another List, with the V2 x and y values corresponding to the uv coordinates of the texture (doesn’t matter if it’s just a red texture, but important if using a texture atlas) (each V2 corresponding to the same index in the vertices array)
Assign these Lists to the mesh’s vertices, triangles and uvs arrays (using .ToArray()).
4. ???
5. Profit!

Thank you!

I may have another issue. The player is going to draw lines within a box, every corners of the shape he draws will act as vertice, I can handle that. How do I handle the second step though?

If I can’t anticipate what the shape will look like, how can I preset triangles so that they don’t end up ouside what would theoritically be the shape, if that makes any sense?

See attached drawing.

“A” is the first shape created, so far, so good. Then player creates “B” (in blue are the vertices for “B”). I’m not sure what would be the way to go to avoid having the “- - - -” red line happening, if that makes sense.

The Unity community should have you covered there. Just check if any currenty moved or created vertex is inside one of the already present triangles, using a function like this:

http://wiki.unity3d.com/index.php/PolyContainsPoint

ugh, it sounds like its going to get complex but will do the trick.

Thank you very much mate, I appreciate it.