Beispiel #1
0
        public static Mesh3D CreateQuad(float w, float h, Color4 color)
        {
            var mesh = new Mesh3D(4, 6);
            var a    = new Vertex3D(new Vector3(w * 0.5f, h * 0.5f), Vector3.Forward, new Vector2(0f, 0f), color);
            var b    = new Vertex3D(new Vector3(w * -0.5f, h * 0.5f), Vector3.Forward, new Vector2(1f, 0f), color);
            var c    = new Vertex3D(new Vector3(w * -0.5f, h * -0.5f), Vector3.Forward, new Vector2(1f, 1f), color);
            var d    = new Vertex3D(new Vector3(w * 0.5f, h * -0.5f), Vector3.Forward, new Vector2(0f, 1f), color);

            mesh.AddQuad(ref a, ref b, ref c, ref d);
            mesh.CalculateNormals();
            mesh.Update();
            return(mesh);
        }
Beispiel #2
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);
        }