Exemplo n.º 1
0
    void OnDrawGizmos()
    {
        //Set color depending on selection
        if (Array.IndexOf(UnityEditor.Selection.gameObjects, gameObject) >= 0)
        {
            Gizmos.color = Color.yellow;
        }
        else
        {
            Gizmos.color = new Color(1, 0.6f, 0f);
        }

        //Loop through each curve in spline
        for (int i = 0; i < CurveCount; i++)
        {
            Bezier3DCurve curve = GetCurve(i);

            //Get curve in world space
            Vector3 a, b, c, d;
            a = transform.TransformPoint(curve.a);
            b = transform.TransformPoint(curve.b + curve.a);
            c = transform.TransformPoint(curve.c + curve.d);
            d = transform.TransformPoint(curve.d);

            int     segments = 50;
            float   spacing  = 1f / segments;
            Vector3 prev     = Bezier3DCurve.GetPoint(a, b, c, d, 0f);
            for (int k = 0; k <= segments; k++)
            {
                Vector3 cur = Bezier3DCurve.GetPoint(a, b, c, d, k * spacing);
                Gizmos.DrawLine(prev, cur);
                prev = cur;
            }
        }
    }
Exemplo n.º 2
0
    /// <summary> Return up vector at set distance along the <see cref="Bezier3DSpline"/>. </summary>
    public void GetPoint(float dist, out Vector3 result)
    {
        Bezier3DCurve curve = GetCurveDistance(dist, out dist);

        curve.GetPoint(curve.Dist2Time(dist), out result);
        result = transform.TransformPoint(result);
    }
Exemplo n.º 3
0
    public void InsertKnot(int i, Knot knot)
    {
        Bezier3DCurve curve;

        if (i == 0)
        {
            curve = new Bezier3DCurve(knot.position, knot.handleOut, -curves[0].b, curves[0].a, cacheDensity);
        }
        else if (i == CurveCount)
        {
            curve = GetCurve(i - 1);
        }
        else
        {
            curve = GetCurve(i);
        }

        List <Bezier3DCurve> curveList = new List <Bezier3DCurve>(curves);

        curveList.Insert(i, curve);
        curves = curveList.ToArray();

        autoKnot.Insert(i, knot.auto);
        orientations.Insert(i, knot.orientation);
        SetKnot(i, knot);
    }
Exemplo n.º 4
0
        public override object GetValue(NodePort port)
        {
            object o = base.GetValue(port);

            if (o != null)
            {
                return(o);
            }

            float  resolution = 0;
            object obj        = GetInputByFieldName("resolution").GetInputValue();

            if (obj != null)
            {
                resolution = (float)obj;
            }
            else
            {
                resolution = this.resolution;
            }

            Bezier3DSpline spline;

            if (GetInputByFieldName("spline").TryGetInputValue(out spline))
            {
                List <Vector2> points = new List <Vector2>();

                for (int i = 0; i < spline.KnotCount; i++)
                {
                    Vector3 pos = spline.GetKnot(i).position;
                    points.Add(new Vector2(pos.x, pos.z));

                    Bezier3DCurve curve = spline.GetCurve(i);
                    if (!curve.isLinear)
                    {
                        for (int k = 1; k < resolution; k++)
                        {
                            float t = (float)k / resolution;
                            pos = curve.GetPoint(t);
                            points.Add(new Vector2(pos.x, pos.z));
                        }
                    }
                }
                int[]          tris  = Triangulate(points);
                List <Vector3> verts = points.Select(x => new Vector3(x.x, 0, x.y)).ToList();
                List <Vector3> norms = points.Select(x => Vector3.up).ToList();

                Mesh mesh = new Mesh();
                mesh.vertices  = verts.ToArray();
                mesh.triangles = tris;
                mesh.normals   = norms.ToArray();
                List <Model> output = new List <Model>();
                Material     mat    = GetInputByFieldName("material").GetInputValue() as Material;
                Material[]   mats   = new Material[] { mat };
                output.Add(new Model(mesh, mats));
                return(output);
            }
            return(null);
        }
Exemplo n.º 5
0
    /// <summary> Return point at lerped position where 0 = start, 1 = end </summary>
    public Vector3 GetPointLocal(float dist)
    {
        Bezier3DCurve curve  = GetCurveDistance(dist, out dist);
        Vector3       result = Vector3.zero;

        curve.GetPoint(curve.Dist2Time(dist), out result);
        return(result);
    }
