Skip to content

Instantly share code, notes, and snippets.

@kaorun55
Last active June 1, 2016 06:04
Show Gist options
  • Save kaorun55/32b65837f82069d17be0f5cd01bae98e to your computer and use it in GitHub Desktop.
Save kaorun55/32b65837f82069d17be0f5cd01bae98e to your computer and use it in GitHub Desktop.
IEnumerator SaveMesh()
{
if (!isSaveMesh)
{
yield break;
}
isSaveMesh = false;
string modelData = "";
int offset = 0;
int count = 0;
var filters = mapping.GetMeshFilters();
#if false
var filter = filters.First();
#else
foreach (var filter in filters)
#endif
{
if (filter != null)
{
// OBJ形式で出力する
var mesh = filter.sharedMesh;
modelData += string.Format("o object.{0}\n", ++count);
foreach (var vertex in mesh.vertices)
{
// ローカル座標をワールド座標に変換する
var v = filter.transform.TransformPoint(vertex);
modelData += string.Format("v {0} {1} {2}\n", v.x, v.y, v.z);
}
modelData += "\n";
// 面を書く
for (int i = 0; i < mesh.triangles.Length; i += 3)
{
modelData += string.Format("f {0} {1} {2}\n",
mesh.triangles[i + 0] + 1 + offset,
mesh.triangles[i + 1] + 1 + offset,
mesh.triangles[i + 2] + 1 + offset);
}
modelData += "\n";
modelData += "\n";
// 頂点インデックスのオフセットを更新
offset += mesh.vertexCount;
}
}
#if !UNITY_EDITOR
var ao = KnownFolders.CameraRoll.CreateFileAsync("mapping.obj", CreationCollisionOption.GenerateUniqueName);
ao.Completed = async delegate {
if (ao.Status == Windows.Foundation.AsyncStatus.Completed)
{
var file = ao.GetResults();
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var imageBuffer = Encoding.ASCII.GetBytes(modelData);
await stream.WriteAsync(imageBuffer.AsBuffer());
}
Debug.Log("write comlete");
}
else if(ao.Status == Windows.Foundation.AsyncStatus.Error)
{
Debug.Log(ao.ErrorCode.Message);
}
};
while (ao.Status == Windows.Foundation.AsyncStatus.Started)
{
yield return new WaitForEndOfFrame();
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment