Exemplo n.º 1
0
        public Mesh ExtrudeAlong(BezierSpline spline, Vector3 down)
        {
            MeshBuilder meshBuilder = new MeshBuilder();
            meshBuilder.BeforeNext(Matrix4x4.identity, new Vector3());

            Vector3 lastTangent = spline.GetFirstDerivative(0, spline.pts[0]);
            Quaternion quat = Quaternion.LookRotation(lastTangent, -down);
            Vector3 up = down;
            Vertex[] lastVerts = null;

            for(int i = 0; i < spline.pts.Length; i ++)
            {
                BezierSpline.BezierPoint pt = spline.pts[i];
                if (Vector3.Distance(pt.startPos, pt.endPos) > 0.001f)
                {

                    int subdivisions = pt.numSubdivisions;

                    for (int div = 0; div <= subdivisions; div++)
                    {
                        float t = div / (float)pt.numSubdivisions;
                        Vector3 ptT = spline.GetPoint(t, pt);
                        Vector3 tangent = spline.GetFirstDerivative(t, pt).normalized;
                        quat = Quaternion.FromToRotation(lastTangent, tangent) * quat;

                        lastVerts = BuildNextSection(meshBuilder, ptT, tangent, quat, lastVerts);
                        lastTangent = tangent;
                    }
                }
            }

            AddCap(meshBuilder, new Vector3[] { lastVerts[0].position, lastVerts[2].position, lastVerts[4].position, lastVerts[6].position }, true);
            return meshBuilder.DoneCreateMesh();
        }