Skip to content

Instantly share code, notes, and snippets.

@Hyperparticle
Last active November 24, 2017 13:03
Show Gist options
  • Select an option

  • Save Hyperparticle/e3bb8681cbe88d02fb8af9bc91ac804b to your computer and use it in GitHub Desktop.

Select an option

Save Hyperparticle/e3bb8681cbe88d02fb8af9bc91ac804b to your computer and use it in GitHub Desktop.

Revisions

  1. Hyperparticle revised this gist Nov 24, 2017. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions DrawRectangleV0.cs
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,3 @@
    /// <summary>
    /// A 2D physics rectangle that is drawn by specifying the positions of
    /// its two opposite corners.
    /// </summary>
    public class DrawRectangle : MonoBehaviour
    {
    public Color FillColor = Color.white;
  2. Hyperparticle renamed this gist Nov 24, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. Hyperparticle created this gist Nov 23, 2017.
    85 changes: 85 additions & 0 deletions DrawRectangle.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,85 @@
    /// <summary>
    /// A 2D physics rectangle that is drawn by specifying the positions of
    /// its two opposite corners.
    /// </summary>
    public class DrawRectangle : MonoBehaviour
    {
    public Color FillColor = Color.white;

    private MeshFilter _meshFilter;
    private LineRenderer _lineRenderer;

    // Start and end vertices (in absolute coordinates)
    private readonly List<Vector2> _vertices = new List<Vector2>(2);

    public bool ShapeFinished { get { return _vertices.Count >= 2; } }

    private void Awake()
    {
    _meshFilter = GetComponent<MeshFilter>();
    _lineRenderer = GetComponent<LineRenderer>();
    }

    public void AddVertex(Vector2 vertex)
    {
    if (ShapeFinished) {
    return;
    }

    _vertices.Add(vertex);
    UpdateShape(vertex);
    }

    public void UpdateShape(Vector2 newVertex)
    {
    if (_vertices.Count < 2) {
    return;
    }

    _vertices[_vertices.Count - 1] = newVertex;

    // Set the gameobject's position to be the center of mass
    var center = _vertices.Centroid();
    transform.position = center;

    // Update the mesh relative to the transform
    var relativeVertices = _vertices.Select(v => v - center).ToArray();
    _meshFilter.mesh = RectangleMesh(relativeVertices[0], relativeVertices[1], FillColor);

    // Update the shape's outline
    _lineRenderer.positionCount = _meshFilter.mesh.vertices.Length;
    _lineRenderer.SetPositions(_meshFilter.mesh.vertices);
    }

    /// <summary>
    /// Creates and returns a rectangle mesh given two vertices on its
    /// opposite corners and fills it with the given color.
    /// </summary>
    private static Mesh RectangleMesh(Vector2 v0, Vector2 v1, Color fillColor)
    {
    // Calculate implied verticies from corner vertices
    // Note: vertices must be adjacent to each other for Triangulator to work properly
    var v2 = new Vector2(v0.x, v1.y);
    var v3 = new Vector2(v1.x, v0.y);
    var rectangleVertices = new[] { v0, v2, v1, v3 };

    // Find all the triangles in the shape
    var triangles = new Triangulator(rectangleVertices).Triangulate();

    // Assign each vertex the fill color
    var colors = Enumerable.Repeat(fillColor, rectangleVertices.Length).ToArray();

    var mesh = new Mesh {
    name = "Rectangle",
    vertices = rectangleVertices.ToVector3(),
    triangles = triangles,
    colors = colors
    };

    mesh.RecalculateNormals();
    mesh.RecalculateBounds();
    mesh.RecalculateTangents();

    return mesh;
    }
    }