예제 #1
0
    Vector3 GetCross(int csect, float ca, Vector3 off)
    {
        MegaLoftSection lsection = loftsections[csect];

        int       curve = lsection.curve;
        int       k     = -1;
        Matrix4x4 tm1   = Matrix4x4.identity;

        float start = crossStart;
        float len   = crossEnd;

        if (lsection.uselen)
        {
            start = lsection.start;
            len   = lsection.length;
        }

        MegaShape shape = lsection.shape;

        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));

        float alpha = start + (ca * len);

        //float alpha = start + ca;

        if (shape.splines[curve].closed)
        {
            alpha = Mathf.Repeat(alpha, 1.0f);
        }

        Vector3 pos = tm1.MultiplyPoint3x4(shape.splines[curve].InterpCurve3D(alpha, shape.normalizedInterp, ref k) + off + lsection.offset);

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

        return(pos);
    }
예제 #2
0
    Vector3 GetCross(MegaShapeLoft loft, float ca)
    {
        MegaShape section = layerSection;

        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));

        int lk = -1;

        float alpha = crossStart + ca;

        MegaSpline cspl = section.splines[crosscurve];

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

        Vector3 off = Vector3.zero;

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

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

        return(tm1.MultiplyPoint3x4(section.splines[crosscurve].InterpCurve3D(alpha, section.normalizedInterp, ref lk) + off));
    }
    public override int BuildMesh(MegaShapeLoft loft, int triindex)
    {
        MegaShape path = layerPath;

        if (tangent < 0.1f)
        {
            tangent = 0.1f;
        }

        //if ( snaptopath )
        //{
        //mat = path.transform.worldToLocalMatrix * transform.localToWorldMatrix;	// * transform.worldToLocalMatrix;	//mat = surfaceLoft.transform.localToWorldMatrix;
        //mat = transform.localToWorldMatrix * layerPath.transform.worldToLocalMatrix;	// * transform.worldToLocalMatrix;	//mat = surfaceLoft.transform.localToWorldMatrix;
        //mat = layerPath.transform.worldToLocalMatrix;	// * transform.localToWorldMatrix;	// * transform.worldToLocalMatrix;	//mat = surfaceLoft.transform.localToWorldMatrix;
        //}
        //else
        //{
        mat = Matrix4x4.identity;                       //path.transform.localToWorldMatrix;
        //}
        //mat = Matrix4x4.identity;	//transform.worldToLocalMatrix;
        //mat = transform.localToWorldMatrix * path.transform.worldToLocalMatrix;	// * transform.worldToLocalMatrix;	//mat = surfaceLoft.transform.localToWorldMatrix;
        //mat = path.transform.worldToLocalMatrix * transform.localToWorldMatrix;	// * transform.worldToLocalMatrix;	//mat = surfaceLoft.transform.localToWorldMatrix;

        tm = Matrix4x4.identity;

        switch (axis)
        {
        case MegaAxis.X: MegaMatrix.RotateY(ref tm, -Mathf.PI * 0.5f); break;

        case MegaAxis.Y: MegaMatrix.RotateX(ref tm, -Mathf.PI * 0.5f); break;

        case MegaAxis.Z: break;
        }

        meshtm = Matrix4x4.identity;
        MegaMatrix.Rotate(ref meshtm, Mathf.Deg2Rad * rot);

        meshrot = Quaternion.Euler(rot);
        tw      = meshrot;

        pathtm.SetTRS(offPath, Quaternion.Euler(rotPath), sclPath);
        float off    = 0.0f;
        int   trioff = 0;

        int vi = 0;
        int fi = 0;

        Vector3 sploff = Vector3.zero;

        if (snap)
        {
            sploff = path.splines[0].knots[0].p - path.splines[curve].knots[0].p;
        }

        int ax = (int)axis;

        float palpha = Start;           // * 0.01f;

        if (startObj != null && StartEnabled)
        {
            Vector3 sscl = StartScale * GlobalScale;
            Vector3 soff = Vector3.Scale(StartOff, sscl);

            off -= startObj.bounds.min[(int)axis] * sscl[ax];
            for (int i = 0; i < sverts.Length; i++)
            {
                Vector3 p = sverts[i];

                p             = Deform(p, path, palpha, off, sscl, RemoveDof, soff, sploff);            // + sploff;
                loftverts[vi] = p;
                loftuvs[vi++] = suvs[i];
            }

            for (int i = 0; i < stris.Length; i++)
            {
                lofttris[fi++] = stris[i] + triindex;
            }

            off   += startObj.bounds.max[(int)axis] * sscl[ax];
            off   += StartGap;
            trioff = vi;
        }

        if (mainObj != null && MainEnabled)
        {
            float mw = mainObj.bounds.size[(int)axis];

            Vector3 mscl = MainScale * GlobalScale;
            Vector3 moff = Vector3.Scale(MainOff, mscl);

            off -= mainObj.bounds.min[(int)axis] * mscl[ax];
            mw  *= mscl[(int)axis];

            for (int r = 0; r < repeat; r++)
            {
                for (int i = 0; i < mverts.Length; i++)
                {
                    Vector3 p = mverts[i];
                    p             = Deform(p, path, palpha, off, mscl, RemoveDof, moff, sploff);                // + sploff;
                    loftverts[vi] = p;
                    loftuvs[vi++] = muvs[i];
                }

                for (int i = 0; i < mtris.Length; i++)
                {
                    lofttris[fi++] = mtris[i] + trioff + triindex;
                }

                off   += mw;
                off   += Gap;
                trioff = vi;
            }

            off -= Gap;
            off += (mainObj.bounds.max[(int)axis] * mscl[ax]) - mw;
        }

        if (endObj != null && EndEnabled)
        {
            Vector3 escl = EndScale * GlobalScale;
            Vector3 eoff = Vector3.Scale(EndOff, escl);

            off -= endObj.bounds.min[(int)axis] * escl[ax];
            off += EndGap;

            for (int i = 0; i < everts.Length; i++)
            {
                Vector3 p = everts[i];
                p             = Deform(p, path, palpha, off, escl, RemoveDof, eoff, sploff);            // + sploff;
                loftverts[vi] = p;
                loftuvs[vi++] = euvs[i];
            }

            for (int i = 0; i < etris.Length; i++)
            {
                lofttris[fi++] = etris[i] + trioff + triindex;
            }

            trioff += everts.Length;
        }

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

        return(triindex);
    }
예제 #4
0
    public override int BuildMesh(MegaShapeLoft loft, int triindex)
    {
        MegaLoftLayerSimple layer = (MegaLoftLayerSimple)surfaceLoft.Layers[surfaceLayer];

        LayerLength = 1.0f / layer.GetLength(surfaceLoft);

        if (tangent < 0.1f)
        {
            tangent = 0.1f;
        }

        //mat = surfaceLoft.transform.localToWorldMatrix;
        //mat = transform.localToWorldMatrix * surfaceLoft.transform.worldToLocalMatrix;	// * transform.worldToLocalMatrix;	//mat = surfaceLoft.transform.localToWorldMatrix;
        mat = surfaceLoft.transform.localToWorldMatrix * transform.worldToLocalMatrix;

        tm = Matrix4x4.identity;

        MegaMatrix.Rotate(ref tm, Mathf.Deg2Rad * tmrot);

        MegaMatrix.Rotate(ref meshtm, Mathf.Deg2Rad * rot);

        meshtm = Matrix4x4.identity;
        MegaMatrix.Rotate(ref meshtm, Mathf.Deg2Rad * rot);

        meshrot = Quaternion.Euler(rot);
        float off    = 0.0f;
        int   trioff = 0;

        int vi = 0;

        float ca = CrossAlpha;

        if (ca > 0.99999f)
        {
            ca = 0.99999f;
        }

        int ax = (int)axis;

        if (startObj != null && StartEnabled)
        {
            Vector3 sscl = StartScale * GlobalScale;
            Vector3 soff = Vector3.Scale(StartOff + Offset, sscl);

            off -= startBounds.min[(int)axis] * sscl[ax];
            for (int i = 0; i < sverts.Length; i++)
            {
                Vector3 p = sverts[i];

                p             = Deform(p, surfaceLoft, layer, start, ca, off, sscl, RemoveDof, soff);
                loftverts[vi] = p;
                loftuvs[vi++] = suvs[i];
            }

            for (int i = 0; i < startlofttris.Count; i++)
            {
                int toff = startlofttris[i].offset;

                for (int t = 0; t < startlofttris[i].sourcetris.Length; t++)
                {
                    startlofttris[i].tris[toff++] = startlofttris[i].sourcetris[t] + trioff + triindex;
                }

                startlofttris[i].offset = toff;
            }

            off   += startBounds.max[(int)axis] * sscl[ax];
            off   += StartGap * GlobalScale;
            trioff = vi;
        }

        if (mainObj != null && MainEnabled)
        {
            for (int i = 0; i < mainlofttris.Count; i++)
            {
                mainlofttris[i].offset = 0;
            }

            float mw = mainBounds.size[(int)axis];

            Vector3 mscl = MainScale * GlobalScale;
            Vector3 moff = Vector3.Scale(MainOff + Offset, mscl);

            off -= mainBounds.min[(int)axis] * mscl[ax];

            mw *= mscl[(int)axis];
            float gaps = Gap * GlobalScale;

            for (int r = 0; r < repeat; r++)
            {
                for (int i = 0; i < mverts.Length; i++)
                {
                    Vector3 p = mverts[i];
                    p             = Deform(p, surfaceLoft, layer, start, ca, off, mscl, RemoveDof, moff);
                    loftverts[vi] = p;
                    loftuvs[vi++] = muvs[i];
                }

                for (int i = 0; i < mainlofttris.Count; i++)
                {
                    int toff = mainlofttris[i].offset;

                    for (int t = 0; t < mainlofttris[i].sourcetris.Length; t++)
                    {
                        mainlofttris[i].tris[toff++] = mainlofttris[i].sourcetris[t] + trioff + triindex;
                    }

                    mainlofttris[i].offset = toff;
                }

                off   += mw;
                off   += gaps;
                trioff = vi;
            }

            off -= gaps;
            off += (mainBounds.max[(int)axis] * mscl[ax]) - mw;
        }

        if (endObj != null && EndEnabled)
        {
            Vector3 escl = EndScale * GlobalScale;
            Vector3 eoff = Vector3.Scale(EndOff + Offset, escl);

            off -= endBounds.min[(int)axis] * escl[ax];
            off += EndGap * GlobalScale;

            for (int i = 0; i < everts.Length; i++)
            {
                Vector3 p = everts[i];
                p             = Deform(p, surfaceLoft, layer, start, ca, off, escl, RemoveDof, eoff);
                loftverts[vi] = p;
                loftuvs[vi++] = euvs[i];
            }

            for (int i = 0; i < endlofttris.Count; i++)
            {
                int toff = endlofttris[i].offset;

                for (int t = 0; t < endlofttris[i].sourcetris.Length; t++)
                {
                    endlofttris[i].tris[toff++] = endlofttris[i].sourcetris[t] + trioff + triindex;
                }

                endlofttris[i].offset = toff;
            }

            trioff += everts.Length;
        }

        return(triindex);
    }
예제 #5
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);
    }
    public override int BuildMesh(MegaShapeLoft loft, int triindex)
    {
        MegaLoftLayerSimple layer = (MegaLoftLayerSimple)surfaceLoft.Layers[surfaceLayer];

        LayerLength = 1.0f / layer.GetLength(surfaceLoft);

        if (tangent < 0.1f)
        {
            tangent = 0.1f;
        }

        //mat = surfaceLoft.transform.localToWorldMatrix;
        //mat = transform.localToWorldMatrix * surfaceLoft.transform.worldToLocalMatrix;	// * transform.worldToLocalMatrix;	//mat = surfaceLoft.transform.localToWorldMatrix;
        mat = surfaceLoft.transform.localToWorldMatrix * transform.worldToLocalMatrix;

        tm = Matrix4x4.identity;

        MegaMatrix.Rotate(ref tm, Mathf.Deg2Rad * tmrot);

        meshtm = Matrix4x4.identity;
        MegaMatrix.Rotate(ref meshtm, Mathf.Deg2Rad * rot);

        meshrot = Quaternion.Euler(rot);

        float off    = 0.0f;
        int   trioff = 0;

        int vi = 0;
        int fi = 0;

        float ca = CrossAlpha;          //Mathf.Repeat(CrossAlpha, 1.0001f);

        if (startObj != null && StartEnabled)
        {
            // deform start along the curve
            // so for each vertex find offset to calc alpha to find rotation and position of vert
            // need to add vert to list, also uv and tri (uv is a copy)

            Vector3 sscl = StartScale * GlobalScale;
            Vector3 soff = Vector3.Scale(StartOff + Offset, sscl);

            off -= startObj.bounds.min[(int)axis] * sscl[(int)axis];                    // - startObj.bounds.size[(int)axis];	//0.0f;	//sz;
            for (int i = 0; i < sverts.Length; i++)
            {
                Vector3 p = sverts[i];

                p             = Deform(p, surfaceLoft, layer, start, ca, off, sscl, RemoveDof, soff);
                loftverts[vi] = p;                      // + StartOff;
                loftuvs[vi++] = suvs[i];
            }

            // Tris are a copy, could use InsertRange
            for (int i = 0; i < stris.Length; i++)
            {
                lofttris[fi++] = stris[i] + triindex;
            }

            off   += startObj.bounds.max[(int)axis] * sscl[(int)axis]; //sw;
            off   += StartGap * GlobalScale;
            trioff = vi;                                               //verts.Count;
        }

        if (mainObj != null && MainEnabled)
        {
            float mw = mainObj.bounds.size[(int)axis];                  // * (GlobalScale * 0.01f);

            Vector3 mscl = MainScale * GlobalScale;
            Vector3 moff = Vector3.Scale(MainOff + Offset, mscl);

            off -= mainObj.bounds.min[(int)axis] * mscl[(int)axis];

            mw *= mscl[(int)axis];

            float gaps = Gap * GlobalScale;

            for (int r = 0; r < repeat; r++)
            {
                for (int i = 0; i < mverts.Length; i++)
                {
                    Vector3 p = mverts[i];
                    p             = Deform(p, surfaceLoft, layer, start, ca, off, mscl, RemoveDof, moff);
                    loftverts[vi] = p;                          // + MainOff;
                    loftuvs[vi++] = muvs[i];
                }

                for (int i = 0; i < mtris.Length; i++)
                {
                    lofttris[fi++] = mtris[i] + trioff + triindex;
                }

                off   += mw;
                off   += gaps;
                trioff = vi;                    //verts.Count;
            }

            off -= gaps;                //Gap;
            off += (mainObj.bounds.max[(int)axis] * mscl[(int)axis]) - mw;
        }

        if (endObj != null && EndEnabled)
        {
            Vector3 escl = EndScale * GlobalScale;
            Vector3 eoff = Vector3.Scale(EndOff + Offset, escl);

            off -= endObj.bounds.min[(int)axis] * escl[(int)axis];
            off += EndGap * GlobalScale;

            for (int i = 0; i < everts.Length; i++)
            {
                Vector3 p = everts[i];
                p             = Deform(p, surfaceLoft, layer, start, ca, off, escl, RemoveDof, eoff);
                loftverts[vi] = p;                      // + EndOff;
                loftuvs[vi++] = euvs[i];
            }

            for (int i = 0; i < etris.Length; i++)
            {
                lofttris[fi++] = etris[i] + trioff + triindex;
            }

            trioff += everts.Length;
        }

        return(triindex);
    }
