Пример #1
0
        private Mesh CreateIcosphere(int recursionLevel)
        {
            Mesh mesh = new Icosahedron();

            if (recursionLevel == 0)
            {
                return(mesh);
            }

            //Add Verts to temp vert list
            foreach (var vert in mesh.Vertices)
            {
                AddNormalizedVertex(vert);
            }

            //Subdivide triangles
            for (var i = 0; i < recursionLevel; i++)
            {
                var faces2 = new List <ushort>();
                var tri    = new List <ushort>();

                for (var k = 0; k < mesh.Triangles.Length; k++)
                {
                    tri.Add(mesh.Triangles[k]);
                    if (tri.Count != 3)
                    {
                        continue;
                    }

                    //Replace triangle by four triangles.
                    var a = MiddlePoint(tri[0], tri[1]);
                    var b = MiddlePoint(tri[1], tri[2]);
                    var c = MiddlePoint(tri[2], tri[0]);

                    var temp = new List <ushort>
                    {
                        tri[0], a, c,
                        tri[1], b, a,
                        tri[2], c, b,
                        a, b, c
                    };

                    faces2.AddRange(temp);

                    tri = new List <ushort>();
                }
                mesh.Triangles = faces2.ToArray();
            }

            mesh.Vertices = _sphereVertices.ToArray();
            NormalAndUvHelper.CreateVertexNormals(mesh);

            mesh.UVs = new float2[mesh.Vertices.Length];

            for (var i = 0; i < mesh.Vertices.Length; i++)
            {
                mesh.UVs[i] = new float2(0.5f + ((float)System.Math.Atan2(mesh.Vertices[i].z, mesh.Vertices[i].x) / (2 * M.Pi)), 0.5f - ((float)System.Math.Asin(mesh.Vertices[i].y) / M.Pi));
            }

            return(mesh);
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Icosahedron" /> class.
        /// All Vertices of the Icosahedron are lying on the unit sphere.
        /// </summary>
        public Icosahedron()
        {
            //Create the 12 vertices of a icosahedron.
            var t = (float)(1.0 + System.Math.Sqrt(5.0)) / 2.0f;

            var verts = new List <float3>
            {
                new float3(-1, t, 0),
                new float3(1, t, 0),
                new float3(-1, -t, 0),
                new float3(1, -t, 0),

                new float3(0, -1, t),
                new float3(0, 1, t),
                new float3(0, -1, -t),
                new float3(0, 1, -t),

                new float3(t, 0, -1),
                new float3(t, 0, 1),
                new float3(-t, 0, -1),
                new float3(-t, 0, 1)
            };

            //Normalize to get the unit icosahedron.
            for (var i = 0; i < verts.Count; i++)
            {
                var vert = verts[i];
                vert     = vert.Normalize();
                verts[i] = vert;
            }

            Vertices = verts.ToArray();

            Triangles = new ushort[]
            {
                5, 11, 0,
                1, 5, 0,
                7, 1, 0,
                10, 7, 0,
                11, 10, 0,
                9, 5, 1,
                4, 11, 5,
                2, 10, 11,
                6, 7, 10,
                8, 1, 7,
                4, 9, 3,
                2, 4, 3,
                6, 2, 3,
                8, 6, 3,
                9, 8, 3,
                5, 9, 4,
                11, 4, 2,
                10, 2, 6,
                7, 6, 8,
                1, 8, 9
            };

            Normals = new float3[] { };
            NormalAndUvHelper.CreateVertexNormals(this);

            UVs = CreateUVs();
            for (var i = 0; i < Vertices.Length; i++)
            {
                UVs[i] =
                    new float2(0.5f + ((float)System.Math.Atan2(Vertices[i].z, Vertices[i].x) / (2 * M.Pi)),
                               0.5f - ((float)System.Math.Asin(Vertices[i].y) / M.Pi));
            }
        }