< Summary

Class:DCL.Helpers.PrimitiveMeshBuilder
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/PrimitiveMeshBuilder.cs
Covered lines:396
Uncovered lines:26
Coverable lines:422
Total lines:754
Line coverage:93.8% (396 of 422)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BuildSphere(...)0%12120100%
BuildPlane(...)0%110100%
BuildCube(...)0%110100%
BuildConeOrCylinder(...)0%108.4672080.84%
BuildCone(...)0%110100%
BuildCylinder(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/PrimitiveMeshBuilder.cs

#LineLine coverage
 1using UnityEngine;
 2
 3/*
 4* PrimitiveMeshBuilder originated in the DecentralandUnityPlugin that can be found at:
 5* https://github.com/fairwood/DecentralandUnityPlugin
 6*/
 7
 8namespace DCL.Helpers
 9{
 10    public class PrimitiveMeshBuilder
 11    {
 12        public static Mesh BuildSphere(float radius)
 13        {
 914            Mesh mesh = new Mesh();
 915            mesh.name = "DCL Sphere";
 16
 17            //float radius = 1f;
 18            // Longitude |||
 919            int nbLong = 24;
 20            // Latitude ---
 921            int nbLat = 16;
 22
 23            #region Vertices
 24
 925            Vector3[] vertices = new Vector3[(nbLong + 1) * nbLat + 2];
 926            float _pi = Mathf.PI;
 927            float _2pi = _pi * 2f;
 28
 929            vertices[0] = Vector3.up * radius;
 30630            for (int lat = 0; lat < nbLat; lat++)
 31            {
 14432                float a1 = _pi * (float) (lat + 1) / (nbLat + 1);
 14433                float sin1 = Mathf.Sin(a1);
 14434                float cos1 = Mathf.Cos(a1);
 35
 748836                for (int lon = 0; lon <= nbLong; lon++)
 37                {
 360038                    float a2 = _2pi * (float) (lon == nbLong ? 0 : lon) / nbLong;
 360039                    float sin2 = Mathf.Sin(a2);
 360040                    float cos2 = Mathf.Cos(a2);
 41
 360042                    vertices[lon + lat * (nbLong + 1) + 1] = new Vector3(sin1 * cos2, cos1, sin1 * sin2) * radius;
 43                }
 44            }
 45
 946            vertices[vertices.Length - 1] = Vector3.up * -radius;
 47
 48            #endregion
 49
 50            #region Normales
 51
 952            Vector3[] normales = new Vector3[vertices.Length];
 725453            for (int n = 0; n < vertices.Length; n++)
 54            {
 361855                normales[n] = vertices[n].normalized;
 56            }
 57
 58            #endregion
 59
 60            #region UVs
 61
 962            Vector2[] uvs = new Vector2[vertices.Length];
 963            uvs[0] = Vector2.up;
 964            uvs[uvs.Length - 1] = Vector2.zero;
 30665            for (int lat = 0; lat < nbLat; lat++)
 66            {
 748867                for (int lon = 0; lon <= nbLong; lon++)
 68                {
 360069                    uvs[lon + lat * (nbLong + 1) + 1] =
 70                        new Vector2(1f - (float) lon / nbLong, (float) (lat + 1) / (nbLat + 1));
 71                }
 72            }
 73            //uvs[lon + lat * (nbLong + 1) + 1] = new Vector2( (float)lon / nbLong, 1f - (float)(lat+1) / (nbLat+1) );
 74
 75            #endregion
 76
 77            #region Triangles
 78
 979            int nbFaces = vertices.Length;
 980            int nbTriangles = nbFaces * 2;
 981            int nbIndexes = nbTriangles * 3;
 982            int[] triangles = new int[nbIndexes];
 83
 84            //Top Cap
 985            int i = 0;
 45086            for (int lon = 0; lon < nbLong; lon++)
 87            {
 21688                triangles[i++] = lon + 2;
 21689                triangles[i++] = lon + 1;
 21690                triangles[i++] = 0;
 91            }
 92
 93            //Middle
 28894            for (int lat = 0; lat < nbLat - 1; lat++)
 95            {
 675096                for (int lon = 0; lon < nbLong; lon++)
 97                {
 324098                    int current = lon + lat * (nbLong + 1) + 1;
 324099                    int next = current + nbLong + 1;
 100
 3240101                    triangles[i++] = current;
 3240102                    triangles[i++] = current + 1;
 3240103                    triangles[i++] = next + 1;
 104
 3240105                    triangles[i++] = current;
 3240106                    triangles[i++] = next + 1;
 3240107                    triangles[i++] = next;
 108                }
 109            }
 110
 111            //Bottom Cap
 450112            for (int lon = 0; lon < nbLong; lon++)
 113            {
 216114                triangles[i++] = vertices.Length - 1;
 216115                triangles[i++] = vertices.Length - (lon + 2) - 1;
 216116                triangles[i++] = vertices.Length - (lon + 1) - 1;
 117            }
 118
 119            #endregion
 120
 9121            mesh.vertices = vertices;
 9122            mesh.normals = normales;
 9123            mesh.uv = uvs;
 9124            mesh.triangles = triangles;
 125
 9126            mesh.RecalculateBounds();
 9127            return mesh;
 128        }
 129
 130        public static Mesh BuildPlane(float _size)
 131        {
 14132            Mesh mesh = new Mesh();
 14133            mesh.name = "DCL Plane";
 14134            Vector3[] vertices = new Vector3[8];
 14135            Vector3[] normals = new Vector3[8];
 14136            Vector2[] uvs = new Vector2[8];
 14137            Color[] colors = new Color[8];
 138
 14139            int[] tris = new int[4 * 3];
 140
 14141            int vIndex = 0;
 14142            Vector3 start = new Vector3(-_size / 2, _size / 2, 0);
 14143            vertices[vIndex++] = new Vector3(-start.x, -start.y, 0);
 14144            vertices[vIndex++] = new Vector3(start.x, -start.y, 0);
 14145            vertices[vIndex++] = new Vector3(start.x, start.y, 0);
 14146            vertices[vIndex++] = new Vector3(-start.x, start.y, 0);
 147
 14148            vertices[vIndex++] = new Vector3(-start.x, -start.y, 0);
 14149            vertices[vIndex++] = new Vector3(start.x, -start.y, 0);
 14150            vertices[vIndex++] = new Vector3(start.x, start.y, 0);
 14151            vertices[vIndex++] = new Vector3(-start.x, start.y, 0);
 152
 14153            vIndex = 0;
 14154            uvs[vIndex++] = new Vector2(0f, 1f);
 14155            uvs[vIndex++] = new Vector2(1f, 1f);
 14156            uvs[vIndex++] = new Vector2(1f, 0f);
 14157            uvs[vIndex++] = new Vector2(0f, 0f);
 158
 14159            uvs[vIndex++] = new Vector2(0f, 1f);
 14160            uvs[vIndex++] = new Vector2(1f, 1f);
 14161            uvs[vIndex++] = new Vector2(1f, 0f);
 14162            uvs[vIndex++] = new Vector2(0f, 0f);
 163
 14164            vIndex = 0;
 14165            normals[vIndex++] = Vector3.forward;
 14166            normals[vIndex++] = Vector3.forward;
 14167            normals[vIndex++] = Vector3.forward;
 14168            normals[vIndex++] = Vector3.forward;
 169
 14170            normals[vIndex++] = Vector3.back;
 14171            normals[vIndex++] = Vector3.back;
 14172            normals[vIndex++] = Vector3.back;
 14173            normals[vIndex++] = Vector3.back;
 174
 14175            vIndex = 0;
 14176            colors[vIndex++] = Color.white;
 14177            colors[vIndex++] = Color.white;
 14178            colors[vIndex++] = Color.white;
 14179            colors[vIndex++] = Color.white;
 180
 14181            colors[vIndex++] = Color.white;
 14182            colors[vIndex++] = Color.white;
 14183            colors[vIndex++] = Color.white;
 14184            colors[vIndex++] = Color.white;
 185
 14186            int cnt = 0;
 14187            tris[cnt++] = 2;
 14188            tris[cnt++] = 1;
 14189            tris[cnt++] = 0;
 14190            tris[cnt++] = 3;
 14191            tris[cnt++] = 2;
 14192            tris[cnt++] = 0;
 193
 14194            tris[cnt++] = 4 + 1;
 14195            tris[cnt++] = 4 + 2;
 14196            tris[cnt++] = 4 + 0;
 14197            tris[cnt++] = 4 + 2;
 14198            tris[cnt++] = 4 + 3;
 14199            tris[cnt++] = 4 + 0;
 200
 14201            mesh.vertices = vertices;
 14202            mesh.normals = normals;
 14203            mesh.uv = uvs;
 14204            mesh.colors = colors;
 205
 14206            mesh.triangles = tris;
 14207            return mesh;
 208        }
 209
 210        public static Mesh BuildCube(float _size)
 211        {
 7212            Mesh mesh = new Mesh();
 7213            mesh.name = "DCL Box";
 7214            Vector3[] vertices = new Vector3[24]; //top bottom left right front back
 7215            Vector3[] normals = new Vector3[24];
 7216            Vector2[] uvs = new Vector2[24];
 7217            Vector2[] uvs2 = new Vector2[24];
 7218            int[] tris = new int[12 * 3];
 219
 7220            int vIndex = 0;
 221            //top and bottom
 7222            Vector3 start = new Vector3(-_size / 2, _size / 2, _size / 2);
 7223            vertices[vIndex++] = start;
 7224            vertices[vIndex++] = start + Vector3.right * _size;
 7225            vertices[vIndex++] = start + Vector3.right * _size + Vector3.back * _size;
 7226            vertices[vIndex++] = start + Vector3.back * _size;
 227
 7228            start = new Vector3(-_size / 2, -_size / 2, _size / 2);
 7229            vertices[vIndex++] = start;
 7230            vertices[vIndex++] = start + Vector3.right * _size;
 7231            vertices[vIndex++] = start + Vector3.right * _size + Vector3.back * _size;
 7232            vertices[vIndex++] = start + Vector3.back * _size;
 233
 234            //left and right
 7235            start = new Vector3(-_size / 2, _size / 2, _size / 2);
 7236            vertices[vIndex++] = start;
 7237            vertices[vIndex++] = start + Vector3.back * _size;
 7238            vertices[vIndex++] = start + Vector3.back * _size + Vector3.down * _size;
 7239            vertices[vIndex++] = start + Vector3.down * _size;
 240
 7241            start = new Vector3(_size / 2, _size / 2, _size / 2);
 7242            vertices[vIndex++] = start;
 7243            vertices[vIndex++] = start + Vector3.back * _size;
 7244            vertices[vIndex++] = start + Vector3.back * _size + Vector3.down * _size;
 7245            vertices[vIndex++] = start + Vector3.down * _size;
 246
 247            //front and back
 7248            start = new Vector3(-_size / 2, _size / 2, _size / 2);
 7249            vertices[vIndex++] = start;
 7250            vertices[vIndex++] = start + Vector3.right * _size;
 7251            vertices[vIndex++] = start + Vector3.right * _size + Vector3.down * _size;
 7252            vertices[vIndex++] = start + Vector3.down * _size;
 253
 7254            start = new Vector3(-_size / 2, _size / 2, -_size / 2);
 7255            vertices[vIndex++] = start;
 7256            vertices[vIndex++] = start + Vector3.right * _size;
 7257            vertices[vIndex++] = start + Vector3.right * _size + Vector3.down * _size;
 7258            vertices[vIndex++] = start + Vector3.down * _size;
 259
 260            //uv
 7261            vIndex = 0;
 262            //top and bottom
 7263            uvs[vIndex++] = new Vector2(1f, 1f);
 7264            uvs[vIndex++] = new Vector2(1f, 0f);
 7265            uvs[vIndex++] = new Vector2(0f, 0f);
 7266            uvs[vIndex++] = new Vector2(0f, 1f);
 267
 7268            uvs[vIndex++] = new Vector2(1f, 0f);
 7269            uvs[vIndex++] = new Vector2(1f, 1f);
 7270            uvs[vIndex++] = new Vector2(0f, 1f);
 7271            uvs[vIndex++] = new Vector2(0f, 0f);
 272
 273            //left and right
 7274            uvs[vIndex++] = new Vector2(1f, 1f);
 7275            uvs[vIndex++] = new Vector2(1f, 0f);
 7276            uvs[vIndex++] = new Vector2(0f, 0f);
 7277            uvs[vIndex++] = new Vector2(0f, 1f);
 278
 7279            uvs[vIndex++] = new Vector2(1f, 0f);
 7280            uvs[vIndex++] = new Vector2(1f, 1f);
 7281            uvs[vIndex++] = new Vector2(0f, 1f);
 7282            uvs[vIndex++] = new Vector2(0f, 0f);
 283
 284            //front and back
 7285            uvs[vIndex++] = new Vector2(0f, 0f);
 7286            uvs[vIndex++] = new Vector2(1f, 0f);
 7287            uvs[vIndex++] = new Vector2(1f, 1f);
 7288            uvs[vIndex++] = new Vector2(0f, 1f);
 289
 7290            uvs[vIndex++] = new Vector2(0f, 1f);
 7291            uvs[vIndex++] = new Vector2(1f, 1f);
 7292            uvs[vIndex++] = new Vector2(1f, 0f);
 7293            uvs[vIndex++] = new Vector2(0f, 0f);
 294
 295            //uv2
 7296            vIndex = 0;
 297            //top and bottom
 7298            uvs2[vIndex++] = new Vector2(1f, 1f);
 7299            uvs2[vIndex++] = new Vector2(1f, 0f);
 7300            uvs2[vIndex++] = new Vector2(0f, 0f);
 7301            uvs2[vIndex++] = new Vector2(0f, 1f);
 302
 7303            uvs2[vIndex++] = new Vector2(1f, 0f);
 7304            uvs2[vIndex++] = new Vector2(1f, 1f);
 7305            uvs2[vIndex++] = new Vector2(0f, 1f);
 7306            uvs2[vIndex++] = new Vector2(0f, 0f);
 307
 308            //left and right
 7309            uvs2[vIndex++] = new Vector2(1f, 1f);
 7310            uvs2[vIndex++] = new Vector2(1f, 0f);
 7311            uvs2[vIndex++] = new Vector2(0f, 0f);
 7312            uvs2[vIndex++] = new Vector2(0f, 1f);
 313
 7314            uvs2[vIndex++] = new Vector2(1f, 0f);
 7315            uvs2[vIndex++] = new Vector2(1f, 1f);
 7316            uvs2[vIndex++] = new Vector2(0f, 1f);
 7317            uvs2[vIndex++] = new Vector2(0f, 0f);
 318
 319            //front and back
 7320            uvs2[vIndex++] = new Vector2(0f, 0f);
 7321            uvs2[vIndex++] = new Vector2(1f, 0f);
 7322            uvs2[vIndex++] = new Vector2(1f, 1f);
 7323            uvs2[vIndex++] = new Vector2(0f, 1f);
 324
 7325            uvs2[vIndex++] = new Vector2(0f, 1f);
 7326            uvs2[vIndex++] = new Vector2(1f, 1f);
 7327            uvs2[vIndex++] = new Vector2(1f, 0f);
 7328            uvs2[vIndex++] = new Vector2(0f, 0f);
 329
 330            //normal
 7331            vIndex = 0;
 332            //top and bottom
 7333            normals[vIndex++] = Vector3.up;
 7334            normals[vIndex++] = Vector3.up;
 7335            normals[vIndex++] = Vector3.up;
 7336            normals[vIndex++] = Vector3.up;
 337
 7338            normals[vIndex++] = Vector3.down;
 7339            normals[vIndex++] = Vector3.down;
 7340            normals[vIndex++] = Vector3.down;
 7341            normals[vIndex++] = Vector3.down;
 342
 343            //left and right
 7344            normals[vIndex++] = Vector3.left;
 7345            normals[vIndex++] = Vector3.left;
 7346            normals[vIndex++] = Vector3.left;
 7347            normals[vIndex++] = Vector3.left;
 348
 7349            normals[vIndex++] = Vector3.right;
 7350            normals[vIndex++] = Vector3.right;
 7351            normals[vIndex++] = Vector3.right;
 7352            normals[vIndex++] = Vector3.right;
 353
 354            //front and back
 7355            normals[vIndex++] = Vector3.forward;
 7356            normals[vIndex++] = Vector3.forward;
 7357            normals[vIndex++] = Vector3.forward;
 7358            normals[vIndex++] = Vector3.forward;
 359
 7360            normals[vIndex++] = Vector3.back;
 7361            normals[vIndex++] = Vector3.back;
 7362            normals[vIndex++] = Vector3.back;
 7363            normals[vIndex++] = Vector3.back;
 364
 365
 7366            int cnt = 0;
 367
 368            //top and bottom
 7369            tris[cnt++] = 0;
 7370            tris[cnt++] = 1;
 7371            tris[cnt++] = 2;
 7372            tris[cnt++] = 0;
 7373            tris[cnt++] = 2;
 7374            tris[cnt++] = 3;
 375
 7376            tris[cnt++] = 4 + 0;
 7377            tris[cnt++] = 4 + 2;
 7378            tris[cnt++] = 4 + 1;
 7379            tris[cnt++] = 4 + 0;
 7380            tris[cnt++] = 4 + 3;
 7381            tris[cnt++] = 4 + 2;
 382
 383            //left and right
 7384            tris[cnt++] = 8 + 0;
 7385            tris[cnt++] = 8 + 1;
 7386            tris[cnt++] = 8 + 2;
 7387            tris[cnt++] = 8 + 0;
 7388            tris[cnt++] = 8 + 2;
 7389            tris[cnt++] = 8 + 3;
 390
 7391            tris[cnt++] = 12 + 0;
 7392            tris[cnt++] = 12 + 2;
 7393            tris[cnt++] = 12 + 1;
 7394            tris[cnt++] = 12 + 0;
 7395            tris[cnt++] = 12 + 3;
 7396            tris[cnt++] = 12 + 2;
 397
 398            //front and back
 7399            tris[cnt++] = 16 + 0;
 7400            tris[cnt++] = 16 + 2;
 7401            tris[cnt++] = 16 + 1;
 7402            tris[cnt++] = 16 + 0;
 7403            tris[cnt++] = 16 + 3;
 7404            tris[cnt++] = 16 + 2;
 405
 7406            tris[cnt++] = 20 + 0;
 7407            tris[cnt++] = 20 + 1;
 7408            tris[cnt++] = 20 + 2;
 7409            tris[cnt++] = 20 + 0;
 7410            tris[cnt++] = 20 + 2;
 7411            tris[cnt++] = 20 + 3;
 412
 7413            mesh.vertices = vertices;
 7414            mesh.normals = normals;
 7415            mesh.uv = uvs;
 7416            mesh.uv2 = uvs2;
 417
 7418            mesh.triangles = tris;
 419
 7420            return mesh;
 421        }
 422
 423        public static Mesh BuildConeOrCylinder(int numVertices, float radiusTop, float radiusBottom, float length,
 424            float openingAngle, bool outside, bool inside, bool isCylinder, Vector3 offsetPos = default(Vector3))
 425        {
 13426            if (openingAngle > 0 && openingAngle < 180)
 427            {
 0428                radiusTop = 0;
 0429                radiusBottom = length * Mathf.Tan(openingAngle * Mathf.Deg2Rad / 2);
 430            }
 431
 13432            string meshName = isCylinder
 433                ? "DCL Cylinder"
 434                : "DCL Cone" + numVertices + "v" + radiusTop + "t" + radiusBottom + "b" + length + "l" + length +
 435                  (outside ? "o" : "") + (inside ? "i" : "");
 436            //string meshPrefabPath = "Assets/Decentraland/Internal/" + meshName + ".asset";
 13437            Mesh mesh = null; //(Mesh)AssetDatabase.LoadAssetAtPath(meshPrefabPath, typeof(Mesh));
 13438            if (mesh == null)
 439            {
 13440                int numVertices2 = numVertices + 1;
 441
 13442                mesh = new Mesh();
 13443                mesh.name = meshName;
 444                // can't access Camera.current
 445                //newCone.transform.position = Camera.current.transform.position + Camera.current.transform.forward * 5.
 13446                int multiplier = (outside ? 1 : 0) + (inside ? 1 : 0);
 13447                int offset = (outside && inside ? 2 * numVertices2 : 0);
 448
 13449                bool bTopCap = isCylinder ? true : false;
 13450                bool bBottomCap = true;
 451
 13452                Vector3[] vertices =
 453                    new Vector3[
 454                        2 * multiplier * numVertices2 + (bTopCap ? (numVertices + 1) : 0) +
 455                        (bBottomCap ? (numVertices + 1) : 0)];
 456                // 0..n-1: top, n..2n-1: bottom
 13457                Vector3[] normals =
 458                    new Vector3[
 459                        2 * multiplier * numVertices2 + (bTopCap ? (numVertices + 1) : 0) +
 460                        (bBottomCap ? (numVertices + 1) : 0)];
 13461                Vector2[] uvs =
 462                    new Vector2[
 463                        2 * multiplier * numVertices2 + (bTopCap ? (numVertices + 1) : 0) +
 464                        (bBottomCap ? (numVertices + 1) : 0)];
 465                int[] tris;
 13466                float slope = Mathf.Atan((radiusBottom - radiusTop) / length); // (rad difference)/height
 13467                float slopeSin = Mathf.Sin(slope);
 13468                float slopeCos = Mathf.Cos(slope);
 469                int i;
 470
 1326471                for (i = 0; i < numVertices; i++)
 472                {
 650473                    float angle = 2 * Mathf.PI * i / numVertices;
 650474                    float angleSin = Mathf.Sin(angle);
 650475                    float angleCos = Mathf.Cos(angle);
 650476                    float angleHalf = 2 * Mathf.PI * (i + 0.5f) / numVertices; // for degenerated normals at cone tips
 650477                    float angleHalfSin = Mathf.Sin(angleHalf);
 650478                    float angleHalfCos = Mathf.Cos(angleHalf);
 479
 650480                    vertices[i] = new Vector3(radiusTop * angleCos, length, radiusTop * angleSin) + offsetPos;
 650481                    vertices[i + numVertices2] =
 482                        new Vector3(radiusBottom * angleCos, 0, radiusBottom * angleSin) + offsetPos;
 483
 650484                    if (radiusTop == 0)
 485                    {
 300486                        normals[i] = new Vector3(angleHalfCos * slopeCos, -slopeSin, angleHalfSin * slopeCos);
 300487                    }
 488                    else
 489                    {
 350490                        normals[i] = new Vector3(angleCos * slopeCos, -slopeSin, angleSin * slopeCos);
 491                    }
 492
 650493                    if (radiusBottom == 0)
 494                    {
 50495                        normals[i + numVertices2] =
 496                            new Vector3(angleHalfCos * slopeCos, -slopeSin, angleHalfSin * slopeCos);
 50497                    }
 498                    else
 499                    {
 600500                        normals[i + numVertices2] = new Vector3(angleCos * slopeCos, -slopeSin, angleSin * slopeCos);
 501                    }
 502
 650503                    uvs[i] = new Vector2(1.0f - 1.0f * i / numVertices, 1);
 650504                    uvs[i + numVertices2] = new Vector2(1.0f - 1.0f * i / numVertices, 0);
 505
 650506                    if (outside && inside)
 507                    {
 508                        // vertices and uvs are identical on inside and outside, so just copy
 0509                        vertices[i + 2 * numVertices2] = vertices[i];
 0510                        vertices[i + 3 * numVertices2] = vertices[i + numVertices2];
 0511                        uvs[i + 2 * numVertices2] = uvs[i];
 0512                        uvs[i + 3 * numVertices2] = uvs[i + numVertices2];
 513                    }
 514
 650515                    if (inside)
 516                    {
 517                        // invert normals
 0518                        normals[i + offset] = -normals[i];
 0519                        normals[i + numVertices2 + offset] = -normals[i + numVertices2];
 520                    }
 521                }
 522
 13523                vertices[numVertices] = vertices[0];
 13524                vertices[numVertices + numVertices2] = vertices[0 + numVertices2];
 13525                uvs[numVertices] = new Vector2(1.0f - 1.0f * numVertices / numVertices, 1);
 13526                uvs[numVertices + numVertices2] = new Vector2(1.0f - 1.0f * numVertices / numVertices, 0);
 13527                normals[numVertices] = normals[0];
 13528                normals[numVertices + numVertices2] = normals[0 + numVertices2];
 529
 530
 13531                int coverTopIndexStart = 2 * multiplier * numVertices2;
 13532                int coverTopIndexEnd = 2 * multiplier * numVertices2 + numVertices;
 533
 13534                if (bTopCap)
 535                {
 612536                    for (i = 0; i < numVertices; i++)
 537                    {
 300538                        float angle = 2 * Mathf.PI * i / numVertices;
 300539                        float angleSin = Mathf.Sin(angle);
 300540                        float angleCos = Mathf.Cos(angle);
 541
 300542                        vertices[coverTopIndexStart + i] =
 543                            new Vector3(radiusTop * angleCos, length, radiusTop * angleSin) + offsetPos;
 300544                        normals[coverTopIndexStart + i] = new Vector3(0, 1, 0);
 300545                        uvs[coverTopIndexStart + i] = new Vector2(angleCos / 2 + 0.5f, angleSin / 2 + 0.5f);
 546                    }
 547
 6548                    vertices[coverTopIndexStart + numVertices] = new Vector3(0, length, 0) + offsetPos;
 6549                    normals[coverTopIndexStart + numVertices] = new Vector3(0, 1, 0);
 6550                    uvs[coverTopIndexStart + numVertices] = new Vector2(0.5f, 0.5f);
 551                }
 552
 553
 13554                int coverBottomIndexStart = coverTopIndexStart + (bTopCap ? 1 : 0) * (numVertices + 1);
 13555                int coverBottomIndexEnd = coverBottomIndexStart + numVertices;
 13556                if (bBottomCap)
 557                {
 1326558                    for (i = 0; i < numVertices; i++)
 559                    {
 650560                        float angle = 2 * Mathf.PI * i / numVertices;
 650561                        float angleSin = Mathf.Sin(angle);
 650562                        float angleCos = Mathf.Cos(angle);
 563
 650564                        vertices[coverBottomIndexStart + i] =
 565                            new Vector3(radiusBottom * angleCos, 0f, radiusBottom * angleSin) +
 566                            offsetPos;
 650567                        normals[coverBottomIndexStart + i] = new Vector3(0, -1, 0);
 650568                        uvs[coverBottomIndexStart + i] = new Vector2(angleCos / 2 + 0.5f, angleSin / 2 + 0.5f);
 569                    }
 570
 13571                    vertices[coverBottomIndexStart + numVertices] = new Vector3(0, 0f, 0) + offsetPos;
 13572                    normals[coverBottomIndexStart + numVertices] = new Vector3(0, -1, 0);
 13573                    uvs[coverBottomIndexStart + numVertices] = new Vector2(0.5f, 0.5f);
 574                }
 575
 13576                mesh.vertices = vertices;
 13577                mesh.normals = normals;
 13578                mesh.uv = uvs;
 579
 580                // create triangles
 581                // here we need to take care of point order, depending on inside and outside
 13582                int cnt = 0;
 13583                if (radiusTop == 0)
 584                {
 585                    // top cone
 6586                    tris =
 587                        new int[numVertices2 * 3 * multiplier + ((bTopCap ? 1 : 0) * numVertices * 3) +
 588                                ((bBottomCap ? 1 : 0) * numVertices * 3)
 589                        ];
 6590                    if (outside)
 591                    {
 612592                        for (i = 0; i < numVertices; i++)
 593                        {
 300594                            tris[cnt++] = i + numVertices2;
 300595                            tris[cnt++] = i;
 300596                            tris[cnt++] = i + 1 + numVertices2;
 597                            //              if(i==numVertices-1)
 598                            //                tris[cnt++]=numVertices;
 599                            //              else
 600                            //                tris[cnt++]=i+1+numVertices;
 601                        }
 602                    }
 603
 6604                    if (inside)
 605                    {
 0606                        for (i = offset; i < numVertices + offset; i++)
 607                        {
 0608                            tris[cnt++] = i;
 0609                            tris[cnt++] = i + numVertices2;
 0610                            tris[cnt++] = i + 1 + numVertices2;
 611                            //              if(i==numVertices-1+offset)
 612                            //                tris[cnt++]=numVertices+offset;
 613                            //              else
 614                            //                tris[cnt++]=i+1+numVertices;
 615                        }
 616                    }
 0617                }
 7618                else if (radiusBottom == 0)
 619                {
 620                    // bottom cone
 1621                    tris =
 622                        new int[numVertices2 * 3 * multiplier + ((bTopCap ? 1 : 0) * numVertices * 3) +
 623                                ((bBottomCap ? 1 : 0) * numVertices * 3)
 624                        ];
 1625                    if (outside)
 626                    {
 102627                        for (i = 0; i < numVertices; i++)
 628                        {
 50629                            tris[cnt++] = i;
 50630                            tris[cnt++] = i + 1;
 631                            //              if(i==numVertices-1)
 632                            //                tris[cnt++]=0;
 633                            //              else
 634                            //                tris[cnt++]=i+1;
 50635                            tris[cnt++] = i + numVertices2;
 636                        }
 637                    }
 638
 1639                    if (inside)
 640                    {
 0641                        for (i = offset; i < numVertices + offset; i++)
 642                        {
 643                            //              if(i==numVertices-1+offset)
 644                            //                tris[cnt++]=offset;
 645                            //              else
 646                            //                tris[cnt++]=i+1;
 0647                            tris[cnt++] = i + 1;
 0648                            tris[cnt++] = i;
 0649                            tris[cnt++] = i + numVertices2;
 650                        }
 651                    }
 0652                }
 653                else
 654                {
 655                    // truncated cone
 6656                    tris =
 657                        new int[numVertices2 * 6 * multiplier + ((bTopCap ? 1 : 0) * numVertices * 3) +
 658                                ((bBottomCap ? 1 : 0) * numVertices * 3)
 659                        ];
 6660                    if (outside)
 661                    {
 612662                        for (i = 0; i < numVertices; i++)
 663                        {
 300664                            int ip1 = i + 1;
 665                            //              if(ip1==numVertices)
 666                            //                ip1=0;
 667
 300668                            tris[cnt++] = i;
 300669                            tris[cnt++] = ip1;
 300670                            tris[cnt++] = i + numVertices2;
 671
 300672                            tris[cnt++] = ip1 + numVertices2;
 300673                            tris[cnt++] = i + numVertices2;
 300674                            tris[cnt++] = ip1;
 675                        }
 676                    }
 677
 6678                    if (inside)
 679                    {
 0680                        for (i = offset; i < numVertices + offset; i++)
 681                        {
 0682                            int ip1 = i + 1;
 683                            //              if(ip1==numVertices+offset)
 684                            //                ip1=offset;
 685
 0686                            tris[cnt++] = ip1;
 0687                            tris[cnt++] = i;
 0688                            tris[cnt++] = i + numVertices2;
 689
 0690                            tris[cnt++] = i + numVertices2;
 0691                            tris[cnt++] = ip1 + numVertices2;
 0692                            tris[cnt++] = ip1;
 693                        }
 694                    }
 695                }
 696
 13697                if (bTopCap)
 698                {
 612699                    for (i = 0; i < numVertices; ++i)
 700                    {
 300701                        int next = coverTopIndexStart + i + 1;
 702
 300703                        if (next == coverTopIndexEnd)
 704                        {
 6705                            next = coverTopIndexStart;
 706                        }
 707
 300708                        tris[cnt++] = next;
 300709                        tris[cnt++] = coverTopIndexStart + i;
 300710                        tris[cnt++] = coverTopIndexEnd;
 711                    }
 712                }
 713
 13714                if (bBottomCap)
 715                {
 1326716                    for (i = 0; i < numVertices; ++i)
 717                    {
 650718                        int next = coverBottomIndexStart + i + 1;
 650719                        if (next == coverBottomIndexEnd)
 720                        {
 13721                            next = coverBottomIndexStart;
 722                        }
 723
 650724                        tris[cnt++] = coverBottomIndexEnd;
 650725                        tris[cnt++] = coverBottomIndexStart + i;
 650726                        tris[cnt++] = next;
 727                    }
 728                }
 729
 13730                mesh.triangles = tris;
 731                //AssetDatabase.CreateAsset(mesh, meshPrefabPath);
 732                //AssetDatabase.SaveAssets();
 733            }
 734
 13735            return mesh;
 736        }
 737
 738        public static Mesh BuildCone(int numVertices, float radiusTop, float radiusBottom, float length,
 739            float openingAngle, bool outside, bool inside)
 740        {
 7741            return BuildConeOrCylinder(numVertices, radiusTop, radiusBottom, length, openingAngle, outside, inside,
 742                false,
 743                new Vector3(0f, -length / 2, 0f));
 744        }
 745
 746        public static Mesh BuildCylinder(int numVertices, float radiusTop, float radiusBottom, float length,
 747            float openingAngle, bool outside, bool inside)
 748        {
 6749            return BuildConeOrCylinder(numVertices, radiusTop, radiusBottom, length, openingAngle, outside, inside,
 750                true,
 751                new Vector3(0f, -length / 2, 0f));
 752        }
 753    }
 754}