< 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        {
 814            Mesh mesh = new Mesh();
 815            mesh.name = "DCL Sphere";
 16
 17            //float radius = 1f;
 18            // Longitude |||
 819            int nbLong = 24;
 20            // Latitude ---
 821            int nbLat = 16;
 22
 23            #region Vertices
 24
 825            Vector3[] vertices = new Vector3[(nbLong + 1) * nbLat + 2];
 826            float _pi = Mathf.PI;
 827            float _2pi = _pi * 2f;
 28
 829            vertices[0] = Vector3.up * radius;
 27230            for (int lat = 0; lat < nbLat; lat++)
 31            {
 12832                float a1 = _pi * (float) (lat + 1) / (nbLat + 1);
 12833                float sin1 = Mathf.Sin(a1);
 12834                float cos1 = Mathf.Cos(a1);
 35
 665636                for (int lon = 0; lon <= nbLong; lon++)
 37                {
 320038                    float a2 = _2pi * (float) (lon == nbLong ? 0 : lon) / nbLong;
 320039                    float sin2 = Mathf.Sin(a2);
 320040                    float cos2 = Mathf.Cos(a2);
 41
 320042                    vertices[lon + lat * (nbLong + 1) + 1] = new Vector3(sin1 * cos2, cos1, sin1 * sin2) * radius;
 43                }
 44            }
 45
 846            vertices[vertices.Length - 1] = Vector3.up * -radius;
 47
 48            #endregion
 49
 50            #region Normales
 51
 852            Vector3[] normales = new Vector3[vertices.Length];
 644853            for (int n = 0; n < vertices.Length; n++)
 54            {
 321655                normales[n] = vertices[n].normalized;
 56            }
 57
 58            #endregion
 59
 60            #region UVs
 61
 862            Vector2[] uvs = new Vector2[vertices.Length];
 863            uvs[0] = Vector2.up;
 864            uvs[uvs.Length - 1] = Vector2.zero;
 27265            for (int lat = 0; lat < nbLat; lat++)
 66            {
 665667                for (int lon = 0; lon <= nbLong; lon++)
 68                {
 320069                    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
 879            int nbFaces = vertices.Length;
 880            int nbTriangles = nbFaces * 2;
 881            int nbIndexes = nbTriangles * 3;
 882            int[] triangles = new int[nbIndexes];
 83
 84            //Top Cap
 885            int i = 0;
 40086            for (int lon = 0; lon < nbLong; lon++)
 87            {
 19288                triangles[i++] = lon + 2;
 19289                triangles[i++] = lon + 1;
 19290                triangles[i++] = 0;
 91            }
 92
 93            //Middle
 25694            for (int lat = 0; lat < nbLat - 1; lat++)
 95            {
 600096                for (int lon = 0; lon < nbLong; lon++)
 97                {
 288098                    int current = lon + lat * (nbLong + 1) + 1;
 288099                    int next = current + nbLong + 1;
 100
 2880101                    triangles[i++] = current;
 2880102                    triangles[i++] = current + 1;
 2880103                    triangles[i++] = next + 1;
 104
 2880105                    triangles[i++] = current;
 2880106                    triangles[i++] = next + 1;
 2880107                    triangles[i++] = next;
 108                }
 109            }
 110
 111            //Bottom Cap
 400112            for (int lon = 0; lon < nbLong; lon++)
 113            {
 192114                triangles[i++] = vertices.Length - 1;
 192115                triangles[i++] = vertices.Length - (lon + 2) - 1;
 192116                triangles[i++] = vertices.Length - (lon + 1) - 1;
 117            }
 118
 119            #endregion
 120
 8121            mesh.vertices = vertices;
 8122            mesh.normals = normales;
 8123            mesh.uv = uvs;
 8124            mesh.triangles = triangles;
 125
 8126            mesh.RecalculateBounds();
 8127            return mesh;
 128        }
 129
 130        public static Mesh BuildPlane(float _size)
 131        {
 15132            Mesh mesh = new Mesh();
 15133            mesh.name = "DCL Plane";
 15134            Vector3[] vertices = new Vector3[8];
 15135            Vector3[] normals = new Vector3[8];
 15136            Vector2[] uvs = new Vector2[8];
 15137            Color[] colors = new Color[8];
 138
 15139            int[] tris = new int[4 * 3];
 140
 15141            int vIndex = 0;
 15142            Vector3 start = new Vector3(-_size / 2, _size / 2, 0);
 15143            vertices[vIndex++] = new Vector3(-start.x, -start.y, 0);
 15144            vertices[vIndex++] = new Vector3(start.x, -start.y, 0);
 15145            vertices[vIndex++] = new Vector3(start.x, start.y, 0);
 15146            vertices[vIndex++] = new Vector3(-start.x, start.y, 0);
 147
 15148            vertices[vIndex++] = new Vector3(-start.x, -start.y, 0);
 15149            vertices[vIndex++] = new Vector3(start.x, -start.y, 0);
 15150            vertices[vIndex++] = new Vector3(start.x, start.y, 0);
 15151            vertices[vIndex++] = new Vector3(-start.x, start.y, 0);
 152
 15153            vIndex = 0;
 15154            uvs[vIndex++] = new Vector2(0f, 1f);
 15155            uvs[vIndex++] = new Vector2(1f, 1f);
 15156            uvs[vIndex++] = new Vector2(1f, 0f);
 15157            uvs[vIndex++] = new Vector2(0f, 0f);
 158
 15159            uvs[vIndex++] = new Vector2(0f, 1f);
 15160            uvs[vIndex++] = new Vector2(1f, 1f);
 15161            uvs[vIndex++] = new Vector2(1f, 0f);
 15162            uvs[vIndex++] = new Vector2(0f, 0f);
 163
 15164            vIndex = 0;
 15165            normals[vIndex++] = Vector3.forward;
 15166            normals[vIndex++] = Vector3.forward;
 15167            normals[vIndex++] = Vector3.forward;
 15168            normals[vIndex++] = Vector3.forward;
 169
 15170            normals[vIndex++] = Vector3.back;
 15171            normals[vIndex++] = Vector3.back;
 15172            normals[vIndex++] = Vector3.back;
 15173            normals[vIndex++] = Vector3.back;
 174
 15175            vIndex = 0;
 15176            colors[vIndex++] = Color.white;
 15177            colors[vIndex++] = Color.white;
 15178            colors[vIndex++] = Color.white;
 15179            colors[vIndex++] = Color.white;
 180
 15181            colors[vIndex++] = Color.white;
 15182            colors[vIndex++] = Color.white;
 15183            colors[vIndex++] = Color.white;
 15184            colors[vIndex++] = Color.white;
 185
 15186            int cnt = 0;
 15187            tris[cnt++] = 2;
 15188            tris[cnt++] = 1;
 15189            tris[cnt++] = 0;
 15190            tris[cnt++] = 3;
 15191            tris[cnt++] = 2;
 15192            tris[cnt++] = 0;
 193
 15194            tris[cnt++] = 4 + 1;
 15195            tris[cnt++] = 4 + 2;
 15196            tris[cnt++] = 4 + 0;
 15197            tris[cnt++] = 4 + 2;
 15198            tris[cnt++] = 4 + 3;
 15199            tris[cnt++] = 4 + 0;
 200
 15201            mesh.vertices = vertices;
 15202            mesh.normals = normals;
 15203            mesh.uv = uvs;
 15204            mesh.colors = colors;
 205
 15206            mesh.triangles = tris;
 15207            return mesh;
 208        }
 209
 210        public static Mesh BuildCube(float _size)
 211        {
 52212            Mesh mesh = new Mesh();
 52213            mesh.name = "DCL Box";
 52214            Vector3[] vertices = new Vector3[24]; //top bottom left right front back
 52215            Vector3[] normals = new Vector3[24];
 52216            Vector2[] uvs = new Vector2[24];
 52217            Vector2[] uvs2 = new Vector2[24];
 52218            int[] tris = new int[12 * 3];
 219
 52220            int vIndex = 0;
 221            //top and bottom
 52222            Vector3 start = new Vector3(-_size / 2, _size / 2, _size / 2);
 52223            vertices[vIndex++] = start;
 52224            vertices[vIndex++] = start + Vector3.right * _size;
 52225            vertices[vIndex++] = start + Vector3.right * _size + Vector3.back * _size;
 52226            vertices[vIndex++] = start + Vector3.back * _size;
 227
 52228            start = new Vector3(-_size / 2, -_size / 2, _size / 2);
 52229            vertices[vIndex++] = start;
 52230            vertices[vIndex++] = start + Vector3.right * _size;
 52231            vertices[vIndex++] = start + Vector3.right * _size + Vector3.back * _size;
 52232            vertices[vIndex++] = start + Vector3.back * _size;
 233
 234            //left and right
 52235            start = new Vector3(-_size / 2, _size / 2, _size / 2);
 52236            vertices[vIndex++] = start;
 52237            vertices[vIndex++] = start + Vector3.back * _size;
 52238            vertices[vIndex++] = start + Vector3.back * _size + Vector3.down * _size;
 52239            vertices[vIndex++] = start + Vector3.down * _size;
 240
 52241            start = new Vector3(_size / 2, _size / 2, _size / 2);
 52242            vertices[vIndex++] = start;
 52243            vertices[vIndex++] = start + Vector3.back * _size;
 52244            vertices[vIndex++] = start + Vector3.back * _size + Vector3.down * _size;
 52245            vertices[vIndex++] = start + Vector3.down * _size;
 246
 247            //front and back
 52248            start = new Vector3(-_size / 2, _size / 2, _size / 2);
 52249            vertices[vIndex++] = start;
 52250            vertices[vIndex++] = start + Vector3.right * _size;
 52251            vertices[vIndex++] = start + Vector3.right * _size + Vector3.down * _size;
 52252            vertices[vIndex++] = start + Vector3.down * _size;
 253
 52254            start = new Vector3(-_size / 2, _size / 2, -_size / 2);
 52255            vertices[vIndex++] = start;
 52256            vertices[vIndex++] = start + Vector3.right * _size;
 52257            vertices[vIndex++] = start + Vector3.right * _size + Vector3.down * _size;
 52258            vertices[vIndex++] = start + Vector3.down * _size;
 259
 260            //uv
 52261            vIndex = 0;
 262            //top and bottom
 52263            uvs[vIndex++] = new Vector2(1f, 1f);
 52264            uvs[vIndex++] = new Vector2(1f, 0f);
 52265            uvs[vIndex++] = new Vector2(0f, 0f);
 52266            uvs[vIndex++] = new Vector2(0f, 1f);
 267
 52268            uvs[vIndex++] = new Vector2(1f, 0f);
 52269            uvs[vIndex++] = new Vector2(1f, 1f);
 52270            uvs[vIndex++] = new Vector2(0f, 1f);
 52271            uvs[vIndex++] = new Vector2(0f, 0f);
 272
 273            //left and right
 52274            uvs[vIndex++] = new Vector2(1f, 1f);
 52275            uvs[vIndex++] = new Vector2(1f, 0f);
 52276            uvs[vIndex++] = new Vector2(0f, 0f);
 52277            uvs[vIndex++] = new Vector2(0f, 1f);
 278
 52279            uvs[vIndex++] = new Vector2(1f, 0f);
 52280            uvs[vIndex++] = new Vector2(1f, 1f);
 52281            uvs[vIndex++] = new Vector2(0f, 1f);
 52282            uvs[vIndex++] = new Vector2(0f, 0f);
 283
 284            //front and back
 52285            uvs[vIndex++] = new Vector2(0f, 0f);
 52286            uvs[vIndex++] = new Vector2(1f, 0f);
 52287            uvs[vIndex++] = new Vector2(1f, 1f);
 52288            uvs[vIndex++] = new Vector2(0f, 1f);
 289
 52290            uvs[vIndex++] = new Vector2(0f, 1f);
 52291            uvs[vIndex++] = new Vector2(1f, 1f);
 52292            uvs[vIndex++] = new Vector2(1f, 0f);
 52293            uvs[vIndex++] = new Vector2(0f, 0f);
 294
 295            //uv2
 52296            vIndex = 0;
 297            //top and bottom
 52298            uvs2[vIndex++] = new Vector2(1f, 1f);
 52299            uvs2[vIndex++] = new Vector2(1f, 0f);
 52300            uvs2[vIndex++] = new Vector2(0f, 0f);
 52301            uvs2[vIndex++] = new Vector2(0f, 1f);
 302
 52303            uvs2[vIndex++] = new Vector2(1f, 0f);
 52304            uvs2[vIndex++] = new Vector2(1f, 1f);
 52305            uvs2[vIndex++] = new Vector2(0f, 1f);
 52306            uvs2[vIndex++] = new Vector2(0f, 0f);
 307
 308            //left and right
 52309            uvs2[vIndex++] = new Vector2(1f, 1f);
 52310            uvs2[vIndex++] = new Vector2(1f, 0f);
 52311            uvs2[vIndex++] = new Vector2(0f, 0f);
 52312            uvs2[vIndex++] = new Vector2(0f, 1f);
 313
 52314            uvs2[vIndex++] = new Vector2(1f, 0f);
 52315            uvs2[vIndex++] = new Vector2(1f, 1f);
 52316            uvs2[vIndex++] = new Vector2(0f, 1f);
 52317            uvs2[vIndex++] = new Vector2(0f, 0f);
 318
 319            //front and back
 52320            uvs2[vIndex++] = new Vector2(0f, 0f);
 52321            uvs2[vIndex++] = new Vector2(1f, 0f);
 52322            uvs2[vIndex++] = new Vector2(1f, 1f);
 52323            uvs2[vIndex++] = new Vector2(0f, 1f);
 324
 52325            uvs2[vIndex++] = new Vector2(0f, 1f);
 52326            uvs2[vIndex++] = new Vector2(1f, 1f);
 52327            uvs2[vIndex++] = new Vector2(1f, 0f);
 52328            uvs2[vIndex++] = new Vector2(0f, 0f);
 329
 330            //normal
 52331            vIndex = 0;
 332            //top and bottom
 52333            normals[vIndex++] = Vector3.up;
 52334            normals[vIndex++] = Vector3.up;
 52335            normals[vIndex++] = Vector3.up;
 52336            normals[vIndex++] = Vector3.up;
 337
 52338            normals[vIndex++] = Vector3.down;
 52339            normals[vIndex++] = Vector3.down;
 52340            normals[vIndex++] = Vector3.down;
 52341            normals[vIndex++] = Vector3.down;
 342
 343            //left and right
 52344            normals[vIndex++] = Vector3.left;
 52345            normals[vIndex++] = Vector3.left;
 52346            normals[vIndex++] = Vector3.left;
 52347            normals[vIndex++] = Vector3.left;
 348
 52349            normals[vIndex++] = Vector3.right;
 52350            normals[vIndex++] = Vector3.right;
 52351            normals[vIndex++] = Vector3.right;
 52352            normals[vIndex++] = Vector3.right;
 353
 354            //front and back
 52355            normals[vIndex++] = Vector3.forward;
 52356            normals[vIndex++] = Vector3.forward;
 52357            normals[vIndex++] = Vector3.forward;
 52358            normals[vIndex++] = Vector3.forward;
 359
 52360            normals[vIndex++] = Vector3.back;
 52361            normals[vIndex++] = Vector3.back;
 52362            normals[vIndex++] = Vector3.back;
 52363            normals[vIndex++] = Vector3.back;
 364
 365
 52366            int cnt = 0;
 367
 368            //top and bottom
 52369            tris[cnt++] = 0;
 52370            tris[cnt++] = 1;
 52371            tris[cnt++] = 2;
 52372            tris[cnt++] = 0;
 52373            tris[cnt++] = 2;
 52374            tris[cnt++] = 3;
 375
 52376            tris[cnt++] = 4 + 0;
 52377            tris[cnt++] = 4 + 2;
 52378            tris[cnt++] = 4 + 1;
 52379            tris[cnt++] = 4 + 0;
 52380            tris[cnt++] = 4 + 3;
 52381            tris[cnt++] = 4 + 2;
 382
 383            //left and right
 52384            tris[cnt++] = 8 + 0;
 52385            tris[cnt++] = 8 + 1;
 52386            tris[cnt++] = 8 + 2;
 52387            tris[cnt++] = 8 + 0;
 52388            tris[cnt++] = 8 + 2;
 52389            tris[cnt++] = 8 + 3;
 390
 52391            tris[cnt++] = 12 + 0;
 52392            tris[cnt++] = 12 + 2;
 52393            tris[cnt++] = 12 + 1;
 52394            tris[cnt++] = 12 + 0;
 52395            tris[cnt++] = 12 + 3;
 52396            tris[cnt++] = 12 + 2;
 397
 398            //front and back
 52399            tris[cnt++] = 16 + 0;
 52400            tris[cnt++] = 16 + 2;
 52401            tris[cnt++] = 16 + 1;
 52402            tris[cnt++] = 16 + 0;
 52403            tris[cnt++] = 16 + 3;
 52404            tris[cnt++] = 16 + 2;
 405
 52406            tris[cnt++] = 20 + 0;
 52407            tris[cnt++] = 20 + 1;
 52408            tris[cnt++] = 20 + 2;
 52409            tris[cnt++] = 20 + 0;
 52410            tris[cnt++] = 20 + 2;
 52411            tris[cnt++] = 20 + 3;
 412
 52413            mesh.vertices = vertices;
 52414            mesh.normals = normals;
 52415            mesh.uv = uvs;
 52416            mesh.uv2 = uvs2;
 417
 52418            mesh.triangles = tris;
 419
 52420            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        {
 14426            if (openingAngle > 0 && openingAngle < 180)
 427            {
 0428                radiusTop = 0;
 0429                radiusBottom = length * Mathf.Tan(openingAngle * Mathf.Deg2Rad / 2);
 430            }
 431
 14432            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";
 14437            Mesh mesh = null; //(Mesh)AssetDatabase.LoadAssetAtPath(meshPrefabPath, typeof(Mesh));
 14438            if (mesh == null)
 439            {
 14440                int numVertices2 = numVertices + 1;
 441
 14442                mesh = new Mesh();
 14443                mesh.name = meshName;
 444                // can't access Camera.current
 445                //newCone.transform.position = Camera.current.transform.position + Camera.current.transform.forward * 5.
 14446                int multiplier = (outside ? 1 : 0) + (inside ? 1 : 0);
 14447                int offset = (outside && inside ? 2 * numVertices2 : 0);
 448
 14449                bool bTopCap = isCylinder ? true : false;
 14450                bool bBottomCap = true;
 451
 14452                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
 14457                Vector3[] normals =
 458                    new Vector3[
 459                        2 * multiplier * numVertices2 + (bTopCap ? (numVertices + 1) : 0) +
 460                        (bBottomCap ? (numVertices + 1) : 0)];
 14461                Vector2[] uvs =
 462                    new Vector2[
 463                        2 * multiplier * numVertices2 + (bTopCap ? (numVertices + 1) : 0) +
 464                        (bBottomCap ? (numVertices + 1) : 0)];
 465                int[] tris;
 14466                float slope = Mathf.Atan((radiusBottom - radiusTop) / length); // (rad difference)/height
 14467                float slopeSin = Mathf.Sin(slope);
 14468                float slopeCos = Mathf.Cos(slope);
 469                int i;
 470
 1428471                for (i = 0; i < numVertices; i++)
 472                {
 700473                    float angle = 2 * Mathf.PI * i / numVertices;
 700474                    float angleSin = Mathf.Sin(angle);
 700475                    float angleCos = Mathf.Cos(angle);
 700476                    float angleHalf = 2 * Mathf.PI * (i + 0.5f) / numVertices; // for degenerated normals at cone tips
 700477                    float angleHalfSin = Mathf.Sin(angleHalf);
 700478                    float angleHalfCos = Mathf.Cos(angleHalf);
 479
 700480                    vertices[i] = new Vector3(radiusTop * angleCos, length, radiusTop * angleSin) + offsetPos;
 700481                    vertices[i + numVertices2] =
 482                        new Vector3(radiusBottom * angleCos, 0, radiusBottom * angleSin) + offsetPos;
 483
 700484                    if (radiusTop == 0)
 485                    {
 300486                        normals[i] = new Vector3(angleHalfCos * slopeCos, -slopeSin, angleHalfSin * slopeCos);
 300487                    }
 488                    else
 489                    {
 400490                        normals[i] = new Vector3(angleCos * slopeCos, -slopeSin, angleSin * slopeCos);
 491                    }
 492
 700493                    if (radiusBottom == 0)
 494                    {
 100495                        normals[i + numVertices2] =
 496                            new Vector3(angleHalfCos * slopeCos, -slopeSin, angleHalfSin * slopeCos);
 100497                    }
 498                    else
 499                    {
 600500                        normals[i + numVertices2] = new Vector3(angleCos * slopeCos, -slopeSin, angleSin * slopeCos);
 501                    }
 502
 700503                    uvs[i] = new Vector2(1.0f - 1.0f * i / numVertices, 1);
 700504                    uvs[i + numVertices2] = new Vector2(1.0f - 1.0f * i / numVertices, 0);
 505
 700506                    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
 700515                    if (inside)
 516                    {
 517                        // invert normals
 0518                        normals[i + offset] = -normals[i];
 0519                        normals[i + numVertices2 + offset] = -normals[i + numVertices2];
 520                    }
 521                }
 522
 14523                vertices[numVertices] = vertices[0];
 14524                vertices[numVertices + numVertices2] = vertices[0 + numVertices2];
 14525                uvs[numVertices] = new Vector2(1.0f - 1.0f * numVertices / numVertices, 1);
 14526                uvs[numVertices + numVertices2] = new Vector2(1.0f - 1.0f * numVertices / numVertices, 0);
 14527                normals[numVertices] = normals[0];
 14528                normals[numVertices + numVertices2] = normals[0 + numVertices2];
 529
 530
 14531                int coverTopIndexStart = 2 * multiplier * numVertices2;
 14532                int coverTopIndexEnd = 2 * multiplier * numVertices2 + numVertices;
 533
 14534                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
 14554                int coverBottomIndexStart = coverTopIndexStart + (bTopCap ? 1 : 0) * (numVertices + 1);
 14555                int coverBottomIndexEnd = coverBottomIndexStart + numVertices;
 14556                if (bBottomCap)
 557                {
 1428558                    for (i = 0; i < numVertices; i++)
 559                    {
 700560                        float angle = 2 * Mathf.PI * i / numVertices;
 700561                        float angleSin = Mathf.Sin(angle);
 700562                        float angleCos = Mathf.Cos(angle);
 563
 700564                        vertices[coverBottomIndexStart + i] =
 565                            new Vector3(radiusBottom * angleCos, 0f, radiusBottom * angleSin) +
 566                            offsetPos;
 700567                        normals[coverBottomIndexStart + i] = new Vector3(0, -1, 0);
 700568                        uvs[coverBottomIndexStart + i] = new Vector2(angleCos / 2 + 0.5f, angleSin / 2 + 0.5f);
 569                    }
 570
 14571                    vertices[coverBottomIndexStart + numVertices] = new Vector3(0, 0f, 0) + offsetPos;
 14572                    normals[coverBottomIndexStart + numVertices] = new Vector3(0, -1, 0);
 14573                    uvs[coverBottomIndexStart + numVertices] = new Vector2(0.5f, 0.5f);
 574                }
 575
 14576                mesh.vertices = vertices;
 14577                mesh.normals = normals;
 14578                mesh.uv = uvs;
 579
 580                // create triangles
 581                // here we need to take care of point order, depending on inside and outside
 14582                int cnt = 0;
 14583                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                }
 8618                else if (radiusBottom == 0)
 619                {
 620                    // bottom cone
 2621                    tris =
 622                        new int[numVertices2 * 3 * multiplier + ((bTopCap ? 1 : 0) * numVertices * 3) +
 623                                ((bBottomCap ? 1 : 0) * numVertices * 3)
 624                        ];
 2625                    if (outside)
 626                    {
 204627                        for (i = 0; i < numVertices; i++)
 628                        {
 100629                            tris[cnt++] = i;
 100630                            tris[cnt++] = i + 1;
 631                            //              if(i==numVertices-1)
 632                            //                tris[cnt++]=0;
 633                            //              else
 634                            //                tris[cnt++]=i+1;
 100635                            tris[cnt++] = i + numVertices2;
 636                        }
 637                    }
 638
 2639                    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
 14697                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
 14714                if (bBottomCap)
 715                {
 1428716                    for (i = 0; i < numVertices; ++i)
 717                    {
 700718                        int next = coverBottomIndexStart + i + 1;
 700719                        if (next == coverBottomIndexEnd)
 720                        {
 14721                            next = coverBottomIndexStart;
 722                        }
 723
 700724                        tris[cnt++] = coverBottomIndexEnd;
 700725                        tris[cnt++] = coverBottomIndexStart + i;
 700726                        tris[cnt++] = next;
 727                    }
 728                }
 729
 14730                mesh.triangles = tris;
 731                //AssetDatabase.CreateAsset(mesh, meshPrefabPath);
 732                //AssetDatabase.SaveAssets();
 733            }
 734
 14735            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        {
 8741            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}