Skip to content

Instantly share code, notes, and snippets.

@TyounanMOTI
Created November 14, 2017 17:13
Show Gist options
  • Save TyounanMOTI/bc080cf908cda9b256cba8765429f6a0 to your computer and use it in GitHub Desktop.
Save TyounanMOTI/bc080cf908cda9b256cba8765429f6a0 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent (typeof(MeshFilter)), ExecuteInEditMode]
public class ArcMesh : MonoBehaviour
{
[SerializeField]
float outerRadius = 1.0f;
[SerializeField]
float innerRadius = 0.5f;
[SerializeField, Range (0.0f, 360.0f)]
float beginAngle = 0.0f;
[SerializeField, Range (0.0f, 360.0f)]
float endAngle = 360.0f;
[SerializeField]
int numVertices = 32;
Mesh mesh;
Vector3[] vertices;
int[] triangles;
void Start ()
{
var filter = GetComponent<MeshFilter> ();
mesh = new Mesh ();
mesh.MarkDynamic ();
filter.mesh = mesh;
vertices = new Vector3[numVertices * 2];
triangles = new int[numVertices * 6];
}
void OnValidate ()
{
if (numVertices < 3) {
numVertices = 3;
}
vertices = new Vector3[numVertices * 2];
triangles = new int[(numVertices - 1) * 6];
}
void Update ()
{
mesh.Clear ();
var beginRadian = beginAngle / 360.0f * 2.0f * Mathf.PI;
var endRadian = endAngle / 360.0f * 2.0f * Mathf.PI;
var radianLength = Mathf.Abs (endRadian - beginRadian);
for (int i = 0; i < vertices.Length; i += 2) {
float phase = beginRadian + i / 2 * radianLength / (numVertices - 1);
var unitCirclePoint = new Vector3 (Mathf.Cos (phase), Mathf.Sin (phase));
vertices [i] = innerRadius * unitCirclePoint;
vertices [i + 1] = outerRadius * unitCirclePoint;
}
for (int i = 0; i < numVertices - 1; i++) {
triangles [i * 6] = i * 2;
triangles [i * 6 + 1] = (i * 2 + 3) % (vertices.Length);
triangles [i * 6 + 2] = i * 2 + 1;
triangles [i * 6 + 3] = i * 2;
triangles [i * 6 + 4] = (i * 2 + 2) % (vertices.Length);
triangles [i * 6 + 5] = (i * 2 + 3) % (vertices.Length);
}
mesh.vertices = vertices;
mesh.triangles = triangles;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment