Пример #1
0
    // we only need to build the savevertex and uvs if mesh def changes, else we can keep the uvs
    void BuildMesh()
    {
        if (!mesh)
        {
            mesh = GetComponent <MeshFilter>().sharedMesh;
            if (mesh == null)
            {
                updatemesh = true;
                return;
            }
        }

        if (hosespline.knots.Count == 0)
        {
            hosespline.AddKnot(Vector3.zero, Vector3.zero, Vector3.zero);
            hosespline.AddKnot(Vector3.zero, Vector3.zero, Vector3.zero);
        }

        FixHoseFillet();

        bool createfree = freecreate;

        if ((!createfree) && ((!custnode) || (!custnode2)))
        {
            createfree = true;
        }

        if (custnode && custnode2)
        {
            createfree = false;
        }

        float Lf = 0.0f;

        Tlocal = Matrix4x4.identity;

        starty = Vector3.zero;

        if (createfree)
        {
            Lf = noreflength;
        }
        else
        {
            starty = custnode.transform.up;             //Vector3.up;
            CalcSpline();
            Lf = Vector3.Distance(custnode.transform.position, custnode2.transform.position);
        }

        MegaHoseType wtype = wiretype;

        int Segs = segments;

        if (Segs < 3)
        {
            Segs = 3;
        }

        if (rebuildcross)
        {
            rebuildcross = false;
            int nfillets = 0;
            int nsides   = 0;

            if (wtype == MegaHoseType.Round)
            {
                NvertsPerRing = rndsides;
                if (NvertsPerRing < 3)
                {
                    NvertsPerRing = 3;
                }
            }
            else
            {
                if (wtype == MegaHoseType.Rectangle)
                {
                    nfillets = rectfilletsides;
                    if (nfillets < 0)
                    {
                        nfillets = 0;
                    }

                    if (smooth == MegaHoseSmooth.SMOOTHNONE)
                    {
                        NvertsPerRing = (nfillets > 0 ? 8 + 4 * (nfillets - 1) : 8);
                    }
                    else
                    {
                        NvertsPerRing = (nfillets > 0 ? 8 + 4 * (nfillets - 1) : 4);                            //4);
                    }
                }
                else
                {
                    nfillets = dsecfilletsides;
                    if (nfillets < 0)
                    {
                        nfillets = 0;
                    }
                    nsides = dsecrndsides;
                    if (nsides < 2)
                    {
                        nsides = 2;
                    }
                    int nsm1 = nsides - 1;
                    NvertsPerRing = (nfillets > 0 ? 6 + nsm1 + 2 * (nfillets - 1): 4 + nsm1);
                }
            }

            NvertsPerRing++;

            int NfacesPerEnd, NfacesPerRing, Nfaces = 0;
            //MegaHoseSmooth SMOOTH = smooth;

            Nverts = (Segs + 1) * (NvertsPerRing + 1);                  // + 2;
            if (capends)
            {
                Nverts += 2;
            }

            NfacesPerEnd  = NvertsPerRing;
            NfacesPerRing = 6 * NvertsPerRing;
            Nfaces        = Segs * NfacesPerRing;       // + 2 * NfacesPerEnd;

            if (capends)
            {
                Nfaces += 2 * NfacesPerEnd;
            }

            if (SaveVertex == null || SaveVertex.Length != NvertsPerRing)
            {
                SaveVertex = new Vector3[NvertsPerRing];
                SaveUV     = new Vector2[NvertsPerRing];
            }

            if (calcnormals)
            {
                if (SaveNormals == null || SaveNormals.Length != NvertsPerRing)
                {
                    SaveNormals = new Vector3[NvertsPerRing];
                }
            }

            MakeSaveVertex(NvertsPerRing, nfillets, nsides, wtype);

            if (verts == null || verts.Length != Nverts)
            {
                verts = new Vector3[Nverts];
                uvs   = new Vector2[Nverts];
                faces = new int[Nfaces * 3];
            }

            if (calcnormals && (normals == null || normals.Length != Nverts))
            {
                normals = new Vector3[Nverts];
            }
        }

        if (Nverts == 0)
        {
            return;
        }

        bool mapmenow = mapmemapme;

        int thisvert = 0;
        int last     = Nverts - 1;
        int last2    = last - 1;
        int lastvpr  = NvertsPerRing;           // - 1;
        int maxseg   = Segs + 1;

        float flexhere;
        float dadjust;
        float flexlen;
        float flex1 = flexstart;
        float flex2 = flexstop;
        int   flexn = flexcycles;
        float flexd = flexdiameter;

        Vector3 ThisPosition;
        Vector2 uv = Vector2.zero;

        Matrix4x4 RingTM    = Matrix4x4.identity;
        Matrix4x4 invRingTM = Matrix4x4.identity;

        Vector3 lastup = starty;

        float sizeadj = 1.0f;

        for (int i = 0; i < maxseg; i++)
        {
            float incr = (float)i / (float)Segs;

            if (createfree)
            {
                ThisPosition = new Vector3(0.0f, 0.0f, Lf * incr);
            }
            else
            {
                int k = 0;
                ThisPosition = hosespline.InterpCurve3D(incr, true, ref k);
            }

            RingTM = GetSplineMat(hosespline, incr, true, ref lastup);

            if (!createfree)
            {
                RingTM = Tlocal * RingTM;
            }

            if (calcnormals)
            {
                invRingTM = RingTM;
                MegaMatrix.NoTrans(ref invRingTM);
            }

            if ((incr > flex1) && (incr < flex2) && flexon)
            {
                flexlen = flex2 - flex1;
                if (flexlen < 0.01f)
                {
                    flexlen = 0.01f;
                }
                flexhere = (incr - flex1) / flexlen;

                float ang = (float)flexn * flexhere * (Mathf.PI * 2.0f) + PIover2;
                dadjust = 1.0f + flexd * (1.0f - Mathf.Sin(ang));                       //(float)flexn * flexhere * (Mathf.PI * 2.0f) + PIover2));
            }
            else
            {
                dadjust = 0.0f;
            }

            if (usebulgecurve)
            {
                if (dadjust == 0.0f)
                {
                    dadjust = 1.0f + (bulge.Evaluate(incr + bulgeoffset) * bulgeamount);
                }
                else
                {
                    dadjust += bulge.Evaluate(incr + bulgeoffset) * bulgeamount;
                }
            }

            if (usesizecurve)
            {
                sizeadj = size.Evaluate(incr);
            }

            uv.x = 0.999999f * incr * uvscale.x;

            for (int j = 0; j < NvertsPerRing; j++)
            {
                int jj = j;                     // % NvertsPerRing;

                if (mapmenow)
                {
                    uv.y          = SaveUV[jj].y;
                    uvs[thisvert] = uv;                         //new Vector2(0.999999f * incr * uvscale.x, SaveUV[jj].y);
                }

                if (dadjust != 0.0f)
                {
                    verts[thisvert] = RingTM.MultiplyPoint(sizeadj * dadjust * SaveVertex[jj]);
                }
                else
                {
                    verts[thisvert] = RingTM.MultiplyPoint(sizeadj * SaveVertex[jj]);
                }


                if (calcnormals)
                {
                    normals[thisvert] = invRingTM.MultiplyPoint(SaveNormals[jj]).normalized;                            //.MultiplyPoint(-SaveNormals[jj]);
                }
                thisvert++;
            }

            if (mapmenow)
            {
                //uvs[Nverts + i] = new Vector2(0.999999f * incr, 0.999f);
            }

            if (capends)
            {
                if (i == 0)
                {
                    verts[last2] = (createfree ? ThisPosition : Tlocal.MultiplyPoint(ThisPosition));
                    if (mapmenow)
                    {
                        uvs[last2] = Vector3.zero;
                    }
                }
                else
                {
                    if (i == Segs)
                    {
                        verts[last] = createfree ? ThisPosition : Tlocal.MultiplyPoint(ThisPosition);
                        if (mapmenow)
                        {
                            uvs[last] = Vector3.zero;
                        }
                    }
                }
            }
        }

        //	Now, set up the faces
        int thisface = 0, v1, v2, v3, v4;

        v3 = last2;
        if (capends)
        {
            for (int i = 0; i < NvertsPerRing - 1; i++)
            {
                v1 = i;
                v2 = (i < lastvpr ? v1 + 1 : v1 - lastvpr);
                //v5 = (i < lastvpr ? v2 : Nverts);

                faces[thisface++] = v2;
                faces[thisface++] = v1;
                faces[thisface++] = v3;
            }
        }

        int ringnum = NvertsPerRing;            // + 1;

        for (int i = 0; i < Segs; i++)
        {
            for (int j = 0; j < NvertsPerRing - 1; j++)
            {
                v1 = i * ringnum + j;
                v2 = v1 + 1;                    //(j < lastvpr? v1 + 1 : v1 - lastvpr);
                v4 = v1 + ringnum;
                v3 = v2 + ringnum;
                faces[thisface++] = v1;
                faces[thisface++] = v2;
                faces[thisface++] = v3;
                faces[thisface++] = v1;
                faces[thisface++] = v3;
                faces[thisface++] = v4;
            }
        }

        int basevert = Segs * ringnum;          //NvertsPerRing;

        v3 = Nverts - 1;
        if (capends)
        {
            for (int i = 0; i < NvertsPerRing - 1; i++)
            {
                v1 = i + basevert;
                v2 = (i < lastvpr? v1 + 1 : v1 - lastvpr);
                //v5 = (i < lastvpr? v2 : Nverts + Segs);
                faces[thisface++] = v1;
                faces[thisface++] = v2;
                faces[thisface++] = v3;
            }
        }

        mesh.Clear();

        mesh.subMeshCount = 1;

        mesh.vertices  = verts;
        mesh.uv        = uvs;
        mesh.triangles = faces;

        if (calcnormals)
        {
            mesh.normals = normals;
        }
        else
        {
            mesh.RecalculateNormals();
        }
        mesh.RecalculateBounds();

#if UNITY_5_5 || UNITY_5_6 || UNITY_2017
#else
        if (optimize)
        {
            ;
        }
#endif

        if (calctangents)
        {
            MegaUtils.BuildTangents(mesh);
        }

        if (recalcCollider)
        {
            if (meshCol == null)
            {
                meshCol = GetComponent <MeshCollider>();
            }

            if (meshCol != null)
            {
                meshCol.sharedMesh = null;
                meshCol.sharedMesh = mesh;
                //bool con = meshCol.convex;
                //meshCol.convex = con;
            }
        }
    }
