Exemplo n.º 1
0
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        Bifurcation bifurcation = (Bifurcation)target;

        if (GUILayout.Button("ReCreateGeometry"))
        {
            bifurcation.Load();
        }
        if (GUILayout.Button("Clear"))
        {
            bifurcation.ClearCurve();
        }
    }
    void ReallocateNodeOverSpline(Transform node, BezierSpline spline, int depth)
    {
        float newPosition;

        // Collide with spline that can belong to 2 possible objects, biffurcation or curve
        Curve       curve       = spline.transform.parent.GetComponent <Curve>();
        Bifurcation bifurcation = spline.transform.parent.GetComponent <Bifurcation>();

        if (curve != null)
        {// we are in a curve
            Debug.DrawLine(transform.position, nodeToFollow.position, Color.cyan);
        }

        if (bifurcation != null)
        {// we are in a bifurcation
            if (bifurcation.splines[0].isPath)
            {
                spline = bifurcation.splines[0];
            }
            else
            {
                spline = bifurcation.splines[1];
            }
        }

        newPosition           = spline.GetClosestPoint(transform.position + transform.forward * distToNode, 0.01f);
        nodeToFollow.position = spline.GetPoint(newPosition);
        nodeToFollow.rotation = spline.GetOrientation(newPosition, transform.up);

        // end or beggining of spline reached, check next/previous spline
        if (newPosition == 2)
        {     // jump to previous spline
            if (depth == 1)
            { // avoid infinite recursive loops
                nodeToFollow.position = spline.startNode.transform.position;
                nodeToFollow.rotation = spline.startNode.transform.rotation;
                return;
            }

            RaycastHit hit;

            Vector3 dir = -spline.startNode.transform.forward;
            //if (spline.startNode.reverse == true) dir *= -1;

            if (Physics.Raycast(spline.startNode.position + dir + new Vector3(0, 2, 0), -transform.up, out hit, 30))
            {
                spline = hit.collider.transform.GetComponent <BezierSpline>();
            }
            ReallocateNodeOverSpline(nodeToFollow, spline, 1);
        }
        if (newPosition == 3)
        {     // jump to next spline
            if (depth == 1)
            { // avoid infinite recursive loops
                nodeToFollow.position = spline.endNode.transform.position;
                nodeToFollow.rotation = spline.endNode.transform.rotation;
                return;
            }

            RaycastHit hit;

            Vector3 dir = spline.endNode.transform.forward;
            //if (spline.endNode.reverse == true)
            //    dir *= -1;

            if (Physics.Raycast(spline.endNode.position + dir + new Vector3(0, 2, 0), -transform.up, out hit, 30))
            {
                spline = hit.collider.transform.GetComponent <BezierSpline>();
            }

            ReallocateNodeOverSpline(nodeToFollow, spline, 1);
        }
    }
