Example #1
0
    // Get the different id sections
    public void FindSections(MegaLoftSection lsection, MegaSpline spline)
    {
        if (spline != null)
        {
            lsection.meshsections.Clear();

            int id = spline.knots[0].id - 1;

            MegaMeshSection msect = null;

            for (int i = 0; i < spline.knots.Count; i++)
            {
                if (spline.knots[i].id != id)
                {
                    id = spline.knots[i].id;
                    if (msect != null)
                    {
                        msect.lastknot = i;
                    }

                    msect = new MegaMeshSection();

                    msect.firstknot = i;
                    msect.mat       = FindMaterial(id);

                    lsection.meshsections.Add(msect);
                }
            }

            lsection.meshsections[lsection.meshsections.Count - 1].lastknot = spline.knots.Count - 1;
        }
    }
Example #2
0
 public override void CopyVertData(ref Vector3[] verts, ref Vector2[] uvs, int offset)
 {
     for (int i = 0; i < meshsections.Count; i++)
     {
         MegaMeshSection ms = meshsections[i];
         Array.Copy(ms.verts.ToArray(), 0, verts, offset, ms.verts.Count);
         Array.Copy(ms.uvs.ToArray(), 0, uvs, offset, ms.uvs.Count);
         offset += ms.verts.Count;
     }
 }
Example #3
0
    public override void CopyVertData(ref Vector3[] verts, ref Vector2[] uvs, ref Color[] cols, int offset)
    {
        MegaLoftSection ls = loftsections[0];

        for (int i = 0; i < ls.meshsections.Count; i++)
        {
            MegaMeshSection ms = ls.meshsections[i];
            Array.Copy(ms.verts.ToArray(), 0, verts, offset, ms.verts.Count);
            Array.Copy(ms.uvs.ToArray(), 0, uvs, offset, ms.uvs.Count);
            Array.Copy(ms.cols.ToArray(), 0, cols, offset, ms.cols.Count);
            offset += ms.verts.Count;
        }
    }
Example #4
0
    void CalcBounds()
    {
        conminz = float.MaxValue;

        for (int i = 0; i < meshsections.Count; i++)
        {
            MegaMeshSection ms = meshsections[i];

            for (int v = 0; v < ms.verts1.Count; v++)
            {
                if (ms.verts1[v].y < conminz)
                {
                    conminz = ms.verts1[v].y;
                }
            }
        }
    }
Example #5
0
    void InitConform()
    {
        for (int m = 0; m < meshsections.Count; m++)
        {
            MegaMeshSection ms = meshsections[m];

            ms.offsets = new float[ms.verts1.Count];
            ms.last    = new float[ms.verts1.Count];

            for (int i = 0; i < ms.verts1.Count; i++)
            {
                ms.offsets[i] = ms.verts1[i].y - conminz;
            }
        }

        // If loft has changed we need to update bounds, could do anyway in builder
        // Only need to do this if target changes, move to SetTarget
        if (target)
        {
            conformCollider = target.GetComponent <Collider>();
        }
    }
Example #6
0
    // Get the different id sections
    void FindSections(MegaSpline spline)
    {
        if (spline != null)
        {
            meshsections.Clear();

            int id = spline.knots[0].id - 1;

            for (int i = 0; i < spline.knots.Count; i++)
            {
                if (spline.knots[i].id != id)
                {
                    id = spline.knots[i].id;
                    MegaMeshSection msect = new MegaMeshSection();

                    msect.mat = FindMaterial(id);

                    meshsections.Add(msect);
                }
            }
        }
    }
Example #7
0
    public override int BuildMesh(MegaShapeLoft loft, int triindex)
    {
        trisstart = triindex;

        if (Lock)
        {
            return(triindex + ((crosses - 2) * (evert - svert)));
        }

        if (layerPath == null || layerSection == null)
        {
            return(triindex);
        }

        MegaSpline pathspline    = layerPath.splines[curve];
        MegaSpline sectionspline = layerSection.splines[crosscurve];

        // so for each loft section run through
        int     vi  = 0;
        Vector2 uv  = Vector2.zero;
        Vector3 p   = Vector3.zero;
        Vector3 scl = Vector3.one;

        float scalemultx = 1.0f;
        float scalemulty = 1.0f;

        Vector3 cmax = crossmax;

        cmax.x = 0.0f;
        cmax.z = 0.0f;

        Vector3 cmin = crossmin;

        cmin.x = 0.0f;
        cmin.z = 0.0f;

        Vector3 totaloff = Vector3.zero;

        float uvstart = pathStart;

        if (UVOrigin == MegaLoftUVOrigin.SplineStart)
        {
            uvstart = 0.0f;
        }

        Matrix4x4 twisttm = Matrix4x4.identity;

        Color col1 = color;

        Matrix4x4 tm;
        Vector3   lastup = loft.up;

        float calpha = 0.0f;

        for (int cr = 0; cr < crosses; cr++)
        {
            float a     = ((float)cr / (float)(crosses - 1));
            float alpha = pathStart + (pathLength * a);

            totaloff = offset;

            if (useOffsetX)
            {
                totaloff.x += offsetCrvX.Evaluate(alpha);
            }

            if (useOffsetY)
            {
                totaloff.y += offsetCrvY.Evaluate(alpha);
            }

            if (useOffsetZ)
            {
                totaloff.z += offsetCrvZ.Evaluate(alpha);
            }

            // get the point on the spline
            if (frameMethod == MegaFrameMethod.New)
            {
                tm = loft.GetDeformMatNewMethod(pathspline, alpha, true, alignCross, ref lastup);
            }
            else
            {
                tm = loft.GetDeformMatNew(pathspline, alpha, true, alignCross);
            }

            if (useTwistCrv)
            {
                float twist = twistCrv.Evaluate(alpha) * twistAmt;

                float tw1 = pathspline.GetTwist(alpha);
                MegaShapeUtils.RotateZ(ref twisttm, Mathf.Deg2Rad * (twist - tw1));
                tm = tm * twisttm;
            }

            if (useCrossScaleCrv)
            {
                float sa = Mathf.Repeat(a + scaleoff, 1.0f);
                scalemultx = crossScaleCrv.Evaluate(sa);
            }

            if (!sepscale)
            {
                scalemulty = scalemultx;
            }
            else
            {
                if (useCrossScaleCrvY)
                {
                    float sa = Mathf.Repeat(a + scaleoffY, 1.0f);
                    scalemulty = crossScaleCrvY.Evaluate(sa);
                }
            }

            scl.x = crossScale.x * scalemultx;                  // Use plus here and have curve as 0010
            scl.y = crossScale.y * scalemulty;

            Vector3 crrot = cmax;
            crrot.y *= scl.y;

            Vector3 cminrot = cmin;
            cminrot.y *= scl.y;

            // Now need to loop through all the meshsections
            for (int m = 0; m < meshsections.Count; m++)
            {
                MegaMeshSection     ms   = meshsections[m];
                MegaMaterialSection mats = sections[ms.mat];

                if (mats.Enabled)
                {
                    if (loft.useColors)
                    {
                        if (mats.colmode == MegaLoftColMode.Loft)
                        {
                            calpha = a;
                        }
                        else
                        {
                            calpha = alpha;
                        }

                        calpha = Mathf.Repeat(calpha + mats.coloffset, 1.0f);
                        col1.r = mats.colR.Evaluate(calpha);
                        col1.g = mats.colG.Evaluate(calpha);
                        col1.b = mats.colB.Evaluate(calpha);
                        col1.a = mats.colA.Evaluate(calpha);
                    }

                    for (int v = 0; v < ms.cverts.Count; v++)
                    {
                        p.x = ms.cverts[v].x * scl.x;
                        p.y = ms.cverts[v].y * scl.y;                           // Curve for this value
                        p.z = ms.cverts[v].z * scl.z;

                        p = tm.MultiplyPoint3x4(p);

                        uv.x = alpha - uvstart;                         //pathStart;
                        uv.y = ms.cuvs[v].y;                            // - crossStart;	// again not sure here start;

                        if (mats.physuv)
                        {
                            uv.x *= pathspline.length;
                            uv.y *= sectionspline.length;
                        }
                        else
                        {
                            if (mats.uvcalcy)
                            {
                                //uv.x = ((a * LoftLength) / sectionspline.length) - uvstart;
                                uv.x = ((alpha * pathspline.length) / sectionspline.length) - uvstart;
                            }
                        }

                        if (conform)
                        {
                            ms.verts1.Add(p + totaloff);
                        }
                        else
                        {
                            ms.verts.Add(p + totaloff);
                        }

                        if (mats.swapuv)
                        {
                            float ux = uv.x;
                            uv.x = uv.y;
                            uv.y = ux;
                        }

                        uv.x *= mats.UVScale.x;
                        uv.y *= mats.UVScale.y;

                        uv.x += mats.UVOffset.x;
                        uv.y += mats.UVOffset.y;

                        ms.uvs.Add(uv);                         //[vi] = uv;

                        if (loft.useColors)
                        {
                            ms.cols.Add(col1);
                        }

                        vi++;
                    }
                }
            }
        }

        //OptmizeMesh();
        // Faces
        int index = triindex;
        int fi    = 0;          // Calc this

        if (enabled)
        {
            if (flip)
            {
                for (int m = 0; m < meshsections.Count; m++)
                {
                    MegaMeshSection     ms   = meshsections[m];
                    MegaMaterialSection mats = sections[ms.mat];

                    if (mats.Enabled)
                    {
                        for (int cr = 0; cr < crosses - 1; cr++)
                        {
                            for (int f = 0; f < ms.cverts.Count - 1; f++)
                            {
                                ms.tris.Add(index + f);
                                ms.tris.Add(index + f + 1);
                                ms.tris.Add(index + f + 1 + ms.cverts.Count);

                                ms.tris.Add(index + f);
                                ms.tris.Add(index + f + 1 + ms.cverts.Count);
                                ms.tris.Add(index + f + ms.cverts.Count);

                                fi += 6;
                            }

                            index += ms.cverts.Count;
                        }
                        index += ms.cverts.Count;
                    }
                }
            }
            else
            {
                for (int m = 0; m < meshsections.Count; m++)
                {
                    MegaMeshSection     ms   = meshsections[m];
                    MegaMaterialSection mats = sections[ms.mat];

                    if (mats.Enabled)
                    {
                        for (int cr = 0; cr < crosses - 1; cr++)
                        {
                            for (int f = 0; f < ms.cverts.Count - 1; f++)
                            {
                                ms.tris.Add(index + f + 1 + ms.cverts.Count);
                                ms.tris.Add(index + f + 1);
                                ms.tris.Add(index + f);

                                ms.tris.Add(index + f + ms.cverts.Count);
                                ms.tris.Add(index + f + 1 + ms.cverts.Count);
                                ms.tris.Add(index + f);

                                fi += 6;
                            }

                            index += ms.cverts.Count;
                        }

                        index += ms.cverts.Count;
                    }
                }
            }
        }

        if (conform)
        {
            CalcBounds();
            DoConform(loft);
        }

        return(triindex + fi);          //triindex;
    }
Example #8
0
    public override Vector3 GetPos1(MegaShapeLoft loft, float ca, float a, float at, out Vector3 p, out Vector3 up)
    {
        MegaMeshSection ms = null;

        ca = Mathf.Clamp(ca, 0.0f, 0.9999f);

        for (int i = 0; i < meshsections.Count; i++)
        {
            ms = meshsections[i];
            if (ca >= ms.castart && ca <= ms.caend)
            {
                break;
            }
        }

        ca = (ca - ms.castart) / (ms.caend - ms.castart);

        ca = Mathf.Repeat(ca, 1.0f);
        a  = Mathf.Repeat(a, 1.0f);

        float findex = (ms.cverts.Count - 1) * ca;
        int   cindex = (int)findex;
        float interp = findex - cindex;

        int cindex1 = cindex + 1;

        int   pindex;
        int   pindex1;
        bool  flip = false;
        float pinterp;

        if (pathLength < 0.9999f || layerPath.splines[curve].closed == false)
        {
            if (a < 0.0f)
            {
                pindex  = 0;
                pinterp = a * GetLength(loft);
                pindex1 = pindex + 1;
            }
            else
            {
                if (a >= 0.999f)
                {
                    pindex  = crosses - 2;
                    pindex1 = crosses - 1;
                    pinterp = (a - 1.0f) * GetLength(loft);
                    flip    = true;
                }
                else
                {
                    float pfindex = (crosses - 1) * a;
                    pindex  = (int)pfindex;
                    pinterp = pfindex - pindex;
                    pindex1 = pindex + 1;
                }
            }
        }
        else
        {
            a = Mathf.Repeat(a, 0.9999f);

            float pfindex = (float)(crosses - 1) * a;
            pindex  = (int)pfindex;
            pinterp = pfindex - pindex;

            pindex1 = pindex + 1;
        }

        Vector3 p1 = ms.verts[(pindex * ms.cverts.Count) + cindex];
        Vector3 p2 = ms.verts[(pindex * ms.cverts.Count) + cindex1];
        Vector3 p3 = ms.verts[(pindex1 * ms.cverts.Count) + cindex];
        Vector3 p4 = ms.verts[(pindex1 * ms.cverts.Count) + cindex1];

        Vector3 pm1   = Vector3.Lerp(p1, p2, interp);
        Vector3 pm2   = Vector3.Lerp(p3, p4, interp);
        Vector3 delta = pm2 - pm1;

        float pa = pinterp + at;

        // Quick calc of face normal
        up = Vector3.Lerp(ms.cnorms[cindex], ms.cnorms[cindex1], interp);

        if (flip)
        {
            p3.x = pm2.x + (delta.x * pinterp);
            p3.y = pm2.y + (delta.y * pinterp);
            p3.z = pm2.z + (delta.z * pinterp);

            p.x = pm2.x + (delta.x * pa);
            p.y = pm2.y + (delta.y * pa);
            p.z = pm2.z + (delta.z * pa);
        }
        else
        {
            p3.x = pm1.x + (delta.x * pinterp);
            p3.y = pm1.y + (delta.y * pinterp);
            p3.z = pm1.z + (delta.z * pinterp);

            p.x = pm1.x + (delta.x * pa);
            p.y = pm1.y + (delta.y * pa);
            p.z = pm1.z + (delta.z * pa);
        }

        return(p3);
    }
Example #9
0
    // Return angles to allow calc up to work
    public override Vector3 GetPosAndFrame(MegaShapeLoft loft, float ca, float a, float at, out Vector3 p, out Vector3 up, out Vector3 right, out Vector3 fwd)
    {
        MegaMeshSection ms = null;

        ca = Mathf.Clamp(ca, 0.0f, 0.9999f);

        for (int i = 0; i < meshsections.Count; i++)
        {
            ms = meshsections[i];
            if (ca >= ms.castart && ca <= ms.caend)
            {
                break;
            }
        }

        ca = (ca - ms.castart) / (ms.caend - ms.castart);

        ca = Mathf.Clamp(ca, 0.0f, 0.9999f);
        float findex  = (float)(ms.cverts.Count - 1) * ca;
        int   cindex  = (int)findex;
        float interp  = findex - cindex;
        int   cindex1 = cindex + 1;

        int   pindex;
        int   pindex1;
        bool  flip = false;
        float pinterp;

        if (pathLength < 0.9999f || layerPath.splines[curve].closed == false)
        {
            if (a < 0.0f)
            {
                pindex  = 0;
                pinterp = a * GetLength(loft);
                pindex1 = pindex + 1;
            }
            else
            {
                if (a >= 0.999f)
                {
                    pindex  = crosses - 2;
                    pindex1 = crosses - 1;
                    pinterp = (a - 1.0f) * GetLength(loft);
                    flip    = true;
                }
                else
                {
                    float pfindex = (crosses - 1) * a;
                    pindex  = (int)pfindex;
                    pinterp = pfindex - pindex;
                    pindex1 = pindex + 1;
                }
            }
        }
        else
        {
            a = Mathf.Repeat(a, 0.9999f);

            float pfindex = (float)(crosses - 1) * a;
            pindex  = (int)pfindex;
            pinterp = pfindex - pindex;

            pindex1 = pindex + 1;
        }

        int ix = (pindex * ms.cverts.Count) + cindex;

        if (ix < 0 || ix >= ms.verts.Count)
        {
            Debug.Log("pindex " + pindex + " cindex " + cindex + " a " + a);
            Debug.Log("ix " + ix + " verts " + ms.verts.Count);
        }

        Vector3 p1 = ms.verts[(pindex * ms.cverts.Count) + cindex];
        Vector3 p2 = ms.verts[(pindex * ms.cverts.Count) + cindex1];
        Vector3 p3 = ms.verts[(pindex1 * ms.cverts.Count) + cindex];
        Vector3 p4 = ms.verts[(pindex1 * ms.cverts.Count) + cindex1];

        Vector3 pm1 = Vector3.Lerp(p1, p2, interp);
        Vector3 pm2 = Vector3.Lerp(p3, p4, interp);

        Vector3 delta = pm2 - pm1;

        float pa = pinterp + at;

        // Quick calc of face normal
        Vector3 n1 = p2 - p1;           // right
        Vector3 n2 = p3 - p1;           // forward

        right = n1.normalized;
        fwd   = n2.normalized;
        up    = Vector3.Cross(n1, n2).normalized;

        if (flip)
        {
            p3.x = pm2.x + (delta.x * pinterp);
            p3.y = pm2.y + (delta.y * pinterp);
            p3.z = pm2.z + (delta.z * pinterp);

            p.x = pm2.x + (delta.x * pa);
            p.y = pm2.y + (delta.y * pa);
            p.z = pm2.z + (delta.z * pa);
        }
        else
        {
            p3.x = pm1.x + (delta.x * pinterp);
            p3.y = pm1.y + (delta.y * pinterp);
            p3.z = pm1.z + (delta.z * pinterp);

            p.x = pm1.x + (delta.x * pa);
            p.y = pm1.y + (delta.y * pa);
            p.z = pm1.z + (delta.z * pa);
        }

        return(p3);
    }
Example #10
0
    // If beyond the start or end then need to extrapolate last or first rows
    public override Vector3 GetPosAndLook(MegaShapeLoft loft, float ca, float a, float at, out Vector3 p)
    {
        MegaMeshSection ms = null;

        ca = Mathf.Clamp(ca, 0.0f, 0.9999f);

        for (int i = 0; i < meshsections.Count; i++)
        {
            ms = meshsections[i];
            if (ca >= ms.castart && ca <= ms.caend)
            {
                break;
            }
        }

        ca = (ca - ms.castart) / (ms.caend - ms.castart);

        ca = Mathf.Clamp(ca, 0.0f, 0.9999f);

        float findex  = (ms.cverts.Count - 1) * ca;
        int   cindex  = (int)findex;
        float interp  = findex - cindex;
        int   cindex1 = cindex + 1;

        bool  flip = false;
        int   pindex;
        int   pindex1;
        float pinterp;

        if (pathLength < 0.9999f || layerPath.splines[curve].closed == false)
        {
            if (a < 0.0f)
            {
                pindex  = 0;
                pindex1 = 1;
                pinterp = a * GetLength(loft);                  // / crosses);
            }
            else
            {
                if (a >= 0.9999f)
                {
                    pindex  = crosses - 1;
                    pindex1 = crosses - 2;
                    pinterp = (a - 1.0f) * GetLength(loft);
                    flip    = true;
                }
                else
                {
                    float pfindex = (crosses - 1) * a;
                    pindex  = (int)pfindex;
                    pinterp = pfindex - pindex;
                    pindex1 = pindex + 1;
                }
            }
        }
        else
        {
            a = Mathf.Repeat(a, 0.9999f);

            float pfindex = (crosses - 1) * a;
            pindex  = (int)pfindex;
            pinterp = pfindex - pindex;
            pindex1 = pindex + 1;
        }

        Vector3 p1 = ms.verts[(pindex * ms.cverts.Count) + cindex];
        Vector3 p2 = ms.verts[(pindex * ms.cverts.Count) + cindex1];
        Vector3 p3 = ms.verts[(pindex1 * ms.cverts.Count) + cindex];
        Vector3 p4 = ms.verts[(pindex1 * ms.cverts.Count) + cindex1];

        float pa = pinterp + at;

        p1.x = p1.x + (p2.x - p1.x) * interp;
        p1.y = p1.y + (p2.y - p1.y) * interp;
        p1.z = p1.z + (p2.z - p1.z) * interp;

        p2.x = p3.x + (p4.x - p3.x) * interp;
        p2.y = p3.y + (p4.y - p3.y) * interp;
        p2.z = p3.z + (p4.z - p3.z) * interp;

        if (flip)
        {
            p2.x = p1.x - p2.x;
            p2.y = p1.y - p2.y;
            p2.z = p1.z - p2.z;
        }
        else
        {
            p2.x = p2.x - p1.x;
            p2.y = p2.y - p1.y;
            p2.z = p2.z - p1.z;
        }

        p3.x = p1.x + (p2.x * pinterp);
        p3.y = p1.y + (p2.y * pinterp);
        p3.z = p1.z + (p2.z * pinterp);

        p.x = p1.x + (p2.x * pa);
        p.y = p1.y + (p2.y * pa);
        p.z = p1.z + (p2.z * pa);

        return(p3);             //Vector3.Lerp(pm1, pm2, pinterp);
    }
Example #11
0
    // This will need changing
    public override Vector3 GetPos(MegaShapeLoft loft, float ca, float a)
    {
        ca = Mathf.Repeat(ca, 1.0f);
        a  = Mathf.Repeat(a, 1.0f);

        // first need to find the material section we are in from ca
        MegaMeshSection ms = null;

        for (int i = 0; i < meshsections.Count; i++)
        {
            ms = meshsections[i];
            if (ca >= ms.castart && ca <= ms.caend)
            {
                break;
            }
        }

        ca = (ca - ms.castart) / (ms.caend - ms.castart);

        // Ok got the section
        float findex = (ms.cverts.Count - 1) * ca;
        int   cindex = (int)findex;
        float interp = findex - cindex;

        int cindex1 = cindex + 1;

        bool  flip = false;
        int   pindex;
        int   pindex1;
        float pinterp;

        if (pathLength < 0.9999f || layerPath.splines[curve].closed == false)
        {
            if (a < 0.0f)
            {
                pindex  = 0;
                pindex1 = 1;
                pinterp = a * GetLength(loft);                  // / crosses);
            }
            else
            {
                if (a >= 0.9999f)
                {
                    pindex  = crosses - 1;
                    pindex1 = crosses - 2;
                    pinterp = (a - 1.0f) * GetLength(loft);
                    flip    = true;
                }
                else
                {
                    float pfindex = (crosses - 1) * a;
                    pindex  = (int)pfindex;
                    pinterp = pfindex - pindex;
                    pindex1 = pindex + 1;
                }
            }
        }
        else
        {
            a = Mathf.Repeat(a, 0.9999f);

            float pfindex = (crosses - 1) * a;
            pindex  = (int)pfindex;
            pinterp = pfindex - pindex;
            pindex1 = pindex + 1;
        }

        Vector3 p1 = ms.verts[(pindex * ms.cverts.Count) + cindex];
        Vector3 p2 = ms.verts[(pindex * ms.cverts.Count) + cindex1];
        Vector3 p3 = ms.verts[(pindex1 * ms.cverts.Count) + cindex];
        Vector3 p4 = ms.verts[(pindex1 * ms.cverts.Count) + cindex1];

        Vector3 pm1 = Vector3.Lerp(p1, p2, interp);
        Vector3 pm2 = Vector3.Lerp(p3, p4, interp);

        Vector3 delta = pm2 - pm1;

        if (flip)
        {
            p3.x = pm2.x + (delta.x * pinterp);
            p3.y = pm2.y + (delta.y * pinterp);
            p3.z = pm2.z + (delta.z * pinterp);
        }
        else
        {
            p3.x = pm1.x + (delta.x * pinterp);
            p3.y = pm1.y + (delta.y * pinterp);
            p3.z = pm1.z + (delta.z * pinterp);
        }

        return(p3);             //Vector3.Lerp(pm1, pm2, pinterp);
    }
Example #12
0
    // We could do a bary centric thing if we grid up the bounds
    void DoConform(MegaShapeLoft loft)
    {
        InitConform();

        if (target && conformCollider)
        {
            Matrix4x4 loctoworld = transform.localToWorldMatrix;

            Matrix4x4 tm    = loctoworld;
            Matrix4x4 invtm = tm.inverse;

            Ray        ray = new Ray();
            RaycastHit hit;

            float ca = conformAmount * loft.conformAmount;

            // When calculating alpha need to do caps sep
            for (int m = 0; m < meshsections.Count; m++)
            {
                MegaMeshSection ms = meshsections[m];
                for (int i = 0; i < ms.verts1.Count; i++)
                {
                    Vector3 origin = tm.MultiplyPoint(ms.verts1[i]);
                    origin.y     += raystartoff;
                    ray.origin    = origin;
                    ray.direction = Vector3.down;

                    ms.verts.Add(ms.verts1[i]);

                    if (conformCollider.Raycast(ray, out hit, raydist))
                    {
                        Vector3 lochit = invtm.MultiplyPoint(hit.point);

                        Vector3 p = ms.verts[i];
                        p.y         = Mathf.Lerp(p.y, lochit.y + ms.offsets[i] + conformOffset, ca);                    //conformAmount);
                        ms.verts[i] = p;
                        ms.last[i]  = p.y;
                    }
                    else
                    {
                        Vector3 ht = ray.origin;
                        ht.y -= raydist;
                        Vector3 p = ms.verts[i];

                        p.y         = ms.last[i];
                        ms.verts[i] = p;
                    }
                }
            }
        }
        else
        {
            for (int m = 0; m < meshsections.Count; m++)
            {
                MegaMeshSection ms = meshsections[m];
                for (int i = 0; i < ms.verts1.Count; i++)
                {
                    ms.verts.Add(ms.verts1[i]);
                }
            }
        }
    }
    public void OnSceneGUI()
    {
        MegaLoftLayerMultiMatComplex layer = (MegaLoftLayerMultiMatComplex)target;
        MegaShapeLoft loft = layer.gameObject.GetComponent <MegaShapeLoft>();

        if (loft == null)
        {
            return;
        }

        if (layer.layerPath == null)
        {
            return;
        }

        if (!layer.showsections)
        {
            return;
        }

        MegaSpline pathspline = layer.layerPath.splines[layer.curve];

        Matrix4x4 pathtm = Matrix4x4.identity;

        //if ( layer.SnapToPath )
        //	pathtm = layer.layerPath.transform.localToWorldMatrix;

        Matrix4x4 twisttm = Matrix4x4.identity;
        Matrix4x4 tm;

        float offx = 0.0f;
        float offy = 0.0f;
        float offz = 0.0f;

        Vector3 lastup = locup;

        for (int i = 1; i < layer.loftsections.Count - 1; i++)
        {
            MegaLoftSection section = layer.loftsections[i];

            float alpha = section.alpha;

            if (layer.useOffsetX)
            {
                offx = layer.offsetCrvX.Evaluate(alpha);
            }

            if (layer.useOffsetY)
            {
                offy = layer.offsetCrvY.Evaluate(alpha);
            }

            if (layer.useOffsetZ)
            {
                offz += layer.offsetCrvZ.Evaluate(alpha);
            }

            //if ( layer.useTwistCrv )
            //{
            //	float twist = layer.twistCrv.Evaluate(alpha);
            //	MegaShapeUtils.RotateZ(ref twisttm, Mathf.Deg2Rad * twist);
            //	tm = pathtm * layer.GetDeformMat(pathspline, alpha, layer.layerPath.normalizedInterp) * twisttm;
            //}
            //else
            //	tm = pathtm * layer.GetDeformMat(pathspline, alpha, layer.layerPath.normalizedInterp);

            if (layer.useTwistCrv)
            {
                float twist = layer.twistCrv.Evaluate(section.alpha);
                float tw1   = layer.layerPath.splines[layer.curve].GetTwist(section.alpha);
                MegaShapeUtils.RotateZ(ref twisttm, Mathf.Deg2Rad * (twist - tw1));
                if (layer.frameMethod == MegaFrameMethod.Old)
                {
                    tm = pathtm * layer.GetDeformMat(layer.layerPath.splines[layer.curve], section.alpha, layer.layerPath.normalizedInterp) * twisttm;
                }
                else
                {
                    tm = pathtm * layer.GetDeformMatNewMethod(layer.layerPath.splines[layer.curve], section.alpha, layer.layerPath.normalizedInterp, ref lastup) * twisttm;
                }
            }
            else
            {
                if (layer.frameMethod == MegaFrameMethod.Old)
                {
                    tm = pathtm * layer.GetDeformMat(layer.layerPath.splines[layer.curve], section.alpha, layer.layerPath.normalizedInterp);
                }
                else
                {
                    tm = pathtm * layer.GetDeformMatNewMethod(layer.layerPath.splines[layer.curve], section.alpha, layer.layerPath.normalizedInterp, ref lastup);
                }
            }


            //Debug.Log("section meshes " + section.meshsections.Count);
            //Debug.Log("verts " + section.meshsections[0].cverts.Count);
            Vector3 p = section.meshsections[0].cverts[0];
            //if ( layer.useScaleXCrv )
            //	p.x *= layer.scaleCrvX.Evaluate(alpha);

            //if ( layer.useScaleYCrv )
            //	p.y *= layer.scaleCrvY.Evaluate(alpha);

            p.x += offx;
            p.y += offy;
            p.z += offz;

            Vector3 tp = p;
            p = tm.MultiplyPoint3x4(p);

            p += layer.offset;

            Matrix4x4 tantm = pathtm * layer.GetDeformMat(pathspline, alpha + 0.01f, layer.layerPath.normalizedInterp);
            Vector3   tan   = tantm.MultiplyPoint3x4(tp);
            tan += layer.offset;

            tan = (tan - p).normalized;

            MegaMeshSection ms = section.meshsections[section.meshsections.Count - 1];
            Vector3         p1 = ms.cverts[ms.cverts.Count - 1];
            //if ( layer.useScaleXCrv )
            //	p1.x *= layer.scaleCrvX.Evaluate(alpha);

            //if ( layer.useScaleYCrv )
            //	p1.y *= layer.scaleCrvY.Evaluate(alpha);

            p1.x += offx;
            p1.y += offy;
            p1.z += offz;

            tp = p1;
            p1 = tm.MultiplyPoint3x4(p1);

            p1           += layer.offset;
            Handles.color = Color.green;
            p             = loft.transform.TransformPoint(p);
            //Vector3 pn = Handles.Slider(p, tan, layer.handlesize, Handles.SphereCap, 0.0f);
            Vector3 pn = Slider(p, tan, layer.handlesize);
            pn = pn - p;
            float delta = pn.magnitude;

            if (Vector3.Dot(tan, pn) < 0.0f)
            {
                delta = -delta;
            }

            section.alpha += delta * 0.0005f;

            float al = section.alpha;                   // + delta * 0.0005f;

            if (al != layer.loftsections[i].alpha)
            {
                if (i > 0)
                {
                    if (al < layer.loftsections[i - 1].alpha)
                    {
                        al = layer.loftsections[i - 1].alpha;
                    }
                }

                if (i < layer.loftsections.Count - 1)
                {
                    if (al > layer.loftsections[i + 1].alpha)
                    {
                        al = layer.loftsections[i + 1].alpha;
                    }
                }

                layer.loftsections[i].alpha = al;
            }

            if (delta != 0.0f)
            {
                GUI.changed  = true;
                loft.rebuild = true;
                EditorUtility.SetDirty(target);
            }

            tan  = tantm.MultiplyPoint3x4(tp);
            tan += layer.offset;
            tan  = (tan - p1).normalized;

            p1 = loft.transform.TransformPoint(p1);

            //pn = Handles.Slider(p1, tan, layer.handlesize, Handles.SphereCap, 0.0f);
            pn = Slider(p1, tan, layer.handlesize);

            pn = pn - p1;

            delta = pn.magnitude;               //Vector3.Distance(p, pn);

            if (Vector3.Dot(tan, pn) < 0.0f)
            {
                delta = -delta;
            }

            al = section.alpha + delta * 0.0005f;

            if (al != layer.loftsections[i].alpha)
            {
                if (i > 0)
                {
                    if (al < layer.loftsections[i - 1].alpha)
                    {
                        al = layer.loftsections[i - 1].alpha;
                    }
                }

                if (i < layer.loftsections.Count - 1)
                {
                    if (al > layer.loftsections[i + 1].alpha)
                    {
                        al = layer.loftsections[i + 1].alpha;
                    }
                }

                layer.loftsections[i].alpha = al;
            }

            if (delta != 0.0f)
            {
                GUI.changed  = true;
                loft.rebuild = true;
                EditorUtility.SetDirty(target);
            }
        }

        if (layer.loftsections.Count > 0)
        {
            if (layer.loftsections[0].alpha != 0.0f)
            {
                layer.loftsections[0].alpha = 0.0f;
            }

            for (int i = 1; i < layer.loftsections.Count - 1; i++)
            {
                if (layer.loftsections[i].alpha <= layer.loftsections[i - 1].alpha)
                {
                    layer.loftsections[i - 1].alpha = layer.loftsections[i].alpha;
                }

                if (layer.loftsections[i].alpha >= layer.loftsections[i + 1].alpha)
                {
                    layer.loftsections[i].alpha = layer.loftsections[i + 1].alpha;
                }
            }

            if (layer.loftsections[layer.loftsections.Count - 1].alpha != 1.0f)
            {
                layer.loftsections[layer.loftsections.Count - 1].alpha = 1.0f;
            }
        }
    }
    static void DrawPath(MegaLoftLayerMultiMatComplex layer)
    {
        MegaShapeLoft loft = layer.gameObject.GetComponent <MegaShapeLoft>();

        if (loft == null)
        {
            return;
        }

        if (layer.layerPath == null)
        {
            return;
        }

        if (layer.loftsections == null || layer.loftsections.Count < 2)
        {
            return;
        }

        // Needs changing
        for (int i = 0; i < layer.loftsections.Count; i++)
        {
            if (layer.loftsections[i].meshsections == null || layer.loftsections[i].meshsections.Count == 0)
            {
                return;
            }
        }

        MegaSpline pathspline = layer.layerPath.splines[layer.curve];

        Matrix4x4 pathtm = Matrix4x4.identity;

        //if ( layer.SnapToPath )
        //	pathtm = layer.layerPath.transform.localToWorldMatrix;

        Color col = Gizmos.color;

        Matrix4x4 twisttm = Matrix4x4.identity;
        Matrix4x4 tm;

        Vector3 lastup = locup;

        for (int i = 0; i < layer.loftsections.Count; i++)
        {
            MegaLoftSection section = layer.loftsections[i];

            //if ( layer.useTwistCrv )
            //{
            //	float twist = layer.twistCrv.Evaluate(section.alpha);
            //	MegaShapeUtils.RotateZ(ref twisttm, Mathf.Deg2Rad * twist);
            //	tm = pathtm * layer.GetDeformMat(pathspline, section.alpha, layer.layerPath.normalizedInterp) * twisttm;	//loft.);
            //}
            //else
            //	tm = pathtm * layer.GetDeformMat(pathspline, section.alpha, layer.layerPath.normalizedInterp);	//loft.);

            if (layer.useTwistCrv)
            {
                float twist = layer.twistCrv.Evaluate(section.alpha);
                float tw1   = pathspline.GetTwist(section.alpha);
                MegaShapeUtils.RotateZ(ref twisttm, Mathf.Deg2Rad * (twist - tw1));
                if (layer.frameMethod == MegaFrameMethod.Old)
                {
                    tm = pathtm * layer.GetDeformMat(pathspline, section.alpha, layer.layerPath.normalizedInterp) * twisttm;
                }
                else
                {
                    tm = pathtm * layer.GetDeformMatNewMethod(pathspline, section.alpha, layer.layerPath.normalizedInterp, ref lastup) * twisttm;
                }
            }
            else
            {
                if (layer.frameMethod == MegaFrameMethod.Old)
                {
                    tm = pathtm * layer.GetDeformMat(pathspline, section.alpha, layer.layerPath.normalizedInterp);
                }
                else
                {
                    tm = pathtm * layer.GetDeformMatNewMethod(pathspline, section.alpha, layer.layerPath.normalizedInterp, ref lastup);
                }
            }

            Vector3 mp1 = Vector3.zero;
            Vector3 mp2 = Vector3.zero;

            for (int m = 0; m < section.meshsections.Count; m++)
            {
                MegaMeshSection ms = section.meshsections[m];
                Vector3         p1 = ms.cverts[0];

                float offx = 0.0f;
                float offy = 0.0f;
                float offz = 0.0f;
                float sclx = 1.0f;
                float scly = 1.0f;

                //if ( layer.useScaleXCrv )
                //	p1.x *= layer.scaleCrvX.Evaluate(section.alpha);

                //if ( layer.useScaleYCrv )
                //	p1.y *= layer.scaleCrvY.Evaluate(section.alpha);

                if (layer.useOffsetX)
                {
                    offx = layer.offsetCrvX.Evaluate(section.alpha);
                }

                if (layer.useOffsetY)
                {
                    offy = layer.offsetCrvY.Evaluate(section.alpha);
                }

                if (layer.useOffsetZ)
                {
                    offz = layer.offsetCrvZ.Evaluate(section.alpha);
                }

                //if ( layer.useScaleXCrv )
                //	sclx = layer.scaleCrvX.Evaluate(section.alpha);

                //if ( layer.useScaleYCrv )
                //	scly = layer.scaleCrvY.Evaluate(section.alpha);

                p1  = tm.MultiplyPoint3x4(p1);
                p1 += layer.offset;

                Gizmos.color = seccol;                  //Color.red;
                //Vector3 mid = Vector3.zero;

                if (m == 0)
                {
                    mp1 = p1;
                }

                for (int v = 1; v < ms.cverts.Count; v++)
                {
                    Vector3 p = ms.cverts[v];
                    p.x *= sclx;
                    p.y *= scly;

                    p.x += offx;
                    p.y += offy;
                    p.z += offz;

                    p  = tm.MultiplyPoint3x4(p);
                    p += layer.offset;

                    Gizmos.DrawLine(loft.transform.TransformPoint(p1), loft.transform.TransformPoint(p));

                    p1  = p;
                    mp2 = p;
                    //if ( v == ms.cverts.Count / 2 )
                    //mid = p;
                }

                //Vector3 mp1 = section.meshsections[0].cverts[0];
                //Vector3 mp2 = section.meshsections[section.meshsections.Count - 1].cverts[section.meshsections[section.meshsections.Count - 1].cverts.Count - 1];
            }

            Handles.color = Color.white;
            Handles.Label(loft.transform.TransformPoint((mp1 + mp2) * 0.5f), "Cross: " + i);
            Gizmos.color = col;
        }

        // Draw outside edge
        //Vector3 sclc = Vector3.one;
        float lerp = 0.0f;

        // The position stuff here is waht we could use instead of mesh verts
        Vector3 last  = Vector3.zero;
        Vector3 last1 = Vector3.zero;

        for (float alpha = 0.0f; alpha <= 1.0f; alpha += 0.005f)
        {
            //if ( layer.useScaleXCrv )
            //	sclc.x = layer.scaleCrvX.Evaluate(alpha);

            //if ( layer.useScaleYCrv )
            //	sclc.y = layer.scaleCrvY.Evaluate(alpha);

            //if ( layer.useTwistCrv )
            //{
            //	float twist = layer.twistCrv.Evaluate(alpha);
            //	MegaShapeUtils.RotateZ(ref twisttm, Mathf.Deg2Rad * twist);
            //	tm = pathtm * layer.GetDeformMat(layer.layerPath.splines[layer.curve], alpha, layer.layerPath.normalizedInterp) * twisttm;	//loft.);
            //}
            //else
            //	tm = pathtm * layer.GetDeformMat(layer.layerPath.splines[layer.curve], alpha, layer.layerPath.normalizedInterp);	//loft.);

            if (layer.useTwistCrv)
            {
                float twist = layer.twistCrv.Evaluate(alpha);
                float tw1   = layer.layerPath.splines[layer.curve].GetTwist(alpha);
                MegaShapeUtils.RotateZ(ref twisttm, Mathf.Deg2Rad * (twist - tw1));
                if (layer.frameMethod == MegaFrameMethod.Old)
                {
                    tm = pathtm * layer.GetDeformMat(layer.layerPath.splines[layer.curve], alpha, layer.layerPath.normalizedInterp) * twisttm;
                }
                else
                {
                    tm = pathtm * layer.GetDeformMatNewMethod(layer.layerPath.splines[layer.curve], alpha, layer.layerPath.normalizedInterp, ref lastup) * twisttm;
                }
            }
            else
            {
                if (layer.frameMethod == MegaFrameMethod.Old)
                {
                    tm = pathtm * layer.GetDeformMat(layer.layerPath.splines[layer.curve], alpha, layer.layerPath.normalizedInterp);
                }
                else
                {
                    tm = pathtm * layer.GetDeformMatNewMethod(layer.layerPath.splines[layer.curve], alpha, layer.layerPath.normalizedInterp, ref lastup);
                }
            }


            // Need to get the crosssection for the given alpha and the lerp value
            int csect = layer.GetSection(alpha, out lerp);

            lerp = layer.ease.easing(0.0f, 1.0f, lerp);

            MegaLoftSection cs1 = layer.loftsections[csect];
            MegaLoftSection cs2 = layer.loftsections[csect + 1];

            MegaMeshSection ms1 = cs1.meshsections[0];
            MegaMeshSection ms2 = cs2.meshsections[0];

            MegaMeshSection ms3 = cs1.meshsections[cs1.meshsections.Count - 1];
            MegaMeshSection ms4 = cs2.meshsections[cs2.meshsections.Count - 1];

            Vector3 p  = Vector3.Lerp(ms1.cverts[0], ms2.cverts[0], lerp);                                       // * sclc;
            Vector3 p1 = Vector3.Lerp(ms3.cverts[ms3.cverts.Count - 1], ms4.cverts[ms4.cverts.Count - 1], lerp); // * sclc;
            //if ( layer.useScaleXCrv )
            //{
            //	p.x *= sclc.x;
            //	p1.x *= sclc.x;
            //}

            //if ( layer.useScaleYCrv )
            //{
            //	p.y *= sclc.y;
            //	p1.y *= sclc.y;
            //}

            if (layer.useOffsetX)
            {
                p.x  += layer.offsetCrvX.Evaluate(alpha);
                p1.x += layer.offsetCrvX.Evaluate(alpha);
            }

            if (layer.useOffsetY)
            {
                p.y  += layer.offsetCrvY.Evaluate(alpha);
                p1.y += layer.offsetCrvY.Evaluate(alpha);
            }

            if (layer.useOffsetZ)
            {
                p.z  += layer.offsetCrvZ.Evaluate(alpha);
                p1.z += layer.offsetCrvZ.Evaluate(alpha);
            }

            p  = tm.MultiplyPoint3x4(p);
            p += layer.offset;

            p1  = tm.MultiplyPoint3x4(p1);
            p1 += layer.offset;

            if (alpha > 0.0f)
            {
                Gizmos.DrawLine(loft.transform.TransformPoint(last), loft.transform.TransformPoint(p));
                Gizmos.DrawLine(loft.transform.TransformPoint(last1), loft.transform.TransformPoint(p1));
            }

            last  = p;
            last1 = p1;
        }
    }