Пример #2
0
    // we only need to build the savevertex and uvs if mesh def changes, else we can keep the uvs
    void BuildMesh()
    {
        if (!mesh)
        {
            mesh = GetComponent <MeshFilter>().sharedMesh;
            if (mesh == null)
            {
                updatemesh = true;
                return;
            }
        }

        if (hosespline.knots.Count == 0)
        {
            hosespline.AddKnot(Vector3.zero, Vector3.zero, Vector3.zero);
            hosespline.AddKnot(Vector3.zero, Vector3.zero, Vector3.zero);
        }

        FixHoseFillet();

        bool createfree = freecreate;

        if ((!createfree) && ((!custnode) || (!custnode2)))
        {
            createfree = true;
        }

        if (custnode && custnode2)
        {
            createfree = false;
        }

        Matrix4x4 mat1, mat2;
        float     Lf = 0.0f;

        Tlocal = Matrix4x4.identity;

        Vector3 startvec, endvec, startpoint, endpoint, endy;

        starty = Vector3.zero;
        roty   = Vector3.zero;
        yangle = 0.0f;

        Vector3 RV = Vector3.zero;

        if (createfree)
        {
            Lf = noreflength;
        }
        else
        {
            RV = up;                    //new Vector3(xtmp, ytmp, ztmp);

            mat1 = custnode.transform.localToWorldMatrix;
            mat2 = custnode2.transform.localToWorldMatrix;

            Matrix4x4 mato1 = Matrix4x4.identity;
            Matrix4x4 mato2 = Matrix4x4.identity;
            mato1 = Matrix4x4.TRS(offset, Quaternion.Euler(rotate), scale);
            mato1 = mato1.inverse;

            mato2 = Matrix4x4.TRS(offset1, Quaternion.Euler(rotate1), scale1);
            mato2 = mato2.inverse;
            S     = transform.localToWorldMatrix;

            Matrix4x4 mat1NT, mat2NT;

            mat1NT = mat1;
            mat2NT = mat2;
            MegaMatrix.NoTrans(ref mat1NT);
            MegaMatrix.NoTrans(ref mat2NT);
            Vector3 P1 = mat1.MultiplyPoint(mato1.GetColumn(3));
            Vector3 P2 = mat2.MultiplyPoint(mato2.GetColumn(3));

            startvec = mat1NT.MultiplyPoint(mato1.GetColumn(2));
            endvec   = mat2NT.MultiplyPoint(mato2.GetColumn(2));
            starty   = mat1NT.MultiplyPoint(mato1.GetColumn(1));
            endy     = mat2NT.MultiplyPoint(mato2.GetColumn(1));

            Matrix4x4 SI = S.inverse;

            Vector3   P0 = SI.MultiplyPoint(P1);
            Matrix4x4 T1 = mat1;
            MegaMatrix.NoTrans(ref T1);

            Vector3 RVw = T1.MultiplyPoint(RV);
            Lf = (P2 - P1).magnitude;
            Vector3 Zw;
            if (Lf < 0.01f)
            {
                Zw = P1.normalized;
            }
            else
            {
                Zw = (P2 - P1).normalized;
            }
            Vector3 Xw = Vector3.Cross(RVw, Zw).normalized;
            Vector3 Yw = Vector3.Cross(Zw, Xw).normalized;

            MegaMatrix.NoTrans(ref SI);

            Vector3 Xs = SI.MultiplyPoint(Xw);
            Vector3 Ys = SI.MultiplyPoint(Yw);
            Vector3 Zs = SI.MultiplyPoint(Zw);
            Tlocal.SetColumn(0, Xs);
            Tlocal.SetColumn(1, Ys);
            Tlocal.SetColumn(2, Zs);
            MegaMatrix.SetTrans(ref Tlocal, P0);

            // move z-axes of end transforms into local frame
            Matrix4x4 TlocalInvNT = Tlocal;
            MegaMatrix.NoTrans(ref TlocalInvNT);

            TlocalInvNT = TlocalInvNT.inverse;

            float tenstop = tension1;                   // * 0.01f;
            float tensbot = tension2;                   // * 0.01f;

            startvec = tensbot * (TlocalInvNT.MultiplyPoint(startvec));
            endvec   = tenstop * (TlocalInvNT.MultiplyPoint(endvec));

            starty = TlocalInvNT.MultiplyPoint(starty);
            endy   = TlocalInvNT.MultiplyPoint(endy);

            yangle = Mathf.Acos(Vector3.Dot(starty, endy));

            if (yangle > EPSILON)
            {
                roty = Vector3.Cross(starty, endy).normalized;
            }
            else
            {
                roty = Vector3.zero;
            }

            startpoint = Vector3.zero;
            endpoint   = new Vector3(0.0f, 0.0f, Lf);

            hosespline.knots[0].p      = startpoint;
            hosespline.knots[0].invec  = startpoint - startvec;
            hosespline.knots[0].outvec = startpoint + startvec;

            hosespline.knots[1].p      = endpoint;
            hosespline.knots[1].invec  = endpoint + endvec;
            hosespline.knots[1].outvec = endpoint - endvec;

            hosespline.CalcLength();                    //10);
        }

        MegaHoseType wtype = wiretype;

        int Segs = segments;

        if (Segs < 3)
        {
            Segs = 3;
        }

        if (rebuildcross)
        {
            rebuildcross = false;
            int nfillets = 0;
            int nsides   = 0;

            if (wtype == MegaHoseType.Round)
            {
                NvertsPerRing = rndsides;
                if (NvertsPerRing < 3)
                {
                    NvertsPerRing = 3;
                }
            }
            else
            {
                if (wtype == MegaHoseType.Rectangle)
                {
                    nfillets = rectfilletsides;
                    if (nfillets < 0)
                    {
                        nfillets = 0;
                    }

                    if (smooth == MegaHoseSmooth.SMOOTHNONE)
                    {
                        NvertsPerRing = (nfillets > 0 ? 8 + 4 * (nfillets - 1) : 8);
                    }
                    else
                    {
                        NvertsPerRing = (nfillets > 0 ? 8 + 4 * (nfillets - 1) : 4);                            //4);
                    }
                }
                else
                {
                    nfillets = dsecfilletsides;
                    if (nfillets < 0)
                    {
                        nfillets = 0;
                    }
                    nsides = dsecrndsides;
                    if (nsides < 2)
                    {
                        nsides = 2;
                    }
                    int nsm1 = nsides - 1;
                    NvertsPerRing = (nfillets > 0 ? 6 + nsm1 + 2 * (nfillets - 1): 4 + nsm1);
                }
            }

            NvertsPerRing++;

            int NfacesPerEnd, NfacesPerRing, Nfaces = 0;
            //MegaHoseSmooth SMOOTH = smooth;

            Nverts = (Segs + 1) * (NvertsPerRing + 1);                  // + 2;
            if (capends)
            {
                Nverts += 2;
            }

            NfacesPerEnd  = NvertsPerRing;
            NfacesPerRing = 6 * NvertsPerRing;
            Nfaces        = Segs * NfacesPerRing;       // + 2 * NfacesPerEnd;

            if (capends)
            {
                Nfaces += 2 * NfacesPerEnd;
            }

            if (SaveVertex == null || SaveVertex.Length != NvertsPerRing)
            {
                SaveVertex = new Vector3[NvertsPerRing];
                SaveUV     = new Vector2[NvertsPerRing];
            }

            if (calcnormals)
            {
                if (SaveNormals == null || SaveNormals.Length != NvertsPerRing)
                {
                    SaveNormals = new Vector3[NvertsPerRing];
                }
            }

            MakeSaveVertex(NvertsPerRing, nfillets, nsides, wtype);

            if (verts == null || verts.Length != Nverts)
            {
                verts = new Vector3[Nverts];
                uvs   = new Vector2[Nverts];
                faces = new int[Nfaces * 3];
            }

            if (calcnormals && (normals == null || normals.Length != Nverts))
            {
                normals = new Vector3[Nverts];
            }
        }

        if (Nverts == 0)
        {
            return;
        }

        bool mapmenow = mapmemapme;

        int thisvert = 0;
        int last     = Nverts - 1;
        int last2    = last - 1;
        int lastvpr  = NvertsPerRing;           // - 1;
        int maxseg   = Segs + 1;

        float flexhere;
        float dadjust;
        float flexlen;
        float flex1 = flexstart;
        float flex2 = flexstop;
        int   flexn = flexcycles;
        float flexd = flexdiameter;

        Vector3 ThisPosition;
        Vector3 ThisXAxis, ThisYAxis, ThisZAxis;
        Vector2 uv = Vector2.zero;

        Matrix4x4 RingTM    = Matrix4x4.identity;
        Matrix4x4 invRingTM = Matrix4x4.identity;

        for (int i = 0; i < maxseg; i++)
        {
            float incr = (float)i / (float)Segs;

            if (createfree)
            {
                ThisPosition = new Vector3(0.0f, 0.0f, Lf * incr);
                ThisXAxis    = new Vector3(1.0f, 0.0f, 0.0f);
                ThisYAxis    = new Vector3(0.0f, 1.0f, 0.0f);
                ThisZAxis    = new Vector3(0.0f, 0.0f, 1.0f);
            }
            else
            {
                int k = 0;
                ThisPosition = hosespline.InterpCurve3D(incr, true, ref k);
                ThisZAxis    = (hosespline.InterpCurve3D(incr + 0.001f, true, ref k) - ThisPosition).normalized;

                ThisYAxis = starty;
                if (yangle > EPSILON)
                {
                    RotateOnePoint(ref ThisYAxis, Vector3.zero, roty, incr * yangle);
                }

                ThisXAxis = Vector3.Cross(ThisYAxis, ThisZAxis).normalized;

                ThisYAxis = Vector3.Cross(ThisZAxis, ThisXAxis);
            }

            RingTM.SetColumn(0, ThisXAxis);
            RingTM.SetColumn(1, ThisYAxis);
            RingTM.SetColumn(2, ThisZAxis);
            MegaMatrix.SetTrans(ref RingTM, ThisPosition);

            if (!createfree)
            {
                RingTM = Tlocal * RingTM;
            }

            if (calcnormals)
            {
                invRingTM = RingTM;
                MegaMatrix.NoTrans(ref invRingTM);
                //invRingTM = invRingTM.inverse.transpose;
            }

            if ((incr > flex1) && (incr < flex2) && flexon)
            {
                flexlen = flex2 - flex1;
                if (flexlen < 0.01f)
                {
                    flexlen = 0.01f;
                }
                flexhere = (incr - flex1) / flexlen;

                float ang = (float)flexn * flexhere * (Mathf.PI * 2.0f) + PIover2;
                dadjust = 1.0f + flexd * (1.0f - Mathf.Sin(ang));                       //(float)flexn * flexhere * (Mathf.PI * 2.0f) + PIover2));
            }
            else
            {
                dadjust = 0.0f;
            }

            if (usebulgecurve)
            {
                if (dadjust == 0.0f)
                {
                    dadjust = 1.0f + (bulge.Evaluate(incr + bulgeoffset) * bulgeamount);
                }
                else
                {
                    dadjust += bulge.Evaluate(incr + bulgeoffset) * bulgeamount;
                }
            }

            uv.x = 0.999999f * incr * uvscale.x;

            for (int j = 0; j < NvertsPerRing; j++)
            {
                int jj = j;                     // % NvertsPerRing;

                if (mapmenow)
                {
                    uv.y          = SaveUV[jj].y;
                    uvs[thisvert] = uv;                         //new Vector2(0.999999f * incr * uvscale.x, SaveUV[jj].y);
                }

                if (dadjust != 0.0f)
                {
                    verts[thisvert] = RingTM.MultiplyPoint(dadjust * SaveVertex[jj]);
                }
                else
                {
                    verts[thisvert] = RingTM.MultiplyPoint(SaveVertex[jj]);
                }

                if (calcnormals)
                {
                    normals[thisvert] = invRingTM.MultiplyPoint(SaveNormals[jj]).normalized;                            //.MultiplyPoint(-SaveNormals[jj]);
                }
                //if ( j == 0 )
                //{
                //	Debug.Log("norm " + normals[thisvert].ToString("0.000") + " save " + SaveNormals[jj].ToString("0.000"));
                //}

                thisvert++;
            }

            if (mapmenow)
            {
                //uvs[Nverts + i] = new Vector2(0.999999f * incr, 0.999f);
            }

            if (capends)
            {
                if (i == 0)
                {
                    verts[last2] = (createfree ? ThisPosition : Tlocal.MultiplyPoint(ThisPosition));
                    if (mapmenow)
                    {
                        uvs[last2] = Vector3.zero;
                    }
                }
                else
                {
                    if (i == Segs)
                    {
                        verts[last] = createfree ? ThisPosition : Tlocal.MultiplyPoint(ThisPosition);
                        if (mapmenow)
                        {
                            uvs[last] = Vector3.zero;
                        }
                    }
                }
            }
        }

        //	Now, set up the faces
        int thisface = 0, v1, v2, v3, v4;

        v3 = last2;
        if (capends)
        {
            for (int i = 0; i < NvertsPerRing - 1; i++)
            {
                v1 = i;
                v2 = (i < lastvpr ? v1 + 1 : v1 - lastvpr);
                //v5 = (i < lastvpr ? v2 : Nverts);

                faces[thisface++] = v2;
                faces[thisface++] = v1;
                faces[thisface++] = v3;
            }
        }

        int ringnum = NvertsPerRing;            // + 1;

        for (int i = 0; i < Segs; i++)
        {
            for (int j = 0; j < NvertsPerRing - 1; j++)
            {
                v1 = i * ringnum + j;
                v2 = v1 + 1;                    //(j < lastvpr? v1 + 1 : v1 - lastvpr);
                v4 = v1 + ringnum;
                v3 = v2 + ringnum;
                faces[thisface++] = v1;
                faces[thisface++] = v2;
                faces[thisface++] = v3;
                faces[thisface++] = v1;
                faces[thisface++] = v3;
                faces[thisface++] = v4;
            }
        }

        int basevert = Segs * ringnum;          //NvertsPerRing;

        v3 = Nverts - 1;
        if (capends)
        {
            for (int i = 0; i < NvertsPerRing - 1; i++)
            {
                v1 = i + basevert;
                v2 = (i < lastvpr? v1 + 1 : v1 - lastvpr);
                //v5 = (i < lastvpr? v2 : Nverts + Segs);
                faces[thisface++] = v1;
                faces[thisface++] = v2;
                faces[thisface++] = v3;
            }
        }

        mesh.Clear();

        mesh.subMeshCount = 1;

        mesh.vertices  = verts;
        mesh.uv        = uvs;
        mesh.triangles = faces;

        if (calcnormals)
        {
            mesh.normals = normals;
        }
        else
        {
            mesh.RecalculateNormals();
        }
        mesh.RecalculateBounds();

#if UNITY_5_5 || UNITY_5_6 || UNITY_2017
#else
        if (optimize)
        {
            ;
        }
#endif

        if (calctangents)
        {
            MegaUtils.BuildTangents(mesh);
        }

        if (recalcCollider)
        {
            if (meshCol == null)
            {
                meshCol = GetComponent <MeshCollider>();
            }

            if (meshCol != null)
            {
                meshCol.sharedMesh = null;
                meshCol.sharedMesh = mesh;
                //bool con = meshCol.convex;
                //meshCol.convex = con;
            }
        }
    }
