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); }
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); }
public static Mesh3D CreateCone(float radius, float height, int numSegments) { float lastx = 0; float lastz = 0; numSegments *= 2; if ((numSegments % 2) == 0) { numSegments++; } var mesh = new Mesh3D(); mesh.AddVertex(new Vertex3D(new Vector3(0f, height / -2f, 0f), new Vector3(0f, -1f, 0f), new Vector2(0.5f, 0.5f))); for (int i = 0; i < numSegments; i++) { float pos = (Calc.Tau / (numSegments - 1)) * i; float x = Calc.Sin(pos); float z = Calc.Cos(pos); if ((i % 2) == 0) { mesh.AddVertex(new Vertex3D(new Vector3(x * radius, height / -2f, z * radius), new Vector3(x, 0f, z), new Vector2(0.5f + z * 0.5f, 0.5f + x * 0.5f))); mesh.AddVertex(new Vertex3D(new Vector3(x * radius, height / -2f, z * radius), new Vector3(0f, -1f, 0f), new Vector2(0.5f + z * 0.5f, 0.5f + x * 0.5f))); } else { mesh.AddVertex(new Vertex3D(new Vector3(0f, height - height / 2f, 0f), new Vector3(0f, 1f, 0f), new Vector2(0.5f, 0.5f))); } lastx = x; lastz = z; } int vo = 4; for (int i = 1; i <= (numSegments - 1) / 2; i++) { mesh.AddIndices(vo, vo - 1, vo - 3); mesh.AddIndices(vo + 1, vo - 2, 0); vo += 3; } mesh.Update(); return(mesh); }
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); }
public static Mesh3D CreateCylinder(float radius, float height, int segments, bool capped) { float lastx = 0f; float lastz = 0f; float lastv = 0f; ++segments; var mesh = new Mesh3D(); if (capped) { mesh.AddVertex(new Vertex3D(new Vector3(0, 0 - (height / 2.0f), 0), new Vector3(0f, -1f, 0f), new Vector2(0.5f, 0.5f))); mesh.AddVertex(new Vertex3D(new Vector3(0, height - (height / 2.0f), 0), new Vector3(0f, 1f, 0f), new Vector2(0.5f, 0.5f))); } for (int i = 0; i < segments; i++) { float v = i / (float)(segments - 1); float pos = (Calc.Tau / (segments - 1)) * i; float x = Calc.Sin(pos); float z = Calc.Cos(pos); mesh.AddVertex(new Vertex3D(new Vector3(x * radius, 0 - (height / 2.0f), z * radius), new Vector3(x, 0f, z), new Vector2(v, 0f))); mesh.AddVertex(new Vertex3D(new Vector3(x * radius, height - (height / 2.0f), z * radius), new Vector3(x, 0f, z), new Vector2(v, 1f))); if (capped) { mesh.AddVertex(new Vertex3D(new Vector3(x * radius, 0 - (height / 2.0f), z * radius), new Vector3(0f, -1f, 0f), new Vector2((0.5f + z * 0.5f), (0.5f + x * 0.5f)))); mesh.AddVertex(new Vertex3D(new Vector3(x * radius, height - (height / 2.0f), z * radius), new Vector3(0f, 1f, 0f), new Vector2((0.5f + z * 0.5f), (0.5f + x * 0.5f)))); } lastx = x; lastz = z; lastv = v; } int vo = 2; int vi = 1; if (capped) { vi = 3; vo = 6; } for (int i = 1; i <= segments - 1; i++) { mesh.AddIndices(vo, vo - vi, vo - vi - 1); mesh.AddIndices(vo, vo + 1, vo - vi); vo += 2; if (capped) { mesh.AddIndices(vo, vo - vi - 1, 0); mesh.AddIndices(1, vo - vi, vo + 1); vo += 2; } } mesh.Update(); return(mesh); }