가운데 구멍 없는 버전과 있는 버전 모두 지원 (Inner Radius)
능지가 부족한 나는 이것을 그리는 데 몇 시간을 소모한 것인가?
UV는 깔끔하게 못 하겠당.
이것이 수학 나형 5등급의 한계
public class DonutProgressMeshDraw : MonoBehaviour {
m_mesh = m_meshFilter.mesh;
if (m_mesh == null) m_mesh = new Mesh();
Vector3[] vertices = new Vector3[_seg + 3];
Vector2[] uv = new Vector2[_seg + 3];
vertices[0] = new Vector3(0, 0, 0) + offset;
float startRad = Mathf.Deg2Rad * (m_startAngle + 180);
float ratio = Mathf.Clamp01(Mathf.Abs(m_startAngle - m_endAngle) / 360);
for (int i = 0; i <= _seg ; i++) {
float angle = (-i * (Mathf.PI * 2.0f) / _seg) * ratio + startRad;
vertices[i + 1]
= (new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * _outRad) + offset;
}
int[] triangles = new int[3 * _seg];
if (ratio >= 1f) { //360도 전방향이면 그냥 용접해버려
for (int i = 0; i < _seg - 1; i++) {
triangles[i * 3] = 0;
triangles[i * 3 + 1] = i + 1;
triangles[i * 3 + 2] = i + 2;
}
triangles[3 * _seg - 3] = 0;
triangles[3 * _seg - 2] = _seg;
triangles[3 * _seg - 1] = 1;
} else {
for (int i = 0; i < _seg; i++) {
triangles[i * 3] = 0;
triangles[i * 3 + 1] = i + 1;
triangles[i * 3 + 2] = i + 2;
}
}
uv[0] = Vector2.zero;
for (int i = 0; i < uv.Length - 1; i++) {
uv[i + 1] = new Vector2((float)i / (uv.Length - 2), 1);
}
vertices[vertices.Length - 1] = vertices[0];
uv[uv.Length - 1] = Vector2.right;
m_mesh.Clear();
m_mesh.vertices = vertices;
m_mesh.triangles = triangles;
m_mesh.uv = uv;
m_mesh.RecalculateNormals();
}
public void SetMeshData(float _inRad, float _outRad, int _seg) {
m_mesh = m_meshFilter.mesh;
if (m_mesh == null) m_mesh = new Mesh();
Vector3[] vertices = new Vector3[(_seg + 1) * 2];
Vector2[] uv = new Vector2[(_seg + 1) * 2];
float startRad = Mathf.Deg2Rad * (m_startAngle + 180);
float ratio = Mathf.Clamp01(Mathf.Abs(m_startAngle - m_endAngle) / 360);
for (int i = 0; i < _seg + 1; i++) {
float angle = (-i * (Mathf.PI * 2.0f) / _seg) * ratio + startRad;
vertices[i] = (new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * _inRad) + offset;
vertices[i + _seg + 1] = (new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * _outRad) + offset;
uv[i] = new Vector2(((float)i / _seg), 0);
uv[i + _seg + 1] = new Vector2(((float)i / _seg), 1);
}
int[] triangles = new int[(_seg) * 6];
for (int i = 0; i < _seg; i++) {
triangles[i * 6] = i;
triangles[i * 6 + 1] = i + _seg + 1;
triangles[i * 6 + 2] = i + _seg + 2;
triangles[i * 6 + 3] = i;
triangles[i * 6 + 4] = i + _seg + 2;
triangles[i * 6 + 5] = i + 1;
}
m_mesh.Clear();
m_mesh.vertices = vertices;
m_mesh.triangles = triangles;
m_mesh.uv = uv;
m_mesh.RecalculateNormals();
}
}