Пример #3
0
    public void RefreshMesh()
    {
        if (!rebuild)
        {
            return;
        }

        int offset = 0;

        // Check on vertex count here, if over 65000 then start a new object
        // TODO: Some layers wont add to mesh, ie colliders and object scatters
        for (int i = 0; i < Layers.Length; i++)
        {
            if (Layers[i].Valid())
            {
                // Only call if verts need rebuilding, so per layer rebuild (spline change will do all)
                Layers[i].BuildMesh(this, offset);

                //Debug.Log("copy " + verts.Length);
                if (useColors)
                {
                    Layers[i].CopyVertData(ref verts, ref uvs, ref cols, offset);
                }
                else
                {
                    Layers[i].CopyVertData(ref verts, ref uvs, offset);
                }

                offset += Layers[i].NumVerts(); //loftverts.Length;
            }
        }

        //Vector2[] lmuvs = mesh.uv2;

        //mesh.Clear();

        mesh.vertices = verts;
        mesh.uv       = uvs;

        //if ( lmuvs.Length == uvs.Length )
        //{
        //Debug.Log("Setting uv2");
        //mesh.uv2 = lmuvs;
        //}

        if (useColors)
        {
            mesh.colors = cols;
        }


        mesh.RecalculateNormals();

        if (Tangents)
        {
            MegaUtils.BuildTangents(mesh);
        }

#if UNITY_5_5 || UNITY_5_6 || UNITY_6
#else
        if (Optimize)
        {
            mesh.Optimize();
        }
#endif

        if (DoBounds)
        {
            mesh.RecalculateBounds();
        }

        if (DoCollider)
        {
            RefreshCollider();
        }
    }
