Beispiel #1
0
    void Update()
    {
        if (animate)
        {
            time += Time.deltaTime * speed;

            switch (LoopMode)
            {
            case MegaRepeatMode.Loop:               t = Mathf.Repeat(time, MaxTime); break;

            case MegaRepeatMode.PingPong:   t = Mathf.PingPong(time, MaxTime); break;

            case MegaRepeatMode.Clamp:              t = Mathf.Clamp(time, 0.0f, MaxTime); break;
            }

            for (int s = 0; s < splines.Count; s++)
            {
                if (splines[s].animations != null && splines[s].animations.Count > 0)
                {
                    for (int i = 0; i < splines[s].animations.Count; i++)
                    {
                        Vector3 pos = splines[s].animations[i].con.GetVector3(t);

                        switch (splines[s].animations[i].t)
                        {
                        case 0: splines[splines[s].animations[i].s].knots[splines[s].animations[i].p].invec = pos;  break;

                        case 1: splines[splines[s].animations[i].s].knots[splines[s].animations[i].p].p = pos;  break;

                        case 2: splines[splines[s].animations[i].s].knots[splines[s].animations[i].p].outvec = pos;  break;
                        }
                    }

                    splines[s].CalcLength(10);                          // could use less here
                }
            }
        }

        // Options here:
        // Uv scale, offset, rotate, physuv, genuv
        // Optimize
        // recalcnorms
        // tangents
        // fill in shape
        // pipe along shape
        // wall along shape
        // double sided
        // extrude on fill
        //BuildMesh();
#if false
        if (makeMesh)
        {
            //makeMesh = false;
            List <Vector3> verts = new List <Vector3>();
            List <Vector2> uvs   = new List <Vector2>();

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

            int[] tris = MegaTriangulator.Triangulate(this, splines[0], sdist, ref verts, ref uvs);

            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[1];

                mr.sharedMaterials = mats;

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

            //Vector3[] verts = new Vector3[splines[0].knots.Count];
            //for ( int i = 0; i < splines[0].knots.Count; i++ )
            //{
            //	verts[i] = splines[0].knots[i].p;
            //}

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

            shapemesh.triangles = tris;
            shapemesh.RecalculateNormals();
            shapemesh.RecalculateBounds();
        }
#endif
    }
Beispiel #2
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;
            }

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

            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;

            float h = Mathf.Abs(Height);

            if (GenUV)
            {
                uvs.Clear();                    // need to stop triangulator doing uvs
                Vector2 uv = Vector2.zero;
                for (int i = 0; i < verts.Count; i++)
                {
                    uv.x = (verts[i].x * UVScale.x) + UVOffset.x;                       // * UVScale.x;
                    uv.y = (verts[i].z * UVScale.y) + UVOffset.y;                       // * UVScale.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);
                    }
                    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;
                Vector3 euv = Vector2.zero;
                // 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;
                    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;
                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);
                    }

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

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

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

                //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;
                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();
        }
    }
Beispiel #3
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)
            {
                MegaMeshPage.BuildTangents(shapemesh);
            }

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