Exemplo n.º 6
0
 /// <summary> Recache all individual curves with new step amount </summary>
 /// <param name="density"> Number of steps per curve </param>
 public void SetCacheDensity(int steps)
 {
     _cacheDensity = steps;
     for (int i = 0; i < CurveCount; i++)
     {
         curves[i] = new Bezier3DCurve(curves[i].a, curves[i].b, curves[i].c, curves[i].d, _cacheDensity);
     }
     _totalLength = GetTotalLength();
 }
Exemplo n.º 7
0
 /// <summary> Flip the spline </summary>
 public void Flip()
 {
     Bezier3DCurve[] curves = new Bezier3DCurve[CurveCount];
     for (int i = 0; i < CurveCount; i++)
     {
         curves[CurveCount - 1 - i] = new Bezier3DCurve(this.curves[i].d, this.curves[i].c, this.curves[i].b, this.curves[i].a, cacheDensity);
     }
     this.curves = curves;
     autoKnot.Reverse();
     orientations.Reverse();
 }
Exemplo n.º 8
0
    public void AddKnot(Knot knot)
    {
        Bezier3DCurve curve = new Bezier3DCurve(curves[CurveCount - 1].d, -curves[CurveCount - 1].c, knot.handleIn, knot.position, cacheDensity);

        List <Bezier3DCurve> curveList = new List <Bezier3DCurve>(curves);

        curveList.Add(curve);
        curves = curveList.ToArray();

        autoKnot.Add(knot.auto);
        orientations.Add(knot.orientation);
        SetKnot(KnotCount - 1, knot);
    }
Exemplo n.º 9
0
    public void RemoveKnot(int i)
    {
        if (i == 0)
        {
            Knot knot = GetKnot(1);

            List <Bezier3DCurve> curveList = new List <Bezier3DCurve>(curves);
            curveList.RemoveAt(0);
            curves = curveList.ToArray();

            autoKnot.RemoveAt(0);
            orientations.RemoveAt(0);

            SetKnot(0, knot);
        }
        else if (i == CurveCount)
        {
            List <Bezier3DCurve> curveList = new List <Bezier3DCurve>(curves);
            curveList.RemoveAt(i - 1);
            curves = curveList.ToArray();

            autoKnot.RemoveAt(i);
            orientations.RemoveAt(i);

            if (autoKnot[KnotCount - 1] != 0)
            {
                SetKnot(KnotCount - 1, GetKnot(KnotCount - 1));
            }
        }
        else
        {
            int preCurveIndex, postCurveIndex;
            GetCurveIndicesForKnot(i, out preCurveIndex, out postCurveIndex);

            Bezier3DCurve curve = new Bezier3DCurve(curves[preCurveIndex].a, curves[preCurveIndex].b, curves[postCurveIndex].c, curves[postCurveIndex].d, cacheDensity);

            curves[preCurveIndex] = curve;

            List <Bezier3DCurve> curveList = new List <Bezier3DCurve>(curves);
            curveList.RemoveAt(postCurveIndex);
            curves = curveList.ToArray();

            autoKnot.RemoveAt(i);
            orientations.RemoveAt(i);

            int preKnotIndex, postKnotIndex;
            GetKnotIndicesForKnot(i, out preKnotIndex, out postKnotIndex);

            SetKnot(preKnotIndex, GetKnot(preKnotIndex));
        }
    }
Exemplo n.º 10
0
        public static void DrawCurveLinesHandles(IBezier3DSplineData splineData, Transform transform = null)
        {
            Handles.color = Color.yellow;

            //Loop through each curve in spline
            var segments = splineData.InterpolationStepsPerCurve;
            var spacing  = 1f / segments;

            for (var i = 0; i < splineData.CurveCount; i++)
            {
                var curve = splineData.GetCurve(i);

                //Get curve in world space
                Vector3 a, b, c, d;

                if (transform != null)
                {
                    a = transform.TransformPoint(curve.StartPoint);
                    b = transform.TransformPoint(curve.FirstHandle + curve.StartPoint);
                    c = transform.TransformPoint(curve.SecondHandle + curve.EndPoint);
                    d = transform.TransformPoint(curve.EndPoint);
                }
                else
                {
                    a = curve.StartPoint;
                    b = curve.FirstHandle + curve.StartPoint;
                    c = curve.SecondHandle + curve.EndPoint;
                    d = curve.EndPoint;
                }

                var prev = Bezier3DCurve.GetPoint(
                    a,
                    b,
                    c,
                    d,
                    0f);

                for (var k = 0; k <= segments; k++)
                {
                    var cur = Bezier3DCurve.GetPoint(
                        a,
                        b,
                        c,
                        d,
                        k * spacing);
                    Handles.DrawLine(prev, cur);
                    prev = cur;
                }
            }
        }
Exemplo n.º 11
0
    /// <summary> Set Knot info in local coordinates </summary>
    public void SetKnot(int i, Knot knot)
    {
        //If knot is set to auto, adjust handles accordingly
        orientations[i] = knot.orientation;
        autoKnot[i]     = knot.auto;
        if (knot.auto != 0)
        {
            AutomateHandles(i, ref knot);
        }

        //Automate knots around this knot
        int preKnotIndex, postKnotIndex;

        GetKnotIndicesForKnot(i, out preKnotIndex, out postKnotIndex);

        Knot preKnot = new Knot();

        if (preKnotIndex != -1)
        {
            preKnot = GetKnot(preKnotIndex);
            if (preKnot.auto != 0)
            {
                int preKnotPreCurveIndex, preKnotPostCurveIndex;
                GetCurveIndicesForKnot(preKnotIndex, out preKnotPreCurveIndex, out preKnotPostCurveIndex);
                if (preKnotPreCurveIndex != -1)
                {
                    AutomateHandles(preKnotIndex, ref preKnot, curves[preKnotPreCurveIndex].a, knot.position);
                    curves[preKnotPreCurveIndex] = new Bezier3DCurve(curves[preKnotPreCurveIndex].a, curves[preKnotPreCurveIndex].b, preKnot.handleIn, preKnot.position, cacheDensity);
                }
                else
                {
                    AutomateHandles(preKnotIndex, ref preKnot, Vector3.zero, knot.position);
                }
            }
        }

        Knot postKnot = new Knot();

        if (postKnotIndex != -1)
        {
            postKnot = GetKnot(postKnotIndex);
            if (postKnot.auto != 0)
            {
                int postKnotPreCurveIndex, postKnotPostCurveIndex;
                GetCurveIndicesForKnot(postKnotIndex, out postKnotPreCurveIndex, out postKnotPostCurveIndex);
                if (postKnotPostCurveIndex != -1)
                {
                    AutomateHandles(postKnotIndex, ref postKnot, knot.position, curves[postKnotPostCurveIndex].d);
                    curves[postKnotPostCurveIndex] = new Bezier3DCurve(postKnot.position, postKnot.handleOut, curves[postKnotPostCurveIndex].c, curves[postKnotPostCurveIndex].d, cacheDensity);
                }
                else
                {
                    AutomateHandles(postKnotIndex, ref postKnot, knot.position, Vector3.zero);
                }
            }
        }

        //Get the curve indices in direct contact with knot
        int preCurveIndex, postCurveIndex;

        GetCurveIndicesForKnot(i, out preCurveIndex, out postCurveIndex);

        //Adjust curves in direct contact with the knot
        if (preCurveIndex != -1)
        {
            curves[preCurveIndex] = new Bezier3DCurve(preKnot.position, preKnot.handleOut, knot.handleIn, knot.position, cacheDensity);
        }
        if (postCurveIndex != -1)
        {
            curves[postCurveIndex] = new Bezier3DCurve(knot.position, knot.handleOut, postKnot.handleIn, postKnot.position, cacheDensity);
        }

        _totalLength = GetTotalLength();
    }
Exemplo n.º 12
0
    /// <summary> Return point at lerped position where 0 = start, 1 = end </summary>
    public Vector3 GetPointLocal(float dist)
    {
        Bezier3DCurve curve = GetCurveDistance(dist, out dist);

        return(curve.GetPoint(curve.Dist2Time(dist)));
    }
Exemplo n.º 13
0
    /// <summary> Return up vector at set distance along the <see cref="Bezier3DSpline"/>. </summary>
    public Vector3 GetPoint(float dist)
    {
        Bezier3DCurve curve = GetCurveDistance(dist, out dist);

        return(transform.TransformPoint(curve.GetPoint(curve.Dist2Time(dist))));
    }
Exemplo n.º 14
0
    /// <summary> Return forward vector at set distance along the <see cref="Bezier3DSpline"/> in local coordinates. Uses approximation. </summary>
    public Vector3 GetForwardLocalFast(float dist)
    {
        Bezier3DCurve curve = GetCurveDistance(dist, out dist);

        return(curve.GetForwardFast(curve.Dist2Time(dist)));
    }
