internal: expose more RenderableManager/LightManager bindings

This commit is contained in:
Nick Fisher
2025-01-06 18:57:26 +08:00
parent 6cefe44c64
commit b1c0d4b2e8
7 changed files with 190 additions and 35 deletions

View File

@@ -1667,6 +1667,8 @@ class ThermionViewerFFI extends ThermionViewer {
throw Exception("Failed to create geometry"); throw Exception("Failed to create geometry");
} }
print(" is shadow caster : ${RenderableManager_isShadowCaster(_renderableManager!, SceneAsset_getEntity(assetPtr))} is shadow recevier : ${RenderableManager_isShadowReceiver(_renderableManager!, SceneAsset_getEntity(assetPtr))} " );
var asset = FFIAsset( var asset = FFIAsset(
assetPtr, _sceneManager!, _engine!, _unlitMaterialProvider!, this); assetPtr, _sceneManager!, _engine!, _unlitMaterialProvider!, this);

View File

@@ -460,7 +460,7 @@ abstract class ThermionViewer {
Future setToneMapping(ToneMapper mapper); Future setToneMapping(ToneMapper mapper);
/// ///
/// Sets the strength of the bloom. /// Sets the strength of the bloom (requires postprocessing).
/// ///
Future setBloom(double bloom); Future setBloom(double bloom);
@@ -597,7 +597,7 @@ abstract class ThermionViewer {
double viewportX, double viewportY, double x, double y, double z); double viewportX, double viewportY, double x, double y, double z);
/// ///
/// Enable/disable postprocessing effects (anti-aliasing, tone mapping). Disabled by default. /// Enable/disable postprocessing effects (anti-aliasing, tone mapping, bloom). Disabled by default.
/// ///
Future setPostProcessing(bool enabled); Future setPostProcessing(bool enabled);

View File

@@ -11,6 +11,13 @@ extern "C"
EMSCRIPTEN_KEEPALIVE void LightManager_setPosition(TLightManager *tLightManager, EntityId light, double x, double y, double z); EMSCRIPTEN_KEEPALIVE void LightManager_setPosition(TLightManager *tLightManager, EntityId light, double x, double y, double z);
EMSCRIPTEN_KEEPALIVE void LightManager_setDirection(TLightManager *tLightManager, EntityId light, double x, double y, double z); EMSCRIPTEN_KEEPALIVE void LightManager_setDirection(TLightManager *tLightManager, EntityId light, double x, double y, double z);
EMSCRIPTEN_KEEPALIVE int LightManager_createLight(TLightManager *tLightManager, EntityId entity, int type);
EMSCRIPTEN_KEEPALIVE void LightManager_destroyLight(TLightManager *tLightManager, EntityId entity);
EMSCRIPTEN_KEEPALIVE void LightManager_setColor(TLightManager *tLightManager, EntityId entity, double r, double g, double b);
EMSCRIPTEN_KEEPALIVE void LightManager_setIntensity(TLightManager *tLightManager, EntityId entity, double intensity);
EMSCRIPTEN_KEEPALIVE void LightManager_setFalloff(TLightManager *tLightManager, EntityId entity, double falloff);
EMSCRIPTEN_KEEPALIVE void LightManager_setSpotLightCone(TLightManager *tLightManager, EntityId entity, double inner, double outer);
EMSCRIPTEN_KEEPALIVE void LightManager_setShadowCaster(TLightManager *tLightManager, EntityId entity, bool enabled);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -8,11 +8,43 @@ extern "C"
{ {
#endif #endif
EMSCRIPTEN_KEEPALIVE void RenderableManager_setMaterialInstanceAt(TRenderableManager *tRenderableManager, EntityId entityId, int primitiveIndex, TMaterialInstance *tMaterialInstance); EMSCRIPTEN_KEEPALIVE void RenderableManager_setMaterialInstanceAt(TRenderableManager *tRenderableManager, EntityId entityId, int primitiveIndex, TMaterialInstance *tMaterialInstance);
EMSCRIPTEN_KEEPALIVE void RenderableManager_setPriority(TRenderableManager *tRenderableManager, EntityId entityId, int priority); EMSCRIPTEN_KEEPALIVE void RenderableManager_setPriority(TRenderableManager *tRenderableManager, EntityId entityId, int priority);
EMSCRIPTEN_KEEPALIVE TMaterialInstance *RenderableManager_getMaterialInstanceAt(TRenderableManager *tRenderableManager, EntityId entityId, int primitiveIndex); EMSCRIPTEN_KEEPALIVE TMaterialInstance *RenderableManager_getMaterialInstanceAt(TRenderableManager *tRenderableManager, EntityId entityId, int primitiveIndex);
EMSCRIPTEN_KEEPALIVE bool RenderableManager_isRenderable(TRenderableManager *tRenderableManager, EntityId entityId); EMSCRIPTEN_KEEPALIVE bool RenderableManager_isRenderable(TRenderableManager *tRenderableManager, EntityId entityId);
// New boolean methods
/**
* Checks if the given entity has a renderable component
*/
EMSCRIPTEN_KEEPALIVE bool RenderableManager_hasComponent(TRenderableManager *tRenderableManager, EntityId entityId);
/**
* Returns true if this manager has no components
*/
EMSCRIPTEN_KEEPALIVE bool RenderableManager_empty(TRenderableManager *tRenderableManager);
/**
* Returns whether a light channel is enabled on a specified renderable
*/
EMSCRIPTEN_KEEPALIVE bool RenderableManager_getLightChannel(TRenderableManager *tRenderableManager, EntityId entityId, unsigned int channel);
/**
* Checks if the renderable can cast shadows
*/
EMSCRIPTEN_KEEPALIVE bool RenderableManager_isShadowCaster(TRenderableManager *tRenderableManager, EntityId entityId);
/**
* Checks if the renderable can receive shadows
*/
EMSCRIPTEN_KEEPALIVE bool RenderableManager_isShadowReceiver(TRenderableManager *tRenderableManager, EntityId entityId);
/**
* Returns whether large-scale fog is enabled for this renderable
*/
EMSCRIPTEN_KEEPALIVE bool RenderableManager_getFogEnabled(TRenderableManager *tRenderableManager, EntityId entityId);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -1,33 +1,94 @@
#include <filament/LightManager.h> #include <filament/LightManager.h>
#include <utils/Entity.h> #include <utils/Entity.h>
#include "c_api/APIExport.h" #include "c_api/APIExport.h"
#include "Log.hpp" #include "Log.hpp"
extern "C"
{
#include "c_api/TLightManager.h" #include "c_api/TLightManager.h"
EMSCRIPTEN_KEEPALIVE void LightManager_setPosition(TLightManager *tLightManager, EntityId light, double x, double y, double z) { extern "C" {
auto lightManager = reinterpret_cast<filament::LightManager*>(tLightManager);
auto instance = lightManager->getInstance(utils::Entity::import(light)); EMSCRIPTEN_KEEPALIVE void LightManager_setPosition(TLightManager *tLightManager, EntityId light, double x, double y, double z) {
if(!instance.isValid()) { auto lightManager = reinterpret_cast<filament::LightManager*>(tLightManager);
Log("Warning: invalid light instance"); auto instance = lightManager->getInstance(utils::Entity::import(light));
return; if (!instance.isValid()) {
} Log("Warning: invalid light instance");
lightManager->setPosition(instance, filament::math::float3 { x, y, z }); return;
}
lightManager->setPosition(instance, filament::math::float3 { static_cast<float>(x), static_cast<float>(y), static_cast<float>(z) });
}
EMSCRIPTEN_KEEPALIVE void LightManager_setDirection(TLightManager *tLightManager, EntityId light, double x, double y, double z) {
auto lightManager = reinterpret_cast<filament::LightManager*>(tLightManager);
auto instance = lightManager->getInstance(utils::Entity::import(light));
if (!instance.isValid()) {
Log("Warning: invalid light instance");
return;
}
lightManager->setDirection(instance, filament::math::float3 { static_cast<float>(x), static_cast<float>(y), static_cast<float>(z) });
}
EMSCRIPTEN_KEEPALIVE int LightManager_createLight(TLightManager *tLightManager, EntityId entity, int type) {
auto* lm = reinterpret_cast<filament::LightManager*>(tLightManager);
filament::LightManager::Type lightType;
switch (type) {
case 0: lightType = filament::LightManager::Type::SUN; break;
case 1: lightType = filament::LightManager::Type::DIRECTIONAL; break;
case 2: lightType = filament::LightManager::Type::POINT; break;
case 3: lightType = filament::LightManager::Type::FOCUSED_SPOT; break;
case 4: lightType = filament::LightManager::Type::SPOT; break;
default: return -1;
} }
EMSCRIPTEN_KEEPALIVE void LightManager_setDirection(TLightManager *tLightManager, EntityId light, double x, double y, double z) { filament::LightManager::Builder builder(lightType);
auto lightManager = reinterpret_cast<filament::LightManager*>(tLightManager); return false;
auto instance = lightManager->getInstance(utils::Entity::import(light)); // auto result = builder.build(*lm->getEngine(), utils::Entity::import(entity));
if(!instance.isValid()) { // return result == filament::LightManager::Result::Success ? 0 : -1;
Log("Warning: invalid light instance"); }
return;
} EMSCRIPTEN_KEEPALIVE void LightManager_destroyLight(TLightManager *tLightManager, EntityId entity) {
lightManager->setPosition(instance, filament::math::float3 { x, y, z }); auto* lm = reinterpret_cast<filament::LightManager*>(tLightManager);
lm->destroy(utils::Entity::import(entity));
}
EMSCRIPTEN_KEEPALIVE void LightManager_setColor(TLightManager *tLightManager, EntityId entity, double r, double g, double b) {
auto* lm = reinterpret_cast<filament::LightManager*>(tLightManager);
auto instance = lm->getInstance(utils::Entity::import(entity));
if (instance.isValid()) {
lm->setColor(instance, {static_cast<float>(r), static_cast<float>(g), static_cast<float>(b)});
} }
} }
EMSCRIPTEN_KEEPALIVE void LightManager_setIntensity(TLightManager *tLightManager, EntityId entity, double intensity) {
auto* lm = reinterpret_cast<filament::LightManager*>(tLightManager);
auto instance = lm->getInstance(utils::Entity::import(entity));
if (instance.isValid()) {
lm->setIntensity(instance, static_cast<float>(intensity));
}
}
EMSCRIPTEN_KEEPALIVE void LightManager_setFalloff(TLightManager *tLightManager, EntityId entity, double falloff) {
auto* lm = reinterpret_cast<filament::LightManager*>(tLightManager);
auto instance = lm->getInstance(utils::Entity::import(entity));
if (instance.isValid()) {
lm->setFalloff(instance, static_cast<float>(falloff));
}
}
EMSCRIPTEN_KEEPALIVE void LightManager_setSpotLightCone(TLightManager *tLightManager, EntityId entity, double inner, double outer) {
auto* lm = reinterpret_cast<filament::LightManager*>(tLightManager);
auto instance = lm->getInstance(utils::Entity::import(entity));
if (instance.isValid()) {
lm->setSpotLightCone(instance, static_cast<float>(inner), static_cast<float>(outer));
}
}
EMSCRIPTEN_KEEPALIVE void LightManager_setShadowCaster(TLightManager *tLightManager, EntityId entity, bool enabled) {
auto* lm = reinterpret_cast<filament::LightManager*>(tLightManager);
auto instance = lm->getInstance(utils::Entity::import(entity));
if (instance.isValid()) {
lm->setShadowCaster(instance, enabled);
}
}
} // extern "C"

View File

@@ -1,4 +1,3 @@
#include <filament/MaterialInstance.h> #include <filament/MaterialInstance.h>
#include <filament/RenderableManager.h> #include <filament/RenderableManager.h>
#include <utils/Entity.h> #include <utils/Entity.h>
@@ -8,7 +7,6 @@
namespace thermion namespace thermion
{ {
extern "C" extern "C"
{ {
using namespace filament; using namespace filament;
@@ -47,5 +45,56 @@ namespace thermion
auto renderableInstance = renderableManager->getInstance(entity); auto renderableInstance = renderableManager->getInstance(entity);
return renderableInstance.isValid(); return renderableInstance.isValid();
} }
EMSCRIPTEN_KEEPALIVE bool RenderableManager_hasComponent(TRenderableManager *tRenderableManager, EntityId entityId) {
auto *renderableManager = reinterpret_cast<filament::RenderableManager *>(tRenderableManager);
const auto &entity = utils::Entity::import(entityId);
return renderableManager->hasComponent(entity);
}
EMSCRIPTEN_KEEPALIVE bool RenderableManager_empty(TRenderableManager *tRenderableManager) {
auto *renderableManager = reinterpret_cast<filament::RenderableManager *>(tRenderableManager);
return renderableManager->empty();
}
EMSCRIPTEN_KEEPALIVE bool RenderableManager_getLightChannel(TRenderableManager *tRenderableManager, EntityId entityId, unsigned int channel) {
auto *renderableManager = reinterpret_cast<filament::RenderableManager *>(tRenderableManager);
const auto &entity = utils::Entity::import(entityId);
auto renderableInstance = renderableManager->getInstance(entity);
if (!renderableInstance.isValid()) {
return false;
}
return renderableManager->getLightChannel(renderableInstance, channel);
}
EMSCRIPTEN_KEEPALIVE bool RenderableManager_isShadowCaster(TRenderableManager *tRenderableManager, EntityId entityId) {
auto *renderableManager = reinterpret_cast<filament::RenderableManager *>(tRenderableManager);
const auto &entity = utils::Entity::import(entityId);
auto renderableInstance = renderableManager->getInstance(entity);
if (!renderableInstance.isValid()) {
return false;
}
return renderableManager->isShadowCaster(renderableInstance);
}
EMSCRIPTEN_KEEPALIVE bool RenderableManager_isShadowReceiver(TRenderableManager *tRenderableManager, EntityId entityId) {
auto *renderableManager = reinterpret_cast<filament::RenderableManager *>(tRenderableManager);
const auto &entity = utils::Entity::import(entityId);
auto renderableInstance = renderableManager->getInstance(entity);
if (!renderableInstance.isValid()) {
return false;
}
return renderableManager->isShadowReceiver(renderableInstance);
}
EMSCRIPTEN_KEEPALIVE bool RenderableManager_getFogEnabled(TRenderableManager *tRenderableManager, EntityId entityId) {
auto *renderableManager = reinterpret_cast<filament::RenderableManager *>(tRenderableManager);
const auto &entity = utils::Entity::import(entityId);
auto renderableInstance = renderableManager->getInstance(entity);
if (!renderableInstance.isValid()) {
return false;
}
return renderableManager->getFogEnabled(renderableInstance);
}
} }
} }

View File

@@ -531,6 +531,9 @@ namespace thermion
{ {
auto light = EntityManager::get().create(); auto light = EntityManager::get().create();
// LightManager::ShadowOptions shadowOptions;
// shadowOptions.stable = true;
auto result = LightManager::Builder(t) auto result = LightManager::Builder(t)
.color(Color::cct(colour)) .color(Color::cct(colour))
.intensity(intensity) .intensity(intensity)
@@ -541,6 +544,7 @@ namespace thermion
.sunHaloFalloff(sunHaloFallof) .sunHaloFalloff(sunHaloFallof)
.position(filament::math::float3(posX, posY, posZ)) .position(filament::math::float3(posX, posY, posZ))
.direction(filament::math::float3(dirX, dirY, dirZ)) .direction(filament::math::float3(dirX, dirY, dirZ))
// .shadowOptions(shadowOptions)
.castShadows(shadows) .castShadows(shadows)
.build(*_engine, light); .build(*_engine, light);
if (result != LightManager::Builder::Result::Success) if (result != LightManager::Builder::Result::Success)