Esempio n. 1
0
    public static Mesh BuildMesh(Vector3 startingPoint, float radius, int iterations, Color vertexColor)
    {
        //x = r cos(v) cos(u)
        //y = r cos(v) sin(u)     u = [0, 2 * Pi)
        //z = r sin(v)            v = [-Pi / 2, Pi / 2]

        Vector3[,] coordMatrix = new Vector3[iterations + 1, iterations + 1];

        List <Vector3> coords = new List <Vector3>();

        for (int v = 0; v <= iterations; v++)
        {
            for (int u = 0; u <= iterations; u++)
            {
                float   ucoord = (float)u / (float)iterations * (2f * Mathf.PI);
                float   vcoord = (float)v / (float)iterations * (Mathf.PI) - (Mathf.PI / 2f);
                float   x      = radius * Mathf.Cos(vcoord) * Mathf.Cos(ucoord);
                float   y      = radius * Mathf.Cos(vcoord) * Mathf.Sin(ucoord);
                float   z      = radius * Mathf.Sin(vcoord);
                Vector3 coord  = new Vector3(x, y, z) + startingPoint;
                coords.Add(coord);
                coordMatrix[u, v] = coord;
            }
        }
        //lines.positionCount = coords.Count;
        //lines.SetPositions(coords.ToArray());

        int         triangleIndex = 0;
        List <Quad> qlist         = new List <Quad>();

        for (int v = 0; v < iterations; v++)
        {
            for (int u = 0; u < iterations; u++)
            {
                qlist.Add(Quad.Build(coordMatrix[u, v], coordMatrix[u + 1, v], coordMatrix[u, v + 1], coordMatrix[u + 1, v + 1], vertexColor, triangleIndex, true));
                triangleIndex += 4;
            }
        }

        Vector3[] verts  = new Vector3[qlist.Count * 4];
        Vector3[] norms  = new Vector3[qlist.Count * 4];
        Color[]   colors = new Color[qlist.Count * 4];
        int[]     tris   = new int[qlist.Count * 6];
        for (int z = 0; z < qlist.Count; z++)
        {
            qlist[z].Vertices.CopyTo(verts, z * 4);
            qlist[z].Normals.CopyTo(norms, z * 4);
            qlist[z].Colors.CopyTo(colors, z * 4);
            qlist[z].TriangleIndices.CopyTo(tris, z * 6);
        }

        Mesh m = new Mesh();

        m.vertices  = verts;
        m.normals   = norms;
        m.colors    = colors;
        m.triangles = tris;
        m.RecalculateNormals();
        m.RecalculateTangents();

        return(m);
    }
    public static Mesh BuildMesh(Vector3 startingPoint, Vector3 endingPoint, float radiusStart, float radiusEnd, Color vertexColor, Vector3 angleRotation, int capSegments, bool capEnds)
    {
        if (startingPoint.y == 0f)
        {
            startingPoint = new Vector3(startingPoint.x, 0.01f, startingPoint.z);
        }

        /*
         * if (endingPoint.y == 0f)
         * {
         *  startingPoint = new Vector3(endingPoint.x, 0.01f, endingPoint.z);
         * }
         */
        float   aMinus       = startingPoint.x;
        Vector3 delta        = new Vector3(aMinus, 0f, 0f);
        Vector3 direction    = endingPoint - startingPoint;
        Vector3 startShifted = (startingPoint - delta).normalized;

        if (startShifted == Vector3.zero)
        {
            startShifted = new Vector3(0.0f, 1.0f, 0.0f);
        }
        Vector3 ndir     = direction.normalized;
        Vector3 rotation = angleRotation;

        List <Vector3[]> vertices = new List <Vector3[]>();

        for (int z = 0; z < capSegments; z++)
        {
            double     pct    = (double)z / (double)capSegments * 360.0f;
            Vector3    angle1 = new Vector3(angleRotation.x + (float)pct, angleRotation.y, angleRotation.z);
            Quaternion a1     = Quaternion.Euler(angle1);
            Quaternion rot1   = (Quaternion.LookRotation(ndir) * a1);
            Vector3    new1   = rot1 * (startShifted * radiusStart) + startingPoint;
            Vector3    new2   = rot1 * (startShifted * radiusEnd) + endingPoint;

            vertices.Add(new Vector3[] { new1, new2 });
        }
        List <Quad> qlist  = new List <Quad>();
        int         tindex = 0;

        for (int z = 1; z <= capSegments; z++)
        {
            if (z == capSegments)
            {
                qlist.Add(Quad.Build(vertices[0][0], vertices[0][1], vertices[z - 1][0], vertices[z - 1][1], vertexColor, tindex));

                if (capEnds)
                {
                    tindex += 4;

                    qlist.Add(Quad.Build(startingPoint, vertices[0][0], startingPoint, vertices[z - 1][0], vertexColor, tindex));
                    tindex += 4;

                    qlist.Add(Quad.Build(vertices[0][1], endingPoint, vertices[z - 1][1], endingPoint, vertexColor, tindex));
                }
            }
            else
            {
                qlist.Add(Quad.Build(vertices[z][0], vertices[z][1], vertices[z - 1][0], vertices[z - 1][1], vertexColor, tindex));

                if (capEnds)
                {
                    tindex += 4;

                    qlist.Add(Quad.Build(startingPoint, vertices[z][0], startingPoint, vertices[z - 1][0], vertexColor, tindex));
                    tindex += 4;

                    qlist.Add(Quad.Build(vertices[z][1], endingPoint, vertices[z - 1][1], endingPoint, vertexColor, tindex));
                }
            }
            tindex += 4;
        }


        Vector3[] verts  = new Vector3[qlist.Count * 4];
        Vector3[] norms  = new Vector3[qlist.Count * 4];
        Color[]   colors = new Color[qlist.Count * 4];
        int[]     tris   = new int[qlist.Count * 6];
        for (int z = 0; z < qlist.Count; z++)
        {
            qlist[z].Vertices.CopyTo(verts, z * 4);
            qlist[z].Normals.CopyTo(norms, z * 4);
            qlist[z].Colors.CopyTo(colors, z * 4);
            qlist[z].TriangleIndices.CopyTo(tris, z * 6);
        }

        /*
         * lineRenderer.positionCount = verts.Length;
         * lineRenderer.SetPositions(verts);
         */

        Mesh m = new Mesh();

        m.vertices  = verts;
        m.normals   = norms;
        m.colors    = colors;
        m.triangles = tris;
        m.RecalculateNormals();
        m.RecalculateTangents();

        return(m);
    }