refactoring
This commit is contained in:
@@ -33,18 +33,6 @@ namespace thermion
|
||||
|
||||
#endif
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE TGltfResourceLoader *GltfResourceLoader_create(TEngine *tEngine) {
|
||||
auto *engine = reinterpret_cast<Engine *>(tEngine);
|
||||
auto *gltfResourceLoader = new gltfio::ResourceLoader({.engine = engine,
|
||||
.normalizeSkinningWeights = true});
|
||||
auto stbDecoder = gltfio::createStbProvider(engine);
|
||||
auto ktxDecoder = gltfio::createKtx2Provider(engine);
|
||||
gltfResourceLoader->addTextureProvider("image/ktx2", ktxDecoder);
|
||||
gltfResourceLoader->addTextureProvider("image/png", stbDecoder);
|
||||
gltfResourceLoader->addTextureProvider("image/jpeg", stbDecoder);
|
||||
|
||||
return reinterpret_cast<TGltfResourceLoader *>(gltfResourceLoader);
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE TGltfAssetLoader *GltfAssetLoader_create(TEngine *tEngine, TMaterialProvider *tMaterialProvider) {
|
||||
auto *engine = reinterpret_cast<filament::Engine *>(tEngine);
|
||||
@@ -66,18 +54,23 @@ EMSCRIPTEN_KEEPALIVE TGltfAssetLoader *GltfAssetLoader_create(TEngine *tEngine,
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE TFilamentAsset *GltfAssetLoader_load(
|
||||
TEngine *tEngine,
|
||||
TGltfAssetLoader *tAssetLoader,
|
||||
TGltfResourceLoader *tGltfResourceLoader,
|
||||
uint8_t *data,
|
||||
size_t length,
|
||||
uint8_t numInstances)
|
||||
{
|
||||
auto *engine = reinterpret_cast<filament::Engine *>(tEngine);
|
||||
auto *assetLoader = reinterpret_cast<gltfio::AssetLoader *>(tAssetLoader);
|
||||
auto *resourceLoader = reinterpret_cast<gltfio::ResourceLoader *>(tGltfResourceLoader);
|
||||
|
||||
std::vector<gltfio::FilamentInstance *> instances(numInstances);
|
||||
gltfio::FilamentAsset *asset;
|
||||
|
||||
gltfio::FilamentAsset *asset = assetLoader->createInstancedAsset((const uint8_t *)data, length, instances.data(), numInstances);
|
||||
if(numInstances > 1) {
|
||||
std::vector<gltfio::FilamentInstance *> instances(numInstances);
|
||||
asset = assetLoader->createInstancedAsset((const uint8_t *)data, length, instances.data(), numInstances);
|
||||
} else {
|
||||
asset = assetLoader->createAsset((const uint8_t *)data, length);
|
||||
}
|
||||
|
||||
if (!asset)
|
||||
{
|
||||
@@ -85,11 +78,15 @@ EMSCRIPTEN_KEEPALIVE TFilamentAsset *GltfAssetLoader_load(
|
||||
return std::nullptr_t();
|
||||
}
|
||||
|
||||
if (!resourceLoader->loadResources(asset))
|
||||
{
|
||||
Log("Unknown error loading glb asset");
|
||||
return std::nullptr_t();
|
||||
const char *const *const resourceUris = asset->getResourceUris();
|
||||
const size_t resourceUriCount = asset->getResourceUriCount();
|
||||
|
||||
Log("glTF asset : %d resource URIs, %d instances", resourceUriCount, numInstances);
|
||||
|
||||
for(int i = 0; i < resourceUriCount; i++) {
|
||||
Log("%s", resourceUris[i]);
|
||||
}
|
||||
|
||||
return reinterpret_cast<TFilamentAsset *>(asset);
|
||||
}
|
||||
|
||||
|
||||
75
thermion_dart/native/src/c_api/TGltfResourceLoader.cpp
Normal file
75
thermion_dart/native/src/c_api/TGltfResourceLoader.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#include "c_api/TGltfResourceLoader.h"
|
||||
|
||||
#include <filament/Engine.h>
|
||||
#include <filament/Fence.h>
|
||||
#include <filament/Material.h>
|
||||
#include <filament/RenderableManager.h>
|
||||
#include <filament/Scene.h>
|
||||
#include <filament/Skybox.h>
|
||||
#include <filament/Texture.h>
|
||||
#include <filament/TextureSampler.h>
|
||||
#include <filament/TransformManager.h>
|
||||
#include <filament/View.h>
|
||||
|
||||
#include <gltfio/Animator.h>
|
||||
#include <gltfio/AssetLoader.h>
|
||||
#include <gltfio/FilamentAsset.h>
|
||||
#include <gltfio/ResourceLoader.h>
|
||||
#include <gltfio/TextureProvider.h>
|
||||
#include <gltfio/math.h>
|
||||
#include <gltfio/materials/uberarchive.h>
|
||||
|
||||
#include <utils/EntityManager.h>
|
||||
#include <utils/NameComponentManager.h>
|
||||
|
||||
#include "Log.hpp"
|
||||
|
||||
#ifdef __cplusplus
|
||||
namespace thermion
|
||||
{
|
||||
extern "C"
|
||||
{
|
||||
using namespace filament;
|
||||
|
||||
#endif
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE TGltfResourceLoader *GltfResourceLoader_create(TEngine *tEngine, const char *relativeResourcePath) {
|
||||
auto *engine = reinterpret_cast<Engine *>(tEngine);
|
||||
auto *gltfResourceLoader = new gltfio::ResourceLoader({
|
||||
.engine = engine,
|
||||
.gltfPath = relativeResourcePath
|
||||
});
|
||||
auto stbDecoder = gltfio::createStbProvider(engine);
|
||||
auto ktxDecoder = gltfio::createKtx2Provider(engine);
|
||||
gltfResourceLoader->addTextureProvider("image/ktx2", ktxDecoder);
|
||||
gltfResourceLoader->addTextureProvider("image/png", stbDecoder);
|
||||
gltfResourceLoader->addTextureProvider("image/jpeg", stbDecoder);
|
||||
|
||||
return reinterpret_cast<TGltfResourceLoader *>(gltfResourceLoader);
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void GltfResourceLoader_destroy(TEngine *tEngine, TGltfResourceLoader *tGltfResourceLoader) {
|
||||
auto *gltfResourceLoader = reinterpret_cast<gltfio::ResourceLoader *>(tGltfResourceLoader);
|
||||
delete gltfResourceLoader;
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void GltfResourceLoader_addResourceData(TGltfResourceLoader *tGltfResourceLoader, const char *uri, uint8_t *data, size_t length) {
|
||||
TRACE("Adding data (length %d) for glTF resource URI %s", length, uri);
|
||||
auto *gltfResourceLoader = reinterpret_cast<gltfio::ResourceLoader *>(tGltfResourceLoader);
|
||||
for(int i = 0; i < 8; i++) {
|
||||
std::cout << static_cast<uint32_t>(data[i]) << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
gltfResourceLoader->addResourceData(uri, { data, length});
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE bool GltfResourceLoader_loadResources(TGltfResourceLoader *tGltfResourceLoader, TFilamentAsset *tFilamentAsset) {
|
||||
auto *gltfResourceLoader = reinterpret_cast<gltfio::ResourceLoader *>(tGltfResourceLoader);
|
||||
auto *filamentAsset = reinterpret_cast<gltfio::FilamentAsset *>(tFilamentAsset);
|
||||
return gltfResourceLoader->loadResources(filamentAsset);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -67,9 +67,8 @@ extern "C"
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE TSceneAsset *SceneAsset_loadGlb(
|
||||
TGltfAssetLoader *tAssetLoader,
|
||||
TGltfResourceLoader *tResourceLoader,
|
||||
TEngine *tEngine,
|
||||
TGltfAssetLoader *tAssetLoader,
|
||||
TNameComponentManager *tNameComponentManager,
|
||||
uint8_t *data,
|
||||
size_t length,
|
||||
@@ -77,20 +76,61 @@ extern "C"
|
||||
) {
|
||||
auto *engine = reinterpret_cast<filament::Engine *>(tEngine);
|
||||
auto *nameComponentManager = reinterpret_cast<utils::NameComponentManager *>(tNameComponentManager);
|
||||
auto *tFilamentAsset = GltfAssetLoader_load(tAssetLoader, tResourceLoader, data, length, numInstances);
|
||||
auto *tFilamentAsset = GltfAssetLoader_load(tEngine, tAssetLoader, data, length, numInstances);
|
||||
auto *filamentAsset = reinterpret_cast<filament::gltfio::FilamentAsset *>(tFilamentAsset);
|
||||
|
||||
auto *assetLoader = reinterpret_cast<filament::gltfio::AssetLoader *>(tAssetLoader);
|
||||
auto *resourceLoader = reinterpret_cast<filament::gltfio::ResourceLoader *>(tResourceLoader);
|
||||
auto *sceneAsset = new GltfSceneAsset(
|
||||
filamentAsset,
|
||||
assetLoader,
|
||||
engine,
|
||||
nameComponentManager
|
||||
);
|
||||
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) {
|
||||
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();
|
||||
TRACE("SceneAsset %d FilamentAsset %d", sceneAsset, filamentAsset);
|
||||
return reinterpret_cast<TFilamentAsset *>(filamentAsset);
|
||||
}
|
||||
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE TSceneAsset *SceneAsset_createGrid(TEngine *tEngine, TMaterial* tMaterial) {
|
||||
auto *engine = reinterpret_cast<filament::Engine *>(tEngine);
|
||||
auto *material = reinterpret_cast<filament::Material *>(tMaterial);
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "c_api/TAnimationManager.h"
|
||||
#include "c_api/TEngine.h"
|
||||
#include "c_api/TGltfAssetLoader.h"
|
||||
#include "c_api/TGltfResourceLoader.h"
|
||||
#include "c_api/TRenderer.h"
|
||||
#include "c_api/TRenderTicker.h"
|
||||
#include "c_api/TRenderTarget.h"
|
||||
@@ -18,12 +19,9 @@
|
||||
#include "c_api/TView.h"
|
||||
#include "c_api/ThermionDartRenderThreadApi.h"
|
||||
|
||||
|
||||
#include "rendering/RenderLoop.hpp"
|
||||
#include "Log.hpp"
|
||||
|
||||
#include "ThreadPool.hpp"
|
||||
|
||||
using namespace thermion;
|
||||
using namespace std::chrono_literals;
|
||||
#include <time.h>
|
||||
@@ -360,9 +358,8 @@ extern "C"
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void SceneAsset_loadGlbRenderThread(
|
||||
TGltfAssetLoader *tAssetLoader,
|
||||
TGltfResourceLoader *tResourceLoader,
|
||||
TEngine *tEngine,
|
||||
TGltfAssetLoader *tAssetLoader,
|
||||
TNameComponentManager *tNameComponentManager,
|
||||
uint8_t *data,
|
||||
size_t length,
|
||||
@@ -372,7 +369,7 @@ extern "C"
|
||||
std::packaged_task<void()> lambda(
|
||||
[=]
|
||||
{
|
||||
auto sceneAsset = SceneAsset_loadGlb(tAssetLoader, tResourceLoader, tEngine, tNameComponentManager, data, length, numInstances);
|
||||
auto sceneAsset = SceneAsset_loadGlb(tEngine, tAssetLoader, tNameComponentManager, data, length, numInstances);
|
||||
callback(sceneAsset);
|
||||
});
|
||||
auto fut = _rl->add_task(lambda);
|
||||
@@ -875,19 +872,54 @@ extern "C"
|
||||
auto fut = _rl->add_task(lambda);
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void GltfResourceLoader_createRenderThread(TEngine *tEngine, void (*callback)(TGltfResourceLoader *)) {
|
||||
EMSCRIPTEN_KEEPALIVE void GltfResourceLoader_createRenderThread(TEngine *tEngine, const char* relativeResourcePath, void (*callback)(TGltfResourceLoader *)) {
|
||||
std::packaged_task<void()> lambda(
|
||||
[=]() mutable
|
||||
{
|
||||
auto loader = GltfResourceLoader_create(tEngine);
|
||||
auto loader = GltfResourceLoader_create(tEngine, relativeResourcePath);
|
||||
callback(loader);
|
||||
});
|
||||
auto fut = _rl->add_task(lambda);
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void GltfResourceLoader_destroyRenderThread(TEngine *tEngine, TGltfResourceLoader *tResourceLoader, void (*callback)()) {
|
||||
std::packaged_task<void()> lambda(
|
||||
[=]() mutable
|
||||
{
|
||||
GltfResourceLoader_destroy(tEngine, tResourceLoader);
|
||||
callback();
|
||||
});
|
||||
auto fut = _rl->add_task(lambda);
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void GltfResourceLoader_loadResourcesRenderThread(TGltfResourceLoader *tGltfResourceLoader, TFilamentAsset *tFilamentAsset, void (*callback)(bool)) {
|
||||
std::packaged_task<void()> lambda(
|
||||
[=]() mutable
|
||||
{
|
||||
auto result = GltfResourceLoader_loadResources(tGltfResourceLoader, tFilamentAsset);
|
||||
callback(result);
|
||||
});
|
||||
auto fut = _rl->add_task(lambda);
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void GltfResourceLoader_addResourceDataRenderThread(
|
||||
TGltfResourceLoader *tGltfResourceLoader,
|
||||
const char *uri,
|
||||
uint8_t *data,
|
||||
size_t length,
|
||||
void (*callback)()) {
|
||||
std::packaged_task<void()> lambda(
|
||||
[=]() mutable
|
||||
{
|
||||
GltfResourceLoader_addResourceData(tGltfResourceLoader, uri, data, length);
|
||||
callback();
|
||||
});
|
||||
auto fut = _rl->add_task(lambda);
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void GltfAssetLoader_loadRenderThread(
|
||||
TEngine *tEngine,
|
||||
TGltfAssetLoader *tAssetLoader,
|
||||
TGltfResourceLoader *tResourceLoader,
|
||||
uint8_t *data,
|
||||
size_t length,
|
||||
uint8_t numInstances,
|
||||
@@ -896,7 +928,7 @@ extern "C"
|
||||
std::packaged_task<void()> lambda(
|
||||
[=]() mutable
|
||||
{
|
||||
auto loader = GltfAssetLoader_load(tAssetLoader, tResourceLoader, data, length, numInstances);
|
||||
auto loader = GltfAssetLoader_load(tEngine, tAssetLoader, data, length, numInstances);
|
||||
callback(loader);
|
||||
});
|
||||
auto fut = _rl->add_task(lambda);
|
||||
|
||||
Reference in New Issue
Block a user