Exemplo n.º 1
0
        public void CalculateSubsegments()
        {
            if (crs.points.Count >= 4)
            {
                subsegments = new List <Vector2>();

                var position = CatmullRomSpline2D.CatmullRom(crs.points[0], crs.points[1], crs.points[2], crs.points[3], 0);

                subsegments.Add(position);

                for (int i = 0; i < crs.points.Count - 3; i++)
                {
                    for (int segment = 1; segment <= crs.subsegmentSteps; segment++)
                    {
                        float s = segment / (float)crs.subsegmentSteps;

                        position = CatmullRomSpline2D.CatmullRom(crs.points[i + 0], crs.points[i + 1], crs.points[i + 2], crs.points[i + 3], s);
                        subsegments.Add(position);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void OnGUI()
        {
            if (crs != lastCrs)
            {
                lastCrs = crs;
                HandleSplineChanges();
            }

            bool repaint = false;

            CatmullRomSpline2D before = crs;

            crs = (CatmullRomSpline2D)EditorGUILayout.ObjectField("Spline:", crs, typeof(CatmullRomSpline2D), false);
            if (before != crs)
            {
                repaint = true;
            }

            if (crs != null)
            {
                int subsegmentStepsBefore = crs.subsegmentSteps;
                int subsegmentStepsAfter  = EditorGUILayout.IntField("Subsegment Steps:", subsegmentStepsBefore);
                if (subsegmentStepsBefore != subsegmentStepsAfter)
                {
                    repaint = true;
                }

                if (crs.points.Count > 0)
                {
                    int remove = -1;
                    for (int i = 0; i < crs.points.Count; i++)
                    {
                        EditorGUILayout.BeginHorizontal();
                        Vector2 positionBefore = crs.points[i];
                        Vector2 positionAfter  = EditorGUILayout.Vector2Field("Point " + (i + 1), positionBefore);
                        if (positionBefore != positionAfter)
                        {
                            crs.points[i] = positionAfter;
                            repaint       = true;
                        }
                        if (GUILayout.Button("Delete"))
                        {
                            remove = i;
                        }
                        EditorGUILayout.EndHorizontal();
                    }

                    if (remove != -1)
                    {
                        crs.points.RemoveAt(remove);
                        repaint = true;
                    }
                }
                else
                {
                    GUILayout.Label("Shift-Click in the scene to add points");
                }
            }

            if (repaint)
            {
                SceneView.RepaintAll();
                HandleSplineChanges();
            }
        }