Skip to content

Commit 5bd548c

Browse files
Handle low memory situations and change API of Importer/Exporter to take in option classes (#454)
* exporter options * Switch to import options. Add an option to throw when out of memory. Implement throwing when out of memory. Still need to properly clean up when that occurs. * Clean up nodes when exceptions are thrown. * more clean up when exception is thrown * Add windows using. Comments. Tabs. * move null check into Cleanup now that it's called multiple places * Rename from CheckMemory to ThrowOnLowMemory to make it more clear what it does. Add comments in all Obsolete attributes.
1 parent 12c6ead commit 5bd548c

File tree

11 files changed

+284
-107
lines changed

11 files changed

+284
-107
lines changed

UnityGLTF/Assets/UnityGLTF/Examples/GLTFExporterTest.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@ namespace UnityGLTF.Examples
55
{
66
public class GLTFExporterTest : MonoBehaviour
77
{
8-
public string RetrieveTexturePath(UnityEngine.Texture texture)
9-
{
10-
return texture.name;
11-
}
12-
138
// Use this for initialization
149
void Awake()
1510
{
16-
var exporter = new GLTFSceneExporter(new[] {transform}, RetrieveTexturePath);
11+
var exporter = new GLTFSceneExporter(new[] {transform}, new ExportOptions());
1712
var appPath = Application.dataPath;
1813
var wwwPath = appPath.Substring(0, appPath.LastIndexOf("Assets")) + "www";
1914
exporter.SaveGLTFandBin(Path.Combine(wwwPath, "TestScene"), "TestScene");

UnityGLTF/Assets/UnityGLTF/Examples/MultiSceneComponent.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@ public class MultiSceneComponent : MonoBehaviour
1111
public string Url;
1212

1313
private GLTFSceneImporter _importer;
14-
private AsyncCoroutineHelper _asyncCoroutineHelper;
15-
private ILoader _loader;
14+
private ImportOptions _importOptions;
1615
private string _fileName;
1716

1817
void Start()
1918
{
2019
Debug.Log("Hit spacebar to change the scene.");
21-
_asyncCoroutineHelper = gameObject.AddComponent<AsyncCoroutineHelper>();
2220
Uri uri = new Uri(Url);
2321
var directoryPath = URIHelper.AbsoluteUriPath(uri);
24-
_loader = new WebRequestLoader(directoryPath);
22+
_importOptions = new ImportOptions
23+
{
24+
ExternalDataLoader = new WebRequestLoader(directoryPath),
25+
AsyncCoroutineHelper = gameObject.AddComponent<AsyncCoroutineHelper>(),
26+
};
2527
_fileName = URIHelper.GetFileFromUri(uri);
2628

2729
LoadScene(SceneIndex);
@@ -46,8 +48,7 @@ async void LoadScene(int SceneIndex)
4648

4749
_importer = new GLTFSceneImporter(
4850
_fileName,
49-
_loader,
50-
_asyncCoroutineHelper
51+
_importOptions
5152
);
5253

5354
_importer.SceneParent = gameObject.transform;

UnityGLTF/Assets/UnityGLTF/Examples/RootMergeComponent.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,12 @@ private async Task Start()
6969
}
7070
GLTFSceneImporter importer = new GLTFSceneImporter(
7171
asset1Root,
72-
loader1,
73-
gameObject.AddComponent<AsyncCoroutineHelper>()
74-
);
72+
null,
73+
new ImportOptions
74+
{
75+
ExternalDataLoader = loader1,
76+
AsyncCoroutineHelper = gameObject.AddComponent<AsyncCoroutineHelper>()
77+
});
7578

7679
importer.MaximumLod = MaximumLod;
7780
importer.IsMultithreaded = Multithreaded;

UnityGLTF/Assets/UnityGLTF/Scripts/Editor/GLTFExportMenu.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ static void ExportSelected()
4242
else
4343
throw new Exception("No objects selected, cannot export.");
4444

45-
var exporter = new GLTFSceneExporter(Selection.transforms, RetrieveTexturePath);
45+
var exportOptions = new ExportOptions { TexturePathRetriever = RetrieveTexturePath };
46+
var exporter = new GLTFSceneExporter(Selection.transforms, exportOptions);
4647

4748
var path = EditorUtility.OpenFolderPanel("glTF Export Path", "", "");
4849
if (!string.IsNullOrEmpty(path)) {
@@ -61,7 +62,8 @@ static void ExportGLBSelected()
6162
else
6263
throw new Exception("No objects selected, cannot export.");
6364

64-
var exporter = new GLTFSceneExporter(Selection.transforms, RetrieveTexturePath);
65+
var exportOptions = new ExportOptions { TexturePathRetriever = RetrieveTexturePath };
66+
var exporter = new GLTFSceneExporter(Selection.transforms, exportOptions);
6567

6668
var path = EditorUtility.OpenFolderPanel("glTF Export Path", "", "");
6769
if (!string.IsNullOrEmpty(path))
@@ -77,7 +79,8 @@ static void ExportScene()
7779
var gameObjects = scene.GetRootGameObjects();
7880
var transforms = Array.ConvertAll(gameObjects, gameObject => gameObject.transform);
7981

80-
var exporter = new GLTFSceneExporter(transforms, RetrieveTexturePath);
82+
var exportOptions = new ExportOptions { TexturePathRetriever = RetrieveTexturePath };
83+
var exporter = new GLTFSceneExporter(transforms, exportOptions);
8184
var path = EditorUtility.OpenFolderPanel("glTF Export Path", "", "");
8285
if (path != "") {
8386
exporter.SaveGLTFandBin (path, scene.name);

UnityGLTF/Assets/UnityGLTF/Scripts/Editor/GLTFImporter.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,15 @@ public override void OnImportAsset(AssetImportContext ctx)
334334

335335
private GameObject CreateGLTFScene(string projectFilePath)
336336
{
337-
ILoader fileLoader = new FileLoader(Path.GetDirectoryName(projectFilePath));
337+
var importOptions = new ImportOptions
338+
{
339+
ExternalDataLoader = new FileLoader(Path.GetDirectoryName(projectFilePath)),
340+
};
338341
using (var stream = File.OpenRead(projectFilePath))
339342
{
340343
GLTFRoot gLTFRoot;
341344
GLTFParser.ParseJson(stream, out gLTFRoot);
342-
var loader = new GLTFSceneImporter(gLTFRoot, fileLoader, null, stream);
345+
var loader = new GLTFSceneImporter(gLTFRoot, stream, importOptions);
343346
loader.MaximumLod = _maximumLod;
344347
loader.IsMultithreaded = true;
345348

UnityGLTF/Assets/UnityGLTF/Scripts/Factories/ImporterFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ namespace UnityGLTF
55
{
66
public abstract class ImporterFactory : ScriptableObject
77
{
8-
public abstract GLTFSceneImporter CreateSceneImporter(string gltfFileName, ILoader externalDataLoader, AsyncCoroutineHelper asyncCoroutineHelper);
8+
public abstract GLTFSceneImporter CreateSceneImporter(string gltfFileName, ImportOptions options);
99
}
1010

1111
public class DefaultImporterFactory : ImporterFactory
1212
{
13-
public override GLTFSceneImporter CreateSceneImporter(string gltfFileName, ILoader externalDataLoader, AsyncCoroutineHelper asyncCoroutineHelper)
13+
public override GLTFSceneImporter CreateSceneImporter(string gltfFileName, ImportOptions options)
1414
{
15-
return new GLTFSceneImporter(gltfFileName, externalDataLoader, asyncCoroutineHelper);
15+
return new GLTFSceneImporter(gltfFileName, options);
1616
}
1717
}
1818
}

UnityGLTF/Assets/UnityGLTF/Scripts/GLTFComponent.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ public class GLTFComponent : MonoBehaviour
3838
public GLTFSceneImporter.ColliderType Collider = GLTFSceneImporter.ColliderType.None;
3939
public GameObject LastLoadedScene { get; private set; } = null;
4040

41-
private AsyncCoroutineHelper asyncCoroutineHelper;
42-
4341
[SerializeField]
4442
private Shader shaderOverride = null;
4543

@@ -68,9 +66,13 @@ private async void Start()
6866

6967
public async Task Load()
7068
{
71-
asyncCoroutineHelper = gameObject.GetComponent<AsyncCoroutineHelper>() ?? gameObject.AddComponent<AsyncCoroutineHelper>();
69+
var importOptions = new ImportOptions
70+
{
71+
AsyncCoroutineHelper = gameObject.GetComponent<AsyncCoroutineHelper>() ?? gameObject.AddComponent<AsyncCoroutineHelper>(),
72+
ExternalDataLoader = null,
73+
};
74+
7275
GLTFSceneImporter sceneImporter = null;
73-
ILoader loader = null;
7476
try
7577
{
7678
Factory = Factory ?? ScriptableObject.CreateInstance<DefaultImporterFactory>();
@@ -90,22 +92,20 @@ public async Task Load()
9092
fullPath = GLTFUri;
9193
}
9294
string directoryPath = URIHelper.GetDirectoryName(fullPath);
93-
loader = new FileLoader(directoryPath);
95+
importOptions.ExternalDataLoader = new FileLoader(directoryPath);
9496
sceneImporter = Factory.CreateSceneImporter(
9597
Path.GetFileName(GLTFUri),
96-
loader,
97-
asyncCoroutineHelper
98+
importOptions
9899
);
99100
}
100101
else
101102
{
102103
string directoryPath = URIHelper.GetDirectoryName(GLTFUri);
103-
loader = new WebRequestLoader(directoryPath);
104+
importOptions.ExternalDataLoader = new WebRequestLoader(directoryPath);
104105

105106
sceneImporter = Factory.CreateSceneImporter(
106107
URIHelper.GetFileFromUri(new Uri(GLTFUri)),
107-
loader,
108-
asyncCoroutineHelper
108+
importOptions
109109
);
110110

111111
}
@@ -151,11 +151,11 @@ public async Task Load()
151151
}
152152
finally
153153
{
154-
if(loader != null)
154+
if(importOptions.ExternalDataLoader != null)
155155
{
156156
sceneImporter?.Dispose();
157157
sceneImporter = null;
158-
loader = null;
158+
importOptions.ExternalDataLoader = null;
159159
}
160160
}
161161
}

UnityGLTF/Assets/UnityGLTF/Scripts/GLTFSceneExporter.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
namespace UnityGLTF
1313
{
14+
public class ExportOptions
15+
{
16+
public GLTFSceneExporter.RetrieveTexturePathDelegate TexturePathRetriever = (texture) => texture.name;
17+
public bool ExportInactivePrimitives = true;
18+
}
19+
1420
public class GLTFSceneExporter
1521
{
1622
public delegate string RetrieveTexturePathDelegate(Texture texture);
@@ -53,7 +59,7 @@ private struct ImageInfo
5359
private List<Material> _materials;
5460
private bool _shouldUseInternalBufferForImages;
5561

56-
private RetrieveTexturePathDelegate _retrieveTexturePathDelegate;
62+
private ExportOptions _exportOptions;
5763

5864
private Material _metalGlossChannelSwapMaterial;
5965
private Material _normalChannelMaterial;
@@ -82,9 +88,19 @@ protected struct PrimKey
8288
/// Create a GLTFExporter that exports out a transform
8389
/// </summary>
8490
/// <param name="rootTransforms">Root transform of object to export</param>
85-
public GLTFSceneExporter(Transform[] rootTransforms, RetrieveTexturePathDelegate retrieveTexturePathDelegate)
91+
[Obsolete("Please switch to GLTFSceneExporter(Transform[] rootTransforms, ExportOptions options). This constructor is deprecated and will be removed in a future release.")]
92+
public GLTFSceneExporter(Transform[] rootTransforms, RetrieveTexturePathDelegate texturePathRetriever)
93+
: this(rootTransforms, new ExportOptions { TexturePathRetriever = texturePathRetriever })
94+
{
95+
}
96+
97+
/// <summary>
98+
/// Create a GLTFExporter that exports out a transform
99+
/// </summary>
100+
/// <param name="rootTransforms">Root transform of object to export</param>
101+
public GLTFSceneExporter(Transform[] rootTransforms, ExportOptions options)
86102
{
87-
_retrieveTexturePathDelegate = retrieveTexturePathDelegate;
103+
_exportOptions = options;
88104

89105
var metalGlossChannelSwapShader = Resources.Load("MetalGlossChannelSwap", typeof(Shader)) as Shader;
90106
_metalGlossChannelSwapMaterial = new Material(metalGlossChannelSwapShader);
@@ -412,7 +428,7 @@ private void ExportTexture(Texture2D texture, string outputPath)
412428

413429
private string ConstructImageFilenamePath(Texture2D texture, string outputPath)
414430
{
415-
var imagePath = _retrieveTexturePathDelegate(texture);
431+
var imagePath = _exportOptions.TexturePathRetriever(texture);
416432
if (string.IsNullOrEmpty(imagePath))
417433
{
418434
imagePath = Path.Combine(outputPath, texture.name);
@@ -1254,7 +1270,7 @@ private ImageId ExportImage(Texture texture, TextureMapType texturMapType)
12541270
textureMapType = texturMapType
12551271
});
12561272

1257-
var imagePath =_retrieveTexturePathDelegate(texture);
1273+
var imagePath = _exportOptions.TexturePathRetriever(texture);
12581274
if (string.IsNullOrEmpty(imagePath))
12591275
{
12601276
imagePath = texture.name;

0 commit comments

Comments
 (0)