Пример #4
0
    void BuildMeshOld(Mesh mesh)
    {
        Width  = Mathf.Clamp(Width, 0.0f, float.MaxValue);
        Length = Mathf.Clamp(Length, 0.0f, float.MaxValue);
        Height = Mathf.Clamp(Height, 0.0f, float.MaxValue);

        LengthSegs = Mathf.Clamp(LengthSegs, 1, 200);
        HeightSegs = Mathf.Clamp(HeightSegs, 1, 200);
        WidthSegs  = Mathf.Clamp(WidthSegs, 1, 200);

        Vector3 vb = new Vector3(Width, Height, Length) / 2.0f;
        Vector3 va = -vb;

        if (PivotBase)
        {
            va.y = 0.0f;
            vb.y = Height;
        }

        if (PivotEdge)
        {
            va.x = 0.0f;
            vb.x = Width;
        }

        float dx = Width / (float)WidthSegs;
        float dy = Height / (float)HeightSegs;
        float dz = Length / (float)LengthSegs;

        Vector3 p = va;

        // Lists should be static, clear out to reuse
        List <Vector3> verts = new List <Vector3>();
        List <Vector2> uvs   = new List <Vector2>();
        List <int>     tris  = new List <int>();
        List <int>     tris1 = new List <int>();
        List <int>     tris2 = new List <int>();

        Vector2 uv = Vector2.zero;

        // Do we have top and bottom
        if (Width > 0.0f && Length > 0.0f)
        {
            //Matrix4x4 tm1 = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0.0f, rotate, 0.0f), Vector3.one);

            p.y = vb.y;
            for (int iz = 0; iz <= LengthSegs; iz++)
            {
                p.x = va.x;
                for (int ix = 0; ix <= WidthSegs; ix++)
                {
                    verts.Add(p);

                    if (genUVs)
                    {
                        uv.x = (p.x + vb.x) / Width;
                        uv.y = (p.z + vb.z) / Length;
                        uvs.Add(uv);
                    }
                    p.x += dx;
                }
                p.z += dz;
            }

            for (int iz = 0; iz < LengthSegs; iz++)
            {
                int kv = iz * (WidthSegs + 1);
                for (int ix = 0; ix < WidthSegs; ix++)
                {
                    MakeQuad1(tris, kv, kv + WidthSegs + 1, kv + WidthSegs + 2, kv + 1);
                    kv++;
                }
            }

            int index = verts.Count;

            p.y = va.y;
            p.z = va.z;

            for (int iy = 0; iy <= LengthSegs; iy++)
            {
                p.x = va.x;
                for (int ix = 0; ix <= WidthSegs; ix++)
                {
                    verts.Add(p);
                    if (genUVs)
                    {
                        uv.x = 1.0f - ((p.x + vb.x) / Width);
                        uv.y = ((p.z + vb.z) / Length);

                        uvs.Add(uv);
                    }
                    p.x += dx;
                }
                p.z += dz;
            }

            for (int iy = 0; iy < LengthSegs; iy++)
            {
                int kv = iy * (WidthSegs + 1) + index;
                for (int ix = 0; ix < WidthSegs; ix++)
                {
                    MakeQuad1(tris1, kv, kv + 1, kv + WidthSegs + 2, kv + WidthSegs + 1);
                    kv++;
                }
            }
        }

        // Front back
        if (Width > 0.0f && Height > 0.0f)
        {
            int index = verts.Count;

            p.z = va.z;
            p.y = va.y;
            for (int iz = 0; iz <= HeightSegs; iz++)
            {
                p.x = va.x;
                for (int ix = 0; ix <= WidthSegs; ix++)
                {
                    verts.Add(p);
                    if (genUVs)
                    {
                        uv.x = (p.x + vb.x) / Width;
                        uv.y = (p.y + vb.y) / Height;
                        uvs.Add(uv);
                    }
                    p.x += dx;
                }
                p.y += dy;
            }

            for (int iz = 0; iz < HeightSegs; iz++)
            {
                int kv = iz * (WidthSegs + 1) + index;
                for (int ix = 0; ix < WidthSegs; ix++)
                {
                    MakeQuad1(tris2, kv, kv + WidthSegs + 1, kv + WidthSegs + 2, kv + 1);
                    kv++;
                }
            }

            index = verts.Count;

            p.z = vb.z;
            p.y = va.y;
            for (int iy = 0; iy <= HeightSegs; iy++)
            {
                p.x = va.x;
                for (int ix = 0; ix <= WidthSegs; ix++)
                {
                    verts.Add(p);
                    if (genUVs)
                    {
                        uv.x = (p.x + vb.x) / Width;
                        uv.y = (p.y + vb.y) / Height;
                        uvs.Add(uv);
                    }
                    p.x += dx;
                }
                p.y += dy;
            }

            for (int iy = 0; iy < HeightSegs; iy++)
            {
                int kv = iy * (WidthSegs + 1) + index;
                for (int ix = 0; ix < WidthSegs; ix++)
                {
                    MakeQuad1(tris2, kv, kv + 1, kv + WidthSegs + 2, kv + WidthSegs + 1);
                    kv++;
                }
            }
        }

        // Left Right
        if (Length > 0.0f && Height > 0.0f)
        {
            int index = verts.Count;

            p.x = vb.x;
            p.y = va.y;
            for (int iz = 0; iz <= HeightSegs; iz++)
            {
                p.z = va.z;
                for (int ix = 0; ix <= LengthSegs; ix++)
                {
                    verts.Add(p);
                    if (genUVs)
                    {
                        uv.x = (p.z + vb.z) / Length;
                        uv.y = (p.y + vb.y) / Height;
                        uvs.Add(uv);
                    }
                    p.z += dz;
                }
                p.y += dy;
            }

            for (int iz = 0; iz < HeightSegs; iz++)
            {
                int kv = iz * (LengthSegs + 1) + index;
                for (int ix = 0; ix < LengthSegs; ix++)
                {
                    MakeQuad1(tris2, kv, kv + LengthSegs + 1, kv + LengthSegs + 2, kv + 1);
                    kv++;
                }
            }

            index = verts.Count;

            p.x = va.x;
            p.y = va.y;
            for (int iy = 0; iy <= HeightSegs; iy++)
            {
                p.z = va.z;
                for (int ix = 0; ix <= LengthSegs; ix++)
                {
                    verts.Add(p);
                    if (genUVs)
                    {
                        uv.x = (p.z + vb.z) / Length;
                        uv.y = (p.y + vb.y) / Height;
                        uvs.Add(uv);
                    }

                    p.z += dz;
                }
                p.y += dy;
            }

            for (int iy = 0; iy < HeightSegs; iy++)
            {
                int kv = iy * (LengthSegs + 1) + index;
                for (int ix = 0; ix < LengthSegs; ix++)
                {
                    MakeQuad1(tris2, kv, kv + 1, kv + LengthSegs + 2, kv + LengthSegs + 1);
                    kv++;
                }
            }
        }

        mesh.Clear();

        mesh.subMeshCount = 3;

        mesh.vertices = verts.ToArray();
        mesh.uv       = uvs.ToArray();
        mesh.SetTriangles(tris.ToArray(), 0);
        mesh.SetTriangles(tris1.ToArray(), 1);
        mesh.SetTriangles(tris2.ToArray(), 2);

        mesh.RecalculateNormals();

        if (tangents)
        {
            MegaUtils.BuildTangents(mesh);
        }

#if UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019
#else
        if (optimize)
        {
            mesh.Optimize();
        }
#endif

        mesh.RecalculateBounds();
    }
Пример #5
0
    public void BuildMeshFromLayersNew()
    {
        // Check for any valid layers
        if (rebuild)            //&& Layers.Length > 0 )
        {
            //Debug.Log("*************** Build Mesh ****************");
            Layers = GetComponents <MegaLoftLayerBase>();

            if (Layers.Length > 0)
            {
                if (mesh == null)                       // Should be shapemesh, and this should be a util function
                {
                    MeshFilter mf = gameObject.GetComponent <MeshFilter>();

                    if (mf == null)
                    {
                        mf      = gameObject.AddComponent <MeshFilter>();
                        mf.name = gameObject.name;
                    }

                    mf.sharedMesh = new Mesh();
                    MeshRenderer mr = gameObject.GetComponent <MeshRenderer>();
                    if (mr == null)
                    {
                        mr = gameObject.AddComponent <MeshRenderer>();
                    }

                    mesh = mf.sharedMesh;                       //Utils.GetMesh(gameObject);
                }

                if (!realtime)
                {
                    rebuild = false;
                }

                int smcount  = 0;
                int numverts = 0;

                for (int i = 0; i < Layers.Length; i++)
                {
                    if (Layers[i].Valid())
                    {
                        //smcount++;
                        //smcount += Layers[i].NumMaterials();
                        Layers[i].PrepareLoft(this, i);
                        smcount  += Layers[i].NumMaterials();
                        numverts += Layers[i].NumVerts();
                    }
                }

                //Debug.Log("1 " + gameObject.name);

                if (numverts > 65535)
                {
                    Debug.LogWarning("Loft Layer will have too many vertices. Lower the detail settings or disable a layer.");
                    return;
                }

                //Debug.Log("verts " + verts.Length + " numverts " + numverts);
                if (verts == null || verts.Length != numverts)
                {
                    verts = new Vector3[numverts];
                }

                if (uvs == null || uvs.Length != numverts)
                {
                    uvs = new Vector2[numverts];
                }

                if (useColors && (cols == null || cols.Length != numverts))
                {
                    cols = new Color[numverts];
                }

                //if ( useColor )
                //{
                //	if ( colors == null || colors.Length != numverts )
                //		colors = new Color[numverts];
                //}

                int offset = 0;

                // Check on vertex count here, if over 65000 then start a new object
                // TODO: Some layers wont add to mesh, ie colliders and object scatters
                for (int i = 0; i < Layers.Length; i++)
                {
                    if (Layers[i].Valid())
                    {
                        // Only call if verts need rebuilding, so per layer rebuild (spline change will do all)
                        Layers[i].BuildMesh(this, offset);

                        //Debug.Log("copy " + verts.Length);
                        if (useColors)
                        {
                            Layers[i].CopyVertData(ref verts, ref uvs, ref cols, offset);
                        }
                        else
                        {
                            Layers[i].CopyVertData(ref verts, ref uvs, offset);
                        }

                        offset += Layers[i].NumVerts();                         //loftverts.Length;
                    }
                }

                //Vector2[] lmuvs = mesh.uv2;

                mesh.Clear();

                mesh.subMeshCount = smcount;
                mesh.vertices     = verts;
                mesh.uv           = uvs;

                //if ( lmuvs.Length == uvs.Length )
                //{
                //Debug.Log("Setting uv2");
                //mesh.uv2 = lmuvs;
                //}

                if (useColors)
                {
                    mesh.colors = cols;
                }

                vertcount = verts.Length;
                polycount = 0;
                Material[] mats = new Material[smcount];

                int mi = 0;

                for (int i = 0; i < Layers.Length; i++)
                {
                    if (Layers[i].Valid())
                    {
                        for (int m = 0; m < Layers[i].NumMaterials(); m++)
                        {
                            mats[mi] = Layers[i].GetMaterial(m);

                            // We should be able to strip this
                            int[] tris = Layers[i].GetTris(m);
                            if (tris == null || tris.Length < 3)
                            {
                                mesh.SetTriangles(emptytris, mi);
                            }
                            else
                            {
                                mesh.SetTriangles(tris, mi);
                            }
                            polycount += tris.Length;
                            mi++;
                        }
                    }
                }

                mesh.RecalculateNormals();

                if (Tangents)
                {
                    MegaUtils.BuildTangents(mesh);
                }

#if UNITY_5_5 || UNITY_5_6 || UNITY_6
#else
                if (Optimize)
                {
                    mesh.Optimize();
                }
#endif

                if (DoBounds)
                {
                    mesh.RecalculateBounds();
                }

                MeshRenderer mr1 = gameObject.GetComponent <MeshRenderer>();
                if (mr1 != null)
                {
                    mr1.sharedMaterials = mats;
                }

                meshCol = GetComponent <MeshCollider>();

                if (DoCollider)
                {
                    RefreshCollider();
                }

                if (!updating)
                {
                    updating = true;
                    //MegaShapeLoft[]	lofts = (MegaShapeLoft[])FindSceneObjectsOfType(typeof(MegaShapeLoft));
                    MegaShapeLoft[] lofts = (MegaShapeLoft[])FindObjectsOfType(typeof(MegaShapeLoft));

                    for (int i = 0; i < lofts.Length; i++)
                    {
                        if (lofts[i] != this && lofts[i].LoftNotify(this, 0))
                        {
                            lofts[i].BuildMeshFromLayersNew();
                        }
                    }
                    updating = false;
                }
            }
        }
    }
