diff --git a/thermion_dart/native/include/c_api/APIBoundaryTypes.h b/thermion_dart/native/include/c_api/APIBoundaryTypes.h index 96428e0d..486d9fb8 100644 --- a/thermion_dart/native/include/c_api/APIBoundaryTypes.h +++ b/thermion_dart/native/include/c_api/APIBoundaryTypes.h @@ -24,6 +24,7 @@ extern "C" typedef struct TGizmo TGizmo; typedef struct TScene TScene; typedef struct TSkybox TSkybox; + typedef struct TIndirectLight TIndirectLight; typedef struct TTransformManager TTransformManager; typedef struct TAnimationManager TAnimationManager; typedef struct TCollisionComponentManager TCollisionComponentManager; diff --git a/thermion_dart/native/include/c_api/TEngine.h b/thermion_dart/native/include/c_api/TEngine.h index f117e857..02bca03c 100644 --- a/thermion_dart/native/include/c_api/TEngine.h +++ b/thermion_dart/native/include/c_api/TEngine.h @@ -44,6 +44,8 @@ EMSCRIPTEN_KEEPALIVE TMaterial *Engine_buildMaterial(TEngine *tEngine, const uin EMSCRIPTEN_KEEPALIVE void Engine_destroyMaterial(TEngine *tEngine, TMaterial *tMaterial); EMSCRIPTEN_KEEPALIVE TScene *Engine_createScene(TEngine *tEngine); EMSCRIPTEN_KEEPALIVE TSkybox *Engine_buildSkybox(TEngine *tEngine, uint8_t* ktxData, size_t length, void(*onTextureUploadComplete)()); +EMSCRIPTEN_KEEPALIVE TIndirectLight *Engine_buildIndirectLight(TEngine *tEngine, uint8_t* ktxData, size_t length, float intensity, void(*onTextureUploadComplete)()); + diff --git a/thermion_dart/native/include/c_api/TGltfAssetLoader.h b/thermion_dart/native/include/c_api/TGltfAssetLoader.h index a9e7d694..8a804aa8 100644 --- a/thermion_dart/native/include/c_api/TGltfAssetLoader.h +++ b/thermion_dart/native/include/c_api/TGltfAssetLoader.h @@ -22,6 +22,7 @@ EMSCRIPTEN_KEEPALIVE TFilamentAsset *GltfAssetLoader_load( size_t length, uint8_t numInstances ); +EMSCRIPTEN_KEEPALIVE TMaterialInstance *GltfAssetLoader_getMaterialInstance(TRenderableManager *tRenderableManager, TFilamentAsset *tAsset); #ifdef __cplusplus } diff --git a/thermion_dart/native/include/c_api/TMaterialProvider.h b/thermion_dart/native/include/c_api/TMaterialProvider.h index 9e313466..35679a7c 100644 --- a/thermion_dart/native/include/c_api/TMaterialProvider.h +++ b/thermion_dart/native/include/c_api/TMaterialProvider.h @@ -7,6 +7,8 @@ extern "C" { #endif + + // EMSCRIPTEN_KEEPALIVE TMaterialProvider *MaterialProvider_create(TEngine *tEngine, uint8_t* data, size_t length); EMSCRIPTEN_KEEPALIVE TMaterialInstance *MaterialProvider_createMaterialInstance(TMaterialProvider *provider, TMaterialKey *key); #ifdef __cplusplus diff --git a/thermion_dart/native/include/c_api/TScene.h b/thermion_dart/native/include/c_api/TScene.h index 85561976..2bfc2dea 100644 --- a/thermion_dart/native/include/c_api/TScene.h +++ b/thermion_dart/native/include/c_api/TScene.h @@ -17,6 +17,7 @@ extern "C" EMSCRIPTEN_KEEPALIVE void Scene_addEntity(TScene* tScene, EntityId entityId); EMSCRIPTEN_KEEPALIVE void Scene_setSkybox(TScene* tScene, TSkybox *skybox); +EMSCRIPTEN_KEEPALIVE void Scene_setIndirectLight(TScene* tScene, TIndirectLight *tIndirectLight); EMSCRIPTEN_KEEPALIVE void Scene_addFilamentAsset(TScene* tScene, TFilamentAsset *asset); diff --git a/thermion_dart/native/include/c_api/ThermionDartRenderThreadApi.h b/thermion_dart/native/include/c_api/ThermionDartRenderThreadApi.h index e36fc23f..6f64ddca 100644 --- a/thermion_dart/native/include/c_api/ThermionDartRenderThreadApi.h +++ b/thermion_dart/native/include/c_api/ThermionDartRenderThreadApi.h @@ -73,6 +73,7 @@ namespace thermion EMSCRIPTEN_KEEPALIVE void Engine_destroyFenceRenderThread(TEngine *tEngine, TFence *tFence, void (*onComplete)()); EMSCRIPTEN_KEEPALIVE void Engine_flushAndWaitRenderThead(TEngine *tEngine, void (*onComplete)()); EMSCRIPTEN_KEEPALIVE void Engine_buildSkyboxRenderThread(TEngine *tEngine, uint8_t *skyboxData, size_t length, void (*onComplete)(TSkybox *), void (*onTextureUploadComplete)()); + EMSCRIPTEN_KEEPALIVE void Engine_buildIndirectLightRenderThread(TEngine *tEngine, uint8_t *iblData, size_t length, float intensity, void (*onComplete)(TIndirectLight *), void (*onTextureUploadComplete)()); EMSCRIPTEN_KEEPALIVE void Renderer_setClearOptionsRenderThread(TRenderer *tRenderer, double clearR, double clearG, double clearB, double clearA, uint8_t clearStencil, bool clear, bool discard, void (*onComplete)()); EMSCRIPTEN_KEEPALIVE void Renderer_beginFrameRenderThread(TRenderer *tRenderer, TSwapChain *tSwapChain, uint64_t frameTimeInNanos, void (*onComplete)(bool)); @@ -198,7 +199,23 @@ namespace thermion EMSCRIPTEN_KEEPALIVE void Image_getChannelsRenderThread(TLinearImage *tLinearImage, void (*onComplete)(uint32_t)); // Texture methods - EMSCRIPTEN_KEEPALIVE void Texture_loadImageRenderThread(TEngine *tEngine, TTexture *tTexture, TLinearImage *tImage, TPixelDataFormat bufferFormat, TPixelDataType pixelDataType, void (*onComplete)(bool)); + EMSCRIPTEN_KEEPALIVE void Texture_buildRenderThread(TEngine *engine, + uint32_t width, + uint32_t height, + uint32_t depth, + uint8_t levels, + uint16_t tUsage, + intptr_t import, + TTextureSamplerType sampler, + TTextureFormat format, void (*onComplete)(TTexture *)); + EMSCRIPTEN_KEEPALIVE void Texture_loadImageRenderThread( + TEngine *tEngine, + TTexture *tTexture, + TLinearImage *tImage, + TPixelDataFormat bufferFormat, + TPixelDataType pixelDataType, + void (*onComplete)(bool) + ); EMSCRIPTEN_KEEPALIVE void Texture_setImageRenderThread( TEngine *tEngine, TTexture *tTexture, diff --git a/thermion_dart/native/src/c_api/TEngine.cpp b/thermion_dart/native/src/c_api/TEngine.cpp index 972f95bd..edb92584 100644 --- a/thermion_dart/native/src/c_api/TEngine.cpp +++ b/thermion_dart/native/src/c_api/TEngine.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -43,31 +44,35 @@ namespace thermion uint64_t TSWAP_CHAIN_CONFIG_APPLE_CVPIXELBUFFER = filament::backend::SWAP_CHAIN_CONFIG_APPLE_CVPIXELBUFFER; uint64_t TSWAP_CHAIN_CONFIG_HAS_STENCIL_BUFFER = filament::backend::SWAP_CHAIN_CONFIG_HAS_STENCIL_BUFFER; - EMSCRIPTEN_KEEPALIVE TEngine *Engine_create(TBackend backend) { + EMSCRIPTEN_KEEPALIVE TEngine *Engine_create(TBackend backend) + { auto *engine = filament::Engine::create(static_cast(backend)); return reinterpret_cast(engine); } - - EMSCRIPTEN_KEEPALIVE TRenderer *Engine_createRenderer(TEngine *tEngine) { + + EMSCRIPTEN_KEEPALIVE TRenderer *Engine_createRenderer(TEngine *tEngine) + { auto *engine = reinterpret_cast(tEngine); auto *renderer = engine->createRenderer(); return reinterpret_cast(renderer); } - EMSCRIPTEN_KEEPALIVE TSwapChain *Engine_createSwapChain(TEngine *tEngine, void *window, uint64_t flags) { + EMSCRIPTEN_KEEPALIVE TSwapChain *Engine_createSwapChain(TEngine *tEngine, void *window, uint64_t flags) + { auto *engine = reinterpret_cast(tEngine); auto *swapChain = engine->createSwapChain(window, flags); - return reinterpret_cast(swapChain); + return reinterpret_cast(swapChain); } - EMSCRIPTEN_KEEPALIVE TSwapChain *Engine_createHeadlessSwapChain(TEngine *tEngine, uint32_t width, uint32_t height, uint64_t flags) { + EMSCRIPTEN_KEEPALIVE TSwapChain *Engine_createHeadlessSwapChain(TEngine *tEngine, uint32_t width, uint32_t height, uint64_t flags) + { auto *engine = reinterpret_cast(tEngine); auto *swapChain = engine->createSwapChain(width, height, flags); - return reinterpret_cast(swapChain); + return reinterpret_cast(swapChain); } - - EMSCRIPTEN_KEEPALIVE TView *Engine_createView(TEngine *tEngine) { + EMSCRIPTEN_KEEPALIVE TView *Engine_createView(TEngine *tEngine) + { auto *engine = reinterpret_cast(tEngine); auto *view = engine->createView(); return reinterpret_cast(view); @@ -94,7 +99,8 @@ namespace thermion return reinterpret_cast(&lightManager); } - EMSCRIPTEN_KEEPALIVE TCamera *Engine_createCamera(TEngine* tEngine) { + EMSCRIPTEN_KEEPALIVE TCamera *Engine_createCamera(TEngine *tEngine) + { auto *engine = reinterpret_cast(tEngine); utils::Entity entity = utils::EntityManager::get().create(); auto *camera = engine->createCamera(entity); @@ -142,41 +148,47 @@ namespace thermion engine->destroy(material); } - EMSCRIPTEN_KEEPALIVE void Engine_destroyTexture(TEngine *tEngine, TTexture *tTexture) { + EMSCRIPTEN_KEEPALIVE void Engine_destroyTexture(TEngine *tEngine, TTexture *tTexture) + { auto *engine = reinterpret_cast(tEngine); auto *texture = reinterpret_cast(tTexture); engine->destroy(texture); } - EMSCRIPTEN_KEEPALIVE TFence *Engine_createFence(TEngine *tEngine) { + EMSCRIPTEN_KEEPALIVE TFence *Engine_createFence(TEngine *tEngine) + { auto *engine = reinterpret_cast(tEngine); auto *fence = engine->createFence(); return reinterpret_cast(fence); } - EMSCRIPTEN_KEEPALIVE void Engine_destroyFence(TEngine *tEngine, TFence *tFence) { + EMSCRIPTEN_KEEPALIVE void Engine_destroyFence(TEngine *tEngine, TFence *tFence) + { auto *engine = reinterpret_cast(tEngine); auto *fence = reinterpret_cast(tFence); Fence::waitAndDestroy(fence); } - EMSCRIPTEN_KEEPALIVE void Engine_flushAndWait(TEngine *tEngine) { + EMSCRIPTEN_KEEPALIVE void Engine_flushAndWait(TEngine *tEngine) + { auto *engine = reinterpret_cast(tEngine); - #ifdef __EMSCRIPTEN__ - engine->execute(); - emscripten_webgl_commit_frame(); - #else - engine->flushAndWait(); - #endif +#ifdef __EMSCRIPTEN__ + engine->execute(); + emscripten_webgl_commit_frame(); +#else + engine->flushAndWait(); +#endif } - EMSCRIPTEN_KEEPALIVE TScene *Engine_createScene(TEngine *tEngine) { + EMSCRIPTEN_KEEPALIVE TScene *Engine_createScene(TEngine *tEngine) + { auto *engine = reinterpret_cast(tEngine); - auto *scene = engine->createScene(); + auto *scene = engine->createScene(); return reinterpret_cast(scene); } - EMSCRIPTEN_KEEPALIVE TSkybox *Engine_buildSkybox(TEngine *tEngine, uint8_t* ktxData, size_t length, void(*onTextureUploadComplete)()) { + EMSCRIPTEN_KEEPALIVE TSkybox *Engine_buildSkybox(TEngine *tEngine, uint8_t *ktxData, size_t length, void (*onTextureUploadComplete)()) + { auto *engine = reinterpret_cast(tEngine); auto copy = new std::vector(ktxData, ktxData + length); image::Ktx1Bundle *skyboxBundle = @@ -186,9 +198,8 @@ namespace thermion std::vector *callbackData = new std::vector{ reinterpret_cast(onTextureUploadComplete), reinterpret_cast(skyboxBundle), - reinterpret_cast(copy) - }; - + reinterpret_cast(copy)}; + auto *texture = ktxreader::Ktx1Reader::createTexture( engine, *skyboxBundle, false, [](void *userdata) @@ -207,19 +218,63 @@ namespace thermion callback(); } delete skyboxBundle; - delete copy; + delete copy; }, - (void*)callbackData); + (void *)callbackData); auto *skybox = filament::Skybox::Builder() .environment(texture) .build(*engine); - + return reinterpret_cast(skybox); } - + EMSCRIPTEN_KEEPALIVE TIndirectLight *Engine_buildIndirectLight(TEngine *tEngine, uint8_t *ktxData, size_t length, float intensity, void (*onTextureUploadComplete)()) + { + auto *engine = reinterpret_cast(tEngine); + auto copy = new std::vector(ktxData, ktxData + length); + image::Ktx1Bundle *iblBundle = + new image::Ktx1Bundle(static_cast(copy->data()), + static_cast(length)); + filament::math::float3 harmonics[9]; + iblBundle->getSphericalHarmonics(harmonics); + + + std::vector *callbackData = new std::vector{ + reinterpret_cast(onTextureUploadComplete), + reinterpret_cast(iblBundle), + reinterpret_cast(copy)}; + + auto *texture = + ktxreader::Ktx1Reader::createTexture( + engine, *iblBundle, false, [](void *userdata) + { + std::vector* vec = (std::vector*)userdata; + + void *callbackPtr = vec->at(0); + image::Ktx1Bundle *iblBundle = reinterpret_cast(vec->at(1)); + std::vector *copy = reinterpret_cast*>(vec->at(2)); + + delete vec; + + if (callbackPtr) + { + void (*callback)(void) = (void (*)(void))callbackPtr; + callback(); + } + delete iblBundle; + delete copy; + }, + + (void *)callbackData); + auto *indirectLight = filament::IndirectLight::Builder() + .reflections(texture) + .irradiance(3, harmonics) + .intensity(intensity) + .build(*engine); + return reinterpret_cast(indirectLight); + } #ifdef __cplusplus } diff --git a/thermion_dart/native/src/c_api/TGltfAssetLoader.cpp b/thermion_dart/native/src/c_api/TGltfAssetLoader.cpp index f74b7fa5..d9d0055e 100644 --- a/thermion_dart/native/src/c_api/TGltfAssetLoader.cpp +++ b/thermion_dart/native/src/c_api/TGltfAssetLoader.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -50,6 +51,7 @@ EMSCRIPTEN_KEEPALIVE TGltfAssetLoader *GltfAssetLoader_create(TEngine *tEngine, auto *materialProvider = reinterpret_cast(tMaterialProvider); if(!materialProvider) { + Log("No material provider specified, using default ubershader provider"); materialProvider = gltfio::createUbershaderProvider( engine, UBERARCHIVE_DEFAULT_DATA, @@ -91,6 +93,24 @@ EMSCRIPTEN_KEEPALIVE TFilamentAsset *GltfAssetLoader_load( return reinterpret_cast(asset); } +EMSCRIPTEN_KEEPALIVE TMaterialInstance *GltfAssetLoader_getMaterialInstance(TRenderableManager *tRenderableManager, TFilamentAsset *tAsset) { + auto *renderableManager = reinterpret_cast(tRenderableManager); + auto *asset = reinterpret_cast(tAsset); + auto renderable = asset->getRenderableEntities(); + for(int i =0; i < asset->getRenderableEntityCount(); i++) { + auto renderableInstance = renderableManager->getInstance(renderable[i]); + if(!renderableInstance.isValid()) { + Log("INVALID RENDERABLE"); + continue; + } + auto mi = renderableManager->getMaterialInstanceAt(renderableInstance, 0); + mi->setParameter("baseColorFactor", filament::math::float4 { 1.0f, 0.0f, 0.0f, 1.0f}); + } + auto renderableInstance = renderableManager->getInstance(renderable[0]); + auto mi = renderableManager->getMaterialInstanceAt(renderableInstance, 0); + return reinterpret_cast(mi); +} + #ifdef __cplusplus } } diff --git a/thermion_dart/native/src/c_api/TMaterialProvider.cpp b/thermion_dart/native/src/c_api/TMaterialProvider.cpp index 5d5cfc40..59c46178 100644 --- a/thermion_dart/native/src/c_api/TMaterialProvider.cpp +++ b/thermion_dart/native/src/c_api/TMaterialProvider.cpp @@ -15,7 +15,7 @@ namespace thermion extern "C" { #endif - + EMSCRIPTEN_KEEPALIVE TMaterialInstance *MaterialProvider_createMaterialInstance(TMaterialProvider *tMaterialProvider, TMaterialKey *materialConfig) { gltfio::MaterialKey config; diff --git a/thermion_dart/native/src/c_api/TScene.cpp b/thermion_dart/native/src/c_api/TScene.cpp index d1432e4c..539178ec 100644 --- a/thermion_dart/native/src/c_api/TScene.cpp +++ b/thermion_dart/native/src/c_api/TScene.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -35,6 +36,12 @@ namespace thermion scene->setSkybox(skybox); } + EMSCRIPTEN_KEEPALIVE void Scene_setIndirectLight(TScene* tScene, TIndirectLight *tIndirectLight) { + auto *scene = reinterpret_cast(tScene); + auto *light = reinterpret_cast(tIndirectLight); + scene->setIndirectLight(light); + } + EMSCRIPTEN_KEEPALIVE void Scene_addFilamentAsset(TScene* tScene, TFilamentAsset *tAsset) { auto *scene = reinterpret_cast(tScene); auto *asset = reinterpret_cast(tAsset); diff --git a/thermion_dart/native/src/c_api/TTexture.cpp b/thermion_dart/native/src/c_api/TTexture.cpp index d93ad7b2..f750f947 100644 --- a/thermion_dart/native/src/c_api/TTexture.cpp +++ b/thermion_dart/native/src/c_api/TTexture.cpp @@ -491,7 +491,7 @@ namespace thermion EMSCRIPTEN_KEEPALIVE TTextureSampler *TextureSampler_create() { - auto *sampler = new filament::TextureSampler(filament::TextureSampler::CompareMode::COMPARE_TO_TEXTURE); + auto *sampler = new filament::TextureSampler(); return reinterpret_cast(sampler); }