PersistentTrails.MeshFactory.createCone C# (CSharp) Method

createCone() public static method

public static createCone ( float radius, float height, int approx ) : Mesh
radius float
height float
approx int
return UnityEngine.Mesh
        public static Mesh createCone(float radius, float height, int approx)
        {
            Mesh cone = new Mesh();

            //create vertices
            int numVertices = approx + 1;
            Vector3[] vertices = new Vector3[numVertices];
            vertices[0] = new Vector3(0, 0, height);

            float angleIncrement = 2.0f * (float) Math.PI / approx;

            for(uint i=0; i<approx; ++i){
                float angle = angleIncrement * i; //angle from 0 to 2PI
                vertices[i+1] = new Vector3(    (float) Math.Cos(angle) * radius,
                                                (float) Math.Sin(angle) * radius,
                                                0);
            }

            //create triangles (three indices per face)
            int[] triangleVertexIndices = new int[approx * 3 + (approx-1) * 3];

            //mantle
            for(int i=0; i < approx; ++i){
                triangleVertexIndices[3 * i + 0] = 0; //first corner is always the top
                triangleVertexIndices[3 * i + 1] = i + 1;
                triangleVertexIndices[3 * i + 2] = (i + 2) % numVertices;
                //i=0 => 0, 1, 2
                //i=1 => 0, 2, 3
            }

            int indexOffset = 3 * approx;
            //base
            for(int i=0; i < approx - 1; ++i){
                triangleVertexIndices[indexOffset + 3 * i + 0] = 1; //first corner is always the top
                triangleVertexIndices[indexOffset + 3 * i + 2] = i + 2;
                triangleVertexIndices[indexOffset + 3 * i + 1] = (i + 3) % numVertices;
                //i=0 => 1, 2, 3
                //i=1 => 1, 3, 4
            }

            //texture coordinates

            //put it all together
            cone.vertices = vertices;
            cone.triangles = triangleVertexIndices;

            //Auto-Normals
            cone.RecalculateNormals();

            return cone;
        }

Usage Example

        private void initDefaultValues()
        {
            TrackName        = "unnamed track";
            Description      = "";
            waypoints        = new List <Waypoint>();
            logEntries       = new List <LogEntry>();
            directionMarkers = new List <GameObject>();

            SamplingFactor              = 1;
            LineColor                   = Color.green;
            LineWidth                   = 0.2f;
            NumDirectionMarkers         = 0;
            ConeRadiusToLineWidthFactor = 30;
            ReplayColliders             = false;

            //this.renderCoords = new List<Vector3>();

            //init mesh and directionmarkes
            directionMarkerMesh = MeshFactory.createCone(1, 2, 12);

            EndAction       = EndActions.STOP;
            LoopClosureTime = 0f;

            for (int i = 0; i < 20; ++i)
            {
                GameObject marker = MeshFactory.makeMeshGameObject(ref directionMarkerMesh, "cone");
                marker.GetComponent <Renderer>().material          = new Material(Shader.Find("KSP/Emissive/Diffuse"));
                marker.GetComponent <Renderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
                marker.GetComponent <Renderer>().receiveShadows    = false;
                marker.GetComponent <Renderer>().enabled           = false;

                directionMarkers.Add(marker);
            }
        }