< 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:457
Uncovered lines:24
Coverable lines:481
Total lines:834
Line coverage:95% (457 of 481)
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%104.2972081.6%
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        {
 1114            Mesh mesh = new Mesh();
 1115            mesh.name = "DCL Sphere";
 16
 17            //float radius = 1f;
 18            // Longitude |||
 1119            int nbLong = 24;
 20            // Latitude ---
 1121            int nbLat = 16;
 22
 23            #region Vertices
 24
 1125            Vector3[] vertices = new Vector3[(nbLong + 1) * nbLat + 2];
 1126            float _pi = Mathf.PI;
 1127            float _2pi = _pi * 2f;
 28
 1129            vertices[0] = Vector3.up * radius;
 37430            for (int lat = 0; lat < nbLat; lat++)
 31            {
 17632                float a1 = _pi * (float) (lat + 1) / (nbLat + 1);
 17633                float sin1 = Mathf.Sin(a1);
 17634                float cos1 = Mathf.Cos(a1);
 35
 915236                for (int lon = 0; lon <= nbLong; lon++)
 37                {
 440038                    float a2 = _2pi * (float) (lon == nbLong ? 0 : lon) / nbLong;
 440039                    float sin2 = Mathf.Sin(a2);
 440040                    float cos2 = Mathf.Cos(a2);
 41
 440042                    vertices[lon + lat * (nbLong + 1) + 1] = new Vector3(sin1 * cos2, cos1, sin1 * sin2) * radius;
 43                }
 44            }
 45
 1146            vertices[vertices.Length - 1] = Vector3.up * -radius;
 47
 48            #endregion
 49
 50            #region Normales
 51
 1152            Vector3[] normales = new Vector3[vertices.Length];
 886653            for (int n = 0; n < vertices.Length; n++)
 54            {
 442255                normales[n] = vertices[n].normalized;
 56            }
 57
 58            #endregion
 59
 60            #region UVs
 61
 1162            Vector2[] uvs = new Vector2[vertices.Length];
 1163            uvs[0] = Vector2.up;
 1164            uvs[uvs.Length - 1] = Vector2.zero;
 37465            for (int lat = 0; lat < nbLat; lat++)
 66            {
 915267                for (int lon = 0; lon <= nbLong; lon++)
 68                {
 440069                    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
 1179            int nbFaces = vertices.Length;
 1180            int nbTriangles = nbFaces * 2;
 1181            int nbIndexes = nbTriangles * 3;
 1182            int[] triangles = new int[nbIndexes];
 83
 84            //Top Cap
 1185            int i = 0;
 55086            for (int lon = 0; lon < nbLong; lon++)
 87            {
 26488                triangles[i++] = lon + 2;
 26489                triangles[i++] = lon + 1;
 26490                triangles[i++] = 0;
 91            }
 92
 93            //Middle
 35294            for (int lat = 0; lat < nbLat - 1; lat++)
 95            {
 825096                for (int lon = 0; lon < nbLong; lon++)
 97                {
 396098                    int current = lon + lat * (nbLong + 1) + 1;
 396099                    int next = current + nbLong + 1;
 100
 3960101                    triangles[i++] = current;
 3960102                    triangles[i++] = current + 1;
 3960103                    triangles[i++] = next + 1;
 104
 3960105                    triangles[i++] = current;
 3960106                    triangles[i++] = next + 1;
 3960107                    triangles[i++] = next;
 108                }
 109            }
 110
 111            //Bottom Cap
 550112            for (int lon = 0; lon < nbLong; lon++)
 113            {
 264114                triangles[i++] = vertices.Length - 1;
 264115                triangles[i++] = vertices.Length - (lon + 2) - 1;
 264116                triangles[i++] = vertices.Length - (lon + 1) - 1;
 117            }
 118
 119            #endregion
 120
 11121            mesh.vertices = vertices;
 11122            mesh.normals = normales;
 11123            mesh.uv = uvs;
 11124            mesh.triangles = triangles;
 125
 11126            mesh.RecalculateBounds();
 11127            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        {
 1212            Mesh mesh = new Mesh();
 1213            mesh.name = "DCL Plane";
 1214            Vector3[] vertices = new Vector3[8];
 1215            Vector3[] normals = new Vector3[8];
 1216            Vector2[] uvs = new Vector2[8];
 1217            Color[] colors = new Color[8];
 218
 1219            int[] tris = new int[4 * 3];
 220
 1221            int vIndex = 0;
 1222            Vector3 start = new Vector3(-_size / 2, _size / 2, 0);
 1223            vertices[vIndex++] = new Vector3(-start.x, start.y, 0);
 1224            vertices[vIndex++] = new Vector3(start.x, start.y, 0);
 1225            vertices[vIndex++] = new Vector3(start.x, -start.y, 0);
 1226            vertices[vIndex++] = new Vector3(-start.x,-start.y, 0);
 227
 1228            vertices[vIndex++] = new Vector3(-start.x, start.y, 0);
 1229            vertices[vIndex++] = new Vector3(start.x, start.y, 0);
 1230            vertices[vIndex++] = new Vector3(start.x, -start.y, 0);
 1231            vertices[vIndex++] = new Vector3(-start.x, -start.y, 0);
 232
 1233            vIndex = 0;
 1234            uvs[vIndex++] = new Vector2(0f, 1f);
 1235            uvs[vIndex++] = new Vector2(1f, 1f);
 1236            uvs[vIndex++] = new Vector2(1f, 0f);
 1237            uvs[vIndex++] = new Vector2(0f, 0f);
 238
 1239            uvs[vIndex++] = new Vector2(0f, 1f);
 1240            uvs[vIndex++] = new Vector2(1f, 1f);
 1241            uvs[vIndex++] = new Vector2(1f, 0f);
 1242            uvs[vIndex++] = new Vector2(0f, 0f);
 243
 1244            vIndex = 0;
 1245            normals[vIndex++] = Vector3.forward;
 1246            normals[vIndex++] = Vector3.forward;
 1247            normals[vIndex++] = Vector3.forward;
 1248            normals[vIndex++] = Vector3.forward;
 249
 1250            normals[vIndex++] = Vector3.back;
 1251            normals[vIndex++] = Vector3.back;
 1252            normals[vIndex++] = Vector3.back;
 1253            normals[vIndex++] = Vector3.back;
 254
 1255            vIndex = 0;
 1256            colors[vIndex++] = Color.white;
 1257            colors[vIndex++] = Color.white;
 1258            colors[vIndex++] = Color.white;
 1259            colors[vIndex++] = Color.white;
 260
 1261            colors[vIndex++] = Color.white;
 1262            colors[vIndex++] = Color.white;
 1263            colors[vIndex++] = Color.white;
 1264            colors[vIndex++] = Color.white;
 265
 1266            int cnt = 0;
 1267            tris[cnt++] = 2;
 1268            tris[cnt++] = 1;
 1269            tris[cnt++] = 0;
 1270            tris[cnt++] = 3;
 1271            tris[cnt++] = 2;
 1272            tris[cnt++] = 0;
 273
 1274            tris[cnt++] = 4 + 1;
 1275            tris[cnt++] = 4 + 2;
 1276            tris[cnt++] = 4 + 0;
 1277            tris[cnt++] = 4 + 2;
 1278            tris[cnt++] = 4 + 3;
 1279            tris[cnt++] = 4 + 0;
 280
 1281            mesh.vertices = vertices;
 1282            mesh.normals = normals;
 1283            mesh.uv = uvs;
 1284            mesh.colors = colors;
 285
 1286            mesh.triangles = tris;
 1287            return mesh;
 288        }
 289
 290        public static Mesh BuildCube(float _size)
 291        {
 26292            Mesh mesh = new Mesh();
 26293            mesh.name = "DCL Box";
 26294            Vector3[] vertices = new Vector3[24]; //top bottom left right front back
 26295            Vector3[] normals = new Vector3[24];
 26296            Vector2[] uvs = new Vector2[24];
 26297            Vector2[] uvs2 = new Vector2[24];
 26298            int[] tris = new int[12 * 3];
 299
 26300            int vIndex = 0;
 301            //top and bottom
 26302            Vector3 start = new Vector3(-_size / 2, _size / 2, _size / 2);
 26303            vertices[vIndex++] = start;
 26304            vertices[vIndex++] = start + Vector3.right * _size;
 26305            vertices[vIndex++] = start + Vector3.right * _size + Vector3.back * _size;
 26306            vertices[vIndex++] = start + Vector3.back * _size;
 307
 26308            start = new Vector3(-_size / 2, -_size / 2, _size / 2);
 26309            vertices[vIndex++] = start;
 26310            vertices[vIndex++] = start + Vector3.right * _size;
 26311            vertices[vIndex++] = start + Vector3.right * _size + Vector3.back * _size;
 26312            vertices[vIndex++] = start + Vector3.back * _size;
 313
 314            //left and right
 26315            start = new Vector3(-_size / 2, _size / 2, _size / 2);
 26316            vertices[vIndex++] = start;
 26317            vertices[vIndex++] = start + Vector3.back * _size;
 26318            vertices[vIndex++] = start + Vector3.back * _size + Vector3.down * _size;
 26319            vertices[vIndex++] = start + Vector3.down * _size;
 320
 26321            start = new Vector3(_size / 2, _size / 2, _size / 2);
 26322            vertices[vIndex++] = start;
 26323            vertices[vIndex++] = start + Vector3.back * _size;
 26324            vertices[vIndex++] = start + Vector3.back * _size + Vector3.down * _size;
 26325            vertices[vIndex++] = start + Vector3.down * _size;
 326
 327            //front and back
 26328            start = new Vector3(-_size / 2, _size / 2, _size / 2);
 26329            vertices[vIndex++] = start;
 26330            vertices[vIndex++] = start + Vector3.right * _size;
 26331            vertices[vIndex++] = start + Vector3.right * _size + Vector3.down * _size;
 26332            vertices[vIndex++] = start + Vector3.down * _size;
 333
 26334            start = new Vector3(-_size / 2, _size / 2, -_size / 2);
 26335            vertices[vIndex++] = start;
 26336            vertices[vIndex++] = start + Vector3.right * _size;
 26337            vertices[vIndex++] = start + Vector3.right * _size + Vector3.down * _size;
 26338            vertices[vIndex++] = start + Vector3.down * _size;
 339
 340            //uv
 26341            vIndex = 0;
 342            //top and bottom
 26343            uvs[vIndex++] = new Vector2(1f, 1f);
 26344            uvs[vIndex++] = new Vector2(1f, 0f);
 26345            uvs[vIndex++] = new Vector2(0f, 0f);
 26346            uvs[vIndex++] = new Vector2(0f, 1f);
 347
 26348            uvs[vIndex++] = new Vector2(1f, 0f);
 26349            uvs[vIndex++] = new Vector2(1f, 1f);
 26350            uvs[vIndex++] = new Vector2(0f, 1f);
 26351            uvs[vIndex++] = new Vector2(0f, 0f);
 352
 353            //left and right
 26354            uvs[vIndex++] = new Vector2(1f, 1f);
 26355            uvs[vIndex++] = new Vector2(1f, 0f);
 26356            uvs[vIndex++] = new Vector2(0f, 0f);
 26357            uvs[vIndex++] = new Vector2(0f, 1f);
 358
 26359            uvs[vIndex++] = new Vector2(1f, 0f);
 26360            uvs[vIndex++] = new Vector2(1f, 1f);
 26361            uvs[vIndex++] = new Vector2(0f, 1f);
 26362            uvs[vIndex++] = new Vector2(0f, 0f);
 363
 364            //front and back
 26365            uvs[vIndex++] = new Vector2(0f, 0f);
 26366            uvs[vIndex++] = new Vector2(1f, 0f);
 26367            uvs[vIndex++] = new Vector2(1f, 1f);
 26368            uvs[vIndex++] = new Vector2(0f, 1f);
 369
 26370            uvs[vIndex++] = new Vector2(0f, 1f);
 26371            uvs[vIndex++] = new Vector2(1f, 1f);
 26372            uvs[vIndex++] = new Vector2(1f, 0f);
 26373            uvs[vIndex++] = new Vector2(0f, 0f);
 374
 375            //uv2
 26376            vIndex = 0;
 377            //top and bottom
 26378            uvs2[vIndex++] = new Vector2(1f, 1f);
 26379            uvs2[vIndex++] = new Vector2(1f, 0f);
 26380            uvs2[vIndex++] = new Vector2(0f, 0f);
 26381            uvs2[vIndex++] = new Vector2(0f, 1f);
 382
 26383            uvs2[vIndex++] = new Vector2(1f, 0f);
 26384            uvs2[vIndex++] = new Vector2(1f, 1f);
 26385            uvs2[vIndex++] = new Vector2(0f, 1f);
 26386            uvs2[vIndex++] = new Vector2(0f, 0f);
 387
 388            //left and right
 26389            uvs2[vIndex++] = new Vector2(1f, 1f);
 26390            uvs2[vIndex++] = new Vector2(1f, 0f);
 26391            uvs2[vIndex++] = new Vector2(0f, 0f);
 26392            uvs2[vIndex++] = new Vector2(0f, 1f);
 393
 26394            uvs2[vIndex++] = new Vector2(1f, 0f);
 26395            uvs2[vIndex++] = new Vector2(1f, 1f);
 26396            uvs2[vIndex++] = new Vector2(0f, 1f);
 26397            uvs2[vIndex++] = new Vector2(0f, 0f);
 398
 399            //front and back
 26400            uvs2[vIndex++] = new Vector2(0f, 0f);
 26401            uvs2[vIndex++] = new Vector2(1f, 0f);
 26402            uvs2[vIndex++] = new Vector2(1f, 1f);
 26403            uvs2[vIndex++] = new Vector2(0f, 1f);
 404
 26405            uvs2[vIndex++] = new Vector2(0f, 1f);
 26406            uvs2[vIndex++] = new Vector2(1f, 1f);
 26407            uvs2[vIndex++] = new Vector2(1f, 0f);
 26408            uvs2[vIndex++] = new Vector2(0f, 0f);
 409
 410            //normal
 26411            vIndex = 0;
 412            //top and bottom
 26413            normals[vIndex++] = Vector3.up;
 26414            normals[vIndex++] = Vector3.up;
 26415            normals[vIndex++] = Vector3.up;
 26416            normals[vIndex++] = Vector3.up;
 417
 26418            normals[vIndex++] = Vector3.down;
 26419            normals[vIndex++] = Vector3.down;
 26420            normals[vIndex++] = Vector3.down;
 26421            normals[vIndex++] = Vector3.down;
 422
 423            //left and right
 26424            normals[vIndex++] = Vector3.left;
 26425            normals[vIndex++] = Vector3.left;
 26426            normals[vIndex++] = Vector3.left;
 26427            normals[vIndex++] = Vector3.left;
 428
 26429            normals[vIndex++] = Vector3.right;
 26430            normals[vIndex++] = Vector3.right;
 26431            normals[vIndex++] = Vector3.right;
 26432            normals[vIndex++] = Vector3.right;
 433
 434            //front and back
 26435            normals[vIndex++] = Vector3.forward;
 26436            normals[vIndex++] = Vector3.forward;
 26437            normals[vIndex++] = Vector3.forward;
 26438            normals[vIndex++] = Vector3.forward;
 439
 26440            normals[vIndex++] = Vector3.back;
 26441            normals[vIndex++] = Vector3.back;
 26442            normals[vIndex++] = Vector3.back;
 26443            normals[vIndex++] = Vector3.back;
 444
 445
 26446            int cnt = 0;
 447
 448            //top and bottom
 26449            tris[cnt++] = 0;
 26450            tris[cnt++] = 1;
 26451            tris[cnt++] = 2;
 26452            tris[cnt++] = 0;
 26453            tris[cnt++] = 2;
 26454            tris[cnt++] = 3;
 455
 26456            tris[cnt++] = 4 + 0;
 26457            tris[cnt++] = 4 + 2;
 26458            tris[cnt++] = 4 + 1;
 26459            tris[cnt++] = 4 + 0;
 26460            tris[cnt++] = 4 + 3;
 26461            tris[cnt++] = 4 + 2;
 462
 463            //left and right
 26464            tris[cnt++] = 8 + 0;
 26465            tris[cnt++] = 8 + 1;
 26466            tris[cnt++] = 8 + 2;
 26467            tris[cnt++] = 8 + 0;
 26468            tris[cnt++] = 8 + 2;
 26469            tris[cnt++] = 8 + 3;
 470
 26471            tris[cnt++] = 12 + 0;
 26472            tris[cnt++] = 12 + 2;
 26473            tris[cnt++] = 12 + 1;
 26474            tris[cnt++] = 12 + 0;
 26475            tris[cnt++] = 12 + 3;
 26476            tris[cnt++] = 12 + 2;
 477
 478            //front and back
 26479            tris[cnt++] = 16 + 0;
 26480            tris[cnt++] = 16 + 2;
 26481            tris[cnt++] = 16 + 1;
 26482            tris[cnt++] = 16 + 0;
 26483            tris[cnt++] = 16 + 3;
 26484            tris[cnt++] = 16 + 2;
 485
 26486            tris[cnt++] = 20 + 0;
 26487            tris[cnt++] = 20 + 1;
 26488            tris[cnt++] = 20 + 2;
 26489            tris[cnt++] = 20 + 0;
 26490            tris[cnt++] = 20 + 2;
 26491            tris[cnt++] = 20 + 3;
 492
 26493            mesh.vertices = vertices;
 26494            mesh.normals = normals;
 26495            mesh.uv = uvs;
 26496            mesh.uv2 = uvs2;
 497
 26498            mesh.triangles = tris;
 499
 26500            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        {
 25506            if (openingAngle > 0 && openingAngle < 180)
 507            {
 0508                radiusTop = 0;
 0509                radiusBottom = length * Mathf.Tan(openingAngle * Mathf.Deg2Rad / 2);
 510            }
 511
 25512            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";
 25517            Mesh mesh = null; //(Mesh)AssetDatabase.LoadAssetAtPath(meshPrefabPath, typeof(Mesh));
 25518            if (mesh == null)
 519            {
 25520                int numVertices2 = numVertices + 1;
 521
 25522                mesh = new Mesh();
 25523                mesh.name = meshName;
 524                // can't access Camera.current
 525                //newCone.transform.position = Camera.current.transform.position + Camera.current.transform.forward * 5.
 25526                int multiplier = (outside ? 1 : 0) + (inside ? 1 : 0);
 25527                int offset = (outside && inside ? 2 * numVertices2 : 0);
 528
 25529                bool bTopCap = isCylinder ? true : false;
 25530                bool bBottomCap = true;
 531
 25532                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
 25537                Vector3[] normals =
 538                    new Vector3[
 539                        2 * multiplier * numVertices2 + (bTopCap ? (numVertices + 1) : 0) +
 540                        (bBottomCap ? (numVertices + 1) : 0)];
 25541                Vector2[] uvs =
 542                    new Vector2[
 543                        2 * multiplier * numVertices2 + (bTopCap ? (numVertices + 1) : 0) +
 544                        (bBottomCap ? (numVertices + 1) : 0)];
 545                int[] tris;
 25546                float slope = Mathf.Atan((radiusBottom - radiusTop) / length); // (rad difference)/height
 25547                float slopeSin = Mathf.Sin(slope);
 25548                float slopeCos = Mathf.Cos(slope);
 549                int i;
 550
 2550551                for (i = 0; i < numVertices; i++)
 552                {
 1250553                    float angle = 2 * Mathf.PI * i / numVertices;
 1250554                    float angleSin = Mathf.Sin(angle);
 1250555                    float angleCos = Mathf.Cos(angle);
 1250556                    float angleHalf = 2 * Mathf.PI * (i + 0.5f) / numVertices; // for degenerated normals at cone tips
 1250557                    float angleHalfSin = Mathf.Sin(angleHalf);
 1250558                    float angleHalfCos = Mathf.Cos(angleHalf);
 559
 1250560                    vertices[i] = new Vector3(radiusTop * angleCos, length, radiusTop * angleSin) + offsetPos;
 1250561                    vertices[i + numVertices2] =
 562                        new Vector3(radiusBottom * angleCos, 0, radiusBottom * angleSin) + offsetPos;
 563
 1250564                    if (radiusTop == 0)
 565                    {
 350566                        normals[i] = new Vector3(angleHalfCos * slopeCos, -slopeSin, angleHalfSin * slopeCos);
 567                    }
 568                    else
 569                    {
 900570                        normals[i] = new Vector3(angleCos * slopeCos, -slopeSin, angleSin * slopeCos);
 571                    }
 572
 1250573                    if (radiusBottom == 0)
 574                    {
 300575                        normals[i + numVertices2] =
 576                            new Vector3(angleHalfCos * slopeCos, -slopeSin, angleHalfSin * slopeCos);
 577                    }
 578                    else
 579                    {
 950580                        normals[i + numVertices2] = new Vector3(angleCos * slopeCos, -slopeSin, angleSin * slopeCos);
 581                    }
 582
 1250583                    uvs[i] = new Vector2(1.0f - 1.0f * i / numVertices, 1);
 1250584                    uvs[i + numVertices2] = new Vector2(1.0f - 1.0f * i / numVertices, 0);
 585
 1250586                    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
 1250595                    if (inside)
 596                    {
 597                        // invert normals
 0598                        normals[i + offset] = -normals[i];
 0599                        normals[i + numVertices2 + offset] = -normals[i + numVertices2];
 600                    }
 601                }
 602
 25603                vertices[numVertices] = vertices[0];
 25604                vertices[numVertices + numVertices2] = vertices[0 + numVertices2];
 25605                uvs[numVertices] = new Vector2(1.0f - 1.0f * numVertices / numVertices, 1);
 25606                uvs[numVertices + numVertices2] = new Vector2(1.0f - 1.0f * numVertices / numVertices, 0);
 25607                normals[numVertices] = normals[0];
 25608                normals[numVertices + numVertices2] = normals[0 + numVertices2];
 609
 610
 25611                int coverTopIndexStart = 2 * multiplier * numVertices2;
 25612                int coverTopIndexEnd = 2 * multiplier * numVertices2 + numVertices;
 613
 25614                if (bTopCap)
 615                {
 1224616                    for (i = 0; i < numVertices; i++)
 617                    {
 600618                        float angle = 2 * Mathf.PI * i / numVertices;
 600619                        float angleSin = Mathf.Sin(angle);
 600620                        float angleCos = Mathf.Cos(angle);
 621
 600622                        vertices[coverTopIndexStart + i] =
 623                            new Vector3(radiusTop * angleCos, length, radiusTop * angleSin) + offsetPos;
 600624                        normals[coverTopIndexStart + i] = new Vector3(0, 1, 0);
 600625                        uvs[coverTopIndexStart + i] = new Vector2(angleCos / 2 + 0.5f, angleSin / 2 + 0.5f);
 626                    }
 627
 12628                    vertices[coverTopIndexStart + numVertices] = new Vector3(0, length, 0) + offsetPos;
 12629                    normals[coverTopIndexStart + numVertices] = new Vector3(0, 1, 0);
 12630                    uvs[coverTopIndexStart + numVertices] = new Vector2(0.5f, 0.5f);
 631                }
 632
 633
 25634                int coverBottomIndexStart = coverTopIndexStart + (bTopCap ? 1 : 0) * (numVertices + 1);
 25635                int coverBottomIndexEnd = coverBottomIndexStart + numVertices;
 25636                if (bBottomCap)
 637                {
 2550638                    for (i = 0; i < numVertices; i++)
 639                    {
 1250640                        float angle = 2 * Mathf.PI * i / numVertices;
 1250641                        float angleSin = Mathf.Sin(angle);
 1250642                        float angleCos = Mathf.Cos(angle);
 643
 1250644                        vertices[coverBottomIndexStart + i] =
 645                            new Vector3(radiusBottom * angleCos, 0f, radiusBottom * angleSin) +
 646                            offsetPos;
 1250647                        normals[coverBottomIndexStart + i] = new Vector3(0, -1, 0);
 1250648                        uvs[coverBottomIndexStart + i] = new Vector2(angleCos / 2 + 0.5f, angleSin / 2 + 0.5f);
 649                    }
 650
 25651                    vertices[coverBottomIndexStart + numVertices] = new Vector3(0, 0f, 0) + offsetPos;
 25652                    normals[coverBottomIndexStart + numVertices] = new Vector3(0, -1, 0);
 25653                    uvs[coverBottomIndexStart + numVertices] = new Vector2(0.5f, 0.5f);
 654                }
 655
 25656                mesh.vertices = vertices;
 25657                mesh.normals = normals;
 25658                mesh.uv = uvs;
 659
 660                // create triangles
 661                // here we need to take care of point order, depending on inside and outside
 25662                int cnt = 0;
 25663                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                    }
 697                }
 18698                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                    }
 732                }
 733                else
 734                {
 735                    // truncated cone
 12736                    tris =
 737                        new int[numVertices2 * 6 * multiplier + ((bTopCap ? 1 : 0) * numVertices * 3) +
 738                                ((bBottomCap ? 1 : 0) * numVertices * 3)
 739                        ];
 12740                    if (outside)
 741                    {
 1224742                        for (i = 0; i < numVertices; i++)
 743                        {
 600744                            int ip1 = i + 1;
 745                            //              if(ip1==numVertices)
 746                            //                ip1=0;
 747
 600748                            tris[cnt++] = i;
 600749                            tris[cnt++] = ip1;
 600750                            tris[cnt++] = i + numVertices2;
 751
 600752                            tris[cnt++] = ip1 + numVertices2;
 600753                            tris[cnt++] = i + numVertices2;
 600754                            tris[cnt++] = ip1;
 755                        }
 756                    }
 757
 12758                    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
 25777                if (bTopCap)
 778                {
 1224779                    for (i = 0; i < numVertices; ++i)
 780                    {
 600781                        int next = coverTopIndexStart + i + 1;
 782
 600783                        if (next == coverTopIndexEnd)
 784                        {
 12785                            next = coverTopIndexStart;
 786                        }
 787
 600788                        tris[cnt++] = next;
 600789                        tris[cnt++] = coverTopIndexStart + i;
 600790                        tris[cnt++] = coverTopIndexEnd;
 791                    }
 792                }
 793
 25794                if (bBottomCap)
 795                {
 2550796                    for (i = 0; i < numVertices; ++i)
 797                    {
 1250798                        int next = coverBottomIndexStart + i + 1;
 1250799                        if (next == coverBottomIndexEnd)
 800                        {
 25801                            next = coverBottomIndexStart;
 802                        }
 803
 1250804                        tris[cnt++] = coverBottomIndexEnd;
 1250805                        tris[cnt++] = coverBottomIndexStart + i;
 1250806                        tris[cnt++] = next;
 807                    }
 808                }
 809
 25810                mesh.triangles = tris;
 811                //AssetDatabase.CreateAsset(mesh, meshPrefabPath);
 812                //AssetDatabase.SaveAssets();
 813            }
 814
 25815            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        {
 12829            return BuildConeOrCylinder(numVertices, radiusTop, radiusBottom, length, openingAngle, outside, inside,
 830                true,
 831                new Vector3(0f, -length / 2, 0f));
 832        }
 833    }
 834}