Exemplo n.º 3
0
    void ChangeToNextSpline()
    {
        TrackElement currentCurve = currentSpline.curve;

        if (currentSpline.curve.GetType() == typeof(Curve))
        {// If we are in a Curve
            int currentSplineIndex = currentCurve.splines.IndexOf(currentSpline);

            if (!reverse)
            {
                // If there are more splines in the current curve, change to next
                if (currentSplineIndex + 1 <= currentCurve.splines.Count - 1)
                {
                    currentSpline = currentCurve.splines[currentSplineIndex + 1];
                    t             = t - 1;
                    return;
                }
                else
                {     // Jump to next TrackElement
                    if (((Curve)currentCurve).nextCurve.GetType() == typeof(Curve))
                    { // Jump from curve to curve
                        Curve nextCurve = (Curve)((Curve)currentCurve).nextCurve;
                        if (nextCurve.nextCurve == currentCurve)
                        {// --->/<---
                            currentSpline = nextCurve.splines[nextCurve.splines.Count - 1];
                            reverse       = true;
                            // Update t
                            t = 1 - (t - 1);
                        }
                        else if (nextCurve.previousCurve == currentCurve)
                        {// --->/--->
                            currentSpline = nextCurve.splines[0];
                            reverse       = false;
                            // Update t
                            t = t - 1;
                        }
                    }

                    if (((Curve)currentCurve).nextCurve.GetType() == typeof(Bifurcation))
                    {// Jump from curve to bifurcation.
                        Bifurcation nextBifurcation = (Bifurcation)((Curve)currentCurve).nextCurve;

                        if (nextBifurcation.nextCurveRight == currentCurve)
                        {// --->/---> (From Right path to Start)
                            currentSpline = nextBifurcation.splines[0];
                            reverse       = true;
                            // Update t
                            t = 1 - (t - 1);
                        }
                        else if (nextBifurcation.nextCurveLeft == currentCurve)
                        {// --->/---> (From Left path to Start)
                            currentSpline = nextBifurcation.splines[1];
                            reverse       = true;
                            // Update t
                            t = 1 - (t - 1);
                        }

                        // Here we are sure that we are entering the bifurcation from the start node.
                        else if (nextBifurcation.splines[0].isPath)
                        {// --->/---> (From Start to Right path)
                            currentSpline = nextBifurcation.splines[0];
                            reverse       = false;
                            // Update t
                            t = t - 1;
                        }
                        else if (nextBifurcation.splines[1].isPath)
                        {// --->\---> (From Start to Left path)
                            currentSpline = nextBifurcation.splines[1];
                            reverse       = false;
                            // Update t
                            t = t - 1;
                        }
                    }
                }
            }
            else // reverse mode
            {
                // If there are more splines in the current curve, change to previous spline
                if (currentSplineIndex - 1 >= 0)
                {
                    currentSpline = currentCurve.splines[currentSplineIndex - 1];
                    t             = 1 + t;
                    return;
                }
                else
                {     // Jump to next TrackElement
                    if (((Curve)currentCurve).previousCurve.GetType() == typeof(Curve))
                    { // Jump to curve
                        Curve previousCurve = (Curve)((Curve)currentCurve).previousCurve;
                        if (previousCurve.nextCurve == currentCurve)
                        {// <---/<---
                            currentSpline = previousCurve.splines[((Curve)currentCurve).previousCurve.splines.Count - 1];
                            reverse       = true;
                            // Update t
                            t = t + 1;
                        }
                        else if (previousCurve.previousCurve == currentCurve)
                        {// <---/--->
                            currentSpline = previousCurve.splines[0];
                            reverse       = false;
                            // Update t
                            t = 1 - (t + 1);
                        }
                    }
                    if (((Curve)currentCurve).previousCurve.GetType() == typeof(Bifurcation))
                    {// Jump from curve to bifurcation.
                        Bifurcation nextBifurcation = (Bifurcation)(((Curve)currentCurve).previousCurve);

                        if (nextBifurcation.nextCurveRight == currentCurve)
                        {// --->/---> (From Right path to Start)
                            currentSpline = nextBifurcation.splines[0];
                            reverse       = true;
                            // Update t
                            t = 1 + t;
                        }
                        else if (nextBifurcation.nextCurveLeft == currentCurve)
                        {// --->\---> (From Left path to Start)
                            currentSpline = nextBifurcation.splines[1];
                            reverse       = true;
                            // Update t
                            t = 1 + t;
                        }
                        else if (nextBifurcation.previousCurve == currentCurve)
                        {     // --->\---> (From Start to..)
                            if (nextBifurcation.splines[0].isPath)
                            { // --->/---> (From Start to Right path)
                                currentSpline = nextBifurcation.splines[0];
                                reverse       = false;
                                // Update t
                                t = 1 - (1 - t);
                            }
                            else if (nextBifurcation.splines[1].isPath)
                            {// --->\---> (From Start to Left path)
                                currentSpline = nextBifurcation.splines[1];
                                reverse       = false;
                                // Update t
                                t = 1 - (1 - t);
                            }
                        }
                    }
                }
            }
        }
        else if (currentSpline.curve.GetType() == typeof(Bifurcation))
        {// If we are in a Bifurcation
            Bifurcation bifurcation = (Bifurcation)currentSpline.curve;
            if (!reverse)
            {
                // Start to Right
                if (currentSpline == bifurcation.splines[0] && bifurcation.nextCurveRight.previousCurve == bifurcation)
                {     // Start to Straight Right
                    if (bifurcation.nextCurveRight.GetType() == typeof(Bifurcation))
                    { // bifurcation to bifurcation
                        Bifurcation nextBifurcation = (Bifurcation)(bifurcation.nextCurveRight);
                        if (nextBifurcation.splines[0].isPath)
                        {
                            currentSpline = nextBifurcation.splines[0];
                        }
                        else if (nextBifurcation.splines[1].isPath)
                        {
                            currentSpline = nextBifurcation.splines[1];
                        }

                        reverse = false;
                        // Update t
                        t = t - 1;
                    }
                    else
                    {// bifurcation to Straight curve
                        currentSpline = bifurcation.nextCurveRight.splines[0];
                        reverse       = false;
                        // Update t
                        t = t - 1;
                    }
                }
                else if (currentSpline == bifurcation.splines[0])
                {// Start to Reverse Curve
                    currentSpline = bifurcation.nextCurveRight.splines[bifurcation.nextCurveRight.splines.Count - 1];
                    reverse       = true;
                    // Update t
                    t = 1 - (1 - t);
                }

                // Start to Left
                if (currentSpline == bifurcation.splines[1] && bifurcation.nextCurveLeft.previousCurve == bifurcation)
                {     // Start to Straight Left
                    if (bifurcation.nextCurveLeft.GetType() == typeof(Bifurcation))
                    { // bifurcation to bifurcation
                        Bifurcation nextBifurcation = (Bifurcation)(bifurcation.nextCurveLeft);
                        if (nextBifurcation.splines[0].isPath)
                        {
                            currentSpline = nextBifurcation.splines[0];
                        }
                        else if (nextBifurcation.splines[1].isPath)
                        {
                            currentSpline = nextBifurcation.splines[1];
                        }

                        reverse = false;
                        // Update t
                        t = t - 1;
                    }
                    else
                    {// bifurcation to Straight curve
                        currentSpline = bifurcation.nextCurveLeft.splines[0];
                        reverse       = false;
                        // Update t
                        t = t - 1;
                    }
                }
                else if (currentSpline == bifurcation.splines[1])
                {// Start to Reverse Curve
                    currentSpline = bifurcation.nextCurveLeft.splines[bifurcation.nextCurveLeft.splines.Count - 1];
                    reverse       = true;
                    // Update t
                    t = 1 - (1 - t);
                }
            }
            else
            {     // Reverse mode
                if (bifurcation.previousCurve.GetType() == typeof(Bifurcation))
                { // back to another bifurcation
                    Bifurcation previousBif = (Bifurcation)bifurcation.previousCurve;
                    if (previousBif.nextCurveRight == bifurcation)
                    {
                        currentSpline = previousBif.splines[0];
                    }
                    if (previousBif.nextCurveLeft == bifurcation)
                    {
                        currentSpline = previousBif.splines[1];
                    }

                    reverse = true;
                    t       = t + 1;
                }
                else
                {// back to a Curve
                    currentSpline = bifurcation.previousCurve.splines[bifurcation.previousCurve.splines.Count - 1];
                    reverse       = true;
                    t             = t + 1;
                }
            }
        }

        return;
    }