#Circular mesh

1 messages · Page 1 of 1 (latest)

spare elk
#

I have a cylinder mesh class whose end cap can be abused to do this:

public static void Append(
    int subdivisions,
    List<Vector3> vertices,
    List<Vector2> uv0,
    List<int> indices,
    Vector3 maxX,
    Vector3 maxY,
    float innerRadius,
    float outerRadius
)
{
    const float startAngle = -Mathf.PI * 0.5f;
    for (int i = 0; i <= subdivisions; i++)
    {
        float x = i / (float)subdivisions;
        float a = startAngle + x * Mathf.PI * 2;
        float u = Mathf.Cos(a);
        float v = Mathf.Sin(a);
        vertices.Add(maxX * (u * innerRadius) + maxY * (v * innerRadius));
        vertices.Add(maxX * (u * outerRadius) + maxY * (v * outerRadius));
        uv0.Add(new Vector2(1 - x, 0));
        uv0.Add(new Vector2(1 - x, 1));

        int index = i * 2;
        if (i != subdivisions)
        {
            indices.Add(indicesOffset + index + 3);
            indices.Add(indicesOffset + index + 1);
            indices.Add(indicesOffset + index);

            indices.Add(indicesOffset + index + 2);
            indices.Add(indicesOffset + index + 3);
            indices.Add(indicesOffset + index);
        }
    }
}```
#

if you provided maxX to be Vector3.right and maxY to be Vector3.up, and innerRadius to be 0

#

subdivisions being the edge count along the circumference

severe berry
#

Is the innerRadius to make the cylinder hollow?

spare elk
#

This makes an annulus, if the inner radius is 0 the annulus is a circle

#

It should give you something like this

#

which is presumably what you're asking for

severe berry
#

Yeah, that's exactly what I'm asking for! Thank you so much for this!

#

Sorry for the slow replies too, I'm fairly new to Unity, so it's taking me a little longer to decipher

severe berry
#

Or since we're dealing with vectors, would it be <maxX,0,0> for maxX and <0,maxY,0> for maxY?

spare elk
#

I'm not sure what you're dealing with, but in my implementation maxX and maxY are just two perpendicular vectors defining the plane the annulus is on

#

the X and Y components created from the polar coordinates are scaled by them to position the verts

severe berry
spare elk
#

Yes

severe berry
#

Not direction, but the angle

#

Either way

#

Okay

#

Thanks

spare elk
#

They are two unit vectors that define the coordinate system

#

(1, 0, 0) and (0, 1, 0) would create a mesh aligned on XY

#

could certainly be better named, but I haven't refactored this code 😛

severe berry
#

So

maxY = new Vector3 (0, 1, 0)```
would suffice?
spare elk
#

Yes, those are the coordinates I mentioned originally