Пример #6
0
    public void BuildMesh(Mesh mesh)
    {
        if (shape == null)
        {
            return;
        }

        verts.Clear();
        uvs.Clear();
        tris.Clear();
        dtris.Clear();
        //normals.Clear();

        pshape = MakePolyShape(shape, steps);

        Matrix4x4 axismat = Matrix4x4.identity;

        Quaternion q = Quaternion.identity;

        switch (direction)
        {
        case MegaAxis.X: q = Quaternion.Euler(90.0f, 0.0f, 0.0f); break;

        case MegaAxis.Y: q = Quaternion.Euler(0.0f, 90.0f, 0.0f); break;

        case MegaAxis.Z: q = Quaternion.Euler(0.0f, 0.0f, 90.0f); break;
        }

        int vertLevels = segments + 1;

        axismat.SetTRS(axis, q, Vector3.one);
        Matrix4x4 iaxis = axismat.inverse;

        Matrix4x4 rotmat = Matrix4x4.identity;

        float totlen = pshape.length[pshape.length.Count - 1];

        Vector2 uv  = Vector2.zero;
        Vector2 uv1 = uv;

        Matrix4x4 uvmat = Matrix4x4.identity;

        uvmat.SetTRS(Vector3.zero, Quaternion.Euler(0.0f, 0.0f, uvrotate), Vector3.one);

        Vector3 p   = Vector3.zero;
        Vector3 scl = scale;
        //Vector3 norm = Vector3.zero;
        Vector3 lsp = Vector3.zero;

        int   vindex   = 0;
        float alphasub = 0.0f;

        if (!pivotbase)
        {
            alphasub = 1.0f;
        }
        for (int level = 0; level < vertLevels; level++)
        {
            uv.y = (float)level / (float)segments;
            float ang = (uv.y * degrees) + startang;                    //360.0f;
            rotmat = Matrix4x4.identity;

            MegaMatrix.RotateZ(ref rotmat, ang * Mathf.Deg2Rad);

            Matrix4x4 tm = iaxis * rotmat * axismat;

            //Vector3 lp = Vector3.zero;

            float cumlen = 0.0f;

            //int nix = normals.Count;

            //Debug.Log("minz " + min.z + " limz " + limits.z);
            for (int v = 0; v < pshape.points.Count; v++)
            {
                lsp = pshape.points[v];                 //Vector3.Scale(pshape.points[v], scl);

                //float halpha = 1.0f - ((lsp.z - min.z) / limits.z);
                float halpha = alphasub - ((lsp.z - min.z) / limits.z);

                //lsp.x += profilecrv.Evaluate(halpha);
                //lsp.y += offsetycrv.Evaluate(halpha);

                if (usescalecrv)
                {
                    //float halpha = (lsp.z - min.z) / limits.z;

                    float adj = scaleamthgt.Evaluate(halpha) * globalscale;
                    scl.x = 1.0f + (scalexcrv.Evaluate(uv.y) * adj);
                    scl.y = 1.0f + (scaleycrv.Evaluate(uv.y) * adj);
                    scl.z = 1.0f + (scalezcrv.Evaluate(uv.y) * adj);
                }

                lsp.x *= scl.x;
                lsp.y *= scl.y;
                lsp.z *= scl.z;

                if (twist != 0.0f)
                {
                    float tang = ((twist * halpha * twistcrv.Evaluate(halpha)) + ang) * Mathf.Deg2Rad;
                    float c    = Mathf.Cos(tang);
                    float s    = Mathf.Sin(tang);

                    rotmat[0, 0] = c;
                    rotmat[0, 1] = s;
                    rotmat[1, 0] = -s;
                    rotmat[1, 1] = c;

                    tm = iaxis * rotmat * axismat;
                }
#if false
                norm.x = -(lsp.y - lp.y);
                norm.y = lsp.x - lp.x;
                norm.z = 0.0f;                  //lsp.z - lp.z;
#endif
                //norm.x = -(lsp.z - lp.z);	//- (lsp.y - lp.y);
                //norm.y = 0.0f;	//lsp.x - lp.x;
                //norm.z = lsp.x - lp.x; //0.0f;	//lsp.z - lp.z;

                //lp = lsp;

                p = tm * lsp;                   //Vector3.Scale(lsp, scl);

                //p.x += offsetxcrv.Evaluate(halpha);
                //p.y += offsetycrv.Evaluate(halpha);

                verts.Add(p);
#if false
                if (v == 0)
                {
                }
                else
                {
                    if (v == 1)
                    {
                        if (flip)
                        {
                            normals.Add(-tm.MultiplyVector(norm).normalized);
                        }
                        else
                        {
                            normals.Add(tm.MultiplyVector(norm).normalized);
                        }
                    }

                    if (flip)
                    {
                        normals.Add(-tm.MultiplyVector(norm).normalized);
                    }
                    else
                    {
                        normals.Add(tm.MultiplyVector(norm).normalized);
                    }
                }
#endif
                cumlen = pshape.length[v];

                uv.x   = cumlen / totlen;               // / cumlen;
                uv1    = uv;
                uv1.x *= uvscale.x;
                uv1.y *= uvscale.y;
                uv1   += uvoffset;

                uv1 = uvmat.MultiplyPoint(uv1);
                uvs.Add(uv1);
            }

            //if ( shape.splines[curve].closed )
            //{
            //normals[normals.Count - 1] = normals[nix];
            //}
        }

        // Faces
        int vcount = pshape.points.Count;

        for (int level = 0; level < vertLevels - 1; level++)
        {
            int voff = level * vcount;

            for (int p1 = 0; p1 < pshape.points.Count - 1; p1++)
            {
                int v1 = p1 + voff;
                int v2 = v1 + 1;
                int v3 = level == vertLevels - 1 ? p1 : v1 + vcount;
                int v4 = v3 + 1;

                if (flip)
                {
                    tris.Add(v1);
                    tris.Add(v4);
                    tris.Add(v2);

                    tris.Add(v1);
                    tris.Add(v3);
                    tris.Add(v4);
                }
                else
                {
                    tris.Add(v2);
                    tris.Add(v4);
                    tris.Add(v1);

                    tris.Add(v4);
                    tris.Add(v3);
                    tris.Add(v1);
                }
            }
        }

        if (doublesided)
        {
            int vc = verts.Count;

            for (int i = 0; i < vc; i++)
            {
                verts.Add(verts[i]);
                uvs.Add(uvs[i]);
                normals.Add(-normals[i]);
                vindex++;
            }

            for (int i = 0; i < tris.Count; i += 3)
            {
                int v1 = tris[i];
                int v2 = tris[i + 1];
                int v3 = tris[i + 2];

                dtris.Add(v3 + vc);
                dtris.Add(v2 + vc);
                dtris.Add(v1 + vc);
            }
        }

        //MeshFilter mf = GetComponent<MeshFilter>();
        //mesh = mf.sharedMesh;
        //if ( mesh == null )
        //{
        //	mesh = new Mesh();
        //	mesh.name = "Lathe";
        //	mf.sharedMesh = mesh;
        //}

        mesh.Clear();
        mesh.vertices = verts.ToArray();
        mesh.uv       = uvs.ToArray();

        if (doublesided)
        {
            mesh.subMeshCount = 2;
        }
        else
        {
            mesh.subMeshCount = 1;
        }

        mesh.SetTriangles(tris.ToArray(), 0);

        if (doublesided)
        {
            mesh.SetTriangles(dtris.ToArray(), 1);
        }

        mesh.RecalculateBounds();
        //mesh.normals = normals.ToArray();
        mesh.RecalculateNormals();

        if (degrees == 360.0f)
        {
            Vector3[] n = mesh.normals;

            for (int v = 0; v < pshape.points.Count; v++)
            {
                Vector3 nn = n[v] + n[n.Length - pshape.points.Count + v];
                n[v] = n[n.Length - pshape.points.Count + v] = nn.normalized;
            }

            mesh.normals = n;
        }

        if (buildTangents)
        {
            MegaUtils.BuildTangents(mesh);
        }
    }
Пример #7
0
    public void BuildMeshNew(MegaScatterLayer inf)
    {
        if (verts.Count == 0 || inf.obj == null)
        {
            return;
        }

        GameObject cobj = new GameObject();

        if (hideObjects)
        {
            cobj.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector | HideFlags.NotEditable;
        }
        cobj.name = "Scatter Mesh";
        MeshFilter mf = cobj.AddComponent <MeshFilter>();

        mf.sharedMesh = new Mesh();
        MeshRenderer cmr = cobj.AddComponent <MeshRenderer>();

        cmr.sharedMaterials = inf.obj.GetComponent <MeshRenderer>().sharedMaterials;

        MeshCollider meshCol = null;

        if (!inf.nocollider)
        {
            if (inf.buildcollider)
            {
                meshCol = cobj.AddComponent <MeshCollider>();
            }
        }

        if (shape != null)
        {
            cobj.transform.position   = shape.gameObject.transform.position;
            cobj.transform.localScale = shape.gameObject.transform.localScale;
            cobj.transform.rotation   = shape.gameObject.transform.rotation;
        }
        else
        {
            cobj.transform.position   = transform.position;
            cobj.transform.localScale = transform.localScale;
            cobj.transform.rotation   = transform.rotation;
        }

        cobj.transform.parent = transform;
        cobj.isStatic         = inf.markstatic;

        Mesh mesh;

        if (Application.isPlaying)
        {
            mesh = mf.mesh;
        }
        else
        {
            mesh = mf.sharedMesh;
        }

        mesh.Clear();
        mesh.subMeshCount = submeshes.Count;
        mesh.vertices     = verts.ToArray();
        mesh.uv           = uvs.ToArray();
        mesh.normals      = norms.ToArray();
        mesh.colors       = colors.ToArray();

        for (int i = 0; i < submeshes.Count; i++)
        {
            mesh.SetTriangles(submeshes[i].tris.ToArray(), i);
        }

        mesh.RecalculateBounds();

        if (inf.buildtangents)
        {
            MegaUtils.BuildTangents(mesh);
        }

        if (!inf.nocollider)
        {
            if (inf.buildcollider)
            {
                //MeshCollider meshCol = cobj.AddComponent<MeshCollider>();

                if (inf.proxymesh)
                {
                    Mesh colmesh = meshCol.sharedMesh;

                    if (colmesh == mesh)
                    {
                        colmesh = new Mesh();
                    }

                    colmesh.Clear();
                    colmesh.vertices  = proxyverts.ToArray();
                    colmesh.normals   = proxynorms.ToArray();
                    colmesh.triangles = proxytris.ToArray();

                    meshCol.sharedMesh = null;
                    meshCol.sharedMesh = colmesh;
                    meshCol.enabled    = false;
                }
                else
                {
                    meshCol.sharedMesh = null;
                    meshCol.sharedMesh = mesh;
                    meshCol.enabled    = false;
                }
            }
            else
            {
                BoxCollider bc = cobj.AddComponent <BoxCollider>();
                bc.enabled = false;
            }
        }

        objcount++;
    }