Example #15
0
    void BuildPolyShape(MegaLoftSection lsection, int steps, Vector3 off1, float width)
    {
        int       curve = lsection.curve;
        int       lk    = -1;
        Matrix4x4 tm1   = Matrix4x4.identity;
        MegaShape shape = lsection.shape;

        //verts.Clear();
        //uvs.Clear();
        //norms.Clear();

        MegaMatrix.Translate(ref tm1, pivot);
        Vector3 rot = crossRot + lsection.rot;

        MegaMatrix.Rotate(ref tm1, new Vector3(Mathf.Deg2Rad * rot.x, Mathf.Deg2Rad * rot.y, Mathf.Deg2Rad * rot.z));

        svert = verts.Count;

        {
            float dst = crossDist;

            if (dst < 0.01f)
            {
                dst = 0.01f;
            }

            svert = verts.Count;

            float alpha = crossStart;
            float cend  = crossStart + crossEnd;

            if (alpha < 0.0f)
            {
                alpha = 0.0f;
            }

            if (cend > 1.0f)
            {
                cend = 1.0f;
            }

            MegaSpline cspl = shape.splines[curve];

            if (cspl.closed)
            {
                if (alpha < 0.0f)
                {
                    alpha += 1.0f;
                }
            }

            Vector3 off = off1;                 //Vector3.zero;
            if (snap)
            {
                off = cspl.knots[0].p - cspl.knots[0].p;
            }

            if (cspl.closed)
            {
                alpha = Mathf.Repeat(alpha, 1.0f);
            }

            lsection.meshsections.Clear();
            FindSections(lsection, cspl);

            Vector3 pos = tm1.MultiplyPoint3x4(cspl.InterpCurve3D(alpha, shape.normalizedInterp, ref lk) + off);

            //Debug.Log("MeshSections " + lsection.meshsections.Count);

            for (int i = 0; i < lsection.meshsections.Count; i++)
            {
                MegaMeshSection     ms = lsection.meshsections[i];              //new MegaMeshSection();
                MegaMaterialSection s  = sections[ms.mat];

                int   k1 = ms.firstknot;
                int   k2 = ms.lastknot;
                float l1 = 0.0f;
                float l2 = 0.0f;

                //Debug.Log("k1 " + k1 + " k2 " + k2);
                if (k1 > 0)
                {
                    l1 = cspl.knots[k1 - 1].length;
                }
                else
                {
                    l1 = 0.0f;
                }

                if (k2 < cspl.knots.Count - 1)
                {
                    l2 = cspl.knots[k2 - 1].length;
                }
                else
                {
                    l2 = cspl.length;
                }

                //float slen = l2 - l1;
                //Debug.Log("l1 " + l1 + " l2 " + l2);

                float a1 = l1 / cspl.length;
                float a2 = l2 / cspl.length;
                ms.castart = a1;
                ms.caend   = a2;

                for (int kn = k1; kn < k2; kn++)
                {
                    pos = cspl.knots[kn].Interpolate(0.0f, cspl.knots[kn + 1]) + lsection.offset;

                    pos.x *= lsection.scale.x;
                    pos.y *= lsection.scale.y;
                    pos.z *= lsection.scale.z;

                    pos = tm1.MultiplyPoint3x4(pos + off);
                    ms.cverts.Add(pos);

                    for (int j = 1; j < s.steps; j++)
                    {
                        float ka = (float)j / (float)s.steps;
                        pos = cspl.knots[kn].Interpolate(ka, cspl.knots[kn + 1]) + lsection.offset;

                        pos.x *= lsection.scale.x;
                        pos.y *= lsection.scale.y;
                        pos.z *= lsection.scale.z;

                        pos = tm1.MultiplyPoint3x4(pos + off);
                        ms.cverts.Add(pos);
                    }
                }

                pos = cspl.knots[k2 - 1].Interpolate(1.0f, cspl.knots[k2]) + lsection.offset;

                pos.x *= lsection.scale.x;
                pos.y *= lsection.scale.y;
                pos.z *= lsection.scale.z;

                pos = tm1.MultiplyPoint3x4(pos + off);
                ms.cverts.Add(pos);
                //pos = tm1.MultiplyPoint3x4(cspl.knots[k2].p + off);
                //ms.cverts.Add(pos);
            }

            // Do uv and col now
            for (int i = 0; i < lsection.meshsections.Count; i++)
            {
                MegaMeshSection ms1 = lsection.meshsections[i];

                ms1.len = 0.0f;
                //int k1 = ms1.firstknot;
                //int k2 = ms1.lastknot;

                //Debug.Log("k1 " + k1 + " k2 " + k2);
                //float l1 = cspl.knots[k2].length - cspl.knots[k1].length;

                Vector2 uv1 = Vector2.zero;
                ms1.cuvs.Add(uv1);
                ms1.ccols.Add(Color.white);

                for (int v = 1; v < ms1.cverts.Count; v++)
                {
                    ms1.len += Vector3.Distance(ms1.cverts[v], ms1.cverts[v - 1]);
                }

                for (int v = 1; v < ms1.cverts.Count; v++)
                {
                    uv1.y += Vector3.Distance(ms1.cverts[v], ms1.cverts[v - 1]) / ms1.len;
                    //uv1.y /= len;	//Vector3.Distance(ms1.cverts[v], ms1.cverts[v - 1]);
                    ms1.cuvs.Add(uv1);
                    ms1.ccols.Add(Color.white);
                }
            }

            // Add end point
            if (cspl.closed)
            {
                alpha = Mathf.Repeat(cend, 1.0f);
            }
        }

        // Calc normals
        Vector3 up = Vector3.zero;
        Vector3 n1 = Vector3.zero;

        for (int i = 0; i < lsection.meshsections.Count; i++)
        {
            MegaMeshSection ms1 = lsection.meshsections[i];

            if (ms1.cverts.Count > 1)
            {
                for (int v = 0; v < ms1.cverts.Count; v++)
                {
                    if (v < ms1.cverts.Count - 1)
                    {
                        n1 = (ms1.cverts[v + 1] - ms1.cverts[v]);
                    }
                    else
                    {
                        n1 = (ms1.cverts[v] - ms1.cverts[v - 1]);
                    }

                    up.x = -n1.y;
                    up.y = n1.x;
                    up.z = 0.0f;
                    ms1.cnorms.Add(up);
                }
            }
        }
    }
Example #16
0
    // Get the different id sections
    void FindSections(MegaSpline spline)
    {
        if ( spline != null )
        {
            meshsections.Clear();

            int id = spline.knots[0].id - 1;

            for ( int i = 0; i < spline.knots.Count; i++ )
            {
                if ( spline.knots[i].id != id )
                {
                    id = spline.knots[i].id;
                    MegaMeshSection msect = new MegaMeshSection();

                    msect.mat = FindMaterial(id);

                    meshsections.Add(msect);
                }
            }
        }
    }
Example #17
0
    public override int BuildMesh(MegaShapeLoft loft, int triindex)
    {
        if (Lock)
        {
            return(triindex);
        }

        if (layerPath == null || layerPath.splines == null || layerPath.splines.Count == 0)
        {
            return(triindex);
        }

        if (loftsections.Count < 2)             //== 0 )
        {
            return(triindex);
        }

        Vector2 uv = Vector2.zero;
        Vector3 p  = Vector3.zero;

        //int wc = 1;	//ActualCrossVerts;
        float lerp = 0.0f;

        Matrix4x4 pathtm = Matrix4x4.identity;

        //if ( SnapToPath )
        //	pathtm = layerPath.transform.localToWorldMatrix;

        Matrix4x4 twisttm = Matrix4x4.identity;

        //Vector3 sclc = Vector2.one;
        Matrix4x4 tm;

        float offx = 0.0f;
        float offy = 0.0f;
        float offz = 0.0f;

        MegaSpline pathspline = layerPath.splines[curve];

        //bool clsd = layerPath.splines[curve].closed;

        float uvstart = pathStart;

        if (UVOrigin == MegaLoftUVOrigin.SplineStart)
        {
            uvstart = 0.0f;
        }

        Vector3 lastup = locup;

        Color col1 = color;

        float calpha = 0.0f;

        //float uvalpha = 0.0f;

        for (int pi = 0; pi < crosses; pi++)
        {
            float alpha     = (float)pi / (float)(crosses - 1);         //PathSteps;
            float pathalpha = pathStart + (pathLength * alpha);

            //uvalpha = pathalpha;
            //if ( clsd )
            //	pathalpha = Mathf.Repeat(pathalpha, 1.0f);

            //if ( useScaleXCrv )
            //	sclc.x = scaleCrvX.Evaluate(pathalpha);

            //if ( useScaleYCrv )
            //	sclc.y = scaleCrvY.Evaluate(pathalpha);

            if (useTwistCrv)
            {
                float twist = twistCrv.Evaluate(pathalpha);
                float tw1   = layerPath.splines[curve].GetTwist(pathalpha);
                MegaShapeUtils.RotateZ(ref twisttm, Mathf.Deg2Rad * (twist - tw1));
                if (frameMethod == MegaFrameMethod.Old)
                {
                    tm = pathtm * GetDeformMat(layerPath.splines[curve], pathalpha, layerPath.normalizedInterp) * twisttm;
                }
                else
                {
                    tm = pathtm * GetDeformMatNewMethod(layerPath.splines[curve], pathalpha, layerPath.normalizedInterp, ref lastup) * twisttm;
                }
            }
            else
            {
                if (frameMethod == MegaFrameMethod.Old)
                {
                    tm = pathtm * GetDeformMat(layerPath.splines[curve], pathalpha, layerPath.normalizedInterp);
                }
                else
                {
                    tm = pathtm * GetDeformMatNewMethod(layerPath.splines[curve], pathalpha, layerPath.normalizedInterp, ref lastup);
                }
            }
            // Need to get the crosssection for the given alpha and the lerp value
            int csect = GetSection(pathalpha, out lerp);

            lerp = ease.easing(0.0f, 1.0f, lerp);

            MegaLoftSection cs1 = loftsections[csect];
            MegaLoftSection cs2 = loftsections[csect + 1];

            //MegaSpline sectionspline = cs1.shape.splines[cs1.curve];

            if (useOffsetX)
            {
                offx = offsetCrvX.Evaluate(pathalpha);
            }

            if (useOffsetY)
            {
                offy = offsetCrvY.Evaluate(pathalpha);
            }

            if (useOffsetZ)
            {
                offz = offsetCrvZ.Evaluate(pathalpha);
            }

            for (int i = 0; i < cs1.meshsections.Count; i++)
            {
                MegaMeshSection ms0 = loftsections[0].meshsections[i];

                //float slen = sectionspline.knots[ms0.lastknot].length - sectionspline.knots[ms0.firstknot].length;

                MegaMaterialSection mats = sections[ms0.mat];

                if (mats.Enabled)
                {
                    MegaMeshSection ms1 = cs1.meshsections[i];
                    MegaMeshSection ms2 = cs2.meshsections[i];

                    if (loft.useColors)
                    {
                        if (mats.colmode == MegaLoftColMode.Loft)
                        {
                            calpha = alpha;
                        }
                        else
                        {
                            calpha = pathalpha;
                        }

                        calpha = Mathf.Repeat(calpha + mats.coloffset, 1.0f);
                        col1.r = mats.colR.Evaluate(calpha);
                        col1.g = mats.colG.Evaluate(calpha);
                        col1.b = mats.colB.Evaluate(calpha);
                        col1.a = mats.colA.Evaluate(calpha);
                    }

                    for (int v = 0; v < ms1.cverts.Count; v++)
                    {
                        p = Vector3.Lerp(ms1.cverts[v], ms2.cverts[v], lerp);                           // Easing here?
                        //if ( useScaleXCrv )
                        //	p.x *= sclc.x;

                        //if ( useScaleYCrv )
                        //	p.y *= sclc.y;

                        p.x += offx;
                        p.y += offy;
                        p.z += offz;

                        p = tm.MultiplyPoint3x4(p);

                        p += offset;

                        //int ix = (pi * wc) + v;
                        if (conform)
                        {
                            ms0.verts1.Add(p);
                        }
                        else
                        {
                            ms0.verts.Add(p);
                        }

                        uv.y = Mathf.Lerp(ms1.cuvs[v].y, ms2.cuvs[v].y, lerp);

                        uv.x = alpha - uvstart;                         //pathStart;
                        //uv.y = ms.cuvs[v].y;	// - crossStart;	// again not sure here start;

                        if (mats.physuv)
                        {
                            uv.x *= pathspline.length;
                            uv.y *= ms0.len;                                    //sectionspline.length;
                        }
                        else
                        {
                            if (mats.uvcalcy)
                            {
                                //uv.x = ((alpha * LoftLength) / sectionspline.length) - uvstart;
                                uv.x = ((alpha * pathspline.length) / ms0.len) - uvstart;
                            }
                        }

                        if (mats.swapuv)
                        {
                            float ux = uv.x;
                            uv.x = uv.y;
                            uv.y = ux;
                        }

                        uv.x *= mats.UVScale.x;
                        uv.y *= mats.UVScale.y;

                        uv.x += mats.UVOffset.x;
                        uv.y += mats.UVOffset.y;

                        ms0.uvs.Add(uv);                                //[vi] = uv;

                        if (loft.useColors)
                        {
                            ms0.cols.Add(col1);
                        }

#if false
                        uv.y = uvstart + alpha;

                        uv.x *= UVScale.x;
                        uv.y *= UVScale.y;

                        uv.x += UVOffset.x;
                        uv.y += UVOffset.y;
#endif
                        //ms0.uvs.Add(uv);
                    }
                }
            }
        }

        int index = triindex;
        int fi    = 0;          // Calc this

        if (enabled)
        {
            if (flip)
            {
                for (int m = 0; m < loftsections[0].meshsections.Count; m++)
                {
                    MegaMeshSection     ms   = loftsections[0].meshsections[m];
                    MegaMaterialSection mats = sections[ms.mat];

                    if (mats.Enabled)
                    {
                        for (int cr = 0; cr < crosses - 1; cr++)
                        {
                            for (int f = 0; f < ms.cverts.Count - 1; f++)
                            {
                                ms.tris.Add(index + f);
                                ms.tris.Add(index + f + 1);
                                ms.tris.Add(index + f + 1 + ms.cverts.Count);

                                ms.tris.Add(index + f);
                                ms.tris.Add(index + f + 1 + ms.cverts.Count);
                                ms.tris.Add(index + f + ms.cverts.Count);

                                fi += 6;
                            }

                            index += ms.cverts.Count;
                        }
                        index += ms.cverts.Count;
                    }
                }
            }
            else
            {
                for (int m = 0; m < loftsections[0].meshsections.Count; m++)
                {
                    MegaMeshSection     ms   = loftsections[0].meshsections[m];
                    MegaMaterialSection mats = sections[ms.mat];

                    if (mats.Enabled)
                    {
                        for (int cr = 0; cr < crosses - 1; cr++)
                        {
                            for (int f = 0; f < ms.cverts.Count - 1; f++)
                            {
                                ms.tris.Add(index + f + 1 + ms.cverts.Count);
                                ms.tris.Add(index + f + 1);
                                ms.tris.Add(index + f);

                                ms.tris.Add(index + f + ms.cverts.Count);
                                ms.tris.Add(index + f + 1 + ms.cverts.Count);
                                ms.tris.Add(index + f);

                                fi += 6;
                            }

                            index += ms.cverts.Count;
                        }

                        index += ms.cverts.Count;
                    }
                }
            }
        }

        if (conform)
        {
            CalcBounds();
            DoConform(loft);
        }

        return(triindex + fi);          //triindex;
    }
