Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API for loading meshes on their own #477

Merged
merged 3 commits into from
Aug 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 75 additions & 57 deletions UnityGLTF/Assets/UnityGLTF/Scripts/GLTFSceneImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,38 +330,11 @@ public IEnumerator LoadScene(int sceneIndex = -1, bool showSceneObj = true, Acti
/// <returns></returns>
public async Task LoadNodeAsync(int nodeIndex, CancellationToken cancellationToken)
{
try
await SetupLoad(async () =>
{
lock (this)
{
if (_isRunning)
{
throw new GLTFLoadException("Cannot call LoadNode while GLTFSceneImporter is already running");
}

_isRunning = true;
}

if (_gltfRoot == null)
{
await LoadJson(_gltfFileName);
}

if (_assetCache == null)
{
_assetCache = new AssetCache(_gltfRoot);
}

CreatedObject = await GetNode(nodeIndex, cancellationToken);
InitializeGltfTopLevelObject();
}
finally
{
lock (this)
{
_isRunning = false;
}
}
});
}

/// <summary>
Expand All @@ -371,49 +344,45 @@ public async Task LoadNodeAsync(int nodeIndex, CancellationToken cancellationTok
/// <returns></returns>
public virtual async Task<Material> LoadMaterialAsync(int materialIndex)
{
try
await SetupLoad(async () =>
{
lock (this)
{
if (_isRunning)
{
throw new GLTFLoadException("Cannot CreateTexture while GLTFSceneImporter is already running");
}

_isRunning = true;
}

if (_gltfRoot == null)
{
await LoadJson(_gltfFileName);
}

if (materialIndex < 0 || materialIndex >= _gltfRoot.Materials.Count)
{
throw new ArgumentException($"There is no material for index {materialIndex}");
}

if (_assetCache == null)
{
_assetCache = new AssetCache(_gltfRoot);
}

if (_assetCache.MaterialCache[materialIndex] == null)
{
var def = _gltfRoot.Materials[materialIndex];
await ConstructMaterialImageBuffers(def);
await ConstructMaterial(def, materialIndex);
}
}
finally
});
return _assetCache.MaterialCache[materialIndex].UnityMaterialWithVertexColor;
}

/// <summary>
/// Load a Mesh from the glTF by index
/// </summary>
/// <param name="meshIndex"></param>
/// <returns></returns>
public virtual async Task<Mesh> LoadMeshAsync(int meshIndex, CancellationToken cancellationToken)
{
await SetupLoad(async () =>
{
lock (this)
if (meshIndex < 0 || meshIndex >= _gltfRoot.Meshes.Count)
{
_isRunning = false;
throw new ArgumentException($"There is no mesh for index {meshIndex}");
}
}

return _assetCache.MaterialCache[materialIndex].UnityMaterialWithVertexColor;
if (_assetCache.MeshCache[meshIndex] == null)
{
var def = _gltfRoot.Meshes[meshIndex];
await ConstructMeshAttributes(def, new MeshId() { Id = meshIndex, Root = _gltfRoot });
await ConstructMesh(def, meshIndex, cancellationToken);
}
});
return _assetCache.MeshCache[meshIndex].LoadedMesh;
}

/// <summary>
Expand Down Expand Up @@ -1589,7 +1558,7 @@ protected virtual async Task ConstructMesh(GLTFMesh mesh, int meshIndex, Cancell

if (_assetCache.MeshCache[meshIndex] == null)
{
throw new Exception("Cannot generate mesh before ConstructBufferData is called!");
throw new Exception("Cannot generate mesh before ConstructMeshAttributes is called!");
}

var totalVertCount = mesh.Primitives.Aggregate((uint)0, (sum, p) => sum + p.Attributes[SemanticProperties.POSITION].Value.Count);
Expand Down Expand Up @@ -2107,6 +2076,11 @@ public virtual async Task LoadTextureAsync(GLTFTexture texture, int textureIndex
_isRunning = true;
}

if (_options.ThrowOnLowMemory)
{
_memoryChecker = new MemoryChecker();
}

if (_gltfRoot == null)
{
await LoadJson(_gltfFileName);
Expand Down Expand Up @@ -2340,5 +2314,49 @@ private void Cleanup()
_assetCache = null;
}
}

private async Task SetupLoad(Func<Task> callback)
{
try
{
lock (this)
{
if (_isRunning)
{
throw new GLTFLoadException("Cannot start a load while GLTFSceneImporter is already running");
}

_isRunning = true;
}

if (_options.ThrowOnLowMemory)
{
_memoryChecker = new MemoryChecker();
}

if (_gltfRoot == null)
{
await LoadJson(_gltfFileName);
}

if (_assetCache == null)
{
_assetCache = new AssetCache(_gltfRoot);
}

await callback();
}
catch
{
Cleanup();
}
finally
{
lock (this)
{
_isRunning = false;
}
}
}
}
}