-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathMRMeshSave.cs
90 lines (74 loc) · 3.3 KB
/
MRMeshSave.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
namespace MR
{
public partial class DotNet
{
public struct NamedMeshXf
{
public string name = "";
public AffineXf3f toWorld = new AffineXf3f();
public Mesh? mesh = null;
public NamedMeshXf() { }
}
[StructLayout(LayoutKind.Sequential)]
struct MRMeshSaveNamedXfMesh
{
public string name = "";
public MRAffineXf3f toWorld = new MRAffineXf3f();
public IntPtr mesh = IntPtr.Zero;
public MRMeshSaveNamedXfMesh() { }
}
public class MeshSave
{
[DllImport("MRMeshC", CharSet = CharSet.Ansi)]
private static extern IntPtr mrStringData(IntPtr str);
[DllImport("MRMeshC", CharSet = CharSet.Ansi)]
private static extern void mrMeshSaveSceneToObj(IntPtr objects, ulong objectsNum, string file, ref IntPtr errorString);
[DllImport("MRMeshC", CharSet = CharSet.Ansi)]
private static extern void mrLoadIOExtras();
[DllImport("MRMeshC", CharSet = CharSet.Ansi)]
private static extern void mrMeshSaveToAnySupportedFormat(IntPtr mesh, string file, ref MRSaveSettings settings, ref IntPtr errorStr);
/// saves mesh to file of any supported format
public static void ToAnySupportedFormat(Mesh mesh, string path, SaveSettings? settings = null)
{
mrLoadIOExtras();
IntPtr errString = IntPtr.Zero;
MRSaveSettings mrSettings = settings is null ? new MRSaveSettings() : settings.Value.ToNative();
mrMeshSaveToAnySupportedFormat(mesh.mesh_, path, ref mrSettings, ref errString);
if (errString != IntPtr.Zero)
{
var errData = mrStringData(errString);
string errorMessage = MarshalNativeUtf8ToManagedString(errData);
throw new SystemException(errorMessage);
}
}
/// saves a number of named meshes in .obj file
public static void SceneToObj(List<NamedMeshXf> meshes, string file)
{
int sizeOfNamedXfMesh = Marshal.SizeOf(typeof(MRMeshSaveNamedXfMesh));
IntPtr nativeMeshes = Marshal.AllocHGlobal(meshes.Count * sizeOfNamedXfMesh);
try
{
for (int i = 0; i < meshes.Count; i++)
{
MRMeshSaveNamedXfMesh mrMesh = new MRMeshSaveNamedXfMesh();
mrMesh.name = meshes[i].name;
mrMesh.toWorld = meshes[i].toWorld.xf_;
var mesh = meshes[i].mesh;
if (mesh != null)
mrMesh.mesh = mesh.mesh_;
Marshal.StructureToPtr(mrMesh, IntPtr.Add(nativeMeshes, i * sizeOfNamedXfMesh), false);
}
IntPtr errString = new IntPtr();
mrMeshSaveSceneToObj(nativeMeshes, (ulong)meshes.Count, file, ref errString);
}
finally
{
Marshal.FreeHGlobal(nativeMeshes);
}
}
}
};
}