allow creating more glTF instances than pre-allocated

This commit is contained in:
Nick Fisher
2025-06-20 18:46:36 +08:00
parent 37c35906d6
commit e1cb517417
2 changed files with 16 additions and 14 deletions

View File

@@ -63,12 +63,12 @@ namespace thermion
{
auto instanceNumber = _instances.size();
if (instanceNumber > _asset->getAssetInstanceCount() - 1)
if (instanceNumber >= _asset->getAssetInstanceCount() - 1)
{
Log("glTF asset was created with %d instances reserved, and %d instances have been used. Increase the number of instances pre-allocated when the asset is loaded.",
_asset->getAssetInstanceCount(), _instances.size()
TRACE("Warning: %d/%d pre-allocated instances already consumed. You may wish to pre-allocate a larger number.",
_asset->getAssetInstanceCount(), _instances.size()
);
return std::nullptr_t();
_assetLoader->createInstance(_asset);
}
TRACE("Creating instance %d", instanceNumber);
auto instance = _asset->getAssetInstances()[instanceNumber];

View File

@@ -64,15 +64,8 @@ void main() async {
});
});
test('create gltf instance', () async {
test('create pre-allocated gltf instance', () async {
await testHelper.withViewer((viewer) async {
await viewer
.loadIbl("file://${testHelper.testDir}/assets/default_env_ibl.ktx");
await viewer.loadSkybox(
"file://${testHelper.testDir}/assets/default_env_skybox.ktx");
await viewer.setPostProcessing(true);
await viewer.setAntiAliasing(false, true, false);
// Loading a glTF asset always creates a single instance behind the scenes,
// but the entities exposed by the asset are used to manipulate all
// instances as a group, not the singular "default" instance.
@@ -88,7 +81,7 @@ void main() async {
var defaultInstance = await asset.getInstance(0);
await viewer.addToScene(defaultInstance);
await testHelper.capture(viewer.view, "gltf_without_instance");
var instance = await asset.createInstance();
await instance.setTransform(Matrix4.translation(Vector3(1, 0, 0)));
await testHelper.capture(viewer.view, "gltf_instance_created");
@@ -96,7 +89,16 @@ void main() async {
await testHelper.capture(viewer.view, "gltf_instance_add_to_scene");
await viewer.removeFromScene(instance);
await testHelper.capture(viewer.view, "gltf_instance_remove_from_scene");
});
// above, we pre-allocated two instances and have used all of them
// calling createInstance now will still create another instance, but will be slower than allocating.
var instance2 = await asset.createInstance();
await instance2.setTransform(Matrix4.translation(Vector3(-1, 0, 0)));
await viewer.addToScene(instance2);
await testHelper.capture(viewer.view, "gltf_instance2_add_to_scene");
}, addSkybox: true);
});
test('physics simulation with 100 instances', () async {