Draw a Midle Circle Unity

Procedural Mesh in Unity, Function-2 Lets Depict A Circle

What are we upward to?

In my previous article, we learned how to generate a procedural mesh for a triangle in Unity. In this postal service, I will write a slightly complicated mesh, a circle.

The computer graphics perspective of a circle:

We know that whatsoever object in CG is essentially made out of triangles and quads. But how do we depict a circle using triangles?? Triangles are made out of straight faces. How practise we draw curves using merely triangles?

Notice any polygon that has more than 5 sides. The more the number of sides a polygon has, the more information technology looks like a circle. If we increase the number of sides in the polygon to infinity you have an ideal circumvolve. Simply computers are finite machines. We cant depict infinite triangles just to get a mere circle. And then, allow's limit the number of sides to say 24, which is just enough to make the polygon look like a circle. Basically, we draw a circle equally a polygon with a large number of sides, which is definitely possible to draw with triangles.

Mathematical perspective of a circumvolve:

We saw the CG perspective of the circle as a polygon with a large number of sides. Now let us attempt to understand the circle from a mathematical perspective. A circle with radius r and center (h,k) can be defined past a following Cartesian equation.

(10h)2 + (yone thousand)2 = rtwo

If we take origin equally the center, then the equation reduces to
x2 + y 2 = rtwo.

If we know the radius of the circle, we have 2 variables left. We can try to plug in all the possible values for x and y and run into if they lie on the given circle. But this is computationally intensive. Instead, at that place is a meliorate arroyo. In CG we mostly utilise the spherical coordinate arrangement. In spherical coordinates, the circle can be defined every bit follows.

x =r cos(theta)
y= r sin(theta)
z=0

Where theta is the angle between 00 and 3600. To describe a circle from this equation, all we need to practise is have a constant radius, and sweep the angle theta beyond all the possible values between 00 and 3600. This will give u.s. all the points that prevarication on the circumference of the circumvolve. This is mode better than the previous approach.

Writing the script:

Once we take the mathematical equation of the circle all that is left is translating the equation to a set of instructions your dumb motorcar can sympathize.

Let us start define few variables.

float val = iii.14285f / 180f;//one degree = val radians
float radius = i.0f;
int deltaAngle = 15;

The first variable val that we will utilize for converting the theta from degrees to radians. Nosotros need a radius for our circle. Let us define a variable to hold the stride value. It is better to choose a number that divides 360. Then let us choose the origin as the eye of the triangle.

Let the states add the center of the circle into our vertices array. Annotation that this vertex is going to be a common vertex for all the triangles that make up this circle. Lets also find the commencement point (point1) on the circumference at theta=0. Nosotros also map its UVs.

Vector3 center = Vector3.zero;
circleVerteices.Add together(centre);
uvs.Add(new Vector2(0.5f, 0.5f));
int triangleCount = 0;

bladder x1 = radius * Mathf.Cos(0);
float y1 = radius * Mathf.Sin(0);
float z1 = 0;
Vector3 point1 = new Vector3(x1, y1, z1);
circleVerteices.Add(point1);
uvs.Add(new Vector2((x1 + radius) / ii * radius, (y1 + radius) / 2 * radius));

At present nosotros should start finding other points on the circumference of the circumvolve.

for (int i = 0; i < 359; i = i + deltaAngle)
{
float x2 = radius * Mathf.Cos((i + deltaAngle) * val);
float y2 = radius * Mathf.Sin((i + deltaAngle) * val);
float z2 = 0;
Vector3 point2 = new Vector3(x2, y2, z2);
circleVerteices.Add together(point2);
uvs.Add together(new Vector2((x2 + radius)/ 2 * radius, (y2 +radius)/ 2 * radius));
triangles.Add(0);
triangles.Add together(triangleCount + two);
triangles.Add together(triangleCount + 1);
triangleCount++;
point1 = point2;
}

What we are doing hither is sweeping theta from 0 to 360 in steps of deltaAngle. At each step, we find the point that lies on the circumference and stores that signal to the vertices list. We will apply a variable to keep track of the number of triangles that we draw. This variable is used in defining the triangles. As explained earlier each triangle has one common vertex, that is the centre of the circle. The other 2 points would be the newly plant point and the previously found betoken. Also, it is of import to ascertain the triangles in clockwise order.

I have added the UV mapping hither. This is important if y'all want to add an image overlay on superlative of the mesh. The UVs are mapped in a linear guild from pinnacle left to bottom correct.
Finally increase the triangle count, likewise the current point must be assigned to point1 as it will be referred to in the side by side iteration.

m.vertices = circleVerteices.ToArray();
m.triangles = triangles.ToArray();
1000.uv = uvs.ToArray();

Add together the obtained lists to the mesh. Salvage the script and run the scene in the editor. Yous should run into the circle in your scene.

Try playing around with the values like radius and deltaAngle to go the circle for your use case. You will get a smoother circle as you lot subtract the deltaAngle.

The code for this tutorial forth with other procedural meshes in this series is available here.
If this post helped you I would love to hear your comments. In the next tutorial, let's start drawing 3D shapes.

abelallit1940.blogspot.com

Source: https://medium.com/@sujay_reddy/procedural-mesh-in-unity-part-2-lets-draw-a-circle-d99881147d64

0 Response to "Draw a Midle Circle Unity"

แสดงความคิดเห็น

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel