public static Mesh CreateCube(float size = 1.0f) { float s2 = size * 0.5f; var mesh = new Mesh(); //corners mesh.positions.Add(new Vector3(s2, s2, -s2)); //0 mesh.positions.Add(new Vector3(s2, s2, s2)); //1 mesh.positions.Add(new Vector3(-s2, s2, s2)); //2 mesh.positions.Add(new Vector3(-s2, s2, -s2)); //3 mesh.positions.Add(new Vector3(s2, -s2, -s2)); //4 mesh.positions.Add(new Vector3(-s2, -s2, -s2)); //5 mesh.positions.Add(new Vector3(-s2, -s2, s2)); //6 mesh.positions.Add(new Vector3(s2, -s2, s2)); //7 //Top Face mesh.ids.Add(0); mesh.ids.Add(1); mesh.ids.Add(2); mesh.ids.Add(0); mesh.ids.Add(2); mesh.ids.Add(3); //Bottom Face mesh.ids.Add(4); mesh.ids.Add(5); mesh.ids.Add(6); mesh.ids.Add(4); mesh.ids.Add(6); mesh.ids.Add(7); //Front Face mesh.ids.Add(1); mesh.ids.Add(7); mesh.ids.Add(6); mesh.ids.Add(1); mesh.ids.Add(6); mesh.ids.Add(2); //Back Face mesh.ids.Add(0); mesh.ids.Add(3); mesh.ids.Add(5); mesh.ids.Add(0); mesh.ids.Add(5); mesh.ids.Add(4); //Left face mesh.ids.Add(2); mesh.ids.Add(6); mesh.ids.Add(5); mesh.ids.Add(2); mesh.ids.Add(5); mesh.ids.Add(3); //Right face mesh.ids.Add(1); mesh.ids.Add(0); mesh.ids.Add(4); mesh.ids.Add(1); mesh.ids.Add(4); mesh.ids.Add(7); return mesh.SwitchTriangleMeshWinding(); }
public static Mesh CreateSphere(float radius_ = 1.0f, uint subdivision = 1) { //idea: subdivide icosahedron const float X = 0.525731112119133606f; const float Z = 0.850650808352039932f; var vdata = new float[12,3] { { -X, 0.0f, Z}, { X, 0.0f, Z}, { -X, 0.0f, -Z }, { X, 0.0f, -Z }, { 0.0f, Z, X }, { 0.0f, Z, -X }, { 0.0f, -Z, X }, { 0.0f, -Z, -X }, { Z, X, 0.0f }, { -Z, X, 0.0f }, { Z, -X, 0.0f }, { -Z, -X, 0.0f } }; var tindices = new uint[20,3] { { 0, 4, 1 }, { 0, 9, 4 }, { 9, 5, 4 }, { 4, 5, 8 }, { 4, 8, 1 }, { 8, 10, 1 }, { 8, 3, 10 }, { 5, 3, 8 }, { 5, 2, 3 }, { 2, 7, 3 }, { 7, 10, 3 }, { 7, 6, 10 }, { 7, 11, 6 }, { 11, 0, 6 }, { 0, 1, 6 }, { 6, 1, 10 }, { 9, 0, 11 }, { 9, 11, 2 }, { 9, 2, 5 }, { 7, 2, 11 } }; Mesh mesh = new Mesh(); for (int i = 0; i < 12; ++i) { var p = new Vector3(vdata[i, 0], vdata[i, 1], vdata[i, 2]); mesh.normals.Add(p); mesh.positions.Add(p); } for (int i = 0; i < 20; ++i) { Subdivide(mesh, tindices[i, 0], tindices[i, 1], tindices[i, 2], subdivision); } //scale for (int i = 0; i < mesh.positions.Count; ++i) { mesh.positions[i] *= radius_; } return mesh.SwitchTriangleMeshWinding(); }