Example #1
0
        private void ResetHandleSettings()
        {
            if (selectedIndex == -1)
            {
                return;
            }

            handleScale = Vector3.one;

            Vector3 point = spline.GetControlPoint(selectedIndex);

            if (selectedIndex % 3 == 0)
            {
                Vector3 normal = spline.GetControlNormal(selectedIndex);

                Vector3 guidePos = Vector3.zero;
                if (selectedIndex == 0)
                {
                    guidePos = spline.GetControlPoint(selectedIndex + 1);
                }
                else
                {
                    guidePos = spline.GetControlPoint(selectedIndex - 1);
                }

                Vector3 toGuide = (guidePos - point).normalized;

                Vector3 right = Vector3.Cross(normal, toGuide).normalized;
                normal = Vector3.Cross(-right, toGuide).normalized;

                spline.Normals[selectedIndex] = spline.transform.InverseTransformDirection(normal);

                handleRotation = Quaternion.LookRotation(toGuide, normal);

                if (spline.GetControlPointMode(selectedIndex) == BezierControlPointMode.Free)
                {
                    handleRotation = Quaternion.LookRotation(normal);
                    Quaternion offsetRot = Quaternion.AngleAxis(90f, handleRotation * Vector3.right);
                    handleRotation = offsetRot * handleRotation;
                }
            }
            else
            {
                int nodeIndex = 0;

                if ((selectedIndex - 1) % 3 == 0)                // after node
                {
                    nodeIndex = selectedIndex - 1;
                }
                else
                {
                    nodeIndex = selectedIndex + 1;
                }

                Vector3 normal = spline.GetControlNormal(nodeIndex);

                Vector3 cp = spline.GetControlPoint(nodeIndex);
                handleRotation = Quaternion.LookRotation(point - cp, normal);
            }

            if (Tools.pivotRotation == PivotRotation.Global)
            {
                handleRotation = Quaternion.identity;
            }
        }
Example #2
0
        protected virtual void OnSceneGUI()
        {
            spline          = target as SplinePath;
            handleTransform = spline.transform;

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

                ResetHandleSettings();
            }

            Vector3 p0 = ShowPoint(0);

            for (int i = 1; i < spline.ControlPointCount; i += 3)
            {
                Vector3 p1 = ShowPoint(i);
                Vector3 p2 = ShowPoint(i + 1);
                Vector3 p3 = ShowPoint(i + 2);

                Handles.color = Color.gray;
                Handles.DrawLine(p0, p1);
                Handles.DrawLine(p2, p3);

                Handles.DrawBezier(p0, p3, p1, p2, Color.white, null, 2f);
                p0 = p3;
            }

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

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

            if (spline.NormalType == NormalType.Perpendicular)
            {
                for (int i = 0; i < spline.Normals.Length; i++)
                {
                    if (i % 3 == 0)
                    {
                        Vector3 pos    = spline.GetControlPoint(i);
                        Vector3 normal = spline.GetControlNormal(i);

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

            int numIterations = 10 * spline.ControlPointCount;

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

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

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