< 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:396
Uncovered lines:26
Coverable lines:422
Total lines:754
Line coverage:93.8% (396 of 422)
Covered branches:0
Total branches:0

Metrics

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

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/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        {
 41132            Mesh mesh = new Mesh();
 41133            mesh.name = "DCL Plane";
 41134            Vector3[] vertices = new Vector3[8];
 41135            Vector3[] normals = new Vector3[8];
 41136            Vector2[] uvs = new Vector2[8];
 41137            Color[] colors = new Color[8];
 138
 41139            int[] tris = new int[4 * 3];
 140
 41141            int vIndex = 0;
 41142            Vector3 start = new Vector3(-_size / 2, _size / 2, 0);
 41143            vertices[vIndex++] = new Vector3(-start.x, -start.y, 0);
 41144            vertices[vIndex++] = new Vector3(start.x, -start.y, 0);
 41145            vertices[vIndex++] = new Vector3(start.x, start.y, 0);
 41146            vertices[vIndex++] = new Vector3(-start.x, start.y, 0);
 147
 41148            vertices[vIndex++] = new Vector3(-start.x, -start.y, 0);
 41149            vertices[vIndex++] = new Vector3(start.x, -start.y, 0);
 41150            vertices[vIndex++] = new Vector3(start.x, start.y, 0);
 41151            vertices[vIndex++] = new Vector3(-start.x, start.y, 0);
 152
 41153            vIndex = 0;
 41154            uvs[vIndex++] = new Vector2(0f, 1f);
 41155            uvs[vIndex++] = new Vector2(1f, 1f);
 41156            uvs[vIndex++] = new Vector2(1f, 0f);
 41157            uvs[vIndex++] = new Vector2(0f, 0f);
 158
 41159            uvs[vIndex++] = new Vector2(0f, 1f);
 41160            uvs[vIndex++] = new Vector2(1f, 1f);
 41161            uvs[vIndex++] = new Vector2(1f, 0f);
 41162            uvs[vIndex++] = new Vector2(0f, 0f);
 163
 41164            vIndex = 0;
 41165            normals[vIndex++] = Vector3.forward;
 41166            normals[vIndex++] = Vector3.forward;
 41167            normals[vIndex++] = Vector3.forward;
 41168            normals[vIndex++] = Vector3.forward;
 169
 41170            normals[vIndex++] = Vector3.back;
 41171            normals[vIndex++] = Vector3.back;
 41172            normals[vIndex++] = Vector3.back;
 41173            normals[vIndex++] = Vector3.back;
 174
 41175            vIndex = 0;
 41176            colors[vIndex++] = Color.white;
 41177            colors[vIndex++] = Color.white;
 41178            colors[vIndex++] = Color.white;
 41179            colors[vIndex++] = Color.white;
 180
 41181            colors[vIndex++] = Color.white;
 41182            colors[vIndex++] = Color.white;
 41183            colors[vIndex++] = Color.white;
 41184            colors[vIndex++] = Color.white;
 185
 41186            int cnt = 0;
 41187            tris[cnt++] = 2;
 41188            tris[cnt++] = 1;
 41189            tris[cnt++] = 0;
 41190            tris[cnt++] = 3;
 41191            tris[cnt++] = 2;
 41192            tris[cnt++] = 0;
 193
 41194            tris[cnt++] = 4 + 1;
 41195            tris[cnt++] = 4 + 2;
 41196            tris[cnt++] = 4 + 0;
 41197            tris[cnt++] = 4 + 2;
 41198            tris[cnt++] = 4 + 3;
 41199            tris[cnt++] = 4 + 0;
 200
 41201            mesh.vertices = vertices;
 41202            mesh.normals = normals;
 41203            mesh.uv = uvs;
 41204            mesh.colors = colors;
 205
 41206            mesh.triangles = tris;
 41207            return mesh;
 208        }
 209
 210        public static Mesh BuildCube(float _size)
 211        {
 26212            Mesh mesh = new Mesh();
 26213            mesh.name = "DCL Box";
 26214            Vector3[] vertices = new Vector3[24]; //top bottom left right front back
 26215            Vector3[] normals = new Vector3[24];
 26216            Vector2[] uvs = new Vector2[24];
 26217            Vector2[] uvs2 = new Vector2[24];
 26218            int[] tris = new int[12 * 3];
 219
 26220            int vIndex = 0;
 221            //top and bottom
 26222            Vector3 start = new Vector3(-_size / 2, _size / 2, _size / 2);
 26223            vertices[vIndex++] = start;
 26224            vertices[vIndex++] = start + Vector3.right * _size;
 26225            vertices[vIndex++] = start + Vector3.right * _size + Vector3.back * _size;
 26226            vertices[vIndex++] = start + Vector3.back * _size;
 227
 26228            start = new Vector3(-_size / 2, -_size / 2, _size / 2);
 26229            vertices[vIndex++] = start;
 26230            vertices[vIndex++] = start + Vector3.right * _size;
 26231            vertices[vIndex++] = start + Vector3.right * _size + Vector3.back * _size;
 26232            vertices[vIndex++] = start + Vector3.back * _size;
 233
 234            //left and right
 26235            start = new Vector3(-_size / 2, _size / 2, _size / 2);
 26236            vertices[vIndex++] = start;
 26237            vertices[vIndex++] = start + Vector3.back * _size;
 26238            vertices[vIndex++] = start + Vector3.back * _size + Vector3.down * _size;
 26239            vertices[vIndex++] = start + Vector3.down * _size;
 240
 26241            start = new Vector3(_size / 2, _size / 2, _size / 2);
 26242            vertices[vIndex++] = start;
 26243            vertices[vIndex++] = start + Vector3.back * _size;
 26244            vertices[vIndex++] = start + Vector3.back * _size + Vector3.down * _size;
 26245            vertices[vIndex++] = start + Vector3.down * _size;
 246
 247            //front and back
 26248            start = new Vector3(-_size / 2, _size / 2, _size / 2);
 26249            vertices[vIndex++] = start;
 26250            vertices[vIndex++] = start + Vector3.right * _size;
 26251            vertices[vIndex++] = start + Vector3.right * _size + Vector3.down * _size;
 26252            vertices[vIndex++] = start + Vector3.down * _size;
 253
 26254            start = new Vector3(-_size / 2, _size / 2, -_size / 2);
 26255            vertices[vIndex++] = start;
 26256            vertices[vIndex++] = start + Vector3.right * _size;
 26257            vertices[vIndex++] = start + Vector3.right * _size + Vector3.down * _size;
 26258            vertices[vIndex++] = start + Vector3.down * _size;
 259
 260            //uv
 26261            vIndex = 0;
 262            //top and bottom
 26263            uvs[vIndex++] = new Vector2(1f, 1f);
 26264            uvs[vIndex++] = new Vector2(1f, 0f);
 26265            uvs[vIndex++] = new Vector2(0f, 0f);
 26266            uvs[vIndex++] = new Vector2(0f, 1f);
 267
 26268            uvs[vIndex++] = new Vector2(1f, 0f);
 26269            uvs[vIndex++] = new Vector2(1f, 1f);
 26270            uvs[vIndex++] = new Vector2(0f, 1f);
 26271            uvs[vIndex++] = new Vector2(0f, 0f);
 272
 273            //left and right
 26274            uvs[vIndex++] = new Vector2(1f, 1f);
 26275            uvs[vIndex++] = new Vector2(1f, 0f);
 26276            uvs[vIndex++] = new Vector2(0f, 0f);
 26277            uvs[vIndex++] = new Vector2(0f, 1f);
 278
 26279            uvs[vIndex++] = new Vector2(1f, 0f);
 26280            uvs[vIndex++] = new Vector2(1f, 1f);
 26281            uvs[vIndex++] = new Vector2(0f, 1f);
 26282            uvs[vIndex++] = new Vector2(0f, 0f);
 283
 284            //front and back
 26285            uvs[vIndex++] = new Vector2(0f, 0f);
 26286            uvs[vIndex++] = new Vector2(1f, 0f);
 26287            uvs[vIndex++] = new Vector2(1f, 1f);
 26288            uvs[vIndex++] = new Vector2(0f, 1f);
 289
 26290            uvs[vIndex++] = new Vector2(0f, 1f);
 26291            uvs[vIndex++] = new Vector2(1f, 1f);
 26292            uvs[vIndex++] = new Vector2(1f, 0f);
 26293            uvs[vIndex++] = new Vector2(0f, 0f);
 294
 295            //uv2
 26296            vIndex = 0;
 297            //top and bottom
 26298            uvs2[vIndex++] = new Vector2(1f, 1f);
 26299            uvs2[vIndex++] = new Vector2(1f, 0f);
 26300            uvs2[vIndex++] = new Vector2(0f, 0f);
 26301            uvs2[vIndex++] = new Vector2(0f, 1f);
 302
 26303            uvs2[vIndex++] = new Vector2(1f, 0f);
 26304            uvs2[vIndex++] = new Vector2(1f, 1f);
 26305            uvs2[vIndex++] = new Vector2(0f, 1f);
 26306            uvs2[vIndex++] = new Vector2(0f, 0f);
 307
 308            //left and right
 26309            uvs2[vIndex++] = new Vector2(1f, 1f);
 26310            uvs2[vIndex++] = new Vector2(1f, 0f);
 26311            uvs2[vIndex++] = new Vector2(0f, 0f);
 26312            uvs2[vIndex++] = new Vector2(0f, 1f);
 313
 26314            uvs2[vIndex++] = new Vector2(1f, 0f);
 26315            uvs2[vIndex++] = new Vector2(1f, 1f);
 26316            uvs2[vIndex++] = new Vector2(0f, 1f);
 26317            uvs2[vIndex++] = new Vector2(0f, 0f);
 318
 319            //front and back
 26320            uvs2[vIndex++] = new Vector2(0f, 0f);
 26321            uvs2[vIndex++] = new Vector2(1f, 0f);
 26322            uvs2[vIndex++] = new Vector2(1f, 1f);
 26323            uvs2[vIndex++] = new Vector2(0f, 1f);
 324
 26325            uvs2[vIndex++] = new Vector2(0f, 1f);
 26326            uvs2[vIndex++] = new Vector2(1f, 1f);
 26327            uvs2[vIndex++] = new Vector2(1f, 0f);
 26328            uvs2[vIndex++] = new Vector2(0f, 0f);
 329
 330            //normal
 26331            vIndex = 0;
 332            //top and bottom
 26333            normals[vIndex++] = Vector3.up;
 26334            normals[vIndex++] = Vector3.up;
 26335            normals[vIndex++] = Vector3.up;
 26336            normals[vIndex++] = Vector3.up;
 337
 26338            normals[vIndex++] = Vector3.down;
 26339            normals[vIndex++] = Vector3.down;
 26340            normals[vIndex++] = Vector3.down;
 26341            normals[vIndex++] = Vector3.down;
 342
 343            //left and right
 26344            normals[vIndex++] = Vector3.left;
 26345            normals[vIndex++] = Vector3.left;
 26346            normals[vIndex++] = Vector3.left;
 26347            normals[vIndex++] = Vector3.left;
 348
 26349            normals[vIndex++] = Vector3.right;
 26350            normals[vIndex++] = Vector3.right;
 26351            normals[vIndex++] = Vector3.right;
 26352            normals[vIndex++] = Vector3.right;
 353
 354            //front and back
 26355            normals[vIndex++] = Vector3.forward;
 26356            normals[vIndex++] = Vector3.forward;
 26357            normals[vIndex++] = Vector3.forward;
 26358            normals[vIndex++] = Vector3.forward;
 359
 26360            normals[vIndex++] = Vector3.back;
 26361            normals[vIndex++] = Vector3.back;
 26362            normals[vIndex++] = Vector3.back;
 26363            normals[vIndex++] = Vector3.back;
 364
 365
 26366            int cnt = 0;
 367
 368            //top and bottom
 26369            tris[cnt++] = 0;
 26370            tris[cnt++] = 1;
 26371            tris[cnt++] = 2;
 26372            tris[cnt++] = 0;
 26373            tris[cnt++] = 2;
 26374            tris[cnt++] = 3;
 375
 26376            tris[cnt++] = 4 + 0;
 26377            tris[cnt++] = 4 + 2;
 26378            tris[cnt++] = 4 + 1;
 26379            tris[cnt++] = 4 + 0;
 26380            tris[cnt++] = 4 + 3;
 26381            tris[cnt++] = 4 + 2;
 382
 383            //left and right
 26384            tris[cnt++] = 8 + 0;
 26385            tris[cnt++] = 8 + 1;
 26386            tris[cnt++] = 8 + 2;
 26387            tris[cnt++] = 8 + 0;
 26388            tris[cnt++] = 8 + 2;
 26389            tris[cnt++] = 8 + 3;
 390
 26391            tris[cnt++] = 12 + 0;
 26392            tris[cnt++] = 12 + 2;
 26393            tris[cnt++] = 12 + 1;
 26394            tris[cnt++] = 12 + 0;
 26395            tris[cnt++] = 12 + 3;
 26396            tris[cnt++] = 12 + 2;
 397
 398            //front and back
 26399            tris[cnt++] = 16 + 0;
 26400            tris[cnt++] = 16 + 2;
 26401            tris[cnt++] = 16 + 1;
 26402            tris[cnt++] = 16 + 0;
 26403            tris[cnt++] = 16 + 3;
 26404            tris[cnt++] = 16 + 2;
 405
 26406            tris[cnt++] = 20 + 0;
 26407            tris[cnt++] = 20 + 1;
 26408            tris[cnt++] = 20 + 2;
 26409            tris[cnt++] = 20 + 0;
 26410            tris[cnt++] = 20 + 2;
 26411            tris[cnt++] = 20 + 3;
 412
 26413            mesh.vertices = vertices;
 26414            mesh.normals = normals;
 26415            mesh.uv = uvs;
 26416            mesh.uv2 = uvs2;
 417
 26418            mesh.triangles = tris;
 419
 26420            return mesh;
 421        }
 422
 423        public static Mesh BuildConeOrCylinder(int numVertices, float radiusTop, float radiusBottom, float length,
 424            float openingAngle, bool outside, bool inside, bool isCylinder, Vector3 offsetPos = default(Vector3))
 425        {
 25426            if (openingAngle > 0 && openingAngle < 180)
 427            {
 0428                radiusTop = 0;
 0429                radiusBottom = length * Mathf.Tan(openingAngle * Mathf.Deg2Rad / 2);
 430            }
 431
 25432            string meshName = isCylinder
 433                ? "DCL Cylinder"
 434                : "DCL Cone" + numVertices + "v" + radiusTop + "t" + radiusBottom + "b" + length + "l" + length +
 435                  (outside ? "o" : "") + (inside ? "i" : "");
 436            //string meshPrefabPath = "Assets/Decentraland/Internal/" + meshName + ".asset";
 25437            Mesh mesh = null; //(Mesh)AssetDatabase.LoadAssetAtPath(meshPrefabPath, typeof(Mesh));
 25438            if (mesh == null)
 439            {
 25440                int numVertices2 = numVertices + 1;
 441
 25442                mesh = new Mesh();
 25443                mesh.name = meshName;
 444                // can't access Camera.current
 445                //newCone.transform.position = Camera.current.transform.position + Camera.current.transform.forward * 5.
 25446                int multiplier = (outside ? 1 : 0) + (inside ? 1 : 0);
 25447                int offset = (outside && inside ? 2 * numVertices2 : 0);
 448
 25449                bool bTopCap = isCylinder ? true : false;
 25450                bool bBottomCap = true;
 451
 25452                Vector3[] vertices =
 453                    new Vector3[
 454                        2 * multiplier * numVertices2 + (bTopCap ? (numVertices + 1) : 0) +
 455                        (bBottomCap ? (numVertices + 1) : 0)];
 456                // 0..n-1: top, n..2n-1: bottom
 25457                Vector3[] normals =
 458                    new Vector3[
 459                        2 * multiplier * numVertices2 + (bTopCap ? (numVertices + 1) : 0) +
 460                        (bBottomCap ? (numVertices + 1) : 0)];
 25461                Vector2[] uvs =
 462                    new Vector2[
 463                        2 * multiplier * numVertices2 + (bTopCap ? (numVertices + 1) : 0) +
 464                        (bBottomCap ? (numVertices + 1) : 0)];
 465                int[] tris;
 25466                float slope = Mathf.Atan((radiusBottom - radiusTop) / length); // (rad difference)/height
 25467                float slopeSin = Mathf.Sin(slope);
 25468                float slopeCos = Mathf.Cos(slope);
 469                int i;
 470
 2550471                for (i = 0; i < numVertices; i++)
 472                {
 1250473                    float angle = 2 * Mathf.PI * i / numVertices;
 1250474                    float angleSin = Mathf.Sin(angle);
 1250475                    float angleCos = Mathf.Cos(angle);
 1250476                    float angleHalf = 2 * Mathf.PI * (i + 0.5f) / numVertices; // for degenerated normals at cone tips
 1250477                    float angleHalfSin = Mathf.Sin(angleHalf);
 1250478                    float angleHalfCos = Mathf.Cos(angleHalf);
 479
 1250480                    vertices[i] = new Vector3(radiusTop * angleCos, length, radiusTop * angleSin) + offsetPos;
 1250481                    vertices[i + numVertices2] =
 482                        new Vector3(radiusBottom * angleCos, 0, radiusBottom * angleSin) + offsetPos;
 483
 1250484                    if (radiusTop == 0)
 485                    {
 600486                        normals[i] = new Vector3(angleHalfCos * slopeCos, -slopeSin, angleHalfSin * slopeCos);
 600487                    }
 488                    else
 489                    {
 650490                        normals[i] = new Vector3(angleCos * slopeCos, -slopeSin, angleSin * slopeCos);
 491                    }
 492
 1250493                    if (radiusBottom == 0)
 494                    {
 550495                        normals[i + numVertices2] =
 496                            new Vector3(angleHalfCos * slopeCos, -slopeSin, angleHalfSin * slopeCos);
 550497                    }
 498                    else
 499                    {
 700500                        normals[i + numVertices2] = new Vector3(angleCos * slopeCos, -slopeSin, angleSin * slopeCos);
 501                    }
 502
 1250503                    uvs[i] = new Vector2(1.0f - 1.0f * i / numVertices, 1);
 1250504                    uvs[i + numVertices2] = new Vector2(1.0f - 1.0f * i / numVertices, 0);
 505
 1250506                    if (outside && inside)
 507                    {
 508                        // vertices and uvs are identical on inside and outside, so just copy
 0509                        vertices[i + 2 * numVertices2] = vertices[i];
 0510                        vertices[i + 3 * numVertices2] = vertices[i + numVertices2];
 0511                        uvs[i + 2 * numVertices2] = uvs[i];
 0512                        uvs[i + 3 * numVertices2] = uvs[i + numVertices2];
 513                    }
 514
 1250515                    if (inside)
 516                    {
 517                        // invert normals
 0518                        normals[i + offset] = -normals[i];
 0519                        normals[i + numVertices2 + offset] = -normals[i + numVertices2];
 520                    }
 521                }
 522
 25523                vertices[numVertices] = vertices[0];
 25524                vertices[numVertices + numVertices2] = vertices[0 + numVertices2];
 25525                uvs[numVertices] = new Vector2(1.0f - 1.0f * numVertices / numVertices, 1);
 25526                uvs[numVertices + numVertices2] = new Vector2(1.0f - 1.0f * numVertices / numVertices, 0);
 25527                normals[numVertices] = normals[0];
 25528                normals[numVertices + numVertices2] = normals[0 + numVertices2];
 529
 530
 25531                int coverTopIndexStart = 2 * multiplier * numVertices2;
 25532                int coverTopIndexEnd = 2 * multiplier * numVertices2 + numVertices;
 533
 25534                if (bTopCap)
 535                {
 1224536                    for (i = 0; i < numVertices; i++)
 537                    {
 600538                        float angle = 2 * Mathf.PI * i / numVertices;
 600539                        float angleSin = Mathf.Sin(angle);
 600540                        float angleCos = Mathf.Cos(angle);
 541
 600542                        vertices[coverTopIndexStart + i] =
 543                            new Vector3(radiusTop * angleCos, length, radiusTop * angleSin) + offsetPos;
 600544                        normals[coverTopIndexStart + i] = new Vector3(0, 1, 0);
 600545                        uvs[coverTopIndexStart + i] = new Vector2(angleCos / 2 + 0.5f, angleSin / 2 + 0.5f);
 546                    }
 547
 12548                    vertices[coverTopIndexStart + numVertices] = new Vector3(0, length, 0) + offsetPos;
 12549                    normals[coverTopIndexStart + numVertices] = new Vector3(0, 1, 0);
 12550                    uvs[coverTopIndexStart + numVertices] = new Vector2(0.5f, 0.5f);
 551                }
 552
 553
 25554                int coverBottomIndexStart = coverTopIndexStart + (bTopCap ? 1 : 0) * (numVertices + 1);
 25555                int coverBottomIndexEnd = coverBottomIndexStart + numVertices;
 25556                if (bBottomCap)
 557                {
 2550558                    for (i = 0; i < numVertices; i++)
 559                    {
 1250560                        float angle = 2 * Mathf.PI * i / numVertices;
 1250561                        float angleSin = Mathf.Sin(angle);
 1250562                        float angleCos = Mathf.Cos(angle);
 563
 1250564                        vertices[coverBottomIndexStart + i] =
 565                            new Vector3(radiusBottom * angleCos, 0f, radiusBottom * angleSin) +
 566                            offsetPos;
 1250567                        normals[coverBottomIndexStart + i] = new Vector3(0, -1, 0);
 1250568                        uvs[coverBottomIndexStart + i] = new Vector2(angleCos / 2 + 0.5f, angleSin / 2 + 0.5f);
 569                    }
 570
 25571                    vertices[coverBottomIndexStart + numVertices] = new Vector3(0, 0f, 0) + offsetPos;
 25572                    normals[coverBottomIndexStart + numVertices] = new Vector3(0, -1, 0);
 25573                    uvs[coverBottomIndexStart + numVertices] = new Vector2(0.5f, 0.5f);
 574                }
 575
 25576                mesh.vertices = vertices;
 25577                mesh.normals = normals;
 25578                mesh.uv = uvs;
 579
 580                // create triangles
 581                // here we need to take care of point order, depending on inside and outside
 25582                int cnt = 0;
 25583                if (radiusTop == 0)
 584                {
 585                    // top cone
 12586                    tris =
 587                        new int[numVertices2 * 3 * multiplier + ((bTopCap ? 1 : 0) * numVertices * 3) +
 588                                ((bBottomCap ? 1 : 0) * numVertices * 3)
 589                        ];
 12590                    if (outside)
 591                    {
 1224592                        for (i = 0; i < numVertices; i++)
 593                        {
 600594                            tris[cnt++] = i + numVertices2;
 600595                            tris[cnt++] = i;
 600596                            tris[cnt++] = i + 1 + numVertices2;
 597                            //              if(i==numVertices-1)
 598                            //                tris[cnt++]=numVertices;
 599                            //              else
 600                            //                tris[cnt++]=i+1+numVertices;
 601                        }
 602                    }
 603
 12604                    if (inside)
 605                    {
 0606                        for (i = offset; i < numVertices + offset; i++)
 607                        {
 0608                            tris[cnt++] = i;
 0609                            tris[cnt++] = i + numVertices2;
 0610                            tris[cnt++] = i + 1 + numVertices2;
 611                            //              if(i==numVertices-1+offset)
 612                            //                tris[cnt++]=numVertices+offset;
 613                            //              else
 614                            //                tris[cnt++]=i+1+numVertices;
 615                        }
 616                    }
 0617                }
 13618                else if (radiusBottom == 0)
 619                {
 620                    // bottom cone
 6621                    tris =
 622                        new int[numVertices2 * 3 * multiplier + ((bTopCap ? 1 : 0) * numVertices * 3) +
 623                                ((bBottomCap ? 1 : 0) * numVertices * 3)
 624                        ];
 6625                    if (outside)
 626                    {
 612627                        for (i = 0; i < numVertices; i++)
 628                        {
 300629                            tris[cnt++] = i;
 300630                            tris[cnt++] = i + 1;
 631                            //              if(i==numVertices-1)
 632                            //                tris[cnt++]=0;
 633                            //              else
 634                            //                tris[cnt++]=i+1;
 300635                            tris[cnt++] = i + numVertices2;
 636                        }
 637                    }
 638
 6639                    if (inside)
 640                    {
 0641                        for (i = offset; i < numVertices + offset; i++)
 642                        {
 643                            //              if(i==numVertices-1+offset)
 644                            //                tris[cnt++]=offset;
 645                            //              else
 646                            //                tris[cnt++]=i+1;
 0647                            tris[cnt++] = i + 1;
 0648                            tris[cnt++] = i;
 0649                            tris[cnt++] = i + numVertices2;
 650                        }
 651                    }
 0652                }
 653                else
 654                {
 655                    // truncated cone
 7656                    tris =
 657                        new int[numVertices2 * 6 * multiplier + ((bTopCap ? 1 : 0) * numVertices * 3) +
 658                                ((bBottomCap ? 1 : 0) * numVertices * 3)
 659                        ];
 7660                    if (outside)
 661                    {
 714662                        for (i = 0; i < numVertices; i++)
 663                        {
 350664                            int ip1 = i + 1;
 665                            //              if(ip1==numVertices)
 666                            //                ip1=0;
 667
 350668                            tris[cnt++] = i;
 350669                            tris[cnt++] = ip1;
 350670                            tris[cnt++] = i + numVertices2;
 671
 350672                            tris[cnt++] = ip1 + numVertices2;
 350673                            tris[cnt++] = i + numVertices2;
 350674                            tris[cnt++] = ip1;
 675                        }
 676                    }
 677
 7678                    if (inside)
 679                    {
 0680                        for (i = offset; i < numVertices + offset; i++)
 681                        {
 0682                            int ip1 = i + 1;
 683                            //              if(ip1==numVertices+offset)
 684                            //                ip1=offset;
 685
 0686                            tris[cnt++] = ip1;
 0687                            tris[cnt++] = i;
 0688                            tris[cnt++] = i + numVertices2;
 689
 0690                            tris[cnt++] = i + numVertices2;
 0691                            tris[cnt++] = ip1 + numVertices2;
 0692                            tris[cnt++] = ip1;
 693                        }
 694                    }
 695                }
 696
 25697                if (bTopCap)
 698                {
 1224699                    for (i = 0; i < numVertices; ++i)
 700                    {
 600701                        int next = coverTopIndexStart + i + 1;
 702
 600703                        if (next == coverTopIndexEnd)
 704                        {
 12705                            next = coverTopIndexStart;
 706                        }
 707
 600708                        tris[cnt++] = next;
 600709                        tris[cnt++] = coverTopIndexStart + i;
 600710                        tris[cnt++] = coverTopIndexEnd;
 711                    }
 712                }
 713
 25714                if (bBottomCap)
 715                {
 2550716                    for (i = 0; i < numVertices; ++i)
 717                    {
 1250718                        int next = coverBottomIndexStart + i + 1;
 1250719                        if (next == coverBottomIndexEnd)
 720                        {
 25721                            next = coverBottomIndexStart;
 722                        }
 723
 1250724                        tris[cnt++] = coverBottomIndexEnd;
 1250725                        tris[cnt++] = coverBottomIndexStart + i;
 1250726                        tris[cnt++] = next;
 727                    }
 728                }
 729
 25730                mesh.triangles = tris;
 731                //AssetDatabase.CreateAsset(mesh, meshPrefabPath);
 732                //AssetDatabase.SaveAssets();
 733            }
 734
 25735            return mesh;
 736        }
 737
 738        public static Mesh BuildCone(int numVertices, float radiusTop, float radiusBottom, float length,
 739            float openingAngle, bool outside, bool inside)
 740        {
 13741            return BuildConeOrCylinder(numVertices, radiusTop, radiusBottom, length, openingAngle, outside, inside,
 742                false,
 743                new Vector3(0f, -length / 2, 0f));
 744        }
 745
 746        public static Mesh BuildCylinder(int numVertices, float radiusTop, float radiusBottom, float length,
 747            float openingAngle, bool outside, bool inside)
 748        {
 12749            return BuildConeOrCylinder(numVertices, radiusTop, radiusBottom, length, openingAngle, outside, inside,
 750                true,
 751                new Vector3(0f, -length / 2, 0f));
 752        }
 753    }
 754}