#Circular mesh
1 messages · Page 1 of 1 (latest)
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
Is the innerRadius to make the cylinder hollow?
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
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
Since I'm dealing with polar coordinates, wouldn't maxX and maxY be the same, essentially just the radius?
Or since we're dealing with vectors, would it be <maxX,0,0> for maxX and <0,maxY,0> for maxY?
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
I'm just confused about this line vertices.Add(maxX * (u * outerRadius) + maxY * (v * outerRadius)); I get that u and v are the direction and the outerRadius is the scalar, I still don't understand what the maxX and maxY are. Are they like unit vectors in this case?
Yes
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 😛
So
maxY = new Vector3 (0, 1, 0)```
would suffice?
Yes, those are the coordinates I mentioned originally