Exemple #1
0
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            linePath = target as LinePath;

            if (selectedIndex >= 0 && selectedIndex < linePath.PointCount)
            {
                DrawSelectedPointInspector();
                Tools.hidden = true;
            }
            else
            {
                Tools.hidden = false;
            }

            if (GUILayout.Button("Add Point"))
            {
                Undo.RecordObject(linePath, "Add Point");
                linePath.AddPoint();
                EditorUtility.SetDirty(linePath);
            }

            if (linePath.PointCount > 2 && GUILayout.Button("Remove Point"))
            {
                Undo.RecordObject(linePath, "Remove Point");
                linePath.RemovePoint();
                EditorUtility.SetDirty(linePath);
            }

            if (GUILayout.Button("Reset Normals"))
            {
                Undo.RecordObject(linePath, "Remove Point");

                if (linePath.PointCount <= 2)
                {
                    Vector3 pos  = linePath.GetPoint(0);
                    Vector3 pos2 = linePath.GetPoint(1);

                    Vector3 toNext = (pos2 - pos).normalized;
                    Vector3 right  = Vector3.Cross(Vector3.up, toNext).normalized;

                    Vector3 normal = Vector3.Cross(-right, toNext);
                    if (flipNormal)
                    {
                        normal *= -1f;
                    }

                    for (int i = 0; i < linePath.Normals.Length; i++)
                    {
                        linePath.Normals[i] = normal;
                    }
                }
                else
                {
                    for (int i = 0; i < linePath.Normals.Length; i++)
                    {
                        Vector3 pos = linePath.GetPoint(i);

                        int     prevIndex = linePath.SafePointIndex(i - 1);
                        Vector3 prevPos   = linePath.GetPoint(prevIndex);

                        int     nextIndex = linePath.SafePointIndex(i + 1);
                        Vector3 nextPos   = linePath.GetPoint(nextIndex);

                        Vector3 toNext = (nextPos - pos).normalized;
                        Vector3 toPrev = (prevPos - pos).normalized;

                        Vector3 normal = Vector3.Cross(toPrev, toNext).normalized;
                        if (flipNormal)
                        {
                            normal *= -1f;
                        }

                        linePath.Normals[i] = linePath.transform.InverseTransformDirection(normal);
                    }
                }

                flipNormal = !flipNormal;

                ResetHandleSettings();

                EditorUtility.SetDirty(linePath);
            }

            if (GUI.changed)
            {
                linePath.OnPathChanged(linePath);
            }
        }
Exemple #2
0
        protected virtual void OnSceneGUI()
        {
            linePath        = target as LinePath;
            handleTransform = linePath.transform;

            if (currentTool != Tools.current ||
                currentPivotRotation != Tools.pivotRotation)
            {
                currentTool          = Tools.current;
                currentPivotRotation = Tools.pivotRotation;

                ResetHandleSettings();
            }

            for (int i = 0; i < linePath.PointCount; i++)
            {
                ShowPoint(i);
            }

            for (int i = 1; i < linePath.PointCount; i++)
            {
                Handles.color = Color.white;
                Handles.DrawLine(
                    linePath.transform.TransformPoint(linePath.Points[i]),
                    linePath.transform.TransformPoint(linePath.Points[i - 1]));
            }

            if (linePath.Loop)
            {
                Handles.DrawLine(
                    linePath.transform.TransformPoint(linePath.Points[0]),
                    linePath.transform.TransformPoint(linePath.Points[linePath.Points.Length - 1]));
            }

            Handles.color = Color.green.SetA(0.3f);
            Handles.SphereHandleCap(0, linePath.GetPoint(0f), Quaternion.identity, 0.1f, EventType.Repaint);

            Handles.color = Color.red.SetA(0.3f);
            Handles.SphereHandleCap(0, linePath.GetPoint(1f), Quaternion.identity, 0.1f, EventType.Repaint);

            if (linePath.NormalType == NormalType.Perpendicular)
            {
                for (int i = 0; i < linePath.Normals.Length; i++)
                {
                    Vector3 pos    = linePath.GetPoint(i);
                    Vector3 normal = linePath.GetNormal(i);

                    Handles.color = Color.red;
                    Handles.DrawLine(pos, pos + normal * 0.8f);
                }
            }

            int numIterations = 10 * linePath.PointCount;

            for (int i = 1; i < numIterations; i++)
            {
                float alpha = i / (float)numIterations;

                Vector3 pos    = linePath.GetPoint(alpha);
                Vector3 normal = linePath.GetNormal(alpha);

                Handles.color = Color.green;
                Handles.DrawLine(pos, pos + normal * 0.4f);
            }
        }