3using System.Collections.Generic;
10 public static class pb_Stl_Exporter
15 public static bool Export(
string path, GameObject[] gameObjects,
FileType type)
17 Mesh[] meshes = CreateWorldSpaceMeshesWithTransforms(gameObjects.Select(x => x.transform).ToArray());
20 if(meshes !=
null && meshes.Length > 0)
22 if(!
string.IsNullOrEmpty(path))
23 success = pb_Stl.WriteFile(path, meshes, type);
26 for(
int i = 0; meshes !=
null && i < meshes.Length; i++)
27 Object.DestroyImmediate(meshes[i]);
34 public static string Export(GameObject[] gameObjects,
FileType type)
36 Mesh[] meshes = CreateWorldSpaceMeshesWithTransforms(gameObjects.Select(x => x.transform).ToArray());
38 if(meshes !=
null && meshes.Length > 0)
40 retval = pb_Stl.WriteString(meshes);
43 for(
int i = 0; meshes !=
null && i < meshes.Length; i++)
44 Object.DestroyImmediate(meshes[i]);
52 private static Mesh[] CreateWorldSpaceMeshesWithTransforms(IList<Transform> transforms)
54 if(transforms ==
null || transforms.Count < 1)
60 for(
int i = 0; i < transforms.Count; i++)
61 p += transforms[i].position;
62 Vector3 mesh_center = p / (float) transforms.Count;
64 GameObject root =
new GameObject();
66 root.transform.position = mesh_center;
69 foreach(Transform t
in transforms)
71 GameObject go = (GameObject) GameObject.Instantiate(t.gameObject);
72 go.transform.SetParent(t.parent,
false);
73 go.transform.SetParent(root.transform,
true);
77 root.transform.position =
Vector3.zero;
81 List<MeshFilter> mfs = root.GetComponentsInChildren<MeshFilter>().Where(x => x.sharedMesh !=
null).ToList();
82 int meshCount = mfs.Count;
83 Mesh[] meshes =
new Mesh[meshCount];
85 for(
int i = 0; i < meshCount; i++)
87 Transform t = mfs[i].transform;
89 Vector3[] v = mfs[i].sharedMesh.vertices;
90 Vector3[] n = mfs[i].sharedMesh.normals;
92 for(
int it = 0; it < v.Length; it++)
94 v[it] = t.TransformPoint(v[it]);
95 n[it] = t.TransformDirection(n[it]);
100 m.name = mfs[i].name;
103 m.triangles = mfs[i].sharedMesh.triangles;
109 GameObject.DestroyImmediate(root);