Пример #8
0
    void BuildMesh(Mesh mesh)
    {
        page_width  = Mathf.Clamp(page_width, 0.0f, float.MaxValue);
        page_length = Mathf.Clamp(page_length, 0.0f, float.MaxValue);
        page_height = Mathf.Clamp(page_height, 0.0f, float.MaxValue);

        length_segs = Mathf.Clamp(length_segs, 1, 200);
        height_segs = Mathf.Clamp(height_segs, 1, 200);
        width_segs  = Mathf.Clamp(width_segs, 1, 200);

        Vector3 vb = new Vector3(page_width, page_height, page_length) / 2.0f;
        Vector3 va = -vb;

        va.y = 0.0f;
        vb.y = page_height;

        float dx = page_width / (float)width_segs;
        float dy = page_height / (float)height_segs;
        float dz = page_length / (float)length_segs;

        Vector3 p = va;

        // Lists should be static, clear out to reuse
        List <Vector3> verts = new List <Vector3>();
        List <Vector2> uvs   = new List <Vector2>();
        List <int>     tris  = new List <int>();
        List <int>     tris1 = new List <int>();
        List <int>     tris2 = new List <int>();

        Vector2 uv = Vector2.zero;

        // Do we have top and bottom
        if (page_width > 0.0f && page_length > 0.0f)
        {
            Matrix4x4 tm1 = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0.0f, rotate, 0.0f), Vector3.one);

            Vector3 uv1 = Vector3.zero;

            p.y = vb.y;
            for (int iz = 0; iz <= length_segs; iz++)
            {
                p.x = va.x;
                for (int ix = 0; ix <= width_segs; ix++)
                {
                    verts.Add(p);

                    uv.x = (p.x - va.x) / page_width;
                    uv.y = (p.z + vb.z) / page_length;

                    uv1.x = uv.x - 0.5f;
                    uv1.y = 0.0f;
                    uv1.z = uv.y - 0.5f;

                    uv1  = tm1.MultiplyPoint3x4(uv1);
                    uv.x = 0.5f + uv1.x;
                    uv.y = 0.5f + uv1.z;

                    uvs.Add(uv);
                    p.x += dx;
                }
                p.z += dz;
            }

            for (int iz = 0; iz < length_segs; iz++)
            {
                int kv = iz * (width_segs + 1);
                for (int ix = 0; ix < width_segs; ix++)
                {
                    MakeQuad1(tris, kv, kv + width_segs + 1, kv + width_segs + 2, kv + 1);
                    kv++;
                }
            }

            int index = verts.Count;

            p.y = va.y;
            p.z = va.z;

            for (int iy = 0; iy <= length_segs; iy++)
            {
                p.x = va.x;
                for (int ix = 0; ix <= width_segs; ix++)
                {
                    verts.Add(p);
                    uv.x = 1.0f - ((p.x + vb.x) / page_width);
                    uv.y = ((p.z + vb.z) / page_length);

                    uv1.x = uv.x - 0.5f;
                    uv1.y = 0.0f;
                    uv1.z = uv.y - 0.5f;

                    uv1  = tm1.MultiplyPoint3x4(uv1);
                    uv.x = 0.5f + uv1.x;
                    uv.y = 0.5f + uv1.z;

                    uvs.Add(uv);
                    p.x += dx;
                }
                p.z += dz;
            }

            for (int iy = 0; iy < length_segs; iy++)
            {
                int kv = iy * (width_segs + 1) + index;
                for (int ix = 0; ix < width_segs; ix++)
                {
                    MakeQuad1(tris1, kv, kv + 1, kv + width_segs + 2, kv + width_segs + 1);
                    kv++;
                }
            }
        }

        // Front back
        if (page_width > 0.0f && page_height > 0.0f)
        {
            int index = verts.Count;

            p.z = va.z;
            p.y = va.y;
            for (int iz = 0; iz <= height_segs; iz++)
            {
                p.x = va.x;
                for (int ix = 0; ix <= width_segs; ix++)
                {
                    verts.Add(p);
                    uv.x = (p.x + vb.x) / page_width;
                    uv.y = (p.y + vb.y) / page_height;
                    uvs.Add(uv);
                    p.x += dx;
                }
                p.y += dy;
            }

            for (int iz = 0; iz < height_segs; iz++)
            {
                int kv = iz * (width_segs + 1) + index;
                for (int ix = 0; ix < width_segs; ix++)
                {
                    MakeQuad1(tris2, kv, kv + width_segs + 1, kv + width_segs + 2, kv + 1);
                    kv++;
                }
            }

            index = verts.Count;

            p.z = vb.z;
            p.y = va.y;
            for (int iy = 0; iy <= height_segs; iy++)
            {
                p.x = va.x;
                for (int ix = 0; ix <= width_segs; ix++)
                {
                    verts.Add(p);
                    uv.x = (p.x + vb.x) / page_width;
                    uv.y = (p.y + vb.y) / page_height;
                    uvs.Add(uv);
                    p.x += dx;
                }
                p.y += dy;
            }

            for (int iy = 0; iy < height_segs; iy++)
            {
                int kv = iy * (width_segs + 1) + index;
                for (int ix = 0; ix < width_segs; ix++)
                {
                    MakeQuad1(tris2, kv, kv + 1, kv + width_segs + 2, kv + width_segs + 1);
                    kv++;
                }
            }
        }

        // Left Right
        if (page_length > 0.0f && page_height > 0.0f)
        {
            int index = verts.Count;

            p.x = vb.x;
            p.y = va.y;
            for (int iz = 0; iz <= height_segs; iz++)
            {
                p.z = va.z;
                for (int ix = 0; ix <= length_segs; ix++)
                {
                    verts.Add(p);
                    uv.x = (p.z + vb.z) / page_length;
                    uv.y = (p.y + vb.y) / page_height;
                    uvs.Add(uv);
                    p.z += dz;
                }
                p.y += dy;
            }

            for (int iz = 0; iz < height_segs; iz++)
            {
                int kv = iz * (length_segs + 1) + index;
                for (int ix = 0; ix < length_segs; ix++)
                {
                    MakeQuad1(tris2, kv, kv + length_segs + 1, kv + length_segs + 2, kv + 1);
                    kv++;
                }
            }

            index = verts.Count;

            p.x = va.x;
            p.y = va.y;
            for (int iy = 0; iy <= height_segs; iy++)
            {
                p.z = va.z;
                for (int ix = 0; ix <= length_segs; ix++)
                {
                    verts.Add(p);
                    uv.x = (p.z + vb.z) / page_length;
                    uv.y = (p.y + vb.y) / page_height;
                    uvs.Add(uv);

                    p.z += dz;
                }
                p.y += dy;
            }

            for (int iy = 0; iy < height_segs; iy++)
            {
                int kv = iy * (length_segs + 1) + index;
                for (int ix = 0; ix < length_segs; ix++)
                {
                    MakeQuad1(tris2, kv, kv + 1, kv + length_segs + 2, kv + length_segs + 1);
                    kv++;
                }
            }
        }

        mesh.Clear();

        mesh.subMeshCount = 3;

        mesh.vertices = verts.ToArray();
        mesh.uv       = uvs.ToArray();
        mesh.SetTriangles(tris.ToArray(), 0);
        mesh.SetTriangles(tris1.ToArray(), 1);
        mesh.SetTriangles(tris2.ToArray(), 2);

        mesh.RecalculateNormals();

        if (tangents)
        {
            MegaUtils.BuildTangents(mesh);
        }

        if (optimize)
        {
            mesh.Optimize();
        }

        mesh.RecalculateBounds();
    }
