- Home /
Hexagonal Shape for 360 radius (painting texture)
Hello, I am working on a project that requires the player to paint colors over an ingame texture. Wherever the player clicks, it will paint that pixel and all the other pixels within that radius. Everything works fine. However, we need the paint brush to be hexagon shaped, instead of circular.
Would anyone know how to convert the mentioned script below, so that it replaces the texture in a hexagonal shape? Ive been trying for awhile, and cant quite figure it out. any help would be appreciated.
for (int i = 0; i < ourTexture.width; i++)
{
for (int j = 0; j < ourTexture.width; j++)
{
Vector2 pixPos = new Vector2(i, j);
var dist = Vector2.Distance(centerPixel, pixPos);
var area = 6 * Mathf.Sqrt(3) / 4 * ((int)radiusOfTool ^ 2); //area of hexagon...apparently...
if (dist < radiusOfTool)
{
//paint the pixel within our radius.
PaintPixel(i, j);
}
}
}
small update:
I can calculate/input the 6 points of the hexagon, however, when I try to ignore the sections in green, it ignores the section in green + the section in yellow.. so my hex is beco$$anonymous$$g a rectangle.
So, i guess the issue I am having, is calculating if a pixel is between two points and above.(green section).
Any help on the formula on how to do this, would greatly be appreciated.
Answer by lgarczyn · Nov 08, 2019 at 06:26 PM
If you want to be future-proof, I would suggest using textures for your brushes. This would allow any shape very easily, and even allow easy anti-aliasing.
If you really want a mathematical definition of an hexagon, an hexagon can be defined by three intersecting rectangles, or by its polar equation.
Once you have the polar equation, you simply need to calculate the angle and the distance to the center. If the result of the equation when you replace theta by the angle is larger than the distance, then you are inside the hexagon.
Your answer
Follow this Question
Related Questions
Paint a texture dynamically? 0 Answers
Moving and scaling an object according to hand position. 1 Answer
Painting on Textures at Runtime? 2 Answers
Assigning UV Map to model at runtime 0 Answers
Vehicle color select? 2 Answers