예제 #7
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);
                }
            }
        }
    }
    public override int BuildMesh(MegaShapeLoft loft, int triindex)
    {
        MegaLoftLayerSimple layer = (MegaLoftLayerSimple)surfaceLoft.Layers[surfaceLayer];

        LayerLength = 1.0f / layer.GetLength(surfaceLoft);

        if (tangent < 0.1f)
        {
            tangent = 0.1f;
        }

        //mat = surfaceLoft.transform.localToWorldMatrix;
        mat = surfaceLoft.transform.localToWorldMatrix * transform.worldToLocalMatrix;
        //mat = transform.localToWorldMatrix * surfaceLoft.transform.worldToLocalMatrix;	// * transform.worldToLocalMatrix;	//mat = surfaceLoft.transform.localToWorldMatrix;
        //mat = surfaceLoft.transform.worldToLocalMatrix * transform.localToWorldMatrix;	// * transform.worldToLocalMatrix;	//mat = surfaceLoft.transform.localToWorldMatrix;

        tm = Matrix4x4.identity;
        tw = Quaternion.identity;

        //surfacetolofttm = mat * transform.worldToLocalMatrix;
        //surfacetolofttm = surfaceLoft.transform.localToWorldMatrix * transform.worldToLocalMatrix;
        MegaMatrix.Rotate(ref tm, Mathf.Deg2Rad * tmrot);

        float off    = 0.0f;
        int   trioff = 0;

        int vi = 0;

        float ca = CrossAlpha;

        if (ca > 0.99999f)
        {
            ca = 0.99999f;
        }

        // This is also done in prepareloft
        for (int r = 0; r < rules.Count; r++)
        {
            for (int i = 0; i < rules[r].lofttris.Count; i++)
            {
                rules[r].lofttris[i].offset = 0;
            }
        }

        for (int r = 0; r < loftobjs.Count; r++)
        {
            MegaLoftRule obj = loftobjs[r];

            Vector3 sscl = (scale + obj.scale) * GlobalScale;
            Vector3 soff = Vector3.Scale(offset + obj.offset, sscl);

            off -= obj.bounds.min[(int)axis];
            off += (obj.gapin * obj.bounds.size[(int)axis]);

            for (int i = 0; i < obj.verts.Length; i++)
            {
                Vector3 p = obj.verts[i];

                p             = Deform(p, surfaceLoft, layer, start, ca, off, sscl, RemoveDof, soff);
                loftverts[vi] = p;
                loftuvs[vi++] = obj.uvs[i];
            }

            for (int i = 0; i < obj.lofttris.Count; i++)
            {
                int toff = obj.lofttris[i].offset;

                for (int t = 0; t < obj.lofttris[i].sourcetris.Length; t++)
                {
                    obj.lofttris[i].tris[toff++] = obj.lofttris[i].sourcetris[t] + trioff + triindex;
                }

                obj.lofttris[i].offset = toff;
            }

            off   += obj.bounds.max[(int)axis];
            off   += (obj.gapout * obj.bounds.size[(int)axis]);
            trioff = vi;
        }

        return(triindex);
    }
