< Summary

Class:DCL.Helpers.PrimitiveMeshBuilder
Assembly:Utils
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/Utils/PrimitiveMeshBuilder.cs
Covered lines:459
Uncovered lines:26
Coverable lines:485
Total lines:834
Line coverage:94.6% (459 of 485)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BuildSphere(...)0%12120100%
BuildPlane(...)0%110100%
BuildPlaneV2(...)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/Utils/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        {
 1514            Mesh mesh = new Mesh();
 1515            mesh.name = "DCL Sphere";
 16
 17            //float radius = 1f;
 18            // Longitude |||
 1519            int nbLong = 24;
 20            // Latitude ---
 1521            int nbLat = 16;
 22
 23            #region Vertices
 24
 1525            Vector3[] vertices = new Vector3[(nbLong + 1) * nbLat + 2];
 1526            float _pi = Mathf.PI;
 1527            float _2pi = _pi * 2f;
 28
 1529            vertices[0] = Vector3.up * radius;
 51030            for (int lat = 0; lat < nbLat; lat++)
 31            {
 24032                float a1 = _pi * (float) (lat + 1) / (nbLat + 1);
 24033                float sin1 = Mathf.Sin(a1);
 24034                float cos1 = Mathf.Cos(a1);
 35
 1248036                for (int lon = 0; lon <= nbLong; lon++)
 37                {
 600038                    float a2 = _2pi * (float) (lon == nbLong ? 0 : lon) / nbLong;
 600039                    float sin2 = Mathf.Sin(a2);
 600040                    float cos2 = Mathf.Cos(a2);
 41
 600042                    vertices[lon + lat * (nbLong + 1) + 1] = new Vector3(sin1 * cos2, cos1, sin1 * sin2) * radius;
 43                }
 44            }
 45
 1546            vertices[vertices.Length - 1] = Vector3.up * -radius;
 47
 48            #endregion
 49
 50            #region Normales
 51
 1552            Vector3[] normales = new Vector3[vertices.Length];
 1209053            for (int n = 0; n < vertices.Length; n++)
 54            {
 603055                normales[n] = vertices[n].normalized;
 56            }
 57
 58            #endregion
 59
 60            #region UVs
 61
 1562            Vector2[] uvs = new Vector2[vertices.Length];
 1563            uvs[0] = Vector2.up;
 1564            uvs[uvs.Length - 1] = Vector2.zero;
 51065            for (int lat = 0; lat < nbLat; lat++)
 66            {
 1248067                for (int lon = 0; lon <= nbLong; lon++)
 68                {
 600069                    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
 1579            int nbFaces = vertices.Length;
 1580            int nbTriangles = nbFaces * 2;
 1581            int nbIndexes = nbTriangles * 3;
 1582            int[] triangles = new int[nbIndexes];
 83
 84            //Top Cap
 1585            int i = 0;
 75086            for (int lon = 0; lon < nbLong; lon++)
 87            {
 36088                triangles[i++] = lon + 2;
 36089                triangles[i++] = lon + 1;
 36090                triangles[i++] = 0;
 91            }
 92
 93            //Middle
 48094            for (int lat = 0; lat < nbLat - 1; lat++)
 95            {
 1125096                for (int lon = 0; lon < nbLong; lon++)
 97                {
 540098                    int current = lon + lat * (nbLong + 1) + 1;
 540099                    int next = current + nbLong + 1;
 100
 5400101                    triangles[i++] = current;
 5400102                    triangles[i++] = current + 1;
 5400103                    triangles[i++] = next + 1;
 104
 5400105                    triangles[i++] = current;
 5400106                    triangles[i++] = next + 1;
 5400107                    triangles[i++] = next;
 108                }
 109            }
 110
 111            //Bottom Cap
 750112            for (int lon = 0; lon < nbLong; lon++)
 113            {
 360114                triangles[i++] = vertices.Length - 1;
 360115                triangles[i++] = vertices.Length - (lon + 2) - 1;
 360116                triangles[i++] = vertices.Length - (lon + 1) - 1;
 117            }
 118
 119            #endregion
 120
 15121            mesh.vertices = vertices;
 15122            mesh.normals = normales;
 15123            mesh.uv = uvs;
 15124            mesh.triangles = triangles;
 125
 15126            mesh.RecalculateBounds();
 15127            return mesh;
 128        }
 129
 130        public static Mesh BuildPlane(float _size)
 131        {
 36132            Mesh mesh = new Mesh();
 36133            mesh.name = "DCL Plane";
 36134            Vector3[] vertices = new Vector3[8];
 36135            Vector3[] normals = new Vector3[8];
 36136            Vector2[] uvs = new Vector2[8];
 36137            Color[] colors = new Color[8];
 138
 36139            int[] tris = new int[4 * 3];
 140
 36141            int vIndex = 0;
 36142            Vector3 start = new Vector3(-_size / 2, _size / 2, 0);
 36143            vertices[vIndex++] = new Vector3(-start.x, -start.y, 0);
 36144            vertices[vIndex++] = new Vector3(start.x, -start.y, 0);
 36145            vertices[vIndex++] = new Vector3(start.x, start.y, 0);
 36146            vertices[vIndex++] = new Vector3(-start.x, start.y, 0);
 147
 36148            vertices[vIndex++] = new Vector3(-start.x, -start.y, 0);
 36149            vertices[vIndex++] = new Vector3(start.x, -start.y, 0);
 36150            vertices[vIndex++] = new Vector3(start.x, start.y, 0);
 36151            vertices[vIndex++] = new Vector3(-start.x, start.y, 0);
 152
 36153            vIndex = 0;
 36154            uvs[vIndex++] = new Vector2(0f, 1f);
 36155            uvs[vIndex++] = new Vector2(1f, 1f);
 36156            uvs[vIndex++] = new Vector2(1f, 0f);
 36157            uvs[vIndex++] = new Vector2(0f, 0f);
 158
 36159            uvs[vIndex++] = new Vector2(0f, 1f);
 36160            uvs[vIndex++] = new Vector2(1f, 1f);
 36161            uvs[vIndex++] = new Vector2(1f, 0f);
 36162            uvs[vIndex++] = new Vector2(0f, 0f);
 163
 36164            vIndex = 0;
 36165            normals[vIndex++] = Vector3.forward;
 36166            normals[vIndex++] = Vector3.forward;
 36167            normals[vIndex++] = Vector3.forward;
 36168            normals[vIndex++] = Vector3.forward;
 169
 36170            normals[vIndex++] = Vector3.back;
 36171            normals[vIndex++] = Vector3.back;
 36172            normals[vIndex++] = Vector3.back;
 36173            normals[vIndex++] = Vector3.back;
 174
 36175            vIndex = 0;
 36176            colors[vIndex++] = Color.white;
 36177            colors[vIndex++] = Color.white;
 36178            colors[vIndex++] = Color.white;
 36179            colors[vIndex++] = Color.white;
 180
 36181            colors[vIndex++] = Color.white;
 36182            colors[vIndex++] = Color.white;
 36183            colors[vIndex++] = Color.white;
 36184            colors[vIndex++] = Color.white;
 185
 36186            int cnt = 0;
 36187            tris[cnt++] = 2;
 36188            tris[cnt++] = 1;
 36189            tris[cnt++] = 0;
 36190            tris[cnt++] = 3;
 36191            tris[cnt++] = 2;
 36192            tris[cnt++] = 0;
 193
 36194            tris[cnt++] = 4 + 1;
 36195            tris[cnt++] = 4 + 2;
 36196            tris[cnt++] = 4 + 0;
 36197            tris[cnt++] = 4 + 2;
 36198            tris[cnt++] = 4 + 3;
 36199            tris[cnt++] = 4 + 0;
 200
 36201            mesh.vertices = vertices;
 36202            mesh.normals = normals;
 36203            mesh.uv = uvs;
 36204            mesh.colors = colors;
 205
 36206            mesh.triangles = tris;
 36207            return mesh;
 208        }
 209
 210        public static Mesh BuildPlaneV2(float _size)
 211        {
 5212            Mesh mesh = new Mesh();
 5213            mesh.name = "DCL Plane";
 5214            Vector3[] vertices = new Vector3[8];
 5215            Vector3[] normals = new Vector3[8];
 5216            Vector2[] uvs = new Vector2[8];
 5217            Color[] colors = new Color[8];
 218
 5219            int[] tris = new int[4 * 3];
 220
 5221            int vIndex = 0;
 5222            Vector3 start = new Vector3(-_size / 2, _size / 2, 0);
 5223            vertices[vIndex++] = new Vector3(-start.x, start.y, 0);
 5224            vertices[vIndex++] = new Vector3(start.x, start.y, 0);
 5225            vertices[vIndex++] = new Vector3(start.x, -start.y, 0);
 5226            vertices[vIndex++] = new Vector3(-start.x,-start.y, 0);
 227
 5228            vertices[vIndex++] = new Vector3(-start.x, start.y, 0);
 5229            vertices[vIndex++] = new Vector3(start.x, start.y, 0);
 5230            vertices[vIndex++] = new Vector3(start.x, -start.y, 0);
 5231            vertices[vIndex++] = new Vector3(-start.x, -start.y, 0);
 232
 5233            vIndex = 0;
 5234            uvs[vIndex++] = new Vector2(0f, 1f);
 5235            uvs[vIndex++] = new Vector2(1f, 1f);
 5236            uvs[vIndex++] = new Vector2(1f, 0f);
 5237            uvs[vIndex++] = new Vector2(0f, 0f);
 238
 5239            uvs[vIndex++] = new Vector2(0f, 1f);
 5240            uvs[vIndex++] = new Vector2(1f, 1f);
 5241            uvs[vIndex++] = new Vector2(1f, 0f);
 5242            uvs[vIndex++] = new Vector2(0f, 0f);
 243
 5244            vIndex = 0;
 5245            normals[vIndex++] = Vector3.forward;
 5246            normals[vIndex++] = Vector3.forward;
 5247            normals[vIndex++] = Vector3.forward;
 5248            normals[vIndex++] = Vector3.forward;
 249
 5250            normals[vIndex++] = Vector3.back;
 5251            normals[vIndex++] = Vector3.back;
 5252            normals[vIndex++] = Vector3.back;
 5253            normals[vIndex++] = Vector3.back;
 254
 5255            vIndex = 0;
 5256            colors[vIndex++] = Color.white;
 5257            colors[vIndex++] = Color.white;
 5258            colors[vIndex++] = Color.white;
 5259            colors[vIndex++] = Color.white;
 260
 5261            colors[vIndex++] = Color.white;
 5262            colors[vIndex++] = Color.white;
 5263            colors[vIndex++] = Color.white;
 5264            colors[vIndex++] = Color.white;
 265
 5266            int cnt = 0;
 5267            tris[cnt++] = 2;
 5268            tris[cnt++] = 1;
 5269            tris[cnt++] = 0;
 5270            tris[cnt++] = 3;
 5271            tris[cnt++] = 2;
 5272            tris[cnt++] = 0;
 273
 5274            tris[cnt++] = 4 + 1;
 5275            tris[cnt++] = 4 + 2;
 5276            tris[cnt++] = 4 + 0;
 5277            tris[cnt++] = 4 + 2;
 5278            tris[cnt++] = 4 + 3;
 5279            tris[cnt++] = 4 + 0;
 280
 5281            mesh.vertices = vertices;
 5282            mesh.normals = normals;
 5283            mesh.uv = uvs;
 5284            mesh.colors = colors;
 285
 5286            mesh.triangles = tris;
 5287            return mesh;
 288        }
 289
 290        public static Mesh BuildCube(float _size)
 291        {
 29292            Mesh mesh = new Mesh();
 29293            mesh.name = "DCL Box";
 29294            Vector3[] vertices = new Vector3[24]; //top bottom left right front back
 29295            Vector3[] normals = new Vector3[24];
 29296            Vector2[] uvs = new Vector2[24];
 29297            Vector2[] uvs2 = new Vector2[24];
 29298            int[] tris = new int[12 * 3];
 299
 29300            int vIndex = 0;
 301            //top and bottom
 29302            Vector3 start = new Vector3(-_size / 2, _size / 2, _size / 2);
 29303            vertices[vIndex++] = start;
 29304            vertices[vIndex++] = start + Vector3.right * _size;
 29305            vertices[vIndex++] = start + Vector3.right * _size + Vector3.back * _size;
 29306            vertices[vIndex++] = start + Vector3.back * _size;
 307
 29308            start = new Vector3(-_size / 2, -_size / 2, _size / 2);
 29309            vertices[vIndex++] = start;
 29310            vertices[vIndex++] = start + Vector3.right * _size;
 29311            vertices[vIndex++] = start + Vector3.right * _size + Vector3.back * _size;
 29312            vertices[vIndex++] = start + Vector3.back * _size;
 313
 314            //left and right
 29315            start = new Vector3(-_size / 2, _size / 2, _size / 2);
 29316            vertices[vIndex++] = start;
 29317            vertices[vIndex++] = start + Vector3.back * _size;
 29318            vertices[vIndex++] = start + Vector3.back * _size + Vector3.down * _size;
 29319            vertices[vIndex++] = start + Vector3.down * _size;
 320
 29321            start = new Vector3(_size / 2, _size / 2, _size / 2);
 29322            vertices[vIndex++] = start;
 29323            vertices[vIndex++] = start + Vector3.back * _size;
 29324            vertices[vIndex++] = start + Vector3.back * _size + Vector3.down * _size;
 29325            vertices[vIndex++] = start + Vector3.down * _size;
 326
 327            //front and back
 29328            start = new Vector3(-_size / 2, _size / 2, _size / 2);
 29329            vertices[vIndex++] = start;
 29330            vertices[vIndex++] = start + Vector3.right * _size;
 29331            vertices[vIndex++] = start + Vector3.right * _size + Vector3.down * _size;
 29332            vertices[vIndex++] = start + Vector3.down * _size;
 333
 29334            start = new Vector3(-_size / 2, _size / 2, -_size / 2);
 29335            vertices[vIndex++] = start;
 29336            vertices[vIndex++] = start + Vector3.right * _size;
 29337            vertices[vIndex++] = start + Vector3.right * _size + Vector3.down * _size;
 29338            vertices[vIndex++] = start + Vector3.down * _size;
 339
 340            //uv
 29341            vIndex = 0;
 342            //top and bottom
 29343            uvs[vIndex++] = new Vector2(1f, 1f);
 29344            uvs[vIndex++] = new Vector2(1f, 0f);
 29345            uvs[vIndex++] = new Vector2(0f, 0f);
 29346            uvs[vIndex++] = new Vector2(0f, 1f);
 347
 29348            uvs[vIndex++] = new Vector2(1f, 0f);
 29349            uvs[vIndex++] = new Vector2(1f, 1f);
 29350            uvs[vIndex++] = new Vector2(0f, 1f);
 29351            uvs[vIndex++] = new Vector2(0f, 0f);
 352
 353            //left and right
 29354            uvs[vIndex++] = new Vector2(1f, 1f);
 29355            uvs[vIndex++] = new Vector2(1f, 0f);
 29356            uvs[vIndex++] = new Vector2(0f, 0f);
 29357            uvs[vIndex++] = new Vector2(0f, 1f);
 358
 29359            uvs[vIndex++] = new Vector2(1f, 0f);
 29360            uvs[vIndex++] = new Vector2(1f, 1f);
 29361            uvs[vIndex++] = new Vector2(0f, 1f);
 29362            uvs[vIndex++] = new Vector2(0f, 0f);
 363
 364            //front and back
 29365            uvs[vIndex++] = new Vector2(0f, 0f);
 29366            uvs[vIndex++] = new Vector2(1f, 0f);
 29367            uvs[vIndex++] = new Vector2(1f, 1f);
 29368            uvs[vIndex++] = new Vector2(0f, 1f);
 369
 29370            uvs[vIndex++] = new Vector2(0f, 1f);
 29371            uvs[vIndex++] = new Vector2(1f, 1f);
 29372            uvs[vIndex++] = new Vector2(1f, 0f);
 29373            uvs[vIndex++] = new Vector2(0f, 0f);
 374
 375            //uv2
 29376            vIndex = 0;
 377            //top and bottom
 29378            uvs2[vIndex++] = new Vector2(1f, 1f);
 29379            uvs2[vIndex++] = new Vector2(1f, 0f);
 29380            uvs2[vIndex++] = new Vector2(0f, 0f);
 29381            uvs2[vIndex++] = new Vector2(0f, 1f);
 382
 29383            uvs2[vIndex++] = new Vector2(1f, 0f);
 29384            uvs2[vIndex++] = new Vector2(1f, 1f);
 29385            uvs2[vIndex++] = new Vector2(0f, 1f);
 29386            uvs2[vIndex++] = new Vector2(0f, 0f);
 387
 388            //left and right
 29389            uvs2[vIndex++] = new Vector2(1f, 1f);
 29390            uvs2[vIndex++] = new Vector2(1f, 0f);
 29391            uvs2[vIndex++] = new Vector2(0f, 0f);
 29392            uvs2[vIndex++] = new Vector2(0f, 1f);
 393
 29394            uvs2[vIndex++] = new Vector2(1f, 0f);
 29395            uvs2[vIndex++] = new Vector2(1f, 1f);
 29396            uvs2[vIndex++] = new Vector2(0f, 1f);
 29397            uvs2[vIndex++] = new Vector2(0f, 0f);
 398
 399            //front and back
 29400            uvs2[vIndex++] = new Vector2(0f, 0f);
 29401            uvs2[vIndex++] = new Vector2(1f, 0f);
 29402            uvs2[vIndex++] = new Vector2(1f, 1f);
 29403            uvs2[vIndex++] = new Vector2(0f, 1f);
 404
 29405            uvs2[vIndex++] = new Vector2(0f, 1f);
 29406            uvs2[vIndex++] = new Vector2(1f, 1f);
 29407            uvs2[vIndex++] = new Vector2(1f, 0f);
 29408            uvs2[vIndex++] = new Vector2(0f, 0f);
 409
 410            //normal
 29411            vIndex = 0;
 412            //top and bottom
 29413            normals[vIndex++] = Vector3.up;
 29414            normals[vIndex++] = Vector3.up;
 29415            normals[vIndex++] = Vector3.up;
 29416            normals[vIndex++] = Vector3.up;
 417
 29418            normals[vIndex++] = Vector3.down;
 29419            normals[vIndex++] = Vector3.down;
 29420            normals[vIndex++] = Vector3.down;
 29421            normals[vIndex++] = Vector3.down;
 422
 423            //left and right
 29424            normals[vIndex++] = Vector3.left;
 29425            normals[vIndex++] = Vector3.left;
 29426            normals[vIndex++] = Vector3.left;
 29427            normals[vIndex++] = Vector3.left;
 428
 29429            normals[vIndex++] = Vector3.right;
 29430            normals[vIndex++] = Vector3.right;
 29431            normals[vIndex++] = Vector3.right;
 29432            normals[vIndex++] = Vector3.right;
 433
 434            //front and back
 29435            normals[vIndex++] = Vector3.forward;
 29436            normals[vIndex++] = Vector3.forward;
 29437            normals[vIndex++] = Vector3.forward;
 29438            normals[vIndex++] = Vector3.forward;
 439
 29440            normals[vIndex++] = Vector3.back;
 29441            normals[vIndex++] = Vector3.back;
 29442            normals[vIndex++] = Vector3.back;
 29443            normals[vIndex++] = Vector3.back;
 444
 445
 29446            int cnt = 0;
 447
 448            //top and bottom
 29449            tris[cnt++] = 0;
 29450            tris[cnt++] = 1;
 29451            tris[cnt++] = 2;
 29452            tris[cnt++] = 0;
 29453            tris[cnt++] = 2;
 29454            tris[cnt++] = 3;
 455
 29456            tris[cnt++] = 4 + 0;
 29457            tris[cnt++] = 4 + 2;
 29458            tris[cnt++] = 4 + 1;
 29459            tris[cnt++] = 4 + 0;
 29460            tris[cnt++] = 4 + 3;
 29461            tris[cnt++] = 4 + 2;
 462
 463            //left and right
 29464            tris[cnt++] = 8 + 0;
 29465            tris[cnt++] = 8 + 1;
 29466            tris[cnt++] = 8 + 2;
 29467            tris[cnt++] = 8 + 0;
 29468            tris[cnt++] = 8 + 2;
 29469            tris[cnt++] = 8 + 3;
 470
 29471            tris[cnt++] = 12 + 0;
 29472            tris[cnt++] = 12 + 2;
 29473            tris[cnt++] = 12 + 1;
 29474            tris[cnt++] = 12 + 0;
 29475            tris[cnt++] = 12 + 3;
 29476            tris[cnt++] = 12 + 2;
 477
 478            //front and back
 29479            tris[cnt++] = 16 + 0;
 29480            tris[cnt++] = 16 + 2;
 29481            tris[cnt++] = 16 + 1;
 29482            tris[cnt++] = 16 + 0;
 29483            tris[cnt++] = 16 + 3;
 29484            tris[cnt++] = 16 + 2;
 485
 29486            tris[cnt++] = 20 + 0;
 29487            tris[cnt++] = 20 + 1;
 29488            tris[cnt++] = 20 + 2;
 29489            tris[cnt++] = 20 + 0;
 29490            tris[cnt++] = 20 + 2;
 29491            tris[cnt++] = 20 + 3;
 492
 29493            mesh.vertices = vertices;
 29494            mesh.normals = normals;
 29495            mesh.uv = uvs;
 29496            mesh.uv2 = uvs2;
 497
 29498            mesh.triangles = tris;
 499
 29500            return mesh;
 501        }
 502
 503        public static Mesh BuildConeOrCylinder(int numVertices, float radiusTop, float radiusBottom, float length,
 504            float openingAngle, bool outside, bool inside, bool isCylinder, Vector3 offsetPos = default(Vector3))
 505        {
 28506            if (openingAngle > 0 && openingAngle < 180)
 507            {
 0508                radiusTop = 0;
 0509                radiusBottom = length * Mathf.Tan(openingAngle * Mathf.Deg2Rad / 2);
 510            }
 511
 28512            string meshName = isCylinder
 513                ? "DCL Cylinder"
 514                : "DCL Cone" + numVertices + "v" + radiusTop + "t" + radiusBottom + "b" + length + "l" + length +
 515                  (outside ? "o" : "") + (inside ? "i" : "");
 516            //string meshPrefabPath = "Assets/Decentraland/Internal/" + meshName + ".asset";
 28517            Mesh mesh = null; //(Mesh)AssetDatabase.LoadAssetAtPath(meshPrefabPath, typeof(Mesh));
 28518            if (mesh == null)
 519            {
 28520                int numVertices2 = numVertices + 1;
 521
 28522                mesh = new Mesh();
 28523                mesh.name = meshName;
 524                // can't access Camera.current
 525                //newCone.transform.position = Camera.current.transform.position + Camera.current.transform.forward * 5.
 28526                int multiplier = (outside ? 1 : 0) + (inside ? 1 : 0);
 28527                int offset = (outside && inside ? 2 * numVertices2 : 0);
 528
 28529                bool bTopCap = isCylinder ? true : false;
 28530                bool bBottomCap = true;
 531
 28532                Vector3[] vertices =
 533                    new Vector3[
 534                        2 * multiplier * numVertices2 + (bTopCap ? (numVertices + 1) : 0) +
 535                        (bBottomCap ? (numVertices + 1) : 0)];
 536                // 0..n-1: top, n..2n-1: bottom
 28537                Vector3[] normals =
 538                    new Vector3[
 539                        2 * multiplier * numVertices2 + (bTopCap ? (numVertices + 1) : 0) +
 540                        (bBottomCap ? (numVertices + 1) : 0)];
 28541                Vector2[] uvs =
 542                    new Vector2[
 543                        2 * multiplier * numVertices2 + (bTopCap ? (numVertices + 1) : 0) +
 544                        (bBottomCap ? (numVertices + 1) : 0)];
 545                int[] tris;
 28546                float slope = Mathf.Atan((radiusBottom - radiusTop) / length); // (rad difference)/height
 28547                float slopeSin = Mathf.Sin(slope);
 28548                float slopeCos = Mathf.Cos(slope);
 549                int i;
 550
 2856551                for (i = 0; i < numVertices; i++)
 552                {
 1400553                    float angle = 2 * Mathf.PI * i / numVertices;
 1400554                    float angleSin = Mathf.Sin(angle);
 1400555                    float angleCos = Mathf.Cos(angle);
 1400556                    float angleHalf = 2 * Mathf.PI * (i + 0.5f) / numVertices; // for degenerated normals at cone tips
 1400557                    float angleHalfSin = Mathf.Sin(angleHalf);
 1400558                    float angleHalfCos = Mathf.Cos(angleHalf);
 559
 1400560                    vertices[i] = new Vector3(radiusTop * angleCos, length, radiusTop * angleSin) + offsetPos;
 1400561                    vertices[i + numVertices2] =
 562                        new Vector3(radiusBottom * angleCos, 0, radiusBottom * angleSin) + offsetPos;
 563
 1400564                    if (radiusTop == 0)
 565                    {
 350566                        normals[i] = new Vector3(angleHalfCos * slopeCos, -slopeSin, angleHalfSin * slopeCos);
 350567                    }
 568                    else
 569                    {
 1050570                        normals[i] = new Vector3(angleCos * slopeCos, -slopeSin, angleSin * slopeCos);
 571                    }
 572
 1400573                    if (radiusBottom == 0)
 574                    {
 300575                        normals[i + numVertices2] =
 576                            new Vector3(angleHalfCos * slopeCos, -slopeSin, angleHalfSin * slopeCos);
 300577                    }
 578                    else
 579                    {
 1100580                        normals[i + numVertices2] = new Vector3(angleCos * slopeCos, -slopeSin, angleSin * slopeCos);
 581                    }
 582
 1400583                    uvs[i] = new Vector2(1.0f - 1.0f * i / numVertices, 1);
 1400584                    uvs[i + numVertices2] = new Vector2(1.0f - 1.0f * i / numVertices, 0);
 585
 1400586                    if (outside && inside)
 587                    {
 588                        // vertices and uvs are identical on inside and outside, so just copy
 0589                        vertices[i + 2 * numVertices2] = vertices[i];
 0590                        vertices[i + 3 * numVertices2] = vertices[i + numVertices2];
 0591                        uvs[i + 2 * numVertices2] = uvs[i];
 0592                        uvs[i + 3 * numVertices2] = uvs[i + numVertices2];
 593                    }
 594
 1400595                    if (inside)
 596                    {
 597                        // invert normals
 0598                        normals[i + offset] = -normals[i];
 0599                        normals[i + numVertices2 + offset] = -normals[i + numVertices2];
 600                    }
 601                }
 602
 28603                vertices[numVertices] = vertices[0];
 28604                vertices[numVertices + numVertices2] = vertices[0 + numVertices2];
 28605                uvs[numVertices] = new Vector2(1.0f - 1.0f * numVertices / numVertices, 1);
 28606                uvs[numVertices + numVertices2] = new Vector2(1.0f - 1.0f * numVertices / numVertices, 0);
 28607                normals[numVertices] = normals[0];
 28608                normals[numVertices + numVertices2] = normals[0 + numVertices2];
 609
 610
 28611                int coverTopIndexStart = 2 * multiplier * numVertices2;
 28612                int coverTopIndexEnd = 2 * multiplier * numVertices2 + numVertices;
 613
 28614                if (bTopCap)
 615                {
 1530616                    for (i = 0; i < numVertices; i++)
 617                    {
 750618                        float angle = 2 * Mathf.PI * i / numVertices;
 750619                        float angleSin = Mathf.Sin(angle);
 750620                        float angleCos = Mathf.Cos(angle);
 621
 750622                        vertices[coverTopIndexStart + i] =
 623                            new Vector3(radiusTop * angleCos, length, radiusTop * angleSin) + offsetPos;
 750624                        normals[coverTopIndexStart + i] = new Vector3(0, 1, 0);
 750625                        uvs[coverTopIndexStart + i] = new Vector2(angleCos / 2 + 0.5f, angleSin / 2 + 0.5f);
 626                    }
 627
 15628                    vertices[coverTopIndexStart + numVertices] = new Vector3(0, length, 0) + offsetPos;
 15629                    normals[coverTopIndexStart + numVertices] = new Vector3(0, 1, 0);
 15630                    uvs[coverTopIndexStart + numVertices] = new Vector2(0.5f, 0.5f);
 631                }
 632
 633
 28634                int coverBottomIndexStart = coverTopIndexStart + (bTopCap ? 1 : 0) * (numVertices + 1);
 28635                int coverBottomIndexEnd = coverBottomIndexStart + numVertices;
 28636                if (bBottomCap)
 637                {
 2856638                    for (i = 0; i < numVertices; i++)
 639                    {
 1400640                        float angle = 2 * Mathf.PI * i / numVertices;
 1400641                        float angleSin = Mathf.Sin(angle);
 1400642                        float angleCos = Mathf.Cos(angle);
 643
 1400644                        vertices[coverBottomIndexStart + i] =
 645                            new Vector3(radiusBottom * angleCos, 0f, radiusBottom * angleSin) +
 646                            offsetPos;
 1400647                        normals[coverBottomIndexStart + i] = new Vector3(0, -1, 0);
 1400648                        uvs[coverBottomIndexStart + i] = new Vector2(angleCos / 2 + 0.5f, angleSin / 2 + 0.5f);
 649                    }
 650
 28651                    vertices[coverBottomIndexStart + numVertices] = new Vector3(0, 0f, 0) + offsetPos;
 28652                    normals[coverBottomIndexStart + numVertices] = new Vector3(0, -1, 0);
 28653                    uvs[coverBottomIndexStart + numVertices] = new Vector2(0.5f, 0.5f);
 654                }
 655
 28656                mesh.vertices = vertices;
 28657                mesh.normals = normals;
 28658                mesh.uv = uvs;
 659
 660                // create triangles
 661                // here we need to take care of point order, depending on inside and outside
 28662                int cnt = 0;
 28663                if (radiusTop == 0)
 664                {
 665                    // top cone
 7666                    tris =
 667                        new int[numVertices2 * 3 * multiplier + ((bTopCap ? 1 : 0) * numVertices * 3) +
 668                                ((bBottomCap ? 1 : 0) * numVertices * 3)
 669                        ];
 7670                    if (outside)
 671                    {
 714672                        for (i = 0; i < numVertices; i++)
 673                        {
 350674                            tris[cnt++] = i + numVertices2;
 350675                            tris[cnt++] = i;
 350676                            tris[cnt++] = i + 1 + numVertices2;
 677                            //              if(i==numVertices-1)
 678                            //                tris[cnt++]=numVertices;
 679                            //              else
 680                            //                tris[cnt++]=i+1+numVertices;
 681                        }
 682                    }
 683
 7684                    if (inside)
 685                    {
 0686                        for (i = offset; i < numVertices + offset; i++)
 687                        {
 0688                            tris[cnt++] = i;
 0689                            tris[cnt++] = i + numVertices2;
 0690                            tris[cnt++] = i + 1 + numVertices2;
 691                            //              if(i==numVertices-1+offset)
 692                            //                tris[cnt++]=numVertices+offset;
 693                            //              else
 694                            //                tris[cnt++]=i+1+numVertices;
 695                        }
 696                    }
 0697                }
 21698                else if (radiusBottom == 0)
 699                {
 700                    // bottom cone
 6701                    tris =
 702                        new int[numVertices2 * 3 * multiplier + ((bTopCap ? 1 : 0) * numVertices * 3) +
 703                                ((bBottomCap ? 1 : 0) * numVertices * 3)
 704                        ];
 6705                    if (outside)
 706                    {
 612707                        for (i = 0; i < numVertices; i++)
 708                        {
 300709                            tris[cnt++] = i;
 300710                            tris[cnt++] = i + 1;
 711                            //              if(i==numVertices-1)
 712                            //                tris[cnt++]=0;
 713                            //              else
 714                            //                tris[cnt++]=i+1;
 300715                            tris[cnt++] = i + numVertices2;
 716                        }
 717                    }
 718
 6719                    if (inside)
 720                    {
 0721                        for (i = offset; i < numVertices + offset; i++)
 722                        {
 723                            //              if(i==numVertices-1+offset)
 724                            //                tris[cnt++]=offset;
 725                            //              else
 726                            //                tris[cnt++]=i+1;
 0727                            tris[cnt++] = i + 1;
 0728                            tris[cnt++] = i;
 0729                            tris[cnt++] = i + numVertices2;
 730                        }
 731                    }
 0732                }
 733                else
 734                {
 735                    // truncated cone
 15736                    tris =
 737                        new int[numVertices2 * 6 * multiplier + ((bTopCap ? 1 : 0) * numVertices * 3) +
 738                                ((bBottomCap ? 1 : 0) * numVertices * 3)
 739                        ];
 15740                    if (outside)
 741                    {
 1530742                        for (i = 0; i < numVertices; i++)
 743                        {
 750744                            int ip1 = i + 1;
 745                            //              if(ip1==numVertices)
 746                            //                ip1=0;
 747
 750748                            tris[cnt++] = i;
 750749                            tris[cnt++] = ip1;
 750750                            tris[cnt++] = i + numVertices2;
 751
 750752                            tris[cnt++] = ip1 + numVertices2;
 750753                            tris[cnt++] = i + numVertices2;
 750754                            tris[cnt++] = ip1;
 755                        }
 756                    }
 757
 15758                    if (inside)
 759                    {
 0760                        for (i = offset; i < numVertices + offset; i++)
 761                        {
 0762                            int ip1 = i + 1;
 763                            //              if(ip1==numVertices+offset)
 764                            //                ip1=offset;
 765
 0766                            tris[cnt++] = ip1;
 0767                            tris[cnt++] = i;
 0768                            tris[cnt++] = i + numVertices2;
 769
 0770                            tris[cnt++] = i + numVertices2;
 0771                            tris[cnt++] = ip1 + numVertices2;
 0772                            tris[cnt++] = ip1;
 773                        }
 774                    }
 775                }
 776
 28777                if (bTopCap)
 778                {
 1530779                    for (i = 0; i < numVertices; ++i)
 780                    {
 750781                        int next = coverTopIndexStart + i + 1;
 782
 750783                        if (next == coverTopIndexEnd)
 784                        {
 15785                            next = coverTopIndexStart;
 786                        }
 787
 750788                        tris[cnt++] = next;
 750789                        tris[cnt++] = coverTopIndexStart + i;
 750790                        tris[cnt++] = coverTopIndexEnd;
 791                    }
 792                }
 793
 28794                if (bBottomCap)
 795                {
 2856796                    for (i = 0; i < numVertices; ++i)
 797                    {
 1400798                        int next = coverBottomIndexStart + i + 1;
 1400799                        if (next == coverBottomIndexEnd)
 800                        {
 28801                            next = coverBottomIndexStart;
 802                        }
 803
 1400804                        tris[cnt++] = coverBottomIndexEnd;
 1400805                        tris[cnt++] = coverBottomIndexStart + i;
 1400806                        tris[cnt++] = next;
 807                    }
 808                }
 809
 28810                mesh.triangles = tris;
 811                //AssetDatabase.CreateAsset(mesh, meshPrefabPath);
 812                //AssetDatabase.SaveAssets();
 813            }
 814
 28815            return mesh;
 816        }
 817
 818        public static Mesh BuildCone(int numVertices, float radiusTop, float radiusBottom, float length,
 819            float openingAngle, bool outside, bool inside)
 820        {
 13821            return BuildConeOrCylinder(numVertices, radiusTop, radiusBottom, length, openingAngle, outside, inside,
 822                false,
 823                new Vector3(0f, -length / 2, 0f));
 824        }
 825
 826        public static Mesh BuildCylinder(int numVertices, float radiusTop, float radiusBottom, float length,
 827            float openingAngle, bool outside, bool inside)
 828        {
 15829            return BuildConeOrCylinder(numVertices, radiusTop, radiusBottom, length, openingAngle, outside, inside,
 830                true,
 831                new Vector3(0f, -length / 2, 0f));
 832        }
 833    }
 834}