Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Assets/Shapes2D/Editor/ShapeEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,29 @@ private void SetPolygonCollider2D(Shape shape) {
colliderPoints[i] = shape.transform.InverseTransformPoint(points[i]);
Undo.RecordObject(pc2d, "Set PolygonCollider2D Points");
pc2d.points = colliderPoints;
} else if (shape.settings.shapeType == ShapeType.Path) {
// note - this quick and dirty algorithm only works for paths with a single closed loop. also, it
// calculates the number of points in a naive way so very large shapes will have lots of collider
// points. a smarter algorithm might put more collider points on curvier sections of the path.
if (!pc2d)
pc2d = shape.gameObject.AddComponent<PolygonCollider2D>();
PathSegment[] segments = shape.GetPathWorldSegments();
List<Vector2> colliderPoints = new List<Vector2>();
int pointsPerUnit = 5;
for (int i = 0; i < segments.Length; i++) {
// figure out how many collider points we'll add on this segment based on its length
int numPoints = Mathf.FloorToInt(segments[i].GetLength() * pointsPerUnit);
// add a point for the start of the segment
Vector3 point = segments[i].p0;
colliderPoints.Add(shape.transform.InverseTransformPoint(point));
// add more points along the curve
for (int c = 0; c < numPoints; c++) {
point = segments[i].GetPoint((1f / numPoints) * (c + 1));
colliderPoints.Add(shape.transform.InverseTransformPoint(point));
}
}
Undo.RecordObject(pc2d, "Set PolygonCollider2D Points");
pc2d.points = colliderPoints.ToArray();
}
}

Expand Down Expand Up @@ -828,6 +851,8 @@ public override void OnInspectorGUI() {
}
if (shape.GetComponent<PolygonCollider2D>() && GUILayout.Button("From Polygon Collider 2D"))
FromPolygonCollider2D(shape);
if (GUILayout.Button("Set Polygon Collider 2D"))
SetPolygonCollider2D(shape);
EditorGUI.EndDisabledGroup();
}

Expand Down
120 changes: 120 additions & 0 deletions Assets/Shapes2D/Scripts/Common.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
namespace Shapes2D {

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.Serialization;

/// <summary>
/// Shapes2D base shape types.
/// </summary>
public enum ShapeType {
Ellipse,
Polygon,
Rectangle,
Triangle,
Path
}

/// <summary>
/// Shapes2D fill types.
/// </summary>
public enum FillType {
None,
MatchOutlineColor,
SolidColor,
Gradient,
Grid,
CheckerBoard,
Stripes,
Texture
}

/// <summary>
/// Shapes2D gradient fill types.
/// </summary>
public enum GradientType {
Linear,
Cylindrical,
Radial
}

/// <summary>
/// Shapes2D gradient fill direction.
/// </summary>
public enum GradientAxis {
Horizontal,
Vertical
}

/// <summary>
/// Shapes2D polygon presets.
/// </summary>
public enum PolygonPreset {
Custom, Triangle, Diamond, Pentagon, Hexagon, Heptagon, Octagon, Nonagon, Decagon, Hendecagon,
Dodecagon, Star, Arrow
}

/// <summary>
/// Shapes2D path segment, for Path shape types. It describes a quadratic Bézier curve.
/// </summary>
[System.SerializableAttribute]
public struct PathSegment {
public Vector3 p0, p1, p2;

public PathSegment(Vector2 p0, Vector2 p1, Vector3 p2) {
this.p0 = p0;
this.p1 = p1;
this.p2 = p2;
}

public PathSegment(Vector2 p0, Vector2 p2) {
this.p0 = p0;
this.p2 = p2;
this.p1 = p0 + (p2 - p0) / 2;
}

public void Clamp() {
p0.x = Mathf.Clamp01(p0.x + 0.5f) - 0.5f;
p0.y = Mathf.Clamp01(p0.y + 0.5f) - 0.5f;
p2.x = Mathf.Clamp01(p2.x + 0.5f) - 0.5f;
p2.y = Mathf.Clamp01(p2.y + 0.5f) - 0.5f;
}

public Vector3 midpoint { get { return p0 + (p2 - p0) / 2; } }

public void MakeLinear() {
p1 = p0 + (p2 - p0) / 2;
}

/// <summary>
/// Returns the point on the segment at t, where t is from 0-1.
/// </summary>
public Vector2 GetPoint(float t) {
t = Mathf.Clamp01(t);
// https://stackoverflow.com/questions/5634460/quadratic-bézier-curve-calculate-points :D
return new Vector2(
(1 - t) * (1 - t) * p0.x + 2 * (1 - t) * t * p1.x + t * t * p2.x,
(1 - t) * (1 - t) * p0.y + 2 * (1 - t) * t * p1.y + t * t * p2.y
);
}

/// <summary>
/// Returns the approximate length of this segment by dividing it up and summing the lengths of each part.
/// </summary>
public float GetLength(int subdivisions = 10) {
if (subdivisions <= 0)
throw new System.Exception("Must have at least 1 subdivision in GetLength().");
float length = 0;
Vector3 last = p0;
float step = 1f / subdivisions;
for (int i = 0; i < subdivisions; i++) {
Vector3 p = GetPoint(i * step + step);
length += Vector3.Distance(last, p);
last = p;
}
return length;
}
}

}
11 changes: 11 additions & 0 deletions Assets/Shapes2D/Scripts/Common.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 0 additions & 83 deletions Assets/Shapes2D/Scripts/Shape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,89 +4,6 @@
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.Serialization;

/// <summary>
/// Shapes2D base shape types.
/// </summary>
public enum ShapeType {
Ellipse,
Polygon,
Rectangle,
Triangle,
Path
}

/// <summary>
/// Shapes2D fill types.
/// </summary>
public enum FillType {
None,
MatchOutlineColor,
SolidColor,
Gradient,
Grid,
CheckerBoard,
Stripes,
Texture
}

/// <summary>
/// Shapes2D gradient fill types.
/// </summary>
public enum GradientType {
Linear,
Cylindrical,
Radial
}

/// <summary>
/// Shapes2D gradient fill direction.
/// </summary>
public enum GradientAxis {
Horizontal,
Vertical
}

/// <summary>
/// Shapes2D polygon presets.
/// </summary>
public enum PolygonPreset {
Custom, Triangle, Diamond, Pentagon, Hexagon, Heptagon, Octagon, Nonagon, Decagon, Hendecagon,
Dodecagon, Star, Arrow
}

/// <summary>
/// Shapes2D path segment, for Path shape types. It describes a quadratic Bézier curve.
/// </summary>
[System.SerializableAttribute]
public struct PathSegment {
public Vector3 p0, p1, p2;

public PathSegment(Vector2 p0, Vector2 p1, Vector3 p2) {
this.p0 = p0;
this.p1 = p1;
this.p2 = p2;
}

public PathSegment(Vector2 p0, Vector2 p2) {
this.p0 = p0;
this.p2 = p2;
this.p1 = p0 + (p2 - p0) / 2;
}

public void Clamp() {
p0.x = Mathf.Clamp01(p0.x + 0.5f) - 0.5f;
p0.y = Mathf.Clamp01(p0.y + 0.5f) - 0.5f;
p2.x = Mathf.Clamp01(p2.x + 0.5f) - 0.5f;
p2.y = Mathf.Clamp01(p2.y + 0.5f) - 0.5f;
}

public Vector3 midpoint { get { return p0 + (p2 - p0) / 2; } }

public void MakeLinear() {
p1 = p0 + (p2 - p0) / 2;
}
}

/// <summary>
/// The MonoBehaviour for a Shapes2D Shape.
Expand Down