Exemplo n.º 1
0
        /**
         * <summary>
         * v1s = start of segment 1
         * v1e = end of segment 1
         * v2s = start of segment 2
         * v2e = end of segment 2
         * mv1e = pulled back position on v1-v2
         * mvs2 = pulled back position on v2-v3
         * n1 = planar normal of v1
         * n2 = planar normal of v2
         * </summary>
         * */
        public void CreateSmoothConnection(Vector3 v1s, Vector3 v1e, Vector3 v2s, Vector3 v2e,
                                           Vector3 mv1e, Vector3 mv2s,
                                           Vector3 n1, Vector3 n2)
        {
            float numdivs = 5;
            float step    = 1.0f / numdivs;

            //ds(v1s, "v1s");
            //ds(v1e, "v1e");

            //ds(v2s, "v2s");
            //ds(v2e, "v2e");

            // ds(mv1e, "mv1e");
            // ds(mv2s, "mv2s");

            //ds(mv1e + n1, "mv1e+n1");
            //ds(mv2s + n2, "mv2s+n2");

            Vector3 i1;
            Vector3 dir1 = (v1e - v1s);
            Vector3 dir2 = (v2e - v2s);

            //we use the 2d version of the vectors
            bool b = MathTools.LineLineIntersection(out i1, v1s + n1, dir1, v2s + n2, dir2);

            if (b == false)
            {
                CreateConnection(mv1e, mv2s, n1, n2);
                return;
            }
            //Debug.Log(b);
            //Debug.Log(i1);

            //ds(i1, "i1");

            Vector3 i2;

            dir1 = (v1e - v1s);
            dir2 = (v2e - v2s);
            b    = MathTools.LineLineIntersection(out i2, v1s - n1, dir1, v2s - n2, dir2);

            //inner curve
            Bezier bez1 = new Bezier(mv1e + n1, i1, i1, mv2s + n2);
            //outer curve
            Bezier bez2 = new Bezier(mv1e - n1, i2, i2, mv2s - n2);

            for (float f = 0; f < 1; f += step)
            {
                Vector3 pt1 = bez1.GetPointAtTime(f);
                //ds(pt1, "bezpt1");
                Vector3 pt2 = bez2.GetPointAtTime(f);
                //ds(pt2, "bezpt2");

                Vector3 pt2e = bez2.GetPointAtTime(f + step);
                //ds(pt2e, "bezpt2e");

                Vector3 pt1e = bez1.GetPointAtTime(f + step);
                // ds(pt1e, "bezpt1e");

                //tri1
                v.Add(pt1);
                t.Add(v.Count - 1);
                u.Add(new Vector2(1, f + step));

                v.Add(pt2e);
                t.Add(v.Count - 1);
                u.Add(new Vector2(0, f));

                v.Add(pt2);
                t.Add(v.Count - 1);
                u.Add(new Vector2(0, f + step));

                //tri 2
                v.Add(pt1e);
                t.Add(v.Count - 1);
                u.Add(new Vector2(0, f));

                v.Add(pt2e);
                t.Add(v.Count - 1);
                u.Add(new Vector2(1, f));

                v.Add(pt1);
                t.Add(v.Count - 1);
                u.Add(new Vector2(0, f + step));
            }

            /*
             *
             * b = MathTools.LineLineIntersection(out i1, v1s - n1, dir1, v2s - n2, dir2);
             * Debug.Log(i1);
             *
             * go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
             * go.transform.position = i1;
             * go.transform.parent = this.go.transform;
             */
        }