예제 #9
0
    public override int BuildMesh(MegaShapeLoft loft, int triindex)
    {
        if (tangent < 0.1f)
        {
            tangent = 0.1f;
        }

        //mat = layerPath.transform.localToWorldMatrix;
        //mat = transform.localToWorldMatrix * layerPath.transform.worldToLocalMatrix;	// * transform.worldToLocalMatrix;	//mat = surfaceLoft.transform.localToWorldMatrix;
        mat = Matrix4x4.identity;
        tm  = Matrix4x4.identity;

        MegaMatrix.Rotate(ref tm, Mathf.Deg2Rad * tmrot);
        float off    = 0.0f;
        int   trioff = 0;
        int   vi     = 0;

        Vector3 sploff = Vector3.zero;

        if (snap)
        {
            sploff = layerPath.splines[0].knots[0].p - layerPath.splines[curve].knots[0].p;
        }

        for (int r = 0; r < rules.Count; r++)
        {
            for (int i = 0; i < rules[r].lofttris.Count; i++)
            {
                rules[r].lofttris[i].offset = 0;
            }
        }

        for (int r = 0; r < loftobjs.Count; r++)
        {
            MegaLoftRule obj = loftobjs[r];

            Vector3 sscl = (scale + obj.scale) * GlobalScale;
            Vector3 soff = Vector3.Scale(offset + obj.offset, sscl);

            off -= obj.bounds.min[(int)axis];
            off += (obj.gapin * obj.bounds.size[(int)axis]);

            for (int i = 0; i < obj.verts.Length; i++)
            {
                Vector3 p = obj.verts[i];

                p             = Deform(p, layerPath, start, off, sscl, RemoveDof, soff, sploff);
                loftverts[vi] = p;
                loftuvs[vi++] = obj.uvs[i];
            }

            for (int i = 0; i < obj.lofttris.Count; i++)
            {
                int toff = obj.lofttris[i].offset;

                for (int t = 0; t < obj.lofttris[i].sourcetris.Length; t++)
                {
                    obj.lofttris[i].tris[toff++] = obj.lofttris[i].sourcetris[t] + trioff + triindex;
                }

                obj.lofttris[i].offset = toff;
            }

            off   += obj.bounds.max[(int)axis];
            off   += (obj.gapout * obj.bounds.size[(int)axis]);
            trioff = vi;
        }

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

        return(triindex);
    }
예제 #10
0
    void BuildPolyShape(MegaLoftSection lsection, int steps, Vector3 off, float width)
    {
        int       curve = lsection.curve;
        int       k     = -1;
        Matrix4x4 tm1   = Matrix4x4.identity;
        Vector2   uv    = Vector2.zero;

        float start = crossStart;
        float len   = crossEnd;

        if (lsection.uselen)
        {
            start = lsection.start;
            len   = lsection.length;
        }

        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 alpha = start;

        if (shape.splines[curve].closed)
        {
            alpha = Mathf.Repeat(alpha, 1.0f);
        }

        uv.y = start;

        float   dist = 0.0f;
        Vector3 last = Vector3.zero;

        for (int i = 0; i <= steps; i++)
        {
            alpha = start + (((float)i / (float)steps) * len);

            if (shape.splines[curve].closed)
            {
                alpha = Mathf.Repeat(alpha, 1.0f);
            }

            Vector3 pos = tm1.MultiplyPoint3x4(shape.splines[curve].InterpCurve3D(alpha, shape.normalizedInterp, ref k) + off + lsection.offset);

            pos.x *= lsection.scale.x;
            pos.y *= lsection.scale.y;
            pos.z *= lsection.scale.z;
            verts.Add(pos);

            if (physuv)
            {
                if (i > 0)
                {
                    dist += Vector3.Distance(pos, last);
                }

                last = pos;
                uv.x = (dist / width);
            }
            else
            {
                uv.x = alpha;
            }

            uvs.Add(uv);
        }

        evert = verts.Count - 1;

        uv.y = start + len;

        lsection.crossverts = verts.ToArray();
        lsection.crossuvs   = uvs.ToArray();

        lsection.crosssize = MegaUtils.Extents(lsection.crossverts, out lsection.crossmin, out lsection.crossmax);
    }