< 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:446
Uncovered lines:24
Coverable lines:470
Total lines:825
Line coverage:94.8% (446 of 470)
Covered branches:0
Total branches:0
Covered methods:7
Total methods:7
Method coverage:100% (7 of 7)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BuildSphere(...)0%12120100%
BuildPlane(...)0%110100%
BuildPlaneV2(...)0%220100%
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, int longitude = 24, int latitude = 16)
 13        {
 1214            Mesh mesh = new Mesh();
 1215            mesh.name = "DCL Sphere";
 16
 17            //float radius = 1f;
 18            // Longitude |||
 1219            int nbLong = longitude;
 20            // Latitude ---
 1221            int nbLat = latitude;
 22
 23            #region Vertices
 24
 1225            Vector3[] vertices = new Vector3[(nbLong + 1) * nbLat + 2];
 1226            float _pi = Mathf.PI;
 1227            float _2pi = _pi * 2f;
 28
 1229            vertices[0] = Vector3.up * radius;
 38830            for (int lat = 0; lat < nbLat; lat++)
 31            {
 18232                float a1 = _pi * (float) (lat + 1) / (nbLat + 1);
 18233                float sin1 = Mathf.Sin(a1);
 18234                float cos1 = Mathf.Cos(a1);
 35
 932036                for (int lon = 0; lon <= nbLong; lon++)
 37                {
 447838                    float a2 = _2pi * (float) (lon == nbLong ? 0 : lon) / nbLong;
 447839                    float sin2 = Mathf.Sin(a2);
 447840                    float cos2 = Mathf.Cos(a2);
 41
 447842                    vertices[lon + lat * (nbLong + 1) + 1] = new Vector3(sin1 * cos2, cos1, sin1 * sin2) * radius;
 43                }
 44            }
 45
 1246            vertices[vertices.Length - 1] = Vector3.up * -radius;
 47
 48            #endregion
 49
 50            #region Normales
 51
 1252            Vector3[] normales = new Vector3[vertices.Length];
 902853            for (int n = 0; n < vertices.Length; n++)
 54            {
 450255                normales[n] = vertices[n].normalized;
 56            }
 57
 58            #endregion
 59
 60            #region UVs
 61
 1262            Vector2[] uvs = new Vector2[vertices.Length];
 1263            uvs[0] = Vector2.up;
 1264            uvs[uvs.Length - 1] = Vector2.zero;
 38865            for (int lat = 0; lat < nbLat; lat++)
 66            {
 932067                for (int lon = 0; lon <= nbLong; lon++)
 68                {
 447869                    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
 1279            int nbFaces = vertices.Length;
 1280            int nbTriangles = nbFaces * 2;
 1281            int nbIndexes = nbTriangles * 3;
 1282            int[] triangles = new int[nbIndexes];
 83
 84            //Top Cap
 1285            int i = 0;
 57686            for (int lon = 0; lon < nbLong; lon++)
 87            {
 27688                triangles[i++] = lon + 2;
 27689                triangles[i++] = lon + 1;
 27690                triangles[i++] = 0;
 91            }
 92
 93            //Middle
 36494            for (int lat = 0; lat < nbLat - 1; lat++)
 95            {
 838096                for (int lon = 0; lon < nbLong; lon++)
 97                {
 402098                    int current = lon + lat * (nbLong + 1) + 1;
 402099                    int next = current + nbLong + 1;
 100
 4020101                    triangles[i++] = current;
 4020102                    triangles[i++] = current + 1;
 4020103                    triangles[i++] = next + 1;
 104
 4020105                    triangles[i++] = current;
 4020106                    triangles[i++] = next + 1;
 4020107                    triangles[i++] = next;
 108                }
 109            }
 110
 111            //Bottom Cap
 576112            for (int lon = 0; lon < nbLong; lon++)
 113            {
 276114                triangles[i++] = vertices.Length - 1;
 276115                triangles[i++] = vertices.Length - (lon + 2) - 1;
 276116                triangles[i++] = vertices.Length - (lon + 1) - 1;
 117            }
 118
 119            #endregion
 120
 12121            mesh.vertices = vertices;
 12122            mesh.normals = normales;
 12123            mesh.uv = uvs;
 12124            mesh.triangles = triangles;
 125
 12126            mesh.RecalculateBounds();
 12127            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        // Creates a two-sided quad (clockwise)
 211        public static Mesh BuildPlaneV2(float _size)
 212        {
 1213            Mesh mesh = new Mesh();
 1214            mesh.name = "DCL Plane";
 215
 1216            Vector3 half = new Vector3(_size / 2, _size / 2, 0);
 217
 1218            Vector3[] vertices = new Vector3[8];
 1219            vertices[0] = new Vector3(-half.x, -half.y, 0);
 1220            vertices[1] = new Vector3(-half.x, half.y, 0);
 1221            vertices[2] = new Vector3(half.x, half.y, 0);
 1222            vertices[3] = new Vector3(half.x, -half.y, 0);
 223
 1224            vertices[4] = new Vector3(half.x, -half.y, 0);
 1225            vertices[5] = new Vector3(half.x, half.y, 0);
 1226            vertices[6] = new Vector3(-half.x, half.y, 0);
 1227            vertices[7] = new Vector3(-half.x, -half.y, 0);
 228
 1229            Vector2[] uvs = new Vector2[8];
 1230            uvs[0] = new Vector2(0f, 0f);
 1231            uvs[1] = new Vector2(0f, 1f);
 1232            uvs[2] = new Vector2(1f, 1f);
 1233            uvs[3] = new Vector2(1f, 0f);
 234
 1235            uvs[4] = new Vector2(1f, 0f);
 1236            uvs[5] = new Vector2(1f, 1f);
 1237            uvs[6] = new Vector2(0f, 1f);
 1238            uvs[7] = new Vector2(0f, 0f);
 239
 1240            int[] tris = new int[4 * 3];
 1241            tris[0] = 0;
 1242            tris[1] = 1;
 1243            tris[2] = 2;
 1244            tris[3] = 2;
 1245            tris[4] = 3;
 1246            tris[5] = 0;
 247
 1248            tris[6] = 4;
 1249            tris[7] = 5;
 1250            tris[8] = 6;
 1251            tris[9] = 6;
 1252            tris[10] = 7;
 1253            tris[11] = 4;
 254
 1255            Vector3[] normals = new Vector3[8];
 1256            normals[0] = Vector3.back;
 1257            normals[1] = Vector3.back;
 1258            normals[2] = Vector3.back;
 1259            normals[3] = Vector3.back;
 260
 1261            normals[4] = Vector3.forward;
 1262            normals[5] = Vector3.forward;
 1263            normals[6] = Vector3.forward;
 1264            normals[7] = Vector3.forward;
 265
 1266            Color[] colors = new Color[8];
 267
 18268            for (int i = 0; i < colors.Length; i++)
 269            {
 8270                colors[i] = Color.white;
 271            }
 272
 1273            mesh.vertices = vertices;
 1274            mesh.normals = normals;
 1275            mesh.uv = uvs;
 1276            mesh.colors = colors;
 1277            mesh.triangles = tris;
 1278            return mesh;
 279        }
 280
 281        public static Mesh BuildCube(float _size)
 282        {
 22283            Mesh mesh = new Mesh();
 22284            mesh.name = "DCL Box";
 22285            Vector3[] vertices = new Vector3[24]; //top bottom left right front back
 22286            Vector3[] normals = new Vector3[24];
 22287            Vector2[] uvs = new Vector2[24];
 22288            Vector2[] uvs2 = new Vector2[24];
 22289            int[] tris = new int[12 * 3];
 290
 22291            int vIndex = 0;
 292            //top and bottom
 22293            Vector3 start = new Vector3(-_size / 2, _size / 2, _size / 2);
 22294            vertices[vIndex++] = start;
 22295            vertices[vIndex++] = start + Vector3.right * _size;
 22296            vertices[vIndex++] = start + Vector3.right * _size + Vector3.back * _size;
 22297            vertices[vIndex++] = start + Vector3.back * _size;
 298
 22299            start = new Vector3(-_size / 2, -_size / 2, _size / 2);
 22300            vertices[vIndex++] = start;
 22301            vertices[vIndex++] = start + Vector3.right * _size;
 22302            vertices[vIndex++] = start + Vector3.right * _size + Vector3.back * _size;
 22303            vertices[vIndex++] = start + Vector3.back * _size;
 304
 305            //left and right
 22306            start = new Vector3(-_size / 2, _size / 2, _size / 2);
 22307            vertices[vIndex++] = start;
 22308            vertices[vIndex++] = start + Vector3.back * _size;
 22309            vertices[vIndex++] = start + Vector3.back * _size + Vector3.down * _size;
 22310            vertices[vIndex++] = start + Vector3.down * _size;
 311
 22312            start = new Vector3(_size / 2, _size / 2, _size / 2);
 22313            vertices[vIndex++] = start;
 22314            vertices[vIndex++] = start + Vector3.back * _size;
 22315            vertices[vIndex++] = start + Vector3.back * _size + Vector3.down * _size;
 22316            vertices[vIndex++] = start + Vector3.down * _size;
 317
 318            //front and back
 22319            start = new Vector3(-_size / 2, _size / 2, _size / 2);
 22320            vertices[vIndex++] = start;
 22321            vertices[vIndex++] = start + Vector3.right * _size;
 22322            vertices[vIndex++] = start + Vector3.right * _size + Vector3.down * _size;
 22323            vertices[vIndex++] = start + Vector3.down * _size;
 324
 22325            start = new Vector3(-_size / 2, _size / 2, -_size / 2);
 22326            vertices[vIndex++] = start;
 22327            vertices[vIndex++] = start + Vector3.right * _size;
 22328            vertices[vIndex++] = start + Vector3.right * _size + Vector3.down * _size;
 22329            vertices[vIndex++] = start + Vector3.down * _size;
 330
 331            //uv
 22332            vIndex = 0;
 333            //top and bottom
 22334            uvs[vIndex++] = new Vector2(1f, 1f);
 22335            uvs[vIndex++] = new Vector2(1f, 0f);
 22336            uvs[vIndex++] = new Vector2(0f, 0f);
 22337            uvs[vIndex++] = new Vector2(0f, 1f);
 338
 22339            uvs[vIndex++] = new Vector2(1f, 0f);
 22340            uvs[vIndex++] = new Vector2(1f, 1f);
 22341            uvs[vIndex++] = new Vector2(0f, 1f);
 22342            uvs[vIndex++] = new Vector2(0f, 0f);
 343
 344            //left and right
 22345            uvs[vIndex++] = new Vector2(1f, 1f);
 22346            uvs[vIndex++] = new Vector2(1f, 0f);
 22347            uvs[vIndex++] = new Vector2(0f, 0f);
 22348            uvs[vIndex++] = new Vector2(0f, 1f);
 349
 22350            uvs[vIndex++] = new Vector2(1f, 0f);
 22351            uvs[vIndex++] = new Vector2(1f, 1f);
 22352            uvs[vIndex++] = new Vector2(0f, 1f);
 22353            uvs[vIndex++] = new Vector2(0f, 0f);
 354
 355            //front and back
 22356            uvs[vIndex++] = new Vector2(0f, 0f);
 22357            uvs[vIndex++] = new Vector2(1f, 0f);
 22358            uvs[vIndex++] = new Vector2(1f, 1f);
 22359            uvs[vIndex++] = new Vector2(0f, 1f);
 360
 22361            uvs[vIndex++] = new Vector2(0f, 1f);
 22362            uvs[vIndex++] = new Vector2(1f, 1f);
 22363            uvs[vIndex++] = new Vector2(1f, 0f);
 22364            uvs[vIndex++] = new Vector2(0f, 0f);
 365
 366            //uv2
 22367            vIndex = 0;
 368            //top and bottom
 22369            uvs2[vIndex++] = new Vector2(1f, 1f);
 22370            uvs2[vIndex++] = new Vector2(1f, 0f);
 22371            uvs2[vIndex++] = new Vector2(0f, 0f);
 22372            uvs2[vIndex++] = new Vector2(0f, 1f);
 373
 22374            uvs2[vIndex++] = new Vector2(1f, 0f);
 22375            uvs2[vIndex++] = new Vector2(1f, 1f);
 22376            uvs2[vIndex++] = new Vector2(0f, 1f);
 22377            uvs2[vIndex++] = new Vector2(0f, 0f);
 378
 379            //left and right
 22380            uvs2[vIndex++] = new Vector2(1f, 1f);
 22381            uvs2[vIndex++] = new Vector2(1f, 0f);
 22382            uvs2[vIndex++] = new Vector2(0f, 0f);
 22383            uvs2[vIndex++] = new Vector2(0f, 1f);
 384
 22385            uvs2[vIndex++] = new Vector2(1f, 0f);
 22386            uvs2[vIndex++] = new Vector2(1f, 1f);
 22387            uvs2[vIndex++] = new Vector2(0f, 1f);
 22388            uvs2[vIndex++] = new Vector2(0f, 0f);
 389
 390            //front and back
 22391            uvs2[vIndex++] = new Vector2(0f, 0f);
 22392            uvs2[vIndex++] = new Vector2(1f, 0f);
 22393            uvs2[vIndex++] = new Vector2(1f, 1f);
 22394            uvs2[vIndex++] = new Vector2(0f, 1f);
 395
 22396            uvs2[vIndex++] = new Vector2(0f, 1f);
 22397            uvs2[vIndex++] = new Vector2(1f, 1f);
 22398            uvs2[vIndex++] = new Vector2(1f, 0f);
 22399            uvs2[vIndex++] = new Vector2(0f, 0f);
 400
 401            //normal
 22402            vIndex = 0;
 403            //top and bottom
 22404            normals[vIndex++] = Vector3.up;
 22405            normals[vIndex++] = Vector3.up;
 22406            normals[vIndex++] = Vector3.up;
 22407            normals[vIndex++] = Vector3.up;
 408
 22409            normals[vIndex++] = Vector3.down;
 22410            normals[vIndex++] = Vector3.down;
 22411            normals[vIndex++] = Vector3.down;
 22412            normals[vIndex++] = Vector3.down;
 413
 414            //left and right
 22415            normals[vIndex++] = Vector3.left;
 22416            normals[vIndex++] = Vector3.left;
 22417            normals[vIndex++] = Vector3.left;
 22418            normals[vIndex++] = Vector3.left;
 419
 22420            normals[vIndex++] = Vector3.right;
 22421            normals[vIndex++] = Vector3.right;
 22422            normals[vIndex++] = Vector3.right;
 22423            normals[vIndex++] = Vector3.right;
 424
 425            //front and back
 22426            normals[vIndex++] = Vector3.forward;
 22427            normals[vIndex++] = Vector3.forward;
 22428            normals[vIndex++] = Vector3.forward;
 22429            normals[vIndex++] = Vector3.forward;
 430
 22431            normals[vIndex++] = Vector3.back;
 22432            normals[vIndex++] = Vector3.back;
 22433            normals[vIndex++] = Vector3.back;
 22434            normals[vIndex++] = Vector3.back;
 435
 436
 22437            int cnt = 0;
 438
 439            //top and bottom
 22440            tris[cnt++] = 0;
 22441            tris[cnt++] = 1;
 22442            tris[cnt++] = 2;
 22443            tris[cnt++] = 0;
 22444            tris[cnt++] = 2;
 22445            tris[cnt++] = 3;
 446
 22447            tris[cnt++] = 4 + 0;
 22448            tris[cnt++] = 4 + 2;
 22449            tris[cnt++] = 4 + 1;
 22450            tris[cnt++] = 4 + 0;
 22451            tris[cnt++] = 4 + 3;
 22452            tris[cnt++] = 4 + 2;
 453
 454            //left and right
 22455            tris[cnt++] = 8 + 0;
 22456            tris[cnt++] = 8 + 1;
 22457            tris[cnt++] = 8 + 2;
 22458            tris[cnt++] = 8 + 0;
 22459            tris[cnt++] = 8 + 2;
 22460            tris[cnt++] = 8 + 3;
 461
 22462            tris[cnt++] = 12 + 0;
 22463            tris[cnt++] = 12 + 2;
 22464            tris[cnt++] = 12 + 1;
 22465            tris[cnt++] = 12 + 0;
 22466            tris[cnt++] = 12 + 3;
 22467            tris[cnt++] = 12 + 2;
 468
 469            //front and back
 22470            tris[cnt++] = 16 + 0;
 22471            tris[cnt++] = 16 + 2;
 22472            tris[cnt++] = 16 + 1;
 22473            tris[cnt++] = 16 + 0;
 22474            tris[cnt++] = 16 + 3;
 22475            tris[cnt++] = 16 + 2;
 476
 22477            tris[cnt++] = 20 + 0;
 22478            tris[cnt++] = 20 + 1;
 22479            tris[cnt++] = 20 + 2;
 22480            tris[cnt++] = 20 + 0;
 22481            tris[cnt++] = 20 + 2;
 22482            tris[cnt++] = 20 + 3;
 483
 22484            mesh.vertices = vertices;
 22485            mesh.normals = normals;
 22486            mesh.uv = uvs;
 22487            mesh.uv2 = uvs2;
 488
 22489            mesh.triangles = tris;
 490
 22491            return mesh;
 492        }
 493
 494        public static Mesh BuildConeOrCylinder(int numVertices, float radiusTop, float radiusBottom, float length,
 495            float openingAngle, bool outside, bool inside, bool isCylinder, Vector3 offsetPos = default(Vector3))
 496        {
 24497            if (openingAngle > 0 && openingAngle < 180)
 498            {
 0499                radiusTop = 0;
 0500                radiusBottom = length * Mathf.Tan(openingAngle * Mathf.Deg2Rad / 2);
 501            }
 502
 24503            string meshName = isCylinder
 504                ? "DCL Cylinder"
 505                : "DCL Cone" + numVertices + "v" + radiusTop + "t" + radiusBottom + "b" + length + "l" + length +
 506                  (outside ? "o" : "") + (inside ? "i" : "");
 507            //string meshPrefabPath = "Assets/Decentraland/Internal/" + meshName + ".asset";
 24508            Mesh mesh = null; //(Mesh)AssetDatabase.LoadAssetAtPath(meshPrefabPath, typeof(Mesh));
 24509            if (mesh == null)
 510            {
 24511                int numVertices2 = numVertices + 1;
 512
 24513                mesh = new Mesh();
 24514                mesh.name = meshName;
 515                // can't access Camera.current
 516                //newCone.transform.position = Camera.current.transform.position + Camera.current.transform.forward * 5.
 24517                int multiplier = (outside ? 1 : 0) + (inside ? 1 : 0);
 24518                int offset = (outside && inside ? 2 * numVertices2 : 0);
 519
 24520                bool bTopCap = isCylinder ? true : false;
 24521                bool bBottomCap = true;
 522
 24523                Vector3[] vertices =
 524                    new Vector3[
 525                        2 * multiplier * numVertices2 + (bTopCap ? (numVertices + 1) : 0) +
 526                        (bBottomCap ? (numVertices + 1) : 0)];
 527                // 0..n-1: top, n..2n-1: bottom
 24528                Vector3[] normals =
 529                    new Vector3[
 530                        2 * multiplier * numVertices2 + (bTopCap ? (numVertices + 1) : 0) +
 531                        (bBottomCap ? (numVertices + 1) : 0)];
 24532                Vector2[] uvs =
 533                    new Vector2[
 534                        2 * multiplier * numVertices2 + (bTopCap ? (numVertices + 1) : 0) +
 535                        (bBottomCap ? (numVertices + 1) : 0)];
 536                int[] tris;
 24537                float slope = Mathf.Atan((radiusBottom - radiusTop) / length); // (rad difference)/height
 24538                float slopeSin = Mathf.Sin(slope);
 24539                float slopeCos = Mathf.Cos(slope);
 540                int i;
 541
 2448542                for (i = 0; i < numVertices; i++)
 543                {
 1200544                    float angle = 2 * Mathf.PI * i / numVertices;
 1200545                    float angleSin = Mathf.Sin(angle);
 1200546                    float angleCos = Mathf.Cos(angle);
 1200547                    float angleHalf = 2 * Mathf.PI * (i + 0.5f) / numVertices; // for degenerated normals at cone tips
 1200548                    float angleHalfSin = Mathf.Sin(angleHalf);
 1200549                    float angleHalfCos = Mathf.Cos(angleHalf);
 550
 1200551                    vertices[i] = new Vector3(radiusTop * angleCos, length, radiusTop * angleSin) + offsetPos;
 1200552                    vertices[i + numVertices2] =
 553                        new Vector3(radiusBottom * angleCos, 0, radiusBottom * angleSin) + offsetPos;
 554
 1200555                    if (radiusTop == 0)
 556                    {
 300557                        normals[i] = new Vector3(angleHalfCos * slopeCos, -slopeSin, angleHalfSin * slopeCos);
 558                    }
 559                    else
 560                    {
 900561                        normals[i] = new Vector3(angleCos * slopeCos, -slopeSin, angleSin * slopeCos);
 562                    }
 563
 1200564                    if (radiusBottom == 0)
 565                    {
 300566                        normals[i + numVertices2] =
 567                            new Vector3(angleHalfCos * slopeCos, -slopeSin, angleHalfSin * slopeCos);
 568                    }
 569                    else
 570                    {
 900571                        normals[i + numVertices2] = new Vector3(angleCos * slopeCos, -slopeSin, angleSin * slopeCos);
 572                    }
 573
 1200574                    uvs[i] = new Vector2(1.0f - 1.0f * i / numVertices, 1);
 1200575                    uvs[i + numVertices2] = new Vector2(1.0f - 1.0f * i / numVertices, 0);
 576
 1200577                    if (outside && inside)
 578                    {
 579                        // vertices and uvs are identical on inside and outside, so just copy
 0580                        vertices[i + 2 * numVertices2] = vertices[i];
 0581                        vertices[i + 3 * numVertices2] = vertices[i + numVertices2];
 0582                        uvs[i + 2 * numVertices2] = uvs[i];
 0583                        uvs[i + 3 * numVertices2] = uvs[i + numVertices2];
 584                    }
 585
 1200586                    if (inside)
 587                    {
 588                        // invert normals
 0589                        normals[i + offset] = -normals[i];
 0590                        normals[i + numVertices2 + offset] = -normals[i + numVertices2];
 591                    }
 592                }
 593
 24594                vertices[numVertices] = vertices[0];
 24595                vertices[numVertices + numVertices2] = vertices[0 + numVertices2];
 24596                uvs[numVertices] = new Vector2(1.0f - 1.0f * numVertices / numVertices, 1);
 24597                uvs[numVertices + numVertices2] = new Vector2(1.0f - 1.0f * numVertices / numVertices, 0);
 24598                normals[numVertices] = normals[0];
 24599                normals[numVertices + numVertices2] = normals[0 + numVertices2];
 600
 601
 24602                int coverTopIndexStart = 2 * multiplier * numVertices2;
 24603                int coverTopIndexEnd = 2 * multiplier * numVertices2 + numVertices;
 604
 24605                if (bTopCap)
 606                {
 1224607                    for (i = 0; i < numVertices; i++)
 608                    {
 600609                        float angle = 2 * Mathf.PI * i / numVertices;
 600610                        float angleSin = Mathf.Sin(angle);
 600611                        float angleCos = Mathf.Cos(angle);
 612
 600613                        vertices[coverTopIndexStart + i] =
 614                            new Vector3(radiusTop * angleCos, length, radiusTop * angleSin) + offsetPos;
 600615                        normals[coverTopIndexStart + i] = new Vector3(0, 1, 0);
 600616                        uvs[coverTopIndexStart + i] = new Vector2(angleCos / 2 + 0.5f, angleSin / 2 + 0.5f);
 617                    }
 618
 12619                    vertices[coverTopIndexStart + numVertices] = new Vector3(0, length, 0) + offsetPos;
 12620                    normals[coverTopIndexStart + numVertices] = new Vector3(0, 1, 0);
 12621                    uvs[coverTopIndexStart + numVertices] = new Vector2(0.5f, 0.5f);
 622                }
 623
 624
 24625                int coverBottomIndexStart = coverTopIndexStart + (bTopCap ? 1 : 0) * (numVertices + 1);
 24626                int coverBottomIndexEnd = coverBottomIndexStart + numVertices;
 24627                if (bBottomCap)
 628                {
 2448629                    for (i = 0; i < numVertices; i++)
 630                    {
 1200631                        float angle = 2 * Mathf.PI * i / numVertices;
 1200632                        float angleSin = Mathf.Sin(angle);
 1200633                        float angleCos = Mathf.Cos(angle);
 634
 1200635                        vertices[coverBottomIndexStart + i] =
 636                            new Vector3(radiusBottom * angleCos, 0f, radiusBottom * angleSin) +
 637                            offsetPos;
 1200638                        normals[coverBottomIndexStart + i] = new Vector3(0, -1, 0);
 1200639                        uvs[coverBottomIndexStart + i] = new Vector2(angleCos / 2 + 0.5f, angleSin / 2 + 0.5f);
 640                    }
 641
 24642                    vertices[coverBottomIndexStart + numVertices] = new Vector3(0, 0f, 0) + offsetPos;
 24643                    normals[coverBottomIndexStart + numVertices] = new Vector3(0, -1, 0);
 24644                    uvs[coverBottomIndexStart + numVertices] = new Vector2(0.5f, 0.5f);
 645                }
 646
 24647                mesh.vertices = vertices;
 24648                mesh.normals = normals;
 24649                mesh.uv = uvs;
 650
 651                // create triangles
 652                // here we need to take care of point order, depending on inside and outside
 24653                int cnt = 0;
 24654                if (radiusTop == 0)
 655                {
 656                    // top cone
 6657                    tris =
 658                        new int[numVertices2 * 3 * multiplier + ((bTopCap ? 1 : 0) * numVertices * 3) +
 659                                ((bBottomCap ? 1 : 0) * numVertices * 3)
 660                        ];
 6661                    if (outside)
 662                    {
 612663                        for (i = 0; i < numVertices; i++)
 664                        {
 300665                            tris[cnt++] = i + numVertices2;
 300666                            tris[cnt++] = i;
 300667                            tris[cnt++] = i + 1 + numVertices2;
 668                            //              if(i==numVertices-1)
 669                            //                tris[cnt++]=numVertices;
 670                            //              else
 671                            //                tris[cnt++]=i+1+numVertices;
 672                        }
 673                    }
 674
 6675                    if (inside)
 676                    {
 0677                        for (i = offset; i < numVertices + offset; i++)
 678                        {
 0679                            tris[cnt++] = i;
 0680                            tris[cnt++] = i + numVertices2;
 0681                            tris[cnt++] = i + 1 + numVertices2;
 682                            //              if(i==numVertices-1+offset)
 683                            //                tris[cnt++]=numVertices+offset;
 684                            //              else
 685                            //                tris[cnt++]=i+1+numVertices;
 686                        }
 687                    }
 688                }
 18689                else if (radiusBottom == 0)
 690                {
 691                    // bottom cone
 6692                    tris =
 693                        new int[numVertices2 * 3 * multiplier + ((bTopCap ? 1 : 0) * numVertices * 3) +
 694                                ((bBottomCap ? 1 : 0) * numVertices * 3)
 695                        ];
 6696                    if (outside)
 697                    {
 612698                        for (i = 0; i < numVertices; i++)
 699                        {
 300700                            tris[cnt++] = i;
 300701                            tris[cnt++] = i + 1;
 702                            //              if(i==numVertices-1)
 703                            //                tris[cnt++]=0;
 704                            //              else
 705                            //                tris[cnt++]=i+1;
 300706                            tris[cnt++] = i + numVertices2;
 707                        }
 708                    }
 709
 6710                    if (inside)
 711                    {
 0712                        for (i = offset; i < numVertices + offset; i++)
 713                        {
 714                            //              if(i==numVertices-1+offset)
 715                            //                tris[cnt++]=offset;
 716                            //              else
 717                            //                tris[cnt++]=i+1;
 0718                            tris[cnt++] = i + 1;
 0719                            tris[cnt++] = i;
 0720                            tris[cnt++] = i + numVertices2;
 721                        }
 722                    }
 723                }
 724                else
 725                {
 726                    // truncated cone
 12727                    tris =
 728                        new int[numVertices2 * 6 * multiplier + ((bTopCap ? 1 : 0) * numVertices * 3) +
 729                                ((bBottomCap ? 1 : 0) * numVertices * 3)
 730                        ];
 12731                    if (outside)
 732                    {
 1224733                        for (i = 0; i < numVertices; i++)
 734                        {
 600735                            int ip1 = i + 1;
 736                            //              if(ip1==numVertices)
 737                            //                ip1=0;
 738
 600739                            tris[cnt++] = i;
 600740                            tris[cnt++] = ip1;
 600741                            tris[cnt++] = i + numVertices2;
 742
 600743                            tris[cnt++] = ip1 + numVertices2;
 600744                            tris[cnt++] = i + numVertices2;
 600745                            tris[cnt++] = ip1;
 746                        }
 747                    }
 748
 12749                    if (inside)
 750                    {
 0751                        for (i = offset; i < numVertices + offset; i++)
 752                        {
 0753                            int ip1 = i + 1;
 754                            //              if(ip1==numVertices+offset)
 755                            //                ip1=offset;
 756
 0757                            tris[cnt++] = ip1;
 0758                            tris[cnt++] = i;
 0759                            tris[cnt++] = i + numVertices2;
 760
 0761                            tris[cnt++] = i + numVertices2;
 0762                            tris[cnt++] = ip1 + numVertices2;
 0763                            tris[cnt++] = ip1;
 764                        }
 765                    }
 766                }
 767
 24768                if (bTopCap)
 769                {
 1224770                    for (i = 0; i < numVertices; ++i)
 771                    {
 600772                        int next = coverTopIndexStart + i + 1;
 773
 600774                        if (next == coverTopIndexEnd)
 775                        {
 12776                            next = coverTopIndexStart;
 777                        }
 778
 600779                        tris[cnt++] = next;
 600780                        tris[cnt++] = coverTopIndexStart + i;
 600781                        tris[cnt++] = coverTopIndexEnd;
 782                    }
 783                }
 784
 24785                if (bBottomCap)
 786                {
 2448787                    for (i = 0; i < numVertices; ++i)
 788                    {
 1200789                        int next = coverBottomIndexStart + i + 1;
 1200790                        if (next == coverBottomIndexEnd)
 791                        {
 24792                            next = coverBottomIndexStart;
 793                        }
 794
 1200795                        tris[cnt++] = coverBottomIndexEnd;
 1200796                        tris[cnt++] = coverBottomIndexStart + i;
 1200797                        tris[cnt++] = next;
 798                    }
 799                }
 800
 24801                mesh.triangles = tris;
 802                //AssetDatabase.CreateAsset(mesh, meshPrefabPath);
 803                //AssetDatabase.SaveAssets();
 804            }
 805
 24806            return mesh;
 807        }
 808
 809        public static Mesh BuildCone(int numVertices, float radiusTop, float radiusBottom, float length,
 810            float openingAngle, bool outside, bool inside)
 811        {
 12812            return BuildConeOrCylinder(numVertices, radiusTop, radiusBottom, length, openingAngle, outside, inside,
 813                false,
 814                new Vector3(0f, -length / 2, 0f));
 815        }
 816
 817        public static Mesh BuildCylinder(int numVertices, float radiusTop, float radiusBottom, float length,
 818            float openingAngle, bool outside, bool inside)
 819        {
 12820            return BuildConeOrCylinder(numVertices, radiusTop, radiusBottom, length, openingAngle, outside, inside,
 821                true,
 822                new Vector3(0f, -length / 2, 0f));
 823        }
 824    }
 825}