Example #18
0
    // Have cdist per material
    // Best way is to treat each material section like a layer
    public override bool PrepareLoft(MegaShapeLoft loft, int sc)
    {
        MegaShape	section = layerSection;

        // Look at section and find out how many changes in material there are, ie matid
        // That will be the number of materials
        // Each change will have its own cdist, uv mapping, material

        // We seem to do this below as well
        FindSections(section.splines[crosscurve]);

        Matrix4x4 tm1 = Matrix4x4.identity;
        MegaMatrix.Translate(ref tm1, pivot);
        MegaMatrix.Rotate(ref tm1, new Vector3(Mathf.Deg2Rad * crossRot.x, Mathf.Deg2Rad * crossRot.y, Mathf.Deg2Rad * crossRot.z));

        // Build the cross for each section
        if ( enabled )
        {
            float dst = crossDist;

            if ( dst < 0.01f )
                dst = 0.01f;

            int k	= -1;
            int lk	= -1;

            svert = verts.Count;
            Vector2 uv = Vector2.zero;

            float alpha = crossStart;
            float cend = crossStart + crossEnd;

            if ( alpha < 0.0f )
                alpha = 0.0f;

            if ( cend > 1.0f )
                cend = 1.0f;

            MegaSpline cspl = section.splines[crosscurve];

            if ( cspl.closed )
            {
                if ( alpha < 0.0f )
                    alpha += 1.0f;
            }

            Vector3 off = Vector3.zero;
            if ( snap )
                off = cspl.knots[0].p - cspl.knots[0].p;

            if ( cspl.closed )
                alpha = Mathf.Repeat(alpha, 1.0f);

            meshsections.Clear();

            Vector3 pos = tm1.MultiplyPoint3x4(cspl.InterpCurve3D(alpha, section.normalizedInterp, ref lk) + off);

            int currentid = cspl.knots[lk].id;
            MegaMeshSection msect = new MegaMeshSection();

            msect.castart = 0.0f;
            float uvalpha = crossStart;
            uv.y = uvalpha;	//crossStart;

            msect.vertstart = 0;
            msect.mat = FindMaterial(currentid);
            meshsections.Add(msect);

            // Method to get cdist
            MegaMaterialSection ms = sections[msect.mat];

            msect.cverts.Add(pos);

            bool loop = true;
            //float lastalpha = alpha;
            while ( alpha <= cend )
            {
                if ( loop )
                {
                    alpha += ms.cdist;
                    loop = false;
                }
                if ( alpha > cend )
                    alpha = cend;

                pos = tm1.MultiplyPoint3x4(cspl.InterpCurve3D(alpha, section.normalizedInterp, ref k) + off);

                if ( k != lk )
                {
                    while ( lk != k )
                    {
                        //bool looped = false;
                        lk++;
                        int lk1 = lk % cspl.knots.Count;
                        lk = lk1;

                        if ( cspl.knots[lk].id != currentid )
                        {
                            msect.cverts.Add(tm1.MultiplyPoint3x4(cspl.knots[lk].p + off));
                            float caend = ((cspl.knots[lk].length / cspl.length) - crossStart) / (crossEnd - crossStart);
                            msect.caend = caend;

                            // New material
                            msect = new MegaMeshSection();
                            msect.castart = caend;

                            pos = tm1.MultiplyPoint3x4(cspl.knots[lk].p + off);

                            msect.vertstart = 0;
                            currentid = cspl.knots[lk].id;
                            msect.mat = FindMaterial(currentid);
                            meshsections.Add(msect);
                            ms = sections[msect.mat];

                            msect.cverts.Add(pos);
                            lk1 = lk - 1;
                            if ( lk1 < 0 )
                                lk1 = cspl.knots.Count - 1;

                            alpha = (cspl.knots[lk1].length / cspl.length);	// + crossStart;

                            break;
                        }
                        else
                        {
                            if ( ms.includeknots )
                                msect.cverts.Add(tm1.MultiplyPoint3x4(cspl.knots[lk].p + off));
                            else
                            {
                                if ( cspl.knots[k].id != currentid )
                                {
                                    int kk = lk;
                                    while ( cspl.knots[kk].id == currentid && kk < cspl.knots.Count )
                                    {
                                        kk++;
                                    }
                                    if ( kk >= cspl.knots.Count )
                                        kk = cspl.knots.Count - 1;

                                    msect.cverts.Add(tm1.MultiplyPoint3x4(cspl.knots[kk].p + off));
                                    break;
                                }
                                else
                                    msect.cverts.Add(pos);
                            }
                        }
                    }
                }
                else
                    msect.cverts.Add(pos);

                if ( alpha == cend )
                {
                    if ( msect.cverts[msect.cverts.Count - 1] != pos )
                        msect.cverts.Add(pos);

                    break;
                }

                alpha += ms.cdist;
                if ( alpha > cend )
                    alpha = cend;
            }

            msect.caend = 1.0f;

            // Do uv and col now
            for ( int i = 0; i < meshsections.Count; i++ )
            {
                MegaMeshSection ms1 = meshsections[i];

                Vector2 uv1 = Vector2.zero;
                ms1.cuvs.Add(uv1);
                ms1.ccols.Add(Color.white);

                for ( int v = 1; v < ms1.cverts.Count; v++ )
                {
                    uv1.y += Vector3.Distance(ms1.cverts[v], ms1.cverts[v - 1]);
                    ms1.cuvs.Add(uv1);
                    ms1.ccols.Add(Color.white);
                }
            }

            // Add end point
            if ( section.splines[crosscurve].closed )
                alpha = Mathf.Repeat(cend, 1.0f);
        }

        // Calc normals
        Vector3 up = Vector3.zero;
        Vector3 n1 = Vector3.zero;

        for ( int i = 0; i < meshsections.Count; i++ )
        {
            MegaMeshSection ms1 = meshsections[i];

            if ( ms1.cverts.Count > 1 )
            {
                for ( int v = 0; v < ms1.cverts.Count; v++ )
                {
                    if ( v < ms1.cverts.Count - 1 )
                        n1 = (ms1.cverts[v + 1] - ms1.cverts[v]);
                    else
                        n1 = (ms1.cverts[v] - ms1.cverts[v - 1]);

                    up.x = -n1.y;
                    up.y = n1.x;
                    up.z = 0.0f;
                    ms1.cnorms.Add(up);
                }
            }
        }

        Prepare(loft);

        return true;
    }
