Пример #1
0
        public static Mesh3D CreateSphere(float radius, int segmentsW, int segmentsH, Color4 color)
        {
            float tdelta = 360f / segmentsW;
            float pdelta = 180f / segmentsH;

            ++segmentsW;
            ++segmentsH;
            float   phi = -90f;
            Vector3 v;

            var mesh = new Mesh3D(24, 36);

            mesh.SetVertexCount(segmentsW * segmentsH);
            mesh.SetIndexCount((segmentsW - 1) * (segmentsH - 1) * 6);

            int n = 0;

            for (int i = 0; i < segmentsH; i++)
            {
                float theta = 0f;
                for (int j = 0; j < segmentsW; j++)
                {
                    v.X = radius * Calc.Cos(phi * Calc.PI / 180f) * Calc.Cos(theta * Calc.PI / 180f);
                    v.Y = radius * Calc.Sin(phi * Calc.PI / 180f);
                    v.Z = radius * Calc.Cos(phi * Calc.PI / 180f) * Calc.Sin(theta * Calc.PI / 180f);
                    mesh.vertices[n].Pos = v;
                    mesh.vertices[n].Nor = v.Normalized;
                    mesh.vertices[n].Tex = new Vector2(theta / 360f, (phi + 90f) / 180f);
                    mesh.vertices[n].Col = color;
                    theta += tdelta;
                    ++n;
                }
                phi += pdelta;
            }

            n = 0;
            for (int i = 0; i < segmentsH - 1; i++)
            {
                for (int j = 0; j < segmentsW - 1; j++)
                {
                    mesh.SetTriangle(n++, (i + 1) * segmentsW + j, (i + 1) * segmentsW + j + 1, i * segmentsW + j + 1);
                    mesh.SetTriangle(n++, i * segmentsW + j + 1, i * segmentsW + j, (i + 1) * segmentsW + j);
                }
            }

            mesh.CalculateNormals();
            mesh.Update();
            return(mesh);
        }
Пример #2
0
        public static Mesh3D CreateBox(Vector3 size, Color4 color)
        {
            var mesh = new Mesh3D(24, 36);

            mesh.SetVertexCount(24);
            mesh.SetIndexCount(36);
            size *= 0.5f;

            mesh.SetVertex(0, new Vertex3D(new Vector3(1, 1, 1) * size, Vector3.Forward, new Vector2(0, 0), color));
            mesh.SetVertex(1, new Vertex3D(new Vector3(-1, 1, 1) * size, Vector3.Forward, new Vector2(1, 0), color));
            mesh.SetVertex(2, new Vertex3D(new Vector3(-1, -1, 1) * size, Vector3.Forward, new Vector2(1, 1), color));
            mesh.SetVertex(3, new Vertex3D(new Vector3(1, -1, 1) * size, Vector3.Forward, new Vector2(0, 1), color));

            mesh.SetVertex(4, new Vertex3D(new Vector3(-1, 1, -1) * size, Vector3.Back, new Vector2(0, 0), color));
            mesh.SetVertex(5, new Vertex3D(new Vector3(1, 1, -1) * size, Vector3.Back, new Vector2(1, 0), color));
            mesh.SetVertex(6, new Vertex3D(new Vector3(1, -1, -1) * size, Vector3.Back, new Vector2(1, 1), color));
            mesh.SetVertex(7, new Vertex3D(new Vector3(-1, -1, -1) * size, Vector3.Back, new Vector2(0, 1), color));

            mesh.SetVertex(8, new Vertex3D(new Vector3(-1, 1, 1) * size, Vector3.Left, new Vector2(0, 0), color));
            mesh.SetVertex(9, new Vertex3D(new Vector3(-1, 1, -1) * size, Vector3.Left, new Vector2(1, 0), color));
            mesh.SetVertex(10, new Vertex3D(new Vector3(-1, -1, -1) * size, Vector3.Left, new Vector2(1, 1), color));
            mesh.SetVertex(11, new Vertex3D(new Vector3(-1, -1, 1) * size, Vector3.Left, new Vector2(0, 1), color));

            mesh.SetVertex(12, new Vertex3D(new Vector3(1, 1, -1) * size, Vector3.Right, new Vector2(0, 0), color));
            mesh.SetVertex(13, new Vertex3D(new Vector3(1, 1, 1) * size, Vector3.Right, new Vector2(1, 0), color));
            mesh.SetVertex(14, new Vertex3D(new Vector3(1, -1, 1) * size, Vector3.Right, new Vector2(1, 1), color));
            mesh.SetVertex(15, new Vertex3D(new Vector3(1, -1, -1) * size, Vector3.Right, new Vector2(0, 1), color));

            mesh.SetVertex(16, new Vertex3D(new Vector3(-1, -1, 1) * size, Vector3.Down, new Vector2(0, 0), color));
            mesh.SetVertex(17, new Vertex3D(new Vector3(-1, -1, -1) * size, Vector3.Down, new Vector2(1, 0), color));
            mesh.SetVertex(18, new Vertex3D(new Vector3(1, -1, -1) * size, Vector3.Down, new Vector2(1, 1), color));
            mesh.SetVertex(19, new Vertex3D(new Vector3(1, -1, 1) * size, Vector3.Down, new Vector2(0, 1), color));

            mesh.SetVertex(20, new Vertex3D(new Vector3(-1, 1, 1) * size, Vector3.Up, new Vector2(0, 0), color));
            mesh.SetVertex(21, new Vertex3D(new Vector3(1, 1, 1) * size, Vector3.Up, new Vector2(1, 0), color));
            mesh.SetVertex(22, new Vertex3D(new Vector3(1, 1, -1) * size, Vector3.Up, new Vector2(1, 1), color));
            mesh.SetVertex(23, new Vertex3D(new Vector3(-1, 1, -1) * size, Vector3.Up, new Vector2(0, 1), color));

            for (int i = 0, f = 0; i < 12; i += 2, f += 4)
            {
                mesh.SetTriangle(i, f, f + 1, f + 2);
                mesh.SetTriangle(i + 1, f, f + 2, f + 3);
            }
            //mesh.CalculateNormals();
            mesh.Update();
            return(mesh);
        }