Пример #9
0
    // Best if we calc the normals to avoid issues at join
    public void BuildMesh()
    {
        if (makeMesh)
        {
            //makeMesh = false;

            float sdist = stepdist * 0.1f;
            if (splines[0].length / sdist > 1500.0f)
            {
                sdist = splines[0].length / 1500.0f;
            }

            Vector3 size = Vector3.zero;
            verts.Clear();
            uvs.Clear();
            tris.Clear();
            tris1.Clear();
            tris2.Clear();
            tris = MegaTriangulator.Triangulate(this, splines[0], sdist, ref verts, ref uvs, ref tris, Pivot, ref size);

            if (shapemesh == null)
            {
                MeshFilter mf = gameObject.GetComponent <MeshFilter>();

                if (mf == null)
                {
                    mf = gameObject.AddComponent <MeshFilter>();
                }

                mf.sharedMesh = new Mesh();
                MeshRenderer mr = gameObject.GetComponent <MeshRenderer>();
                if (mr == null)
                {
                    mr = gameObject.AddComponent <MeshRenderer>();
                }

                Material[] mats = new Material[3];

                mr.sharedMaterials = mats;

                shapemesh = mf.sharedMesh;                      //Utils.GetMesh(gameObject);
            }

            int vcount = verts.Count;
            int tcount = tris.Count;

            if (Height < 0.0f)
            {
                Height = 0.0f;
            }
            float h = Height;                   //Mathf.Abs(Height);

            Matrix4x4 tm1 = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(UVRotate.x, UVRotate.y, 0.0f), new Vector3(UVScale.x, 1.0f, UVScale.y));

            //Vector3 size = shapemesh.bounds.size;

            if (GenUV)
            {
                uvs.Clear();                    // need to stop triangulator doing uvs
                Vector2 uv  = Vector2.zero;
                Vector3 uv1 = Vector3.zero;
                for (int i = 0; i < verts.Count; i++)
                {
                    uv1.x = (verts[i].x);                       // * UVScale.x) + UVOffset.x;	// * UVScale.x;
                    uv1.z = (verts[i].z);                       // * UVScale.y) + UVOffset.y;	// * UVScale.y;

                    if (!PhysUV)
                    {
                        uv1.x /= size.x;
                        uv1.z /= size.z;
                    }
                    uv1  = tm1.MultiplyPoint3x4(uv1);
                    uv.x = uv1.x + UVOffset.x;
                    uv.y = uv1.z + UVOffset.y;
                    uvs.Add(uv);
                }
            }

            if (DoubleSided && h != 0.0f)
            {
                //vcount = verts.Count;
                for (int i = 0; i < vcount; i++)
                {
                    Vector3 p = verts[i];

                    if (UseHeightCurve)
                    {
                        float alpha = MegaTriangulator.m_points[i].z / splines[0].length;
                        p.y -= h * heightCrv.Evaluate(alpha + heightOff);
                    }
                    else
                    {
                        p.y -= h;
                    }

                    verts.Add(p);                       //verts[i]);
                    uvs.Add(uvs[i]);
                }

                //tcount = tris.Count;
                for (int i = tcount - 1; i >= 0; i--)
                {
                    tris1.Add(tris[i] + vcount);
                }
            }
#if true
            // Do edge
            if (h != 0.0f)
            {
                int vc = verts.Count;

                Vector3 ep  = Vector3.zero;
                Vector2 euv = Vector2.zero;

                tm1 = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(UVRotate1.x, UVRotate1.y, 0.0f), new Vector3(UVScale1.x, 1.0f, UVScale1.y));

                // Top loop
                for (int i = 0; i < MegaTriangulator.m_points.Count; i++)
                {
                    ep = verts[i];
                    //ep.x = MegaTriangulator.m_points[i].x;
                    //ep.y = 0.0f;
                    //ep.z = MegaTriangulator.m_points[i].y;
                    verts.Add(ep);

                    //euv.x = (MegaTriangulator.m_points[i].z / splines[0].length) * 4.0f;
                    //euv.x = (MegaTriangulator.m_points[i].z * UVScale1.x) + UVOffset1.x;	// / splines[0].length) * 4.0f;
                    //euv.y = UVOffset1.y;	//0.0f;

                    ep.x = (MegaTriangulator.m_points[i].z);                            // * UVScale1.x) + UVOffset1.x;	// / splines[0].length) * 4.0f;

                    if (!PhysUV)
                    {
                        ep.x /= size.x;
                    }

                    ep.y = 0.0f;
                    ep.z = 0.0f;                        //UVOffset1.y;	//0.0f;

                    ep    = tm1.MultiplyPoint3x4(ep);
                    euv.x = ep.x + UVOffset1.x;
                    euv.y = ep.z + UVOffset1.y;

                    uvs.Add(euv);
                }
                // Add first point again
                ep = verts[0];
                //ep.y -= h * heightCrv.Evaluate(0.0f);
                verts.Add(ep);

                //euv.x = 1.0f * 4.0f;	//MegaTriangulator.m_points[0].z / splines[0].length;
                euv.x = (splines[0].length * UVScale1.x) + UVOffset1.x;                 //1.0f * 4.0f;	//MegaTriangulator.m_points[0].z / splines[0].length;
                if (!PhysUV)
                {
                    euv.x /= size.x;
                }
                euv.y = 0.0f + UVOffset1.y;
                uvs.Add(euv);

                // Bot loop
                float hd = 1.0f;

                for (int i = 0; i < MegaTriangulator.m_points.Count; i++)
                {
                    float alpha = MegaTriangulator.m_points[i].z / splines[0].length;

                    ep = verts[i];
                    if (UseHeightCurve)
                    {
                        hd = heightCrv.Evaluate(alpha + heightOff);
                    }

                    ep.y -= h * hd;                             //heightCrv.Evaluate(alpha);

                    verts.Add(ep);

                    ep.x = (MegaTriangulator.m_points[i].z); // * UVScale1.x) + UVOffset1.x;	// / splines[0].length) * 4.0f;
                    ep.z = ep.y;                             //0.0f;	//UVOffset1.y;	//0.0f;
                    ep.y = 0.0f;

                    if (!PhysUV)
                    {
                        ep.x /= size.x;
                        ep.z /= (h * hd);
                    }
                    ep    = tm1.MultiplyPoint3x4(ep);
                    euv.x = ep.x + UVOffset1.x;
                    euv.y = ep.z + UVOffset1.y;

                    //euv.x = (MegaTriangulator.m_points[i].z / splines[0].length) * 4.0f;
                    //euv.x = (MegaTriangulator.m_points[i].z * UVScale1.x) + UVOffset1.x;
                    //euv.y = ((h * hd) * UVScale1.y) + UVOffset1.y;	//1.0f;
                    uvs.Add(euv);
                }
                // Add first point again
                ep = verts[0];

                if (UseHeightCurve)
                {
                    hd = heightCrv.Evaluate(0.0f + heightOff);
                }

                ep.y -= h * hd;                 //heightCrv.Evaluate(0.0f);
                verts.Add(ep);

                ep.x = (MegaTriangulator.m_points[0].z); // * UVScale1.x) + UVOffset1.x;	// / splines[0].length) * 4.0f;
                ep.z = ep.y;                             //0.0f;	//UVOffset1.y;	//0.0f;
                ep.y = 0.0f;

                if (!PhysUV)
                {
                    ep.x /= size.x;
                    ep.z /= (h * hd);
                }
                ep    = tm1.MultiplyPoint3x4(ep);
                euv.x = ep.x + UVOffset1.x;
                euv.y = ep.z + UVOffset1.y;

                //euv.x = (MegaTriangulator.m_points[i].z / splines[0].length) * 4.0f;
                //euv.x = (MegaTriangulator.m_points[i].z * UVScale1.x) + UVOffset1.x;
                //euv.y = ((h * hd) * UVScale1.y) + UVOffset1.y;	//1.0f;
                uvs.Add(euv);

                //euv.x = 1.0f;	//MegaTriangulator.m_points[0].z / splines[0].length;
                //euv.x = (splines[0].length * UVScale1.x) + UVOffset1.x;	//MegaTriangulator.m_points[0].z / splines[0].length;
                //euv.y = (h * hd * UVScale1.y) + UVOffset1.y;	//1.0f;

                //if ( !PhysUV )
                //{
                //	euv.x /= size.x;
                //	euv.y /=
                //}
                //uvs.Add(euv);

                // Faces
                int ecount = MegaTriangulator.m_points.Count + 1;

                int ip = 0;
                for (ip = 0; ip < MegaTriangulator.m_points.Count; ip++)
                {
                    tris2.Add(ip + vc);
                    tris2.Add(ip + vc + ecount);
                    tris2.Add(ip + vc + 1);

                    tris2.Add(ip + vc + 1);
                    tris2.Add(ip + vc + ecount);
                    tris2.Add(ip + vc + ecount + 1);
                }
#if false
                tris.Add(ip + vc);
                tris.Add(ip + vc + ecount);
                tris.Add(vc);

                tris.Add(vc);
                tris.Add(ip + vc + ecount);
                tris.Add(vc + ecount);
#endif
            }
#endif
            shapemesh.Clear();

            shapemesh.vertices = verts.ToArray();
            shapemesh.uv       = uvs.ToArray();

            shapemesh.subMeshCount = 3;
            shapemesh.SetTriangles(tris.ToArray(), 0);
            shapemesh.SetTriangles(tris1.ToArray(), 1);
            shapemesh.SetTriangles(tris2.ToArray(), 2);

            //shapemesh.triangles = tris.ToArray();
            shapemesh.RecalculateNormals();
            shapemesh.RecalculateBounds();

            if (CalcTangents)
            {
                MegaUtils.BuildTangents(shapemesh);
            }

            //if ( mesh != null )
            //{
            //BuildMesh(mesh);
            //MegaModifyObject mo = GetComponent<MegaModifyObject>();
            //if ( mo != null )
            //{
            //	mo.MeshUpdated();
            //}
            //}
        }
    }