Exemplo n.º 15
0
    void DrawSelectedSplitters()
    {
        Handles.color = Color.white;
        //Start add
        if (!spline.closed && activeKnot == 0)
        {
            Bezier3DCurve curve = spline.GetCurve(0);
            Vector3
                a = spline.transform.TransformPoint(curve.a),
                b = spline.transform.TransformDirection(curve.b.normalized) * 2f;

            float handleScale = HandleUtility.GetHandleSize(a);
            b *= handleScale;
            Handles.DrawDottedLine(a, a - b, 3f);
            if (Handles.Button(a - b, Camera.current.transform.rotation, handleScale * handleSize * 0.4f, handleScale * handleSize * 0.4f, Handles.DotHandleCap))
            {
                Undo.RecordObject(spline, "Add Bezier Point");
                Bezier3DSpline.Knot knot = spline.GetKnot(activeKnot);
                spline.InsertKnot(0, new Bezier3DSpline.Knot(curve.a - (curve.b.normalized * handleScale * 2), Vector3.zero, curve.b.normalized * 0.5f, knot.auto, knot.orientation));
                if (onUpdateSpline != null)
                {
                    onUpdateSpline(spline);
                }
            }
        }

        //End add
        if (!spline.closed && activeKnot == spline.CurveCount)
        {
            Bezier3DCurve curve = spline.GetCurve(spline.CurveCount - 1);
            Vector3
                c             = spline.transform.TransformDirection(curve.c.normalized) * 2f,
                d             = spline.transform.TransformPoint(curve.d);
            float handleScale = HandleUtility.GetHandleSize(d);
            c *= handleScale;
            Handles.DrawDottedLine(d, d - c, 3f);
            if (Handles.Button(d - c, Camera.current.transform.rotation, handleScale * handleSize * 0.4f, handleScale * handleSize * 0.4f, Handles.DotHandleCap))
            {
                Undo.RecordObject(spline, "Add Bezier Point");
                Bezier3DSpline.Knot knot = spline.GetKnot(activeKnot);
                spline.AddKnot(new Bezier3DSpline.Knot(curve.d - (curve.c.normalized * handleScale * 2), curve.c.normalized * 0.5f, Vector3.zero, knot.auto, knot.orientation));
                SelectKnot(spline.CurveCount, false);
                if (onUpdateSpline != null)
                {
                    onUpdateSpline(spline);
                }
            }
        }

        // Prev split
        if (spline.closed || activeKnot != 0)
        {
            Bezier3DCurve curve       = spline.GetCurve(activeKnot == 0 ? spline.CurveCount - 1 : activeKnot - 1);
            Vector3       centerLocal = curve.GetPoint(curve.Dist2Time(curve.length * 0.5f));
            Vector3       center      = spline.transform.TransformPoint(centerLocal);

            Vector3 a           = curve.a + curve.b;
            Vector3 b           = curve.c + curve.d;
            Vector3 ab          = (b - a) * 0.3f;
            float   handleScale = HandleUtility.GetHandleSize(center);

            if (Handles.Button(center, Camera.current.transform.rotation, handleScale * handleSize * 0.4f, handleScale * handleSize * 0.4f, Handles.DotHandleCap))
            {
                Undo.RecordObject(spline, "Add Bezier Point");
                Bezier3DSpline.Knot knot = spline.GetKnot(activeKnot);
                spline.InsertKnot(activeKnot == 0 ? spline.CurveCount : activeKnot, new Bezier3DSpline.Knot(centerLocal, -ab, ab, knot.auto, knot.orientation));
                if (activeKnot == 0)
                {
                    SelectKnot(spline.CurveCount - 1, false);
                }
                if (onUpdateSpline != null)
                {
                    onUpdateSpline(spline);
                }
            }
        }

        // Next split
        if (activeKnot != spline.CurveCount)
        {
            Bezier3DCurve curve       = spline.GetCurve(activeKnot);
            Vector3       centerLocal = curve.GetPoint(curve.Dist2Time(curve.length * 0.5f));
            Vector3       center      = spline.transform.TransformPoint(centerLocal);

            Vector3 a           = curve.a + curve.b;
            Vector3 b           = curve.c + curve.d;
            Vector3 ab          = (b - a) * 0.3f;
            float   handleScale = HandleUtility.GetHandleSize(center);
            if (Handles.Button(center, Camera.current.transform.rotation, handleScale * handleSize * 0.4f, handleScale * handleSize * 0.4f, Handles.DotHandleCap))
            {
                Undo.RecordObject(spline, "Add Bezier Point");
                spline.InsertKnot(activeKnot + 1, new Bezier3DSpline.Knot(centerLocal, -ab, ab));
                SelectKnot(activeKnot + 1, false);
                if (onUpdateSpline != null)
                {
                    onUpdateSpline(spline);
                }
            }
        }
    }
Exemplo n.º 16
0
    /// <summary> Return point at lerped position where 0 = start, 1 = end </summary>
    public void GetPointLocal(float dist, out Vector3 result)
    {
        Bezier3DCurve curve = GetCurveDistance(dist, out dist);

        curve.GetPoint(curve.Dist2Time(dist), out result);
    }