Example #19
0
    // Have cdist per material
    // Best way is to treat each material section like a layer
    public override bool PrepareLoft(MegaShapeLoft loft, int sc)
    {
        MegaShape section = layerSection;

        // Look at section and find out how many changes in material there are, ie matid
        // That will be the number of materials
        // Each change will have its own cdist, uv mapping, material

        // We seem to do this below as well
        FindSections(section.splines[crosscurve]);

        Matrix4x4 tm1 = Matrix4x4.identity;

        MegaMatrix.Translate(ref tm1, pivot);
        MegaMatrix.Rotate(ref tm1, new Vector3(Mathf.Deg2Rad * crossRot.x, Mathf.Deg2Rad * crossRot.y, Mathf.Deg2Rad * crossRot.z));

        // Build the cross for each section
        if (enabled)
        {
            float dst = crossDist;

            if (dst < 0.01f)
            {
                dst = 0.01f;
            }

            int k  = -1;
            int lk = -1;

            svert = verts.Count;
            Vector2 uv = Vector2.zero;

            float alpha = crossStart;
            float cend  = crossStart + crossEnd;

            if (alpha < 0.0f)
            {
                alpha = 0.0f;
            }

            if (cend > 1.0f)
            {
                cend = 1.0f;
            }

            MegaSpline cspl = section.splines[crosscurve];

            if (cspl.closed)
            {
                if (alpha < 0.0f)
                {
                    alpha += 1.0f;
                }
            }

            Vector3 off = Vector3.zero;
            if (snap)
            {
                off = cspl.knots[0].p - cspl.knots[0].p;
            }

            if (cspl.closed)
            {
                alpha = Mathf.Repeat(alpha, 1.0f);
            }

            meshsections.Clear();

            Vector3 pos = tm1.MultiplyPoint3x4(cspl.InterpCurve3D(alpha, section.normalizedInterp, ref lk) + off);

            int             currentid = cspl.knots[lk].id;
            MegaMeshSection msect     = new MegaMeshSection();

            msect.castart = 0.0f;
            float uvalpha = crossStart;
            uv.y = uvalpha;             //crossStart;

            msect.vertstart = 0;
            msect.mat       = FindMaterial(currentid);
            meshsections.Add(msect);

            // Method to get cdist
            MegaMaterialSection ms = sections[msect.mat];

            msect.cverts.Add(pos);

            bool loop = true;
            //float lastalpha = alpha;
            while (alpha <= cend)
            {
                if (loop)
                {
                    alpha += ms.cdist;
                    loop   = false;
                }
                if (alpha > cend)
                {
                    alpha = cend;
                }

                pos = tm1.MultiplyPoint3x4(cspl.InterpCurve3D(alpha, section.normalizedInterp, ref k) + off);

                if (k != lk)
                {
                    while (lk != k)
                    {
                        //bool looped = false;
                        lk++;
                        int lk1 = lk % cspl.knots.Count;
                        lk = lk1;

                        if (cspl.knots[lk].id != currentid)
                        {
                            msect.cverts.Add(tm1.MultiplyPoint3x4(cspl.knots[lk].p + off));
                            float caend = ((cspl.knots[lk].length / cspl.length) - crossStart) / (crossEnd - crossStart);
                            msect.caend = caend;

                            // New material
                            msect         = new MegaMeshSection();
                            msect.castart = caend;

                            pos = tm1.MultiplyPoint3x4(cspl.knots[lk].p + off);

                            msect.vertstart = 0;
                            currentid       = cspl.knots[lk].id;
                            msect.mat       = FindMaterial(currentid);
                            meshsections.Add(msect);
                            ms = sections[msect.mat];

                            msect.cverts.Add(pos);
                            lk1 = lk - 1;
                            if (lk1 < 0)
                            {
                                lk1 = cspl.knots.Count - 1;
                            }

                            alpha = (cspl.knots[lk1].length / cspl.length);                             // + crossStart;

                            break;
                        }
                        else
                        {
                            if (ms.includeknots)
                            {
                                msect.cverts.Add(tm1.MultiplyPoint3x4(cspl.knots[lk].p + off));
                            }
                            else
                            {
                                if (cspl.knots[k].id != currentid)
                                {
                                    int kk = lk;
                                    while (cspl.knots[kk].id == currentid && kk < cspl.knots.Count)
                                    {
                                        kk++;
                                    }
                                    if (kk >= cspl.knots.Count)
                                    {
                                        kk = cspl.knots.Count - 1;
                                    }

                                    msect.cverts.Add(tm1.MultiplyPoint3x4(cspl.knots[kk].p + off));
                                    break;
                                }
                                else
                                {
                                    msect.cverts.Add(pos);
                                }
                            }
                        }
                    }
                }
                else
                {
                    msect.cverts.Add(pos);
                }

                if (alpha == cend)
                {
                    if (msect.cverts[msect.cverts.Count - 1] != pos)
                    {
                        msect.cverts.Add(pos);
                    }

                    break;
                }

                alpha += ms.cdist;
                if (alpha > cend)
                {
                    alpha = cend;
                }
            }

            msect.caend = 1.0f;

            // Do uv and col now
            for (int i = 0; i < meshsections.Count; i++)
            {
                MegaMeshSection ms1 = meshsections[i];

                Vector2 uv1 = Vector2.zero;
                ms1.cuvs.Add(uv1);
                ms1.ccols.Add(Color.white);

                for (int v = 1; v < ms1.cverts.Count; v++)
                {
                    uv1.y += Vector3.Distance(ms1.cverts[v], ms1.cverts[v - 1]);
                    ms1.cuvs.Add(uv1);
                    ms1.ccols.Add(Color.white);
                }
            }

            // Add end point
            if (section.splines[crosscurve].closed)
            {
                alpha = Mathf.Repeat(cend, 1.0f);
            }
        }

        // Calc normals
        Vector3 up = Vector3.zero;
        Vector3 n1 = Vector3.zero;

        for (int i = 0; i < meshsections.Count; i++)
        {
            MegaMeshSection ms1 = meshsections[i];

            if (ms1.cverts.Count > 1)
            {
                for (int v = 0; v < ms1.cverts.Count; v++)
                {
                    if (v < ms1.cverts.Count - 1)
                    {
                        n1 = (ms1.cverts[v + 1] - ms1.cverts[v]);
                    }
                    else
                    {
                        n1 = (ms1.cverts[v] - ms1.cverts[v - 1]);
                    }

                    up.x = -n1.y;
                    up.y = n1.x;
                    up.z = 0.0f;
                    ms1.cnorms.Add(up);
                }
            }
        }

        Prepare(loft);

        return(true);
    }
    // Get the different id sections
    public void FindSections(MegaLoftSection lsection, MegaSpline spline)
    {
        if ( spline != null )
        {
            lsection.meshsections.Clear();

            int id = spline.knots[0].id - 1;

            MegaMeshSection msect = null;

            for ( int i = 0; i < spline.knots.Count; i++ )
            {
                if ( spline.knots[i].id != id )
                {
                    id = spline.knots[i].id;
                    if ( msect != null )
                        msect.lastknot = i;

                    msect = new MegaMeshSection();

                    msect.firstknot = i;
                    msect.mat = FindMaterial(id);

                    lsection.meshsections.Add(msect);
                }
            }

            lsection.meshsections[lsection.meshsections.Count - 1].lastknot = spline.knots.Count - 1;
        }
    }