示例#1
0
        public PrettyPolyPoint[] GetCubicBezier()
        {
            int len = points.Length;

            if (len <= 2)
            {
                return(points);
            }
            List <PrettyPolyPoint> newPoints = new List <PrettyPolyPoint>();

            for (int i = 0; i < len; i++)
            {
                if (!closed && i == len - 1)
                {
                    continue;
                }
                PrettyPolyPoint start = points[i];
                PrettyPolyPoint end   = points[(i + 1) % len];
                Vector3         cp1   = start.position + start.outTangent;
                Vector3         cp2   = end.position + end.inTangent;

                for (float j = 0; j < subdivisions; j++)
                {
                    float           t               = j / (float)subdivisions;
                    Vector3         pos             = Interpolate.CubicBezier(start.position, cp1, cp2, end.position, t);
                    PrettyPolyPoint prettyPolyPoint = new PrettyPolyPoint(pos);
                    prettyPolyPoint.color = Color.Lerp(start.color, end.color, t);
                    prettyPolyPoint.size  = Mathf.Lerp(start.size, end.size, t);
                    newPoints.Add(prettyPolyPoint);
                }
            }
            return(newPoints.ToArray());
        }
 public PrettyPolyPoint(PrettyPolyPoint platformPoint)
 {
     this.position = platformPoint.position;
     this.inTangent = platformPoint.inTangent;
     this.outTangent = platformPoint.outTangent;
     this.color = platformPoint.color;
     this.size = platformPoint.size;
 }
示例#3
0
 public PrettyPolyPoint(PrettyPolyPoint platformPoint)
 {
     this.position   = platformPoint.position;
     this.inTangent  = platformPoint.inTangent;
     this.outTangent = platformPoint.outTangent;
     this.color      = platformPoint.color;
     this.size       = platformPoint.size;
 }
示例#4
0
        public PrettyPolyPoint GetClosestPoint(Vector3 point)
        {
            float           sqDist  = Mathf.Infinity;
            PrettyPolyPoint closest = null;

            for (int i = 0; i < points.Length; i++)
            {
                float d = (points[i].position - point).sqrMagnitude;
                if (d < sqDist)
                {
                    sqDist  = d;
                    closest = points[i];
                }
            }
            return(closest);
        }
示例#5
0
        public PrettyPolyPoint[] GetCatmullRom()
        {
            int len = points.Length;

            if (len <= 2)
            {
                return(points);
            }
            List <PrettyPolyPoint> newPoints = new List <PrettyPolyPoint>();

            for (int i = 0; i < len; i++)
            {
                PrettyPolyPoint prev  = points[(i - 1 + len) % len];
                PrettyPolyPoint start = points[i];
                PrettyPolyPoint end   = points[(i + 1) % len];
                PrettyPolyPoint next  = points[(i + 2) % len];

                if (!closed)
                {
                    if (i == 0)
                    {
                        prev = start;
                    }
                    else if (i == len - 2)
                    {
                        next = end;
                    }
                    else if (i == len - 1)
                    {
                        return(newPoints.ToArray());
                    }
                }

                for (float j = 0; j < subdivisions; j++)
                {
                    float           t               = j / (float)subdivisions;
                    Vector3         pos             = Interpolate.CatmullRom(prev.position, start.position, end.position, next.position, t);
                    PrettyPolyPoint prettyPolyPoint = new PrettyPolyPoint(pos);
                    prettyPolyPoint.color = Color.Lerp(start.color, end.color, t);
                    prettyPolyPoint.size  = Mathf.Lerp(start.size, end.size, t);
                    newPoints.Add(prettyPolyPoint);
                }
            }
            return(newPoints.ToArray());
        }
        void OnSceneGUI()
        {
            if (target == null) return;

            Event e = Event.current;

            if (e.type == EventType.ValidateCommand && e.commandName == "UndoRedoPerformed") {
            prettyPoly.UpdateMesh();
            Repaint();
            }
            if (prettyPoly.points.Length == 0) return;

            Handles.matrix = prettyPoly.transform.localToWorldMatrix;
            Handles.color = Color.blue;
            int len = prettyPoly.points.Length;
            for (int i = 0; i < len - (prettyPoly.closed?0:1); i++) {
            Vector3 p1 = prettyPoly.points[i].position;
            Vector3 p2 = prettyPoly.points[(i + 1) % len].position;
            Handles.DrawLine(p1, p2);
            }

            EditorGUI.BeginChangeCheck();
            PrettyPolyPoint[] points = new PrettyPolyPoint[prettyPoly.points.Length];

            if (e.alt) {
            RemovePoint();
            points = prettyPoly.points;
            }
            else {
            if (e.shift) AddPoint();
            for (int i = 0; i < points.Length; i++) {
                PrettyPolyPoint point = new PrettyPolyPoint(prettyPoly.points[i]);

                Handles.color = Color.white;
                GUI.SetNextControlName("pretty poly point " + i);
                if (i == 0) Handles.color = Color.magenta;
                if (i == 1) Handles.color = Color.green;
                float size =GetHandleSize(point.position, 1);
                point.position = Handles.FreeMoveHandle(
                    point.position,
                    Quaternion.identity,
                    size,
                    Vector3.zero,
                    Handles.CircleCap
                );
                point.position.z = 0;

                if (prettyPoly.curveType == PrettyPoly.CurveType.CubicBezier) {
                    point.inTangent = TangentHandle(point.position, point.inTangent);
                    point.outTangent = TangentHandle(point.position, point.outTangent);
                }

                points[i] = point;
            }
            }

            if (EditorGUI.EndChangeCheck()) {
            Undo.RecordObject(target, "moved prettyPoly point");
            prettyPoly.points = points;
            prettyPoly.UpdateMesh();
            prettyPoly.UpdateObjects();
            EditorUtility.SetDirty(target);
            }
        }
        public void UpdateObjects(Transform root, PrettyPolyPoint[] points, bool closed)
        {
            if (prefab == null || points.Length < 2) return;
            FixParams();

            Vector3[] positions = GetOffset(points);

            float pathLength = positions.PathLength(closed);

            switch (layerType) {
            case (LayerType.ScatterEdge):
                AddStroke(root, positions, pathLength, closed);
                break;
            case (LayerType.ScatterFill):
                AddInnerFill(root, positions, pathLength);
                break;
            }
        }
 public Vector3[] GetOffset(PrettyPolyPoint[] points)
 {
     Vector2[] path = System.Array.ConvertAll(points, p => (Vector2)p.position);
     Polygon poly = new Polygon(path);
     return System.Array.ConvertAll(poly.GetOffsetPath(dirOffset), p => (Vector3)p + posOffset);
 }
        void OnSceneGUI()
        {
            if (target == null)
            {
                return;
            }

            Event e = Event.current;

            if (e.type == EventType.ValidateCommand && e.commandName == "UndoRedoPerformed")
            {
                prettyPoly.UpdateMesh();
                Repaint();
            }
            if (prettyPoly.points.Length == 0)
            {
                return;
            }

            Handles.matrix = prettyPoly.transform.localToWorldMatrix;
            Handles.color  = Color.blue;
            int len = prettyPoly.points.Length;

            for (int i = 0; i < len - (prettyPoly.closed?0:1); i++)
            {
                Vector3 p1 = prettyPoly.points[i].position;
                Vector3 p2 = prettyPoly.points[(i + 1) % len].position;
                Handles.DrawLine(p1, p2);
            }

            EditorGUI.BeginChangeCheck();
            PrettyPolyPoint[] points = new PrettyPolyPoint[prettyPoly.points.Length];

            if (e.alt)
            {
                RemovePoint();
                points = prettyPoly.points;
            }
            else
            {
                if (e.shift)
                {
                    AddPoint();
                }
                for (int i = 0; i < points.Length; i++)
                {
                    PrettyPolyPoint point = new PrettyPolyPoint(prettyPoly.points[i]);

                    Handles.color = Color.white;
                    GUI.SetNextControlName("pretty poly point " + i);
                    if (i == 0)
                    {
                        Handles.color = Color.magenta;
                    }
                    if (i == 1)
                    {
                        Handles.color = Color.green;
                    }
                    float size = GetHandleSize(point.position, 1);
                    point.position = Handles.FreeMoveHandle(
                        point.position,
                        Quaternion.identity,
                        size,
                        Vector3.zero,
                        Handles.CircleCap
                        );
                    point.position.z = 0;

                    if (prettyPoly.curveType == PrettyPoly.CurveType.CubicBezier)
                    {
                        point.inTangent  = TangentHandle(point.position, point.inTangent);
                        point.outTangent = TangentHandle(point.position, point.outTangent);
                    }

                    points[i] = point;
                }
            }

            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(target, "moved prettyPoly point");
                prettyPoly.points = points;
                prettyPoly.UpdateMesh();
                prettyPoly.UpdateObjects();
                EditorUtility.SetDirty(target);
            }
        }
示例#10
0
	public void AddCollider (PrettyPolyPoint[] pts) {
		if (closed && solid) {
			gameObject.DestroyComponent<EdgeCollider2D>();
			PolygonCollider2D c = gameObject.GetOrAddComponent<PolygonCollider2D>();
			c.SetPath(0, System.Array.ConvertAll(pts, point => (Vector2)point.position));
		}
		else {
			gameObject.DestroyComponent<PolygonCollider2D>();
			EdgeCollider2D c = gameObject.GetOrAddComponent<EdgeCollider2D>();
			c.points = System.Array.ConvertAll(pts, point => (Vector2)point.position);
			if (closed) {
				List<Vector2> closedPts = new List<Vector2>(c.points);
				closedPts.Add(closedPts[0]);
				c.points = closedPts.ToArray();
			}
		}
	}
示例#11
0
	public PrettyPolyPoint[] GetCubicBezier () {
		int len = points.Length;
		if (len <= 2) return points;
		List<PrettyPolyPoint> newPoints = new List<PrettyPolyPoint>();
		for (int i = 0; i < len; i++) {
			if (!closed && i == len - 1) continue;
			PrettyPolyPoint start = points[i];
			PrettyPolyPoint end = points[(i + 1) % len];
			Vector3 cp1 = start.position + start.outTangent;
			Vector3 cp2 = end.position + end.inTangent;
			
			for (float j = 0; j < subdivisions; j++) {
				float t = j / (float)subdivisions;
				Vector3 pos = Interpolate.CubicBezier(start.position, cp1, cp2, end.position, t);
				PrettyPolyPoint prettyPolyPoint = new PrettyPolyPoint(pos);
				prettyPolyPoint.color = Color.Lerp(start.color, end.color, t);
				prettyPolyPoint.size = Mathf.Lerp(start.size, end.size, t);
				newPoints.Add(prettyPolyPoint);
			}
		}
		return newPoints.ToArray();
	}
示例#12
0
	public PrettyPolyPoint[] GetCatmullRom () {
		int len = points.Length;
		if (len <= 2) return points;
		List<PrettyPolyPoint> newPoints = new List<PrettyPolyPoint>();
		for (int i = 0; i < len; i++) {
			PrettyPolyPoint prev = points[(i - 1 + len) % len];
			PrettyPolyPoint start = points[i];
			PrettyPolyPoint end = points[(i + 1) % len];
			PrettyPolyPoint next = points[(i + 2) % len];

			if (!closed) {
				if (i == 0) prev = start;
				else if (i == len - 2) next = end;
				else if (i == len - 1) return newPoints.ToArray();
			}

			for (float j = 0; j < subdivisions; j++) {
				float t = j / (float)subdivisions;
				Vector3 pos = Interpolate.CatmullRom(prev.position, start.position, end.position, next.position, t);
				PrettyPolyPoint prettyPolyPoint = new PrettyPolyPoint(pos);
				prettyPolyPoint.color = Color.Lerp(start.color, end.color, t);
				prettyPolyPoint.size = Mathf.Lerp(start.size, end.size, t);
				newPoints.Add(prettyPolyPoint);
			}
		}
		return newPoints.ToArray();
	}
        public Mesh GetMesh(PrettyPolyPoint[] points, bool closed, float winding)
        {
            FixParams();
            Vector3[] positions = GetOffset(points);
            if (positions.Length < 2) return null;
            Clear();

            float pathLength = positions.PathLength(closed);
            if (winding < 0) {
            positions = positions.Reverse();
            }
            else {
            positions = positions.Shift(2);
            }

            switch (layerType) {
            case (LayerType.ScatterEdge):
                AddScatterEdge(positions, pathLength, closed);
                break;
            case (LayerType.SolidEdge):
                AddSolidEdge(positions, pathLength, closed);
                break;
            case (LayerType.ScatterFill):
                AddScatterFill(positions, pathLength);
                break;
            case (LayerType.SolidFill):
                AddSolidFill(positions, pathLength);
                break;
            }

            mesh.vertices = verts.ToArray();
            mesh.uv = uvs.ToArray();
            mesh.colors = colors.ToArray();
            mesh.normals = norms.ToArray();
            mesh.tangents = tans.ToArray();
            mesh.triangles = tris.ToArray();

            return mesh;
        }