Advertisement
Guest User

Shapes Bug Repro

a guest
Jun 29th, 2022
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.54 KB | None | 0 0
  1. using Shapes;
  2. using TMPro;
  3. using UnityEngine;
  4.  
  5. [ExecuteAlways]
  6. public class RadarChart : ImmediateModeShapeDrawer
  7. {
  8.     [Header("Drawing Settings")]
  9.     [Range(0.1f, 10f)] public float radius = 2f;
  10.     public float rotation = 0f;
  11.  
  12.  
  13.     [Header("Border")]
  14.     [Range(0.1f, 10f)] public float borderThickness = 1.2f;
  15.     public Color borderColor = Color.black;
  16.  
  17.     public Color backgroundColor = new Color(0.96f, 0.96f, 0.96f);
  18.  
  19.     [Header("Fill")]
  20.     public Color fillColor = new Color(0f, 0.8f, 0f, 0.4f);
  21.     public float fillBorderThickness = 0.1f;
  22.     public Color fillBorderColor = Color.black;
  23.  
  24.     [Header("Guide Lines")]
  25.     [Range(0, 10)] public int numGuideLines = 1;
  26.  
  27.     [Range(0.1f, 10f)] public float guideLineThickness = 0.3f;
  28.     public Color guideLineColor = new Color(0.7f, 0.7f, 0.7f);
  29.  
  30.     [Header("Spokes")]
  31.     public bool drawSpokes = true;
  32.     public Color spokeColor = new Color(0.6f, 0.6f, 0.6f);
  33.     [Range(0.1f, 10f)] public float spokeThickness = 0.1f;
  34.  
  35.     [Header("Contents")]
  36.     public float minValue = -0.1f;
  37.     public float maxValue = 100f;
  38.     public float[] values = new float[]
  39.     {
  40.         42f, 36f, 16f, 35f, 50f, 35f,
  41.     };
  42.  
  43.     [Header("Axes")]
  44.     public Sprite[] axisIcons = null;
  45.     private SpriteRenderer[] renderers = null;
  46.  
  47.     private void Start()
  48.     {
  49.         renderers = GetComponentsInChildren<SpriteRenderer>();
  50.     }
  51.  
  52.     public override void OnEnable()
  53.     {
  54.         base.OnEnable();
  55.         ResetIcons();
  56.     }
  57.  
  58.     public void ResetIcons()
  59.     {
  60.         if (renderers != null)
  61.         {
  62.             for (int i = 0; i < renderers.Length; i++)
  63.             {
  64.                 renderers[i].enabled = i < values.Length;
  65.             }
  66.         }
  67.     }
  68.  
  69.     public float[] Values
  70.     {
  71.         get => values;
  72.         set
  73.         {
  74.             values = value;
  75.             if (renderers != null)
  76.             {
  77.                 for (int i = 0; i < renderers.Length; i++)
  78.                 {
  79.                     renderers[i].enabled = i < values.Length;
  80.                 }
  81.             }
  82.         }
  83.     }
  84.  
  85.     public override void DrawShapes(Camera cam)
  86.     {
  87.         int sides = Mathf.Max(3, Values.Length);
  88.         // Draw.Command enqueues a set of draw commands to render in the given camera
  89.         using (Draw.Command(cam))
  90.         { // all immediate mode drawing should happen within these using-statements
  91.             Draw.ResetAllDrawStates(); // this makes sure no static draw states "leak" over to this scene
  92.             Draw.Matrix = transform.localToWorldMatrix; // this makes it draw in the local space of this transform
  93.  
  94.             // Draw Border
  95.             Draw.SizeSpace = ThicknessSpace.Noots;
  96.             Draw.ThicknessSpace = ThicknessSpace.Noots;
  97.  
  98.             // Draw background.
  99.             Draw.RegularPolygon(Vector3.zero, sides, radius, rotation, 0, backgroundColor);
  100.  
  101.             // Draw guide lines
  102.             /*
  103.              * If num guidelines =
  104.              * 1: draw at 1/2 the radius
  105.              * 2: draw at 1/3 & 2/3 the radius
  106.              * 3: draw at 1/4, 2/4, & 3/4 the radius
  107.              */
  108.             for (int i = 0; i < numGuideLines; i++)
  109.             {
  110.                 float proportion = ((float)i + 1) / (numGuideLines + 1);
  111.                 Draw.RegularPolygonBorder(Vector3.zero, sides, proportion * radius, guideLineThickness, rotation, 0, guideLineColor);
  112.             }
  113.  
  114.             // Draw spokes
  115.             if (drawSpokes)
  116.             {
  117.                 for (int i = 0; i < sides; i++)
  118.                 {
  119.                     float radians = Mathf.PI * 2 * ((float)i / sides) + rotation;
  120.                     Vector3 end = new Vector3(Mathf.Cos(radians), Mathf.Sin(radians), 0) * radius;
  121.                     Draw.Line(Vector3.zero, end, spokeThickness, spokeColor);
  122.  
  123.                     if (renderers != null && i < renderers.Length)
  124.                     {
  125.                         renderers[i].transform.localPosition = end + (end.normalized * 0.3f);
  126.                     }
  127.                 }
  128.             }
  129.  
  130.             // Draw border
  131.             Draw.Thickness = borderThickness;
  132.             Draw.RegularPolygonBorder(Vector3.zero, sides, radius, borderThickness, rotation, 0, borderColor);
  133.  
  134.             // Draw values.
  135.             using (PolygonPath path = new PolygonPath())
  136.             {
  137.                 using (PolylinePath linePath = new PolylinePath())
  138.                 {
  139.                     for (int i = 0; i < Values.Length; i++)
  140.                     {
  141.                         float proportion = Mathf.InverseLerp(minValue, maxValue, Values[i]);
  142.                         float distance = proportion * radius;
  143.                         float radians = ((float)i / Values.Length).RotationsToRadians() + rotation;
  144.  
  145.                         Vector2 point = new Vector2(Mathf.Cos(radians), Mathf.Sin(radians)) * distance;
  146.  
  147.                         path.AddPoint(point);
  148.                         linePath.AddPoint(point);
  149.                     }
  150.  
  151.                     Draw.Polygon(path, fillColor);
  152.                     Draw.Polyline(linePath, true, fillBorderThickness, fillBorderColor);
  153.  
  154.                 }
  155.             }
  156.         }
  157.     }
  158. }
  159.  
  160. public static class FloatExtensions
  161. {
  162.     public static float AngleToRadians(this float angle)
  163.     {
  164.         return angle * Mathf.Deg2Rad;
  165.     }
  166.  
  167.     public static float RadiansToAngle(this float radians)
  168.     {
  169.         return radians * Mathf.Rad2Deg;
  170.     }
  171.  
  172.     public static float RotationsToRadians(this float rotations)
  173.     {
  174.         return rotations * Mathf.PI * 2;
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement