Advertisement
Guest User

CircleOutline

a guest
Jun 27th, 2017
1,678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. [RequireComponent (typeof (MeshFilter), typeof (MeshRenderer))]
  5. public class CircleOutline : MonoBehaviour {
  6.  
  7.     public float Radius;
  8.     public float Width;
  9.     public int Resolution;
  10.  
  11.     // Lists to store generated mesh information
  12.     List<Vector3> vertices = new List<Vector3> ();
  13.     List<int> triangles = new List<int> ();
  14.  
  15.     // Some sanity checks
  16.     private void OnValidate () {
  17.         if (Radius < 0)
  18.             Radius = 0;
  19.         if (Width < 0)
  20.             Width = 0;
  21.         if (Resolution < 3)
  22.             Resolution = 3;
  23.  
  24.         // Reform the mesh
  25.         GenerateMesh ();
  26.     }
  27.  
  28.     public void GenerateMesh () {
  29.         // Clear out existing mesh data
  30.         vertices = new List<Vector3> ();
  31.         triangles = new List<int> ();
  32.  
  33.         // Angle between two "spokes" of the quad ribbon
  34.         float angleStep = 360f / Resolution;
  35.  
  36.         for (int i = 0; i < Resolution; i++) {
  37.             // Nasty trig
  38.             // Forms dirA and dirB as spokes from the center using sin(theta), cos(theta)
  39.             Vector3 dirA = (Vector2) transform.position + new Vector2 (Mathf.Sin (i * angleStep * Mathf.Deg2Rad), Mathf.Cos (i * angleStep * Mathf.Deg2Rad));
  40.             Vector3 dirB = (Vector2) transform.position + new Vector2 (Mathf.Sin ((i + 1) * angleStep * Mathf.Deg2Rad), Mathf.Cos ((i + 1) * angleStep * Mathf.Deg2Rad));
  41.             // Form the quad ribbon one section at a time
  42.             AddArcQuad (dirA, dirB, Radius, Width);
  43.         }
  44.         // Apply the mesh to the MeshFilter
  45.         ApplyMesh ();
  46.     }
  47.  
  48.     void AddArcQuad (Vector2 a, Vector2 b, float radius, float width) {
  49.         int vertexIndex = vertices.Count;
  50.      
  51.         // Multiply a and b by radius for inner vertices
  52.         // Multiply by radius + width for outer vertices
  53.         vertices.Add (a * (radius + width));
  54.         vertices.Add (b * radius);
  55.         vertices.Add (a * radius);
  56.         vertices.Add (b * radius);
  57.         vertices.Add (a * (radius + width));
  58.         vertices.Add (b * (radius + width));
  59.  
  60.         // Add the triangle info
  61.         triangles.Add (vertexIndex);
  62.         triangles.Add (vertexIndex + 1);
  63.         triangles.Add (vertexIndex + 2);
  64.         triangles.Add (vertexIndex + 3);
  65.         triangles.Add (vertexIndex + 4);
  66.         triangles.Add (vertexIndex + 5);
  67.     }
  68.  
  69.     void ApplyMesh () {
  70.         Mesh mesh = new Mesh ();
  71.         mesh.vertices = vertices.ToArray ();
  72.         mesh.triangles = triangles.ToArray ();
  73.  
  74.         // Slow, and throws errors in the editor, but it works.
  75.         GetComponent<MeshFilter> ().mesh = mesh;
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement