add indirect light methods

This commit is contained in:
Nick Fisher
2025-03-17 22:36:53 +08:00
parent b271b7e433
commit 231b03cd17
11 changed files with 139 additions and 33 deletions

View File

@@ -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;

View File

@@ -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)());

View File

@@ -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
}

View File

@@ -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

View File

@@ -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);

View File

@@ -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,

View File

@@ -2,6 +2,7 @@
#include <filament/Engine.h>
#include <filament/Fence.h>
#include <filament/IndirectLight.h>
#include <filament/Material.h>
#include <filament/Scene.h>
#include <filament/Skybox.h>
@@ -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<filament::Engine::Backend>(backend));
return reinterpret_cast<TEngine *>(engine);
}
EMSCRIPTEN_KEEPALIVE TRenderer *Engine_createRenderer(TEngine *tEngine) {
EMSCRIPTEN_KEEPALIVE TRenderer *Engine_createRenderer(TEngine *tEngine)
{
auto *engine = reinterpret_cast<Engine *>(tEngine);
auto *renderer = engine->createRenderer();
return reinterpret_cast<TRenderer *>(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<Engine *>(tEngine);
auto *swapChain = engine->createSwapChain(window, flags);
return reinterpret_cast<TSwapChain*>(swapChain);
return reinterpret_cast<TSwapChain *>(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<Engine *>(tEngine);
auto *swapChain = engine->createSwapChain(width, height, flags);
return reinterpret_cast<TSwapChain*>(swapChain);
return reinterpret_cast<TSwapChain *>(swapChain);
}
EMSCRIPTEN_KEEPALIVE TView *Engine_createView(TEngine *tEngine) {
EMSCRIPTEN_KEEPALIVE TView *Engine_createView(TEngine *tEngine)
{
auto *engine = reinterpret_cast<Engine *>(tEngine);
auto *view = engine->createView();
return reinterpret_cast<TView *>(view);
@@ -94,7 +99,8 @@ namespace thermion
return reinterpret_cast<TLightManager *>(&lightManager);
}
EMSCRIPTEN_KEEPALIVE TCamera *Engine_createCamera(TEngine* tEngine) {
EMSCRIPTEN_KEEPALIVE TCamera *Engine_createCamera(TEngine *tEngine)
{
auto *engine = reinterpret_cast<Engine *>(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<Engine *>(tEngine);
auto *texture = reinterpret_cast<Texture *>(tTexture);
engine->destroy(texture);
}
EMSCRIPTEN_KEEPALIVE TFence *Engine_createFence(TEngine *tEngine) {
EMSCRIPTEN_KEEPALIVE TFence *Engine_createFence(TEngine *tEngine)
{
auto *engine = reinterpret_cast<Engine *>(tEngine);
auto *fence = engine->createFence();
return reinterpret_cast<TFence *>(fence);
}
EMSCRIPTEN_KEEPALIVE void Engine_destroyFence(TEngine *tEngine, TFence *tFence) {
EMSCRIPTEN_KEEPALIVE void Engine_destroyFence(TEngine *tEngine, TFence *tFence)
{
auto *engine = reinterpret_cast<Engine *>(tEngine);
auto *fence = reinterpret_cast<Fence *>(tFence);
Fence::waitAndDestroy(fence);
}
EMSCRIPTEN_KEEPALIVE void Engine_flushAndWait(TEngine *tEngine) {
EMSCRIPTEN_KEEPALIVE void Engine_flushAndWait(TEngine *tEngine)
{
auto *engine = reinterpret_cast<Engine *>(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<Engine *>(tEngine);
auto *scene = engine->createScene();
auto *scene = engine->createScene();
return reinterpret_cast<TScene *>(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<Engine *>(tEngine);
auto copy = new std::vector<uint8_t>(ktxData, ktxData + length);
image::Ktx1Bundle *skyboxBundle =
@@ -186,9 +198,8 @@ namespace thermion
std::vector<void *> *callbackData = new std::vector<void *>{
reinterpret_cast<void *>(onTextureUploadComplete),
reinterpret_cast<void *>(skyboxBundle),
reinterpret_cast<void *>(copy)
};
reinterpret_cast<void *>(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<TSkybox *>(skybox);
}
EMSCRIPTEN_KEEPALIVE TIndirectLight *Engine_buildIndirectLight(TEngine *tEngine, uint8_t *ktxData, size_t length, float intensity, void (*onTextureUploadComplete)())
{
auto *engine = reinterpret_cast<Engine *>(tEngine);
auto copy = new std::vector<uint8_t>(ktxData, ktxData + length);
image::Ktx1Bundle *iblBundle =
new image::Ktx1Bundle(static_cast<const uint8_t *>(copy->data()),
static_cast<uint32_t>(length));
filament::math::float3 harmonics[9];
iblBundle->getSphericalHarmonics(harmonics);
std::vector<void *> *callbackData = new std::vector<void *>{
reinterpret_cast<void *>(onTextureUploadComplete),
reinterpret_cast<void *>(iblBundle),
reinterpret_cast<void *>(copy)};
auto *texture =
ktxreader::Ktx1Reader::createTexture(
engine, *iblBundle, false, [](void *userdata)
{
std::vector<void*>* vec = (std::vector<void*>*)userdata;
void *callbackPtr = vec->at(0);
image::Ktx1Bundle *iblBundle = reinterpret_cast<image::Ktx1Bundle *>(vec->at(1));
std::vector<uint8_t> *copy = reinterpret_cast<std::vector<uint8_t>*>(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<TIndirectLight *>(indirectLight);
}
#ifdef __cplusplus
}

View File

@@ -3,6 +3,7 @@
#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>
@@ -50,6 +51,7 @@ EMSCRIPTEN_KEEPALIVE TGltfAssetLoader *GltfAssetLoader_create(TEngine *tEngine,
auto *materialProvider = reinterpret_cast<gltfio::MaterialProvider *>(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<TFilamentAsset *>(asset);
}
EMSCRIPTEN_KEEPALIVE TMaterialInstance *GltfAssetLoader_getMaterialInstance(TRenderableManager *tRenderableManager, TFilamentAsset *tAsset) {
auto *renderableManager = reinterpret_cast<filament::RenderableManager *>(tRenderableManager);
auto *asset = reinterpret_cast<gltfio::FilamentAsset *>(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<TMaterialInstance*>(mi);
}
#ifdef __cplusplus
}
}

View File

@@ -15,7 +15,7 @@ namespace thermion
extern "C"
{
#endif
EMSCRIPTEN_KEEPALIVE TMaterialInstance *MaterialProvider_createMaterialInstance(TMaterialProvider *tMaterialProvider, TMaterialKey *materialConfig)
{
gltfio::MaterialKey config;

View File

@@ -2,6 +2,7 @@
#include <filament/Engine.h>
#include <filament/Fence.h>
#include <filament/IndirectLight.h>
#include <filament/Material.h>
#include <filament/Scene.h>
#include <filament/Skybox.h>
@@ -35,6 +36,12 @@ namespace thermion
scene->setSkybox(skybox);
}
EMSCRIPTEN_KEEPALIVE void Scene_setIndirectLight(TScene* tScene, TIndirectLight *tIndirectLight) {
auto *scene = reinterpret_cast<Scene *>(tScene);
auto *light = reinterpret_cast<IndirectLight *>(tIndirectLight);
scene->setIndirectLight(light);
}
EMSCRIPTEN_KEEPALIVE void Scene_addFilamentAsset(TScene* tScene, TFilamentAsset *tAsset) {
auto *scene = reinterpret_cast<Scene *>(tScene);
auto *asset = reinterpret_cast<gltfio::FilamentAsset*>(tAsset);

View File

@@ -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<TTextureSampler *>(sampler);
}