fix instancing so that gltf assets always fill the first instance slot
This commit is contained in:
@@ -749,16 +749,16 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
|
|||||||
(cb) => GltfResourceLoader_createRenderThread(engine,
|
(cb) => GltfResourceLoader_createRenderThread(engine,
|
||||||
relativeResourcePath?.toNativeUtf8().cast<Char>() ?? nullptr, cb));
|
relativeResourcePath?.toNativeUtf8().cast<Char>() ?? nullptr, cb));
|
||||||
|
|
||||||
var asset = await withPointerCallback<TSceneAsset>((cb) =>
|
var filamentAsset = await withPointerCallback<TFilamentAsset>((cb) =>
|
||||||
SceneAsset_loadGlbRenderThread(engine, gltfAssetLoader,
|
GltfAssetLoader_loadRenderThread(engine, gltfAssetLoader, data.address,
|
||||||
nameComponentManager, data.address, data.length, numInstances, cb));
|
data.length, numInstances, cb));
|
||||||
|
|
||||||
if (asset == nullptr) {
|
if (filamentAsset == nullptr) {
|
||||||
throw Exception("An error occurred loading the asset");
|
throw Exception("An error occurred loading the asset");
|
||||||
}
|
}
|
||||||
|
|
||||||
var resourceUris = SceneAsset_getResourceUris(asset);
|
var resourceUris = FilamentAsset_getResourceUris(filamentAsset);
|
||||||
var resourceUriCount = SceneAsset_getResourceUriCount(asset);
|
var resourceUriCount = FilamentAsset_getResourceUriCount(filamentAsset);
|
||||||
final resources = <FinalizableUint8List>[];
|
final resources = <FinalizableUint8List>[];
|
||||||
|
|
||||||
for (int i = 0; i < resourceUriCount; i++) {
|
for (int i = 0; i < resourceUriCount; i++) {
|
||||||
@@ -782,10 +782,6 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
|
|||||||
throw UnimplementedError(
|
throw UnimplementedError(
|
||||||
"TODO"); // need to use a NativeFinalizer to ensure the pointer is still valid until resource loader has finished
|
"TODO"); // need to use a NativeFinalizer to ensure the pointer is still valid until resource loader has finished
|
||||||
} else {
|
} else {
|
||||||
final filamentAsset = SceneAsset_getFilamentAsset(asset);
|
|
||||||
if (filamentAsset == nullptr) {
|
|
||||||
throw Exception();
|
|
||||||
}
|
|
||||||
final result = await withBoolCallback((cb) =>
|
final result = await withBoolCallback((cb) =>
|
||||||
GltfResourceLoader_loadResourcesRenderThread(
|
GltfResourceLoader_loadResourcesRenderThread(
|
||||||
gltfResourceLoader, filamentAsset, cb));
|
gltfResourceLoader, filamentAsset, cb));
|
||||||
@@ -794,6 +790,10 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final asset = await withPointerCallback<TSceneAsset>((cb) =>
|
||||||
|
SceneAsset_createFromFilamentAssetRenderThread(
|
||||||
|
engine, gltfAssetLoader, nameComponentManager, filamentAsset, cb));
|
||||||
|
|
||||||
await withVoidCallback((cb) =>
|
await withVoidCallback((cb) =>
|
||||||
GltfResourceLoader_destroyRenderThread(engine, gltfResourceLoader, cb));
|
GltfResourceLoader_destroyRenderThread(engine, gltfResourceLoader, cb));
|
||||||
return FFIAsset(asset, this, animationManager.cast<TAnimationManager>());
|
return FFIAsset(asset, this, animationManager.cast<TAnimationManager>());
|
||||||
|
|||||||
@@ -473,6 +473,18 @@ external ffi.Pointer<TMaterialProvider> GltfAssetLoader_getMaterialProvider(
|
|||||||
ffi.Pointer<TGltfAssetLoader> tAssetLoader,
|
ffi.Pointer<TGltfAssetLoader> tAssetLoader,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ffi.Native<ffi.Int32 Function(ffi.Pointer<TFilamentAsset>)>(isLeaf: true)
|
||||||
|
external int FilamentAsset_getResourceUriCount(
|
||||||
|
ffi.Pointer<TFilamentAsset> tFilamentAsset,
|
||||||
|
);
|
||||||
|
|
||||||
|
@ffi.Native<
|
||||||
|
ffi.Pointer<ffi.Pointer<ffi.Char>> Function(
|
||||||
|
ffi.Pointer<TFilamentAsset>)>(isLeaf: true)
|
||||||
|
external ffi.Pointer<ffi.Pointer<ffi.Char>> FilamentAsset_getResourceUris(
|
||||||
|
ffi.Pointer<TFilamentAsset> tFilamentAsset,
|
||||||
|
);
|
||||||
|
|
||||||
@ffi.Native<TViewport Function(ffi.Pointer<TView>)>(isLeaf: true)
|
@ffi.Native<TViewport Function(ffi.Pointer<TView>)>(isLeaf: true)
|
||||||
external TViewport View_getViewport(
|
external TViewport View_getViewport(
|
||||||
ffi.Pointer<TView> view,
|
ffi.Pointer<TView> view,
|
||||||
@@ -2111,22 +2123,18 @@ external void SceneAsset_destroyRenderThread(
|
|||||||
ffi.Pointer<TEngine>,
|
ffi.Pointer<TEngine>,
|
||||||
ffi.Pointer<TGltfAssetLoader>,
|
ffi.Pointer<TGltfAssetLoader>,
|
||||||
ffi.Pointer<TNameComponentManager>,
|
ffi.Pointer<TNameComponentManager>,
|
||||||
ffi.Pointer<ffi.Uint8>,
|
ffi.Pointer<TFilamentAsset>,
|
||||||
ffi.Size,
|
|
||||||
ffi.Size,
|
|
||||||
ffi.Pointer<
|
ffi.Pointer<
|
||||||
ffi
|
ffi
|
||||||
.NativeFunction<ffi.Void Function(ffi.Pointer<TSceneAsset>)>>)>(
|
.NativeFunction<ffi.Void Function(ffi.Pointer<TSceneAsset>)>>)>(
|
||||||
isLeaf: true)
|
isLeaf: true)
|
||||||
external void SceneAsset_loadGlbRenderThread(
|
external void SceneAsset_createFromFilamentAssetRenderThread(
|
||||||
ffi.Pointer<TEngine> tEngine,
|
ffi.Pointer<TEngine> tEngine,
|
||||||
ffi.Pointer<TGltfAssetLoader> tAssetLoader,
|
ffi.Pointer<TGltfAssetLoader> tAssetLoader,
|
||||||
ffi.Pointer<TNameComponentManager> tNameComponentManager,
|
ffi.Pointer<TNameComponentManager> tNameComponentManager,
|
||||||
ffi.Pointer<ffi.Uint8> data,
|
ffi.Pointer<TFilamentAsset> tFilamentAsset,
|
||||||
int length,
|
|
||||||
int numInstances,
|
|
||||||
ffi.Pointer<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<TSceneAsset>)>>
|
ffi.Pointer<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<TSceneAsset>)>>
|
||||||
callback,
|
onComplete,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ffi.Native<
|
@ffi.Native<
|
||||||
@@ -3234,47 +3242,12 @@ external ffi.Pointer<TSceneAsset> SceneAsset_createGeometry(
|
|||||||
ffi.Pointer<TEngine>,
|
ffi.Pointer<TEngine>,
|
||||||
ffi.Pointer<TGltfAssetLoader>,
|
ffi.Pointer<TGltfAssetLoader>,
|
||||||
ffi.Pointer<TNameComponentManager>,
|
ffi.Pointer<TNameComponentManager>,
|
||||||
ffi.Pointer<ffi.Uint8>,
|
ffi.Pointer<TFilamentAsset>)>(isLeaf: true)
|
||||||
ffi.Size,
|
external ffi.Pointer<TSceneAsset> SceneAsset_createFromFilamentAsset(
|
||||||
ffi.Size)>(isLeaf: true)
|
|
||||||
external ffi.Pointer<TSceneAsset> SceneAsset_loadGlb(
|
|
||||||
ffi.Pointer<TEngine> tEngine,
|
ffi.Pointer<TEngine> tEngine,
|
||||||
ffi.Pointer<TGltfAssetLoader> tAssetLoader,
|
ffi.Pointer<TGltfAssetLoader> tAssetLoader,
|
||||||
ffi.Pointer<TNameComponentManager> tNameComponentManager,
|
ffi.Pointer<TNameComponentManager> tNameComponentManager,
|
||||||
ffi.Pointer<ffi.Uint8> data,
|
ffi.Pointer<TFilamentAsset> tFilamentAsset,
|
||||||
int length,
|
|
||||||
int numInstances,
|
|
||||||
);
|
|
||||||
|
|
||||||
@ffi.Native<
|
|
||||||
ffi.Pointer<TSceneAsset> Function(
|
|
||||||
ffi.Pointer<TGltfAssetLoader>,
|
|
||||||
ffi.Pointer<TGltfResourceLoader>,
|
|
||||||
ffi.Pointer<TEngine>,
|
|
||||||
ffi.Pointer<TNameComponentManager>,
|
|
||||||
ffi.Pointer<ffi.Uint8>,
|
|
||||||
ffi.Size,
|
|
||||||
ffi.Size)>(isLeaf: true)
|
|
||||||
external ffi.Pointer<TSceneAsset> SceneAsset_loadGltf(
|
|
||||||
ffi.Pointer<TGltfAssetLoader> tAssetLoader,
|
|
||||||
ffi.Pointer<TGltfResourceLoader> tResourceLoader,
|
|
||||||
ffi.Pointer<TEngine> tEngine,
|
|
||||||
ffi.Pointer<TNameComponentManager> tNameComponentManager,
|
|
||||||
ffi.Pointer<ffi.Uint8> data,
|
|
||||||
int length,
|
|
||||||
int numInstances,
|
|
||||||
);
|
|
||||||
|
|
||||||
@ffi.Native<ffi.Int32 Function(ffi.Pointer<TSceneAsset>)>(isLeaf: true)
|
|
||||||
external int SceneAsset_getResourceUriCount(
|
|
||||||
ffi.Pointer<TSceneAsset> tSceneAsset,
|
|
||||||
);
|
|
||||||
|
|
||||||
@ffi.Native<
|
|
||||||
ffi.Pointer<ffi.Pointer<ffi.Char>> Function(
|
|
||||||
ffi.Pointer<TSceneAsset>)>(isLeaf: true)
|
|
||||||
external ffi.Pointer<ffi.Pointer<ffi.Char>> SceneAsset_getResourceUris(
|
|
||||||
ffi.Pointer<TSceneAsset> tSceneAsset,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
@ffi.Native<ffi.Pointer<TFilamentAsset> Function(ffi.Pointer<TSceneAsset>)>(
|
@ffi.Native<ffi.Pointer<TFilamentAsset> Function(ffi.Pointer<TSceneAsset>)>(
|
||||||
|
|||||||
@@ -20,6 +20,14 @@ EMSCRIPTEN_KEEPALIVE TFilamentAsset *GltfAssetLoader_load(
|
|||||||
EMSCRIPTEN_KEEPALIVE TMaterialInstance *GltfAssetLoader_getMaterialInstance(TRenderableManager *tRenderableManager, TFilamentAsset *tAsset);
|
EMSCRIPTEN_KEEPALIVE TMaterialInstance *GltfAssetLoader_getMaterialInstance(TRenderableManager *tRenderableManager, TFilamentAsset *tAsset);
|
||||||
EMSCRIPTEN_KEEPALIVE TMaterialProvider *GltfAssetLoader_getMaterialProvider(TGltfAssetLoader *tAssetLoader);
|
EMSCRIPTEN_KEEPALIVE TMaterialProvider *GltfAssetLoader_getMaterialProvider(TGltfAssetLoader *tAssetLoader);
|
||||||
|
|
||||||
|
EMSCRIPTEN_KEEPALIVE int32_t FilamentAsset_getResourceUriCount(
|
||||||
|
TFilamentAsset *tFilamentAsset
|
||||||
|
);
|
||||||
|
|
||||||
|
EMSCRIPTEN_KEEPALIVE const char* const* FilamentAsset_getResourceUris(
|
||||||
|
TFilamentAsset *tFilamentAsset
|
||||||
|
);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -22,38 +22,14 @@ extern "C"
|
|||||||
TMaterialInstance **materialInstances,
|
TMaterialInstance **materialInstances,
|
||||||
int materialInstanceCount
|
int materialInstanceCount
|
||||||
);
|
);
|
||||||
|
EMSCRIPTEN_KEEPALIVE TSceneAsset * SceneAsset_createFromFilamentAsset(
|
||||||
EMSCRIPTEN_KEEPALIVE TSceneAsset *SceneAsset_loadGlb(
|
|
||||||
TEngine *tEngine,
|
TEngine *tEngine,
|
||||||
TGltfAssetLoader *tAssetLoader,
|
TGltfAssetLoader *tAssetLoader,
|
||||||
TNameComponentManager *tNameComponentManager,
|
TNameComponentManager *tNameComponentManager,
|
||||||
const uint8_t *data,
|
TFilamentAsset *tFilamentAsset
|
||||||
size_t length,
|
|
||||||
size_t numInstances
|
|
||||||
);
|
);
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE TSceneAsset *SceneAsset_loadGltf(
|
|
||||||
TGltfAssetLoader *tAssetLoader,
|
|
||||||
TGltfResourceLoader *tResourceLoader,
|
|
||||||
TEngine *tEngine,
|
|
||||||
TNameComponentManager *tNameComponentManager,
|
|
||||||
const uint8_t *data,
|
|
||||||
size_t length,
|
|
||||||
size_t numInstances
|
|
||||||
);
|
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE int32_t SceneAsset_getResourceUriCount(
|
|
||||||
TSceneAsset *tSceneAsset
|
|
||||||
);
|
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE const char* const* SceneAsset_getResourceUris(
|
|
||||||
TSceneAsset *tSceneAsset
|
|
||||||
);
|
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE TFilamentAsset *SceneAsset_getFilamentAsset(TSceneAsset *tSceneAsset);
|
EMSCRIPTEN_KEEPALIVE TFilamentAsset *SceneAsset_getFilamentAsset(TSceneAsset *tSceneAsset);
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE TSceneAsset *SceneAsset_createGrid(TEngine *tEngine, TMaterial * tMaterial);
|
EMSCRIPTEN_KEEPALIVE TSceneAsset *SceneAsset_createGrid(TEngine *tEngine, TMaterial * tMaterial);
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE void SceneAsset_destroy(TSceneAsset *tSceneAsset);
|
EMSCRIPTEN_KEEPALIVE void SceneAsset_destroy(TSceneAsset *tSceneAsset);
|
||||||
EMSCRIPTEN_KEEPALIVE void SceneAsset_addToScene(TSceneAsset *tSceneAsset, TScene *tScene);
|
EMSCRIPTEN_KEEPALIVE void SceneAsset_addToScene(TSceneAsset *tSceneAsset, TScene *tScene);
|
||||||
EMSCRIPTEN_KEEPALIVE void SceneAsset_removeFromScene(TSceneAsset *tSceneAsset, TScene *tScene);
|
EMSCRIPTEN_KEEPALIVE void SceneAsset_removeFromScene(TSceneAsset *tSceneAsset, TScene *tScene);
|
||||||
@@ -68,8 +44,7 @@ extern "C"
|
|||||||
EMSCRIPTEN_KEEPALIVE size_t SceneAsset_getInstanceCount(TSceneAsset *tSceneAsset);
|
EMSCRIPTEN_KEEPALIVE size_t SceneAsset_getInstanceCount(TSceneAsset *tSceneAsset);
|
||||||
EMSCRIPTEN_KEEPALIVE TSceneAsset * SceneAsset_createInstance(TSceneAsset *asset, TMaterialInstance **materialInstances, int materialInstanceCount);
|
EMSCRIPTEN_KEEPALIVE TSceneAsset * SceneAsset_createInstance(TSceneAsset *asset, TMaterialInstance **materialInstances, int materialInstanceCount);
|
||||||
EMSCRIPTEN_KEEPALIVE Aabb3 SceneAsset_getBoundingBox(TSceneAsset *asset);
|
EMSCRIPTEN_KEEPALIVE Aabb3 SceneAsset_getBoundingBox(TSceneAsset *asset);
|
||||||
EMSCRIPTEN_KEEPALIVE Aabb3 SceneAsset_getBoundingBox(TSceneAsset *asset);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -92,14 +92,12 @@ namespace thermion
|
|||||||
FilamentRenderCallback make_render_callback_fn_pointer(FilamentRenderCallback);
|
FilamentRenderCallback make_render_callback_fn_pointer(FilamentRenderCallback);
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE void SceneAsset_destroyRenderThread(TSceneAsset *tSceneAsset, void (*onComplete)());
|
EMSCRIPTEN_KEEPALIVE void SceneAsset_destroyRenderThread(TSceneAsset *tSceneAsset, void (*onComplete)());
|
||||||
EMSCRIPTEN_KEEPALIVE void SceneAsset_loadGlbRenderThread(
|
EMSCRIPTEN_KEEPALIVE void SceneAsset_createFromFilamentAssetRenderThread(
|
||||||
TEngine *tEngine,
|
TEngine *tEngine,
|
||||||
TGltfAssetLoader *tAssetLoader,
|
TGltfAssetLoader *tAssetLoader,
|
||||||
TNameComponentManager *tNameComponentManager,
|
TNameComponentManager *tNameComponentManager,
|
||||||
uint8_t *data,
|
TFilamentAsset *tFilamentAsset,
|
||||||
size_t length,
|
void (*onComplete)(TSceneAsset *)
|
||||||
size_t numInstances,
|
|
||||||
void (*callback)(TSceneAsset *)
|
|
||||||
);
|
);
|
||||||
EMSCRIPTEN_KEEPALIVE void SceneAsset_createInstanceRenderThread(TSceneAsset *asset, TMaterialInstance **tMaterialInstances, int materialInstanceCount, void (*callback)(TSceneAsset *));
|
EMSCRIPTEN_KEEPALIVE void SceneAsset_createInstanceRenderThread(TSceneAsset *asset, TMaterialInstance **tMaterialInstances, int materialInstanceCount, void (*callback)(TSceneAsset *));
|
||||||
EMSCRIPTEN_KEEPALIVE void SceneAsset_createGeometryRenderThread(
|
EMSCRIPTEN_KEEPALIVE void SceneAsset_createGeometryRenderThread(
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ namespace thermion
|
|||||||
class GltfSceneAsset : public SceneAsset
|
class GltfSceneAsset : public SceneAsset
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
GltfSceneAsset(
|
GltfSceneAsset(
|
||||||
gltfio::FilamentAsset *asset,
|
gltfio::FilamentAsset *asset,
|
||||||
gltfio::AssetLoader *assetLoader,
|
gltfio::AssetLoader *assetLoader,
|
||||||
@@ -34,25 +35,13 @@ namespace thermion
|
|||||||
utils::NameComponentManager* ncm,
|
utils::NameComponentManager* ncm,
|
||||||
MaterialInstance **materialInstances = nullptr,
|
MaterialInstance **materialInstances = nullptr,
|
||||||
size_t materialInstanceCount = 0,
|
size_t materialInstanceCount = 0,
|
||||||
int instanceIndex = -1) : _asset(asset),
|
int instanceIndex = 0);
|
||||||
_assetLoader(assetLoader),
|
|
||||||
_engine(engine),
|
|
||||||
_ncm(ncm),
|
|
||||||
_materialInstances(materialInstances),
|
|
||||||
_materialInstanceCount(materialInstanceCount)
|
|
||||||
{
|
|
||||||
TRACE("Created GltfSceneAsset from FilamentAsset %d with %d reserved instances", asset, asset->getAssetInstanceCount());
|
|
||||||
}
|
|
||||||
|
|
||||||
~GltfSceneAsset();
|
~GltfSceneAsset();
|
||||||
|
|
||||||
SceneAsset *createInstance(MaterialInstance **materialInstances = nullptr, size_t materialInstanceCount = 0) override;
|
SceneAsset *createInstance(MaterialInstance **materialInstances = nullptr, size_t materialInstanceCount = 0) override;
|
||||||
|
|
||||||
void destroyInstance(SceneAsset *asset) override {
|
void destroyInstance(SceneAsset *asset) override;
|
||||||
auto it = std::remove_if(_instances.begin(), _instances.end(), [=](auto &sceneAsset)
|
|
||||||
{ return sceneAsset.get() == asset; });
|
|
||||||
_instances.erase(it, _instances.end());
|
|
||||||
};
|
|
||||||
|
|
||||||
SceneAssetType getType() override
|
SceneAssetType getType() override
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include <filament/Scene.h>
|
#include <filament/Scene.h>
|
||||||
|
|
||||||
#include <gltfio/FilamentAsset.h>
|
#include <gltfio/FilamentAsset.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <utils/Entity.h>
|
#include <utils/Entity.h>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include "c_api/TGizmo.h"
|
#include "c_api/TGizmo.h"
|
||||||
#include "c_api/TSceneAsset.h"
|
#include "c_api/TSceneAsset.h"
|
||||||
|
#include "c_api/TGltfAssetLoader.h"
|
||||||
#include "scene/Gizmo.hpp"
|
#include "scene/Gizmo.hpp"
|
||||||
#include "scene/GltfSceneAsset.hpp"
|
#include "scene/GltfSceneAsset.hpp"
|
||||||
#include "resources/translation_gizmo_glb.h"
|
#include "resources/translation_gizmo_glb.h"
|
||||||
@@ -20,56 +21,59 @@ namespace thermion
|
|||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE TGizmo *Gizmo_create(
|
EMSCRIPTEN_KEEPALIVE TGizmo *Gizmo_create(
|
||||||
TEngine *tEngine,
|
TEngine *tEngine,
|
||||||
TGltfAssetLoader *assetLoader,
|
TGltfAssetLoader *tAssetLoader,
|
||||||
TGltfResourceLoader *tGltfResourceLoader,
|
TGltfResourceLoader *tGltfResourceLoader,
|
||||||
TNameComponentManager *tNameComponentManager,
|
TNameComponentManager *tNameComponentManager,
|
||||||
TView *tView,
|
TView *tView,
|
||||||
TMaterial *tMaterial,
|
TMaterial *tMaterial,
|
||||||
TGizmoType tGizmoType) {
|
TGizmoType tGizmoType)
|
||||||
|
{
|
||||||
|
|
||||||
auto *engine = reinterpret_cast<Engine *>(tEngine);
|
auto *engine = reinterpret_cast<Engine *>(tEngine);
|
||||||
auto *view = reinterpret_cast<View *>(tView);
|
auto *view = reinterpret_cast<View *>(tView);
|
||||||
auto *material = reinterpret_cast<Material *>(tMaterial);
|
auto *material = reinterpret_cast<Material *>(tMaterial);
|
||||||
auto *gltfResourceLoader = reinterpret_cast<gltfio::ResourceLoader *>(tGltfResourceLoader);
|
auto *gltfResourceLoader = reinterpret_cast<gltfio::ResourceLoader *>(tGltfResourceLoader);
|
||||||
TSceneAsset *sceneAsset;
|
const uint8_t *data;
|
||||||
|
size_t size;
|
||||||
switch (tGizmoType)
|
switch (tGizmoType)
|
||||||
{
|
{
|
||||||
case GIZMO_TYPE_TRANSLATION:
|
case GIZMO_TYPE_TRANSLATION:
|
||||||
{
|
{
|
||||||
TRACE("Building translation gizmo");
|
TRACE("Building translation gizmo");
|
||||||
sceneAsset = SceneAsset_loadGlb(
|
data = TRANSLATION_GIZMO_GLB_TRANSLATION_GIZMO_DATA;
|
||||||
tEngine,
|
size = TRANSLATION_GIZMO_GLB_TRANSLATION_GIZMO_SIZE;
|
||||||
assetLoader,
|
|
||||||
tNameComponentManager,
|
|
||||||
TRANSLATION_GIZMO_GLB_TRANSLATION_GIZMO_DATA,
|
|
||||||
TRANSLATION_GIZMO_GLB_TRANSLATION_GIZMO_SIZE,
|
|
||||||
3
|
|
||||||
);
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
case GIZMO_TYPE_ROTATION:
|
|
||||||
{
|
|
||||||
TRACE("Building rotation gizmo");
|
|
||||||
sceneAsset = SceneAsset_loadGlb(
|
|
||||||
tEngine,
|
|
||||||
assetLoader,
|
|
||||||
tNameComponentManager,
|
|
||||||
ROTATION_GIZMO_GLB_ROTATION_GIZMO_DATA,
|
|
||||||
ROTATION_GIZMO_GLB_ROTATION_GIZMO_SIZE,
|
|
||||||
3
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
case GIZMO_TYPE_ROTATION:
|
||||||
|
{
|
||||||
|
TRACE("Building rotation gizmo");
|
||||||
|
data = ROTATION_GIZMO_GLB_ROTATION_GIZMO_DATA;
|
||||||
|
size = ROTATION_GIZMO_GLB_ROTATION_GIZMO_SIZE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto *tFilamentAsset = GltfAssetLoader_load(
|
||||||
|
tEngine,
|
||||||
|
tAssetLoader,
|
||||||
|
data,
|
||||||
|
size,
|
||||||
|
3);
|
||||||
|
auto *filamentAsset = reinterpret_cast<gltfio::FilamentAsset *>(tFilamentAsset);
|
||||||
|
auto *sceneAsset = SceneAsset_createFromFilamentAsset(
|
||||||
|
tEngine,
|
||||||
|
tAssetLoader,
|
||||||
|
tNameComponentManager,
|
||||||
|
tFilamentAsset);
|
||||||
|
|
||||||
auto *gltfSceneAsset = reinterpret_cast<GltfSceneAsset *>(sceneAsset);
|
auto *gltfSceneAsset = reinterpret_cast<GltfSceneAsset *>(sceneAsset);
|
||||||
auto *filamentAsset = gltfSceneAsset->getAsset();
|
|
||||||
gltfResourceLoader->loadResources(filamentAsset);
|
gltfResourceLoader->loadResources(filamentAsset);
|
||||||
auto *gizmo = new Gizmo(
|
auto *gizmo = new Gizmo(
|
||||||
gltfSceneAsset,
|
gltfSceneAsset,
|
||||||
engine,
|
engine,
|
||||||
view,
|
view,
|
||||||
material
|
material);
|
||||||
);
|
|
||||||
return reinterpret_cast<TGizmo *>(gizmo);
|
return reinterpret_cast<TGizmo *>(gizmo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -114,6 +114,20 @@ EMSCRIPTEN_KEEPALIVE TMaterialProvider *GltfAssetLoader_getMaterialProvider(TGlt
|
|||||||
return reinterpret_cast<TMaterialProvider *>(&materialProvider);
|
return reinterpret_cast<TMaterialProvider *>(&materialProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EMSCRIPTEN_KEEPALIVE int32_t FilamentAsset_getResourceUriCount(
|
||||||
|
TFilamentAsset *tFilamentAsset
|
||||||
|
) {
|
||||||
|
auto *filamentAsset = reinterpret_cast<gltfio::FilamentAsset *>(tFilamentAsset);
|
||||||
|
return filamentAsset->getResourceUriCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
EMSCRIPTEN_KEEPALIVE const char* const* FilamentAsset_getResourceUris(
|
||||||
|
TFilamentAsset *tFilamentAsset
|
||||||
|
) {
|
||||||
|
auto *filamentAsset = reinterpret_cast<gltfio::FilamentAsset *>(tFilamentAsset);
|
||||||
|
return filamentAsset->getResourceUris();
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,17 +66,14 @@ extern "C"
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE TSceneAsset *SceneAsset_loadGlb(
|
EMSCRIPTEN_KEEPALIVE TSceneAsset *SceneAsset_createFromFilamentAsset(
|
||||||
TEngine *tEngine,
|
TEngine *tEngine,
|
||||||
TGltfAssetLoader *tAssetLoader,
|
TGltfAssetLoader *tAssetLoader,
|
||||||
TNameComponentManager *tNameComponentManager,
|
TNameComponentManager *tNameComponentManager,
|
||||||
const uint8_t *data,
|
TFilamentAsset *tFilamentAsset
|
||||||
size_t length,
|
|
||||||
size_t numInstances
|
|
||||||
) {
|
) {
|
||||||
auto *engine = reinterpret_cast<filament::Engine *>(tEngine);
|
auto *engine = reinterpret_cast<filament::Engine *>(tEngine);
|
||||||
auto *nameComponentManager = reinterpret_cast<utils::NameComponentManager *>(tNameComponentManager);
|
auto *nameComponentManager = reinterpret_cast<utils::NameComponentManager *>(tNameComponentManager);
|
||||||
auto *tFilamentAsset = GltfAssetLoader_load(tEngine, tAssetLoader, data, length, numInstances);
|
|
||||||
auto *filamentAsset = reinterpret_cast<filament::gltfio::FilamentAsset *>(tFilamentAsset);
|
auto *filamentAsset = reinterpret_cast<filament::gltfio::FilamentAsset *>(tFilamentAsset);
|
||||||
|
|
||||||
auto *assetLoader = reinterpret_cast<filament::gltfio::AssetLoader *>(tAssetLoader);
|
auto *assetLoader = reinterpret_cast<filament::gltfio::AssetLoader *>(tAssetLoader);
|
||||||
@@ -89,33 +86,6 @@ extern "C"
|
|||||||
|
|
||||||
return reinterpret_cast<TSceneAsset *>(sceneAsset);
|
return reinterpret_cast<TSceneAsset *>(sceneAsset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE int32_t SceneAsset_getResourceUriCount(
|
|
||||||
TSceneAsset *tSceneAsset
|
|
||||||
) {
|
|
||||||
auto sceneAsset = reinterpret_cast<SceneAsset *>(tSceneAsset);
|
|
||||||
if(sceneAsset->getType() != SceneAsset::SceneAssetType::Gltf) {
|
|
||||||
Log("Error - not a gltf asset");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
auto gltfAsset = reinterpret_cast<GltfSceneAsset *>(tSceneAsset);
|
|
||||||
auto *filamentAsset = gltfAsset->getAsset();
|
|
||||||
return filamentAsset->getResourceUriCount();
|
|
||||||
}
|
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE const char* const* SceneAsset_getResourceUris(
|
|
||||||
TSceneAsset *tSceneAsset
|
|
||||||
) {
|
|
||||||
auto sceneAsset = reinterpret_cast<SceneAsset *>(tSceneAsset);
|
|
||||||
if(sceneAsset->getType() != SceneAsset::SceneAssetType::Gltf) {
|
|
||||||
Log("Error - not a gltf asset");
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
auto gltfAsset = reinterpret_cast<GltfSceneAsset *>(tSceneAsset);
|
|
||||||
auto *filamentAsset = gltfAsset->getAsset();
|
|
||||||
return filamentAsset->getResourceUris();
|
|
||||||
}
|
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE TFilamentAsset *SceneAsset_getFilamentAsset(TSceneAsset *tSceneAsset) {
|
EMSCRIPTEN_KEEPALIVE TFilamentAsset *SceneAsset_getFilamentAsset(TSceneAsset *tSceneAsset) {
|
||||||
auto sceneAsset = reinterpret_cast<SceneAsset *>(tSceneAsset);
|
auto sceneAsset = reinterpret_cast<SceneAsset *>(tSceneAsset);
|
||||||
@@ -130,7 +100,6 @@ extern "C"
|
|||||||
return reinterpret_cast<TFilamentAsset *>(filamentAsset);
|
return reinterpret_cast<TFilamentAsset *>(filamentAsset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE TSceneAsset *SceneAsset_createGrid(TEngine *tEngine, TMaterial* tMaterial) {
|
EMSCRIPTEN_KEEPALIVE TSceneAsset *SceneAsset_createGrid(TEngine *tEngine, TMaterial* tMaterial) {
|
||||||
auto *engine = reinterpret_cast<filament::Engine *>(tEngine);
|
auto *engine = reinterpret_cast<filament::Engine *>(tEngine);
|
||||||
auto *material = reinterpret_cast<filament::Material *>(tMaterial);
|
auto *material = reinterpret_cast<filament::Material *>(tMaterial);
|
||||||
@@ -234,6 +203,11 @@ extern "C"
|
|||||||
return reinterpret_cast<TSceneAsset*>(instance);
|
return reinterpret_cast<TSceneAsset*>(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EMSCRIPTEN_KEEPALIVE size_t SceneAsset_getInstanceCount(TSceneAsset *tSceneAsset) {
|
||||||
|
auto *asset = reinterpret_cast<SceneAsset*>(tSceneAsset);
|
||||||
|
return asset->getInstanceCount();
|
||||||
|
}
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE TSceneAsset *SceneAsset_createInstance(TSceneAsset *tSceneAsset, TMaterialInstance **tMaterialInstances, int materialInstanceCount)
|
EMSCRIPTEN_KEEPALIVE TSceneAsset *SceneAsset_createInstance(TSceneAsset *tSceneAsset, TMaterialInstance **tMaterialInstances, int materialInstanceCount)
|
||||||
{
|
{
|
||||||
auto *materialInstances = reinterpret_cast<MaterialInstance **>(tMaterialInstances);
|
auto *materialInstances = reinterpret_cast<MaterialInstance **>(tMaterialInstances);
|
||||||
|
|||||||
@@ -415,23 +415,6 @@ extern "C"
|
|||||||
auto fut = _renderThread->add_task(lambda);
|
auto fut = _renderThread->add_task(lambda);
|
||||||
}
|
}
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE void SceneAsset_loadGlbRenderThread(
|
|
||||||
TEngine *tEngine,
|
|
||||||
TGltfAssetLoader *tAssetLoader,
|
|
||||||
TNameComponentManager *tNameComponentManager,
|
|
||||||
uint8_t *data,
|
|
||||||
size_t length,
|
|
||||||
size_t numInstances,
|
|
||||||
void (*callback)(TSceneAsset *)
|
|
||||||
) {
|
|
||||||
std::packaged_task<void()> lambda(
|
|
||||||
[=]
|
|
||||||
{
|
|
||||||
auto sceneAsset = SceneAsset_loadGlb(tEngine, tAssetLoader, tNameComponentManager, data, length, numInstances);
|
|
||||||
callback(sceneAsset);
|
|
||||||
});
|
|
||||||
auto fut = _renderThread->add_task(lambda);
|
|
||||||
}
|
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE void SceneAsset_createGeometryRenderThread(
|
EMSCRIPTEN_KEEPALIVE void SceneAsset_createGeometryRenderThread(
|
||||||
TEngine *tEngine,
|
TEngine *tEngine,
|
||||||
@@ -457,6 +440,22 @@ extern "C"
|
|||||||
auto fut = _renderThread->add_task(lambda);
|
auto fut = _renderThread->add_task(lambda);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EMSCRIPTEN_KEEPALIVE void SceneAsset_createFromFilamentAssetRenderThread(
|
||||||
|
TEngine *tEngine,
|
||||||
|
TGltfAssetLoader *tAssetLoader,
|
||||||
|
TNameComponentManager *tNameComponentManager,
|
||||||
|
TFilamentAsset *tFilamentAsset,
|
||||||
|
void (*onComplete)(TSceneAsset *)
|
||||||
|
) {
|
||||||
|
std::packaged_task<void()> lambda(
|
||||||
|
[=]
|
||||||
|
{
|
||||||
|
auto sceneAsset = SceneAsset_createFromFilamentAsset(tEngine, tAssetLoader, tNameComponentManager, tFilamentAsset);
|
||||||
|
onComplete(sceneAsset);
|
||||||
|
});
|
||||||
|
auto fut = _renderThread->add_task(lambda);
|
||||||
|
}
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE void SceneAsset_createInstanceRenderThread(
|
EMSCRIPTEN_KEEPALIVE void SceneAsset_createInstanceRenderThread(
|
||||||
TSceneAsset *asset, TMaterialInstance **tMaterialInstances,
|
TSceneAsset *asset, TMaterialInstance **tMaterialInstances,
|
||||||
int materialInstanceCount,
|
int materialInstanceCount,
|
||||||
|
|||||||
@@ -3,9 +3,49 @@
|
|||||||
#include "scene/GltfSceneAssetInstance.hpp"
|
#include "scene/GltfSceneAssetInstance.hpp"
|
||||||
#include "gltfio/FilamentInstance.h"
|
#include "gltfio/FilamentInstance.h"
|
||||||
#include "Log.hpp"
|
#include "Log.hpp"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <filament/Engine.h>
|
||||||
|
#include <filament/RenderableManager.h>
|
||||||
|
#include <filament/VertexBuffer.h>
|
||||||
|
#include <filament/IndexBuffer.h>
|
||||||
|
#include <gltfio/AssetLoader.h>
|
||||||
|
#include <gltfio/FilamentAsset.h>
|
||||||
|
#include <gltfio/MaterialProvider.h>
|
||||||
|
|
||||||
|
#include <utils/NameComponentManager.h>
|
||||||
|
|
||||||
|
#include "scene/GltfSceneAssetInstance.hpp"
|
||||||
|
#include "components/AnimationComponentManager.hpp"
|
||||||
|
#include "components/CollisionComponentManager.hpp"
|
||||||
|
|
||||||
|
#include "scene/SceneAsset.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace thermion
|
namespace thermion
|
||||||
{
|
{
|
||||||
|
|
||||||
|
GltfSceneAsset::GltfSceneAsset(
|
||||||
|
gltfio::FilamentAsset *asset,
|
||||||
|
gltfio::AssetLoader *assetLoader,
|
||||||
|
Engine *engine,
|
||||||
|
utils::NameComponentManager* ncm,
|
||||||
|
MaterialInstance **materialInstances,
|
||||||
|
size_t materialInstanceCount,
|
||||||
|
int instanceIndex) : _asset(asset),
|
||||||
|
_assetLoader(assetLoader),
|
||||||
|
_engine(engine),
|
||||||
|
_ncm(ncm),
|
||||||
|
_materialInstances(materialInstances),
|
||||||
|
_materialInstanceCount(materialInstanceCount)
|
||||||
|
{
|
||||||
|
createInstance();
|
||||||
|
TRACE("Created GltfSceneAsset from FilamentAsset %d with %d reserved instances", asset, asset->getAssetInstanceCount());
|
||||||
|
}
|
||||||
|
|
||||||
GltfSceneAsset::~GltfSceneAsset()
|
GltfSceneAsset::~GltfSceneAsset()
|
||||||
{
|
{
|
||||||
_instances.clear();
|
_instances.clear();
|
||||||
@@ -14,6 +54,13 @@ namespace thermion
|
|||||||
TRACE("Destroyed");
|
TRACE("Destroyed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GltfSceneAsset::destroyInstance(SceneAsset *asset) {
|
||||||
|
auto it = std::remove_if(_instances.begin(), _instances.end(), [=](auto &sceneAsset)
|
||||||
|
{ return sceneAsset.get() == asset; });
|
||||||
|
_instances.erase(it, _instances.end());
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
SceneAsset *GltfSceneAsset::createInstance(MaterialInstance **materialInstances, size_t materialInstanceCount)
|
SceneAsset *GltfSceneAsset::createInstance(MaterialInstance **materialInstances, size_t materialInstanceCount)
|
||||||
{
|
{
|
||||||
auto instanceNumber = _instances.size();
|
auto instanceNumber = _instances.size();
|
||||||
|
|||||||
40
thermion_dart/test/instancing_tests.dart
Normal file
40
thermion_dart/test/instancing_tests.dart
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
@Timeout(const Duration(seconds: 600))
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
import 'package:vector_math/vector_math_64.dart';
|
||||||
|
import 'helpers.dart';
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
final testHelper = TestHelper("instancing");
|
||||||
|
await testHelper.setup();
|
||||||
|
test('gltf assets always create one instance', () async {
|
||||||
|
await testHelper.withViewer((viewer) async {
|
||||||
|
var asset =
|
||||||
|
await viewer.loadGltf("file://${testHelper.testDir}/assets/cube.glb");
|
||||||
|
expect(await asset.getInstanceCount(), 1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('create 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);
|
||||||
|
|
||||||
|
var asset = await viewer.loadGltf(
|
||||||
|
"file://${testHelper.testDir}/assets/cube.glb",
|
||||||
|
numInstances: 2);
|
||||||
|
|
||||||
|
await testHelper.capture(viewer.view, "gltf");
|
||||||
|
var instance = await asset.createInstance();
|
||||||
|
await viewer.addToScene(instance);
|
||||||
|
print(instance.entity);
|
||||||
|
print(await instance.getChildEntities());
|
||||||
|
|
||||||
|
await instance.setTransform(Matrix4.translation(Vector3(1, 0, 0)));
|
||||||
|
await testHelper.capture(viewer.view, "gltf_with_instance");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user