feature!:

This is a breaking change needed to fully implement instancing and stencil highlighting.

Previously, users would work directly with entities (on the Dart side, ThermionEntity), e.g.

final entity = await viewer.loadGlb("some.glb");

However, Filament "entities" are a lower-level abstraction.

Loading a glTF file, for example, inserts multiple entities into the scene.

For example, each mesh, light, and camera within a glTF asset will be assigned an entity. A top-level (non-renderable) entity will also be created for the glTF asset, which can be used to transform the entire hierarchy.

"Asset" is a better representation for loading/inserting objects into the scene; think of this as a bundle of entities.

Unless you need to work directly with transforms, instancing, materials and renderables, you can work directly with ThermionAsset.
This commit is contained in:
Nick Fisher
2024-11-21 15:04:10 +08:00
parent 9ada6aae64
commit ed444b0615
195 changed files with 18061 additions and 12628 deletions

View File

@@ -0,0 +1,300 @@
#include "Log.hpp"
#include "c_api/APIExport.h"
#include "scene/AnimationManager.hpp"
using namespace thermion;
extern "C"
{
#include "c_api/TAnimationManager.h"
EMSCRIPTEN_KEEPALIVE void AnimationManager_addAnimationComponent(TAnimationManager *tAnimationManager, EntityId entityId)
{
auto animationManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
animationManager->addAnimationComponent(entityId);
}
EMSCRIPTEN_KEEPALIVE void AnimationManager_removeAnimationComponent(TAnimationManager *tAnimationManager, EntityId entityId)
{
auto animationManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
animationManager->removeAnimationComponent(entityId);
}
EMSCRIPTEN_KEEPALIVE bool AnimationManager_setMorphAnimation(
TAnimationManager *tAnimationManager,
EntityId entityId,
const float *const morphData,
const uint32_t *const morphIndices,
int numMorphTargets,
int numFrames,
float frameLengthInMs)
{
auto entity = utils::Entity::import(entityId);
auto *animationManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
auto result = animationManager->setMorphAnimationBuffer(entity, morphData, morphIndices, numMorphTargets, numFrames, frameLengthInMs);
return result;
}
EMSCRIPTEN_KEEPALIVE bool AnimationManager_setMorphTargetWeights(
TAnimationManager *tAnimationManager,
EntityId entityId,
const float *const morphData,
int numWeights)
{
auto entity = utils::Entity::import(entityId);
auto *animationManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
animationManager->setMorphTargetWeights(entity, morphData, numWeights);
return true;
}
EMSCRIPTEN_KEEPALIVE bool AnimationManager_clearMorphAnimation(TAnimationManager *tAnimationManager, EntityId entityId)
{
auto *animManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
auto entity = utils::Entity::import(entityId);
animManager->clearMorphAnimationBuffer(entity);
return true;
}
EMSCRIPTEN_KEEPALIVE void AnimationManager_resetToRestPose(TAnimationManager *tAnimationManager, TSceneAsset *sceneAsset)
{
auto *animManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
auto asset = reinterpret_cast<SceneAsset *>(sceneAsset);
if (asset->getType() == SceneAsset::SceneAssetType::Gltf && asset->isInstance())
{
auto *instance = reinterpret_cast<GltfSceneAssetInstance *>(asset);
animManager->resetToRestPose(instance);
}
}
EMSCRIPTEN_KEEPALIVE void AnimationManager_addBoneAnimation(
TAnimationManager *tAnimationManager,
TSceneAsset *sceneAsset,
int skinIndex,
int boneIndex,
const float *const frameData,
int numFrames,
float frameLengthInMs,
float fadeOutInSecs,
float fadeInInSecs,
float maxDelta)
{
auto *animManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
auto asset = reinterpret_cast<SceneAsset *>(sceneAsset);
if (asset->getType() == SceneAsset::SceneAssetType::Gltf && asset->isInstance())
{
animManager->addBoneAnimation(reinterpret_cast<GltfSceneAssetInstance *>(asset), skinIndex, boneIndex, frameData, numFrames, frameLengthInMs,
fadeOutInSecs, fadeInInSecs, maxDelta);
}
}
EMSCRIPTEN_KEEPALIVE EntityId AnimationManager_getBone(
TAnimationManager *tAnimationManager,
TSceneAsset *sceneAsset,
int skinIndex,
int boneIndex)
{
auto *animManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
auto asset = reinterpret_cast<SceneAsset *>(sceneAsset);
if (asset->getType() == SceneAsset::SceneAssetType::Gltf && asset->isInstance())
{
auto entities = animManager->getBoneEntities(reinterpret_cast<GltfSceneAssetInstance *>(asset), skinIndex);
if (boneIndex < entities.size())
{
return utils::Entity::smuggle(entities[boneIndex]);
}
}
return 0;
}
EMSCRIPTEN_KEEPALIVE void AnimationManager_getRestLocalTransforms(
TAnimationManager *tAnimationManager,
TSceneAsset *sceneAsset,
int skinIndex,
float *const out,
int numBones)
{
auto *animManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
auto asset = reinterpret_cast<SceneAsset *>(sceneAsset);
if (asset->getType() == SceneAsset::SceneAssetType::Gltf && asset->isInstance())
{
auto *instance = reinterpret_cast<GltfSceneAssetInstance *>(asset);
const auto transforms = animManager->getBoneRestTranforms(instance, skinIndex);
auto numTransforms = transforms.size();
if (numTransforms != numBones)
{
Log("Error - %d bone transforms available but you only specified %d.", numTransforms, numBones);
return;
}
for (int boneIndex = 0; boneIndex < numTransforms; boneIndex++)
{
const auto transform = transforms[boneIndex];
for (int colNum = 0; colNum < 4; colNum++)
{
for (int rowNum = 0; rowNum < 4; rowNum++)
{
out[(boneIndex * 16) + (colNum * 4) + rowNum] = transform[colNum][rowNum];
}
}
}
}
}
EMSCRIPTEN_KEEPALIVE void AnimationManager_getInverseBindMatrix(
TAnimationManager *tAnimationManager,
TSceneAsset *sceneAsset,
int skinIndex,
int boneIndex,
float *const out)
{
auto *animManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
auto asset = reinterpret_cast<SceneAsset *>(sceneAsset);
if (asset->getType() == SceneAsset::SceneAssetType::Gltf && asset->isInstance())
{
auto *instance = reinterpret_cast<GltfSceneAssetInstance *>(asset);
auto transform = animManager->getInverseBindMatrix(instance, skinIndex, boneIndex);
for (int colNum = 0; colNum < 4; colNum++)
{
for (int rowNum = 0; rowNum < 4; rowNum++)
{
out[(colNum * 4) + rowNum] = transform[colNum][rowNum];
}
}
}
}
EMSCRIPTEN_KEEPALIVE void AnimationManager_playAnimation(
TAnimationManager *tAnimationManager,
TSceneAsset *sceneAsset,
int index,
bool loop,
bool reverse,
bool replaceActive,
float crossfade,
float startOffset)
{
auto *animManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
auto asset = reinterpret_cast<SceneAsset *>(sceneAsset);
if (asset->getType() == SceneAsset::SceneAssetType::Gltf && asset->isInstance())
{
auto *instance = reinterpret_cast<GltfSceneAssetInstance *>(asset);
animManager->playGltfAnimation(instance, index, loop, reverse, replaceActive, crossfade, startOffset);
}
}
EMSCRIPTEN_KEEPALIVE void AnimationManager_stopAnimation(
TAnimationManager *tAnimationManager,
TSceneAsset *sceneAsset,
int index)
{
auto *animManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
auto asset = reinterpret_cast<SceneAsset *>(sceneAsset);
if (asset->getType() == SceneAsset::SceneAssetType::Gltf && asset->isInstance())
{
auto *instance = reinterpret_cast<GltfSceneAssetInstance *>(asset);
animManager->stopGltfAnimation(instance, index);
}
}
EMSCRIPTEN_KEEPALIVE void AnimationManager_setGltfAnimationFrame(
TAnimationManager *tAnimationManager,
TSceneAsset *tSceneAsset,
int animationIndex,
int frame)
{
auto *animManager = reinterpret_cast<AnimationManager *>(tAnimationManager);
auto asset = reinterpret_cast<SceneAsset *>(tSceneAsset);
if (asset->getType() == SceneAsset::SceneAssetType::Gltf && asset->isInstance())
{
auto *instance = reinterpret_cast<GltfSceneAssetInstance *>(asset);
animManager->setGltfAnimationFrame(instance, animationIndex, frame);
}
}
EMSCRIPTEN_KEEPALIVE float AnimationManager_getAnimationDuration(
TAnimationManager *tAnimationManager,
TSceneAsset *sceneAsset,
int animationIndex)
{
auto instance = ((GltfSceneAssetInstance *)sceneAsset);
return ((AnimationManager *)tAnimationManager)->getGltfAnimationDuration(instance, animationIndex);
}
EMSCRIPTEN_KEEPALIVE int AnimationManager_getAnimationCount(
TAnimationManager *tAnimationManager,
TSceneAsset *sceneAsset)
{
auto instance = ((GltfSceneAssetInstance *)sceneAsset);
auto names = ((AnimationManager *)tAnimationManager)->getGltfAnimationNames(instance);
return (int)names.size();
}
EMSCRIPTEN_KEEPALIVE void AnimationManager_getAnimationName(
TAnimationManager *tAnimationManager,
TSceneAsset *sceneAsset,
char *const outPtr,
int index)
{
auto instance = ((GltfSceneAssetInstance *)sceneAsset);
auto names = ((AnimationManager *)tAnimationManager)->getGltfAnimationNames(instance);
std::string name = names[index];
strcpy(outPtr, name.c_str());
}
EMSCRIPTEN_KEEPALIVE int AnimationManager_getBoneCount(
TAnimationManager *tAnimationManager,
TSceneAsset *sceneAsset,
int skinIndex)
{
auto instance = ((GltfSceneAssetInstance *)sceneAsset);
auto entities = ((AnimationManager *)tAnimationManager)->getBoneEntities(instance, skinIndex);
return (int)entities.size();
}
EMSCRIPTEN_KEEPALIVE void AnimationManager_getBoneNames(
TAnimationManager *tAnimationManager,
TSceneAsset *sceneAsset,
const char **out,
int skinIndex)
{
auto instance = ((GltfSceneAssetInstance *)sceneAsset);
auto entities = ((AnimationManager *)tAnimationManager)->getBoneEntities(instance, skinIndex);
// Note: This needs implementation of a method to get bone names from entities
// Current source doesn't show how bone names are retrieved
}
EMSCRIPTEN_KEEPALIVE bool AnimationManager_updateBoneMatrices(
TAnimationManager *tAnimationManager,
TSceneAsset *sceneAsset)
{
auto instance = ((GltfSceneAssetInstance *)sceneAsset);
((AnimationManager *)tAnimationManager)->updateBoneMatrices(instance);
return true;
}
EMSCRIPTEN_KEEPALIVE int AnimationManager_getMorphTargetNameCount(
TAnimationManager *tAnimationManager,
TSceneAsset *sceneAsset,
EntityId childEntity)
{
auto asset = ((GltfSceneAsset *)sceneAsset);
auto names = ((AnimationManager *)tAnimationManager)->getMorphTargetNames(asset, childEntity);
return (int)names.size();
}
EMSCRIPTEN_KEEPALIVE void AnimationManager_getMorphTargetName(
TAnimationManager *tAnimationManager,
TSceneAsset *sceneAsset,
EntityId childEntity,
char *const outPtr,
int index)
{
auto asset = ((GltfSceneAsset *)sceneAsset);
auto names = ((AnimationManager *)tAnimationManager)->getMorphTargetNames(asset, childEntity);
std::string name = names[index];
strcpy(outPtr, name.c_str());
}
}

View File

@@ -0,0 +1,84 @@
#include <filament/View.h>
#include <filament/Viewport.h>
#include <filament/Engine.h>
#include <filament/ToneMapper.h>
#include <filament/ColorGrading.h>
#include <filament/Camera.h>
#include <utils/Entity.h>
#include "c_api/ThermionDartApi.h"
#include "c_api/TCamera.h"
#include "Log.hpp"
#include "MathUtils.hpp"
#ifdef __cplusplus
namespace thermion
{
extern "C"
{
using namespace filament;
#endif
EMSCRIPTEN_KEEPALIVE void Camera_setCustomProjectionWithCulling(TCamera *tCamera, double4x4 projectionMatrix, double near, double far)
{
auto *camera = reinterpret_cast<Camera *>(tCamera);
camera->setCustomProjection(convert_double4x4_to_mat4(projectionMatrix), near, far);
}
EMSCRIPTEN_KEEPALIVE double4x4 Camera_getModelMatrix(TCamera *tCamera)
{
auto *camera = reinterpret_cast<Camera *>(tCamera);
return convert_mat4_to_double4x4(camera->getModelMatrix());
}
EMSCRIPTEN_KEEPALIVE double4x4 Camera_getViewMatrix(TCamera *const tCamera)
{
auto *camera = reinterpret_cast<Camera *>(tCamera);
return convert_mat4_to_double4x4(camera->getViewMatrix());
}
EMSCRIPTEN_KEEPALIVE EntityId Camera_getEntity(TCamera *tCamera)
{
auto *camera = reinterpret_cast<Camera *>(tCamera);
return utils::Entity::smuggle(camera->getEntity());
}
EMSCRIPTEN_KEEPALIVE double Camera_getFocalLength(TCamera *const tCamera)
{
auto *camera = reinterpret_cast<Camera *>(tCamera);
return camera->getFocalLength() * 1000.0;
}
EMSCRIPTEN_KEEPALIVE double Camera_getNear(TCamera *const tCamera)
{
auto *camera = reinterpret_cast<Camera *>(tCamera);
return camera->getNear();
}
EMSCRIPTEN_KEEPALIVE double Camera_getCullingFar(TCamera *const tCamera)
{
auto *camera = reinterpret_cast<Camera *>(tCamera);
return camera->getCullingFar();
}
EMSCRIPTEN_KEEPALIVE void Camera_setProjection(TCamera *const tCamera, Projection projection, double left, double right,
double bottom, double top,
double near, double far)
{
auto *camera = reinterpret_cast<Camera *>(tCamera);
filament::Camera::Projection filamentProjection;
switch(projection) {
case Projection::Orthographic:
filamentProjection = filament::Camera::Projection::ORTHO;
case Projection::Perspective:
filamentProjection = filament::Camera::Projection::PERSPECTIVE;
}
camera->setProjection(filamentProjection, left, right, bottom, top, near, far);
}
#ifdef __cplusplus
}
}
#endif

View File

@@ -0,0 +1,41 @@
#include <filament/View.h>
#include <filament/Engine.h>
#include <filament/Scene.h>
#include "c_api/TGizmo.h"
#include "scene/Gizmo.hpp"
#include "Log.hpp"
#ifdef __cplusplus
namespace thermion
{
extern "C"
{
using namespace filament;
#endif
EMSCRIPTEN_KEEPALIVE void Gizmo_pick(TGizmo *tGizmo, uint32_t x, uint32_t y, GizmoPickCallback callback)
{
auto *gizmo = reinterpret_cast<Gizmo *>(tGizmo);
gizmo->pick(x, y, reinterpret_cast<Gizmo::GizmoPickCallback>(callback));
}
EMSCRIPTEN_KEEPALIVE void Gizmo_highlight(TGizmo *tGizmo, TGizmoAxis tAxis)
{
auto *gizmo = reinterpret_cast<Gizmo *>(tGizmo);
auto axis = static_cast<Gizmo::Axis>(tAxis);
gizmo->highlight(axis);
}
EMSCRIPTEN_KEEPALIVE void Gizmo_unhighlight(TGizmo *tGizmo)
{
auto *gizmo = reinterpret_cast<Gizmo *>(tGizmo);
gizmo->unhighlight(Gizmo::Axis::X);
gizmo->unhighlight(Gizmo::Axis::Y);
gizmo->unhighlight(Gizmo::Axis::Z);
}
#ifdef __cplusplus
}
}
#endif

View File

@@ -0,0 +1,136 @@
#include <filament/MaterialInstance.h>
#include <math/mat4.h>
#include <math/vec4.h>
#include <math/vec2.h>
#include "Log.hpp"
#include "c_api/TMaterialInstance.h"
#ifdef __cplusplus
namespace thermion
{
extern "C"
{
#endif
EMSCRIPTEN_KEEPALIVE bool MaterialInstance_isStencilWriteEnabled(TMaterialInstance *tMaterialInstance)
{
return reinterpret_cast<::filament::MaterialInstance *>(tMaterialInstance)->isStencilWriteEnabled();
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setDepthWrite(TMaterialInstance *materialInstance, bool enabled)
{
reinterpret_cast<::filament::MaterialInstance *>(materialInstance)->setDepthWrite(enabled);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setDepthCulling(TMaterialInstance *materialInstance, bool enabled)
{
reinterpret_cast<::filament::MaterialInstance *>(materialInstance)->setDepthCulling(enabled);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat4(TMaterialInstance *tMaterialInstance, const char *propertyName, double x, double y, double z, double w)
{
auto *materialInstance = reinterpret_cast<::filament::MaterialInstance *>(tMaterialInstance);
filament::math::float4 data{static_cast<float>(x), static_cast<float>(y), static_cast<float>(z), static_cast<float>(w)};
materialInstance->setParameter(propertyName, data);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat2(TMaterialInstance *materialInstance, const char *propertyName, double x, double y)
{
filament::math::float2 data{static_cast<float>(x), static_cast<float>(y)};
reinterpret_cast<::filament::MaterialInstance *>(materialInstance)->setParameter(propertyName, data);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterFloat(TMaterialInstance *materialInstance, const char *propertyName, double value)
{
reinterpret_cast<::filament::MaterialInstance *>(materialInstance)->setParameter(propertyName, static_cast<float>(value));
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setParameterInt(TMaterialInstance *materialInstance, const char *propertyName, int value)
{
reinterpret_cast<::filament::MaterialInstance *>(materialInstance)->setParameter(propertyName, value);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setDepthFunc(TMaterialInstance *tMaterialInstance, TSamplerCompareFunc tDepthFunc)
{
auto *materialInstance = reinterpret_cast<::filament::MaterialInstance *>(tMaterialInstance);
auto depthFunc = static_cast<filament::MaterialInstance::DepthFunc>(tDepthFunc);
materialInstance->setDepthFunc(depthFunc);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setStencilOpStencilFail(TMaterialInstance *tMaterialInstance,
TStencilOperation tOp, TStencilFace tFace)
{
auto *materialInstance = reinterpret_cast<::filament::MaterialInstance *>(tMaterialInstance);
auto op = static_cast<filament::MaterialInstance::StencilOperation>(tOp);
auto face = static_cast<filament::MaterialInstance::StencilFace>(tFace);
materialInstance->setStencilOpStencilFail(op, face);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setStencilOpDepthFail(TMaterialInstance *tMaterialInstance,
TStencilOperation tOp, TStencilFace tFace)
{
auto *materialInstance = reinterpret_cast<::filament::MaterialInstance *>(tMaterialInstance);
auto op = static_cast<filament::MaterialInstance::StencilOperation>(tOp);
auto face = static_cast<filament::MaterialInstance::StencilFace>(tFace);
materialInstance->setStencilOpDepthFail(op, face);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setStencilOpDepthStencilPass(TMaterialInstance *tMaterialInstance,
TStencilOperation tOp, TStencilFace tFace)
{
auto *materialInstance = reinterpret_cast<::filament::MaterialInstance *>(tMaterialInstance);
auto op = static_cast<filament::MaterialInstance::StencilOperation>(tOp);
auto face = static_cast<filament::MaterialInstance::StencilFace>(tFace);
materialInstance->setStencilOpDepthStencilPass(op, face);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setStencilCompareFunction(TMaterialInstance *tMaterialInstance,
TSamplerCompareFunc tFunc, TStencilFace tFace)
{
auto *materialInstance = reinterpret_cast<::filament::MaterialInstance *>(tMaterialInstance);
auto func = static_cast<filament::MaterialInstance::StencilCompareFunc>(tFunc);
auto face = static_cast<filament::MaterialInstance::StencilFace>(tFace);
materialInstance->setStencilCompareFunction(func, face);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setStencilReferenceValue(TMaterialInstance *tMaterialInstance,
uint8_t value, TStencilFace tFace)
{
auto *materialInstance = reinterpret_cast<::filament::MaterialInstance *>(tMaterialInstance);
auto face = static_cast<filament::MaterialInstance::StencilFace>(tFace);
materialInstance->setStencilReferenceValue(value, face);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setStencilWrite(TMaterialInstance *materialInstance, bool enabled)
{
reinterpret_cast<::filament::MaterialInstance *>(materialInstance)->setStencilWrite(enabled);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setCullingMode(TMaterialInstance *materialInstance, TCullingMode culling)
{
auto *instance = reinterpret_cast<::filament::MaterialInstance *>(materialInstance);
auto cullingMode = static_cast<filament::MaterialInstance::CullingMode>(culling);
instance->setCullingMode(cullingMode);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setStencilReadMask(
TMaterialInstance *materialInstance,
uint8_t mask)
{
auto *instance = reinterpret_cast<::filament::MaterialInstance *>(materialInstance);
instance->setStencilReadMask(mask);
}
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setStencilWriteMask(
TMaterialInstance *materialInstance,
uint8_t mask)
{
auto *instance = reinterpret_cast<::filament::MaterialInstance *>(materialInstance);
instance->setStencilWriteMask(mask);
}
#ifdef __cplusplus
}
}
#endif

View File

@@ -0,0 +1,57 @@
#include <filament/MaterialInstance.h>
#include <gltfio/MaterialProvider.h>
#include <math/mat4.h>
#include <math/vec4.h>
#include <math/vec2.h>
#include "Log.hpp"
#include "c_api/TMaterialProvider.h"
#include "c_api/TMaterialInstance.h"
#ifdef __cplusplus
namespace thermion
{
using namespace filament;
extern "C"
{
#endif
EMSCRIPTEN_KEEPALIVE TMaterialInstance *MaterialProvider_createMaterialInstance(TMaterialProvider *tMaterialProvider, TMaterialKey *materialConfig)
{
gltfio::MaterialKey config;
gltfio::UvMap uvMap;
memset(&config, 0, sizeof(gltfio::MaterialKey));
// Set and log each field
config.unlit = materialConfig->unlit;
config.doubleSided = materialConfig->doubleSided;
config.useSpecularGlossiness = materialConfig->useSpecularGlossiness;
config.alphaMode = static_cast<filament::gltfio::AlphaMode>(materialConfig->alphaMode);
config.hasBaseColorTexture = materialConfig->hasBaseColorTexture;
config.hasClearCoat = materialConfig->hasClearCoat;
config.hasClearCoatNormalTexture = materialConfig->hasClearCoatNormalTexture;
config.hasClearCoatRoughnessTexture = materialConfig->hasClearCoatRoughnessTexture;
config.hasEmissiveTexture = materialConfig->hasEmissiveTexture;
config.hasIOR = materialConfig->hasIOR;
config.hasMetallicRoughnessTexture = materialConfig->hasMetallicRoughnessTexture;
config.hasNormalTexture = materialConfig->hasNormalTexture;
config.hasOcclusionTexture = materialConfig->hasOcclusionTexture;
config.hasSheen = materialConfig->hasSheen;
config.hasSheenColorTexture = materialConfig->hasSheenColorTexture;
config.hasSheenRoughnessTexture = materialConfig->hasSheenRoughnessTexture;
config.hasTextureTransforms = materialConfig->hasTextureTransforms;
config.hasTransmission = materialConfig->hasTransmission;
config.hasTransmissionTexture = materialConfig->hasTransmissionTexture;
config.hasVolume = materialConfig->hasVolume;
config.hasVolumeThicknessTexture = materialConfig->hasVolumeThicknessTexture;
config.baseColorUV = materialConfig->baseColorUV;
config.hasVertexColors = materialConfig->hasVertexColors;
auto *materialProvider = reinterpret_cast<gltfio::MaterialProvider *>(tMaterialProvider);
auto materialInstance = materialProvider->createMaterialInstance(&config, &uvMap);
return reinterpret_cast<TMaterialInstance *>(materialInstance);
}
#ifdef __cplusplus
}
}
#endif

View File

@@ -0,0 +1,20 @@
#include <utils/NameComponentManager.h>
#include "c_api/APIExport.h"
#include "c_api/APIBoundaryTypes.h"
#ifdef __cplusplus
extern "C"
{
#endif
EMSCRIPTEN_KEEPALIVE const char *NameComponentManager_getName(TNameComponentManager *tNameComponentManager, EntityId entity)
{
auto ncm = reinterpret_cast<utils::NameComponentManager *>(tNameComponentManager);
auto instance = ncm->getInstance(utils::Entity::import(entity));
return ncm->getName(instance);
}
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,44 @@
#include <filament/MaterialInstance.h>
#include <filament/RenderableManager.h>
#include <utils/Entity.h>
#include "Log.hpp"
#include "c_api/TRenderableManager.h"
namespace thermion
{
extern "C"
{
using namespace filament;
using namespace utils;
EMSCRIPTEN_KEEPALIVE void RenderableManager_setMaterialInstanceAt(TRenderableManager *tRenderableManager, EntityId entityId, int primitiveIndex, TMaterialInstance *tMaterialInstance)
{
auto *renderableManager = reinterpret_cast<filament::RenderableManager *>(tRenderableManager);
const auto &entity = utils::Entity::import(entityId);
auto renderableInstance = renderableManager->getInstance(entity);
auto materialInstance = reinterpret_cast<MaterialInstance *>(tMaterialInstance);
renderableManager->setMaterialInstanceAt(renderableInstance, primitiveIndex, materialInstance);
}
EMSCRIPTEN_KEEPALIVE TMaterialInstance *RenderableManager_getMaterialInstanceAt(TRenderableManager *tRenderableManager, EntityId entityId, int primitiveIndex) {
auto *renderableManager = reinterpret_cast<filament::RenderableManager *>(tRenderableManager);
const auto &entity = utils::Entity::import(entityId);
auto renderableInstance = renderableManager->getInstance(entity);
if(!renderableInstance.isValid()) {
return nullptr;
}
auto materialInstance = renderableManager->getMaterialInstanceAt(renderableInstance, primitiveIndex);
return reinterpret_cast<TMaterialInstance*>(materialInstance);
}
EMSCRIPTEN_KEEPALIVE void RenderableManager_setPriority(TRenderableManager *tRenderableManager, EntityId entityId, int priority) {
auto *renderableManager = reinterpret_cast<filament::RenderableManager *>(tRenderableManager);
const auto &entity = utils::Entity::import(entityId);
auto renderableInstance = renderableManager->getInstance(entity);
renderableManager->setPriority(renderableInstance, priority);
}
}
}

View File

@@ -0,0 +1,108 @@
#include "c_api/TSceneAsset.h"
#include "scene/SceneAsset.hpp"
#include "scene/GltfSceneAsset.hpp"
using namespace thermion;
#ifdef __cplusplus
extern "C"
{
#endif
EMSCRIPTEN_KEEPALIVE void SceneAsset_addToScene(TSceneAsset *tSceneAsset, TScene *tScene) {
auto *asset = reinterpret_cast<SceneAsset*>(tSceneAsset);
auto *scene = reinterpret_cast<Scene*>(tScene);
asset->addAllEntities(scene);
}
EMSCRIPTEN_KEEPALIVE EntityId SceneAsset_getEntity(TSceneAsset *tSceneAsset) {
auto *asset = reinterpret_cast<SceneAsset*>(tSceneAsset);
return utils::Entity::smuggle(asset->getEntity());
}
EMSCRIPTEN_KEEPALIVE int SceneAsset_getChildEntityCount(TSceneAsset* tSceneAsset) {
auto *asset = reinterpret_cast<SceneAsset*>(tSceneAsset);
return asset->getChildEntityCount();
}
EMSCRIPTEN_KEEPALIVE void SceneAsset_getChildEntities(TSceneAsset* tSceneAsset, EntityId *out)
{
auto *asset = reinterpret_cast<SceneAsset*>(tSceneAsset);
auto entities = asset->getChildEntities();
for(int i = 0; i < asset->getChildEntityCount(); i++) {
out[i] = utils::Entity::smuggle(entities[i]);
}
}
EMSCRIPTEN_KEEPALIVE const utils::Entity *SceneAsset_getCameraEntities(TSceneAsset* tSceneAsset)
{
auto *asset = reinterpret_cast<SceneAsset*>(tSceneAsset);
if (asset->getType() == SceneAsset::SceneAssetType::Gltf && !asset->isInstance())
{
auto gltfSceneAsset = reinterpret_cast<GltfSceneAsset *>(asset);
return gltfSceneAsset->getAsset()->getCameraEntities();
}
else
{
return std::nullptr_t();
}
}
EMSCRIPTEN_KEEPALIVE size_t SceneAsset_getCameraEntityCount(TSceneAsset* tSceneAsset)
{
auto *asset = reinterpret_cast<SceneAsset*>(tSceneAsset);
if (asset->getType() == SceneAsset::SceneAssetType::Gltf && !asset->isInstance())
{
auto gltfSceneAsset = reinterpret_cast<GltfSceneAsset *>(asset);
return gltfSceneAsset->getAsset()->getCameraEntityCount();
}
return -1;
}
EMSCRIPTEN_KEEPALIVE const utils::Entity *SceneAsset_getLightEntities(TSceneAsset* tSceneAsset)
{
auto *asset = reinterpret_cast<SceneAsset*>(tSceneAsset);
if (asset->getType() == SceneAsset::SceneAssetType::Gltf && !asset->isInstance())
{
auto gltfSceneAsset = reinterpret_cast<GltfSceneAsset *>(asset);
return gltfSceneAsset->getAsset()->getLightEntities();
}
return std::nullptr_t();
}
EMSCRIPTEN_KEEPALIVE size_t SceneAsset_getLightEntityCount(TSceneAsset* tSceneAsset)
{
auto *asset = reinterpret_cast<SceneAsset*>(tSceneAsset);
if (asset->getType() == SceneAsset::SceneAssetType::Gltf && !asset->isInstance())
{
auto gltfSceneAsset = reinterpret_cast<GltfSceneAsset *>(asset);
return gltfSceneAsset->getAsset()->getLightEntityCount();
}
return -1;
}
EMSCRIPTEN_KEEPALIVE TSceneAsset *SceneAsset_getInstance(TSceneAsset *tSceneAsset, int index) {
auto *asset = reinterpret_cast<SceneAsset*>(tSceneAsset);
auto *instance = asset->getInstanceAt(index);
return reinterpret_cast<TSceneAsset*>(instance);
}
EMSCRIPTEN_KEEPALIVE TSceneAsset *SceneAsset_createInstance(TSceneAsset *tSceneAsset, TMaterialInstance **tMaterialInstances, int materialInstanceCount)
{
auto *materialInstances = reinterpret_cast<MaterialInstance **>(tMaterialInstances);
auto *sceneAsset = reinterpret_cast<SceneAsset*>(tSceneAsset);
auto *instance = sceneAsset->createInstance(materialInstances, materialInstanceCount);
return reinterpret_cast<TSceneAsset *>(instance);
}
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,219 @@
#include <filament/LightManager.h>
#include "c_api/APIExport.h"
#include "ResourceBuffer.hpp"
#include "FilamentViewer.hpp"
#include "Log.hpp"
using namespace thermion;
extern "C"
{
#include "c_api/TSceneManager.h"
EMSCRIPTEN_KEEPALIVE TScene *SceneManager_getScene(TSceneManager *tSceneManager) {
auto sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
return reinterpret_cast<TScene*>(sceneManager->getScene());
}
EMSCRIPTEN_KEEPALIVE TMaterialProvider *SceneManager_getUnlitMaterialProvider(TSceneManager *tSceneManager) {
auto sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
auto provider = sceneManager->getUnlitMaterialProvider();
return reinterpret_cast<TMaterialProvider*>(provider);
}
EMSCRIPTEN_KEEPALIVE TMaterialProvider *SceneManager_getUbershaderMaterialProvider(TSceneManager *tSceneManager) {
auto sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
auto provider = sceneManager->getUbershaderMaterialProvider();
return reinterpret_cast<TMaterialProvider*>(provider);
}
EMSCRIPTEN_KEEPALIVE TGizmo *SceneManager_createGizmo(TSceneManager *tSceneManager, TView *tView, TScene *tScene)
{
auto sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
auto *scene = reinterpret_cast<Scene *>(tScene);
auto *view = reinterpret_cast<View *>(tView);
auto gizmo = sceneManager->createGizmo(view, scene);
return reinterpret_cast<TGizmo *>(gizmo);
}
EMSCRIPTEN_KEEPALIVE TSceneAsset *SceneManager_loadGlb(TSceneManager *tSceneManager, const char *assetPath, int numInstances, bool keepData)
{
auto *sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
auto *asset = sceneManager->loadGlb(assetPath, numInstances, keepData);
return reinterpret_cast<TSceneAsset *>(asset);
}
EMSCRIPTEN_KEEPALIVE TSceneAsset *SceneManager_loadGltf(TSceneManager *tSceneManager,
const char *assetPath,
const char *relativeResourcePath,
bool keepData)
{
auto *sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
auto *asset = sceneManager->loadGltf(assetPath, relativeResourcePath, 1, keepData);
return reinterpret_cast<TSceneAsset *>(asset);
}
EMSCRIPTEN_KEEPALIVE TSceneAsset *SceneManager_loadGlbFromBuffer(TSceneManager *tSceneManager, const uint8_t *const data, size_t length, bool keepData, int priority, int layer, bool loadResourcesAsync)
{
auto *sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
auto *asset = sceneManager->loadGlbFromBuffer((const uint8_t *)data, length, 1, keepData, priority, layer, loadResourcesAsync);
return reinterpret_cast<TSceneAsset *>(asset);
}
EMSCRIPTEN_KEEPALIVE TCamera *SceneManager_getCameraByName(TSceneManager *tSceneManager, EntityId entityId, const char *name)
{
auto *sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
return nullptr;
}
EMSCRIPTEN_KEEPALIVE Aabb3 SceneManager_getRenderableBoundingBox(TSceneManager *tSceneManager, EntityId entity)
{
auto sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
return sceneManager->getRenderableBoundingBox(entity);
}
EMSCRIPTEN_KEEPALIVE void SceneManager_setVisibilityLayer(TSceneManager *tSceneManager, EntityId entity, int layer)
{
auto *sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
sceneManager->setVisibilityLayer(entity, layer);
}
EMSCRIPTEN_KEEPALIVE TMaterialInstance *SceneManager_createUnlitMaterialInstance(TSceneManager *sceneManager)
{
auto *instance = ((SceneManager *)sceneManager)->createUnlitMaterialInstance();
return reinterpret_cast<TMaterialInstance *>(instance);
}
EMSCRIPTEN_KEEPALIVE TMaterialInstance *SceneManager_createUnlitFixedSizeMaterialInstance(TSceneManager *sceneManager)
{
auto *instance = ((SceneManager *)sceneManager)->createUnlitFixedSizeMaterialInstance();
return reinterpret_cast<TMaterialInstance *>(instance);
}
EMSCRIPTEN_KEEPALIVE TCamera *SceneManager_createCamera(TSceneManager *tSceneManager)
{
auto *sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
return reinterpret_cast<TCamera *>(sceneManager->createCamera());
}
EMSCRIPTEN_KEEPALIVE void SceneManager_destroyCamera(TSceneManager *tSceneManager, TCamera *tCamera)
{
auto *sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
auto *camera = reinterpret_cast<Camera *>(tCamera);
sceneManager->destroyCamera(camera);
}
EMSCRIPTEN_KEEPALIVE size_t SceneManager_getCameraCount(TSceneManager *tSceneManager)
{
auto *sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
return sceneManager->getCameraCount();
}
EMSCRIPTEN_KEEPALIVE TCamera *SceneManager_getCameraAt(TSceneManager *tSceneManager, size_t index)
{
auto *sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
auto *camera = sceneManager->getCameraAt(index);
return reinterpret_cast<TCamera *>(camera);
}
EMSCRIPTEN_KEEPALIVE TSceneAsset *SceneManager_createGeometry(
TSceneManager *tSceneManager,
float *vertices,
int numVertices,
float *normals,
int numNormals,
float *uvs,
int numUvs,
uint16_t *indices,
int numIndices,
int primitiveType,
TMaterialInstance **tMaterialInstances,
int materialInstanceCount,
bool keepData)
{
auto sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
auto castedNumVertices = static_cast<uint32_t>(numVertices);
auto castedNumNormals = static_cast<uint32_t>(numNormals);
auto castedNumUvs = static_cast<uint32_t>(numUvs);
auto castedNumIndices = static_cast<uint32_t>(numIndices);
auto castedPrimitiveType = static_cast<filament::RenderableManager::PrimitiveType>(primitiveType);
auto materialInstances = reinterpret_cast<MaterialInstance **>(tMaterialInstances);
auto *asset = sceneManager->createGeometry(
vertices,
castedNumVertices,
normals,
castedNumNormals,
uvs,
castedNumUvs,
indices,
castedNumIndices,
castedPrimitiveType,
materialInstances,
materialInstanceCount,
keepData);
return reinterpret_cast<TSceneAsset *>(asset);
}
EMSCRIPTEN_KEEPALIVE void SceneManager_destroyMaterialInstance(TSceneManager *sceneManager, TMaterialInstance *instance)
{
((SceneManager *)sceneManager)->destroy(reinterpret_cast<MaterialInstance *>(instance));
}
EMSCRIPTEN_KEEPALIVE int SceneManager_removeFromScene(TSceneManager *sceneManager, EntityId entityId)
{
return ((SceneManager *)sceneManager)->removeFromScene(entityId);
}
EMSCRIPTEN_KEEPALIVE int SceneManager_addToScene(TSceneManager *sceneManager, EntityId entityId)
{
return ((SceneManager *)sceneManager)->addToScene(entityId);
}
EMSCRIPTEN_KEEPALIVE void SceneManager_transformToUnitCube(TSceneManager *sceneManager, EntityId entityId)
{
((SceneManager *)sceneManager)->transformToUnitCube(entityId);
}
EMSCRIPTEN_KEEPALIVE TAnimationManager *SceneManager_getAnimationManager(TSceneManager *tSceneManager)
{
auto *sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
auto *animationManager = sceneManager->getAnimationManager();
return reinterpret_cast<TAnimationManager *>(animationManager);
}
EMSCRIPTEN_KEEPALIVE void *SceneManager_destroyAll(TSceneManager *tSceneManager)
{
auto *sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
sceneManager->destroyAll();
return nullptr;
}
EMSCRIPTEN_KEEPALIVE void *SceneManager_destroyAsset(TSceneManager *tSceneManager, TSceneAsset *tSceneAsset)
{
auto *sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
auto *sceneAsset = reinterpret_cast<SceneAsset *>(tSceneAsset);
sceneManager->destroy(sceneAsset);
return nullptr;
}
EMSCRIPTEN_KEEPALIVE TNameComponentManager *SceneManager_getNameComponentManager(TSceneManager *tSceneManager) {
auto *sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
return reinterpret_cast<TNameComponentManager*>(sceneManager->getNameComponentManager());
}
EMSCRIPTEN_KEEPALIVE size_t SceneManager_getOverlayEntityCount(TSceneManager *tSceneManager) {
auto *sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
return sceneManager->getOverlayEntityCount();
}
EMSCRIPTEN_KEEPALIVE EntityId SceneManager_getOverlayEntityAt(TSceneManager *tSceneManager, size_t index) {
auto *sceneManager = reinterpret_cast<SceneManager *>(tSceneManager);
auto entity = sceneManager->getOverlayEntity(index);
return utils::Entity::smuggle(entity);
}
}

View File

@@ -0,0 +1,155 @@
#include <utils/Entity.h>
#include <filament/TransformManager.h>
#include <filament/math/mat4.h>
#include <gltfio/math.h>
#include "c_api/APIExport.h"
#include "MathUtils.hpp"
#include "Log.hpp"
using namespace thermion;
extern "C"
{
using namespace filament;
using namespace utils;
using namespace filament::gltfio;
#include "c_api/TTransformManager.h"
EMSCRIPTEN_KEEPALIVE double4x4 TransformManager_getLocalTransform(TTransformManager *tTransformManager, EntityId entityId)
{
auto *transformManager = reinterpret_cast<filament::TransformManager *>(tTransformManager);
const auto &entity = utils::Entity::import(entityId);
auto transformInstance = transformManager->getInstance(entity);
if (!transformInstance)
{
Log("Failed to find transform instance");
return double4x4();
}
auto transform = transformManager->getTransformAccurate(transformInstance);
return convert_mat4_to_double4x4(transform);
}
EMSCRIPTEN_KEEPALIVE double4x4 TransformManager_getWorldTransform(TTransformManager *tTransformManager, EntityId entityId)
{
auto *transformManager = reinterpret_cast<filament::TransformManager *>(tTransformManager);
const auto &entity = utils::Entity::import(entityId);
auto transformInstance = transformManager->getInstance(entity);
if (!transformInstance)
{
Log("Failed to find transform instance");
return double4x4();
}
auto transform = transformManager->getWorldTransformAccurate(transformInstance);
return convert_mat4_to_double4x4(transform);
}
EMSCRIPTEN_KEEPALIVE void TransformManager_setTransform(TTransformManager *tTransformManager, EntityId entityId, double4x4 transform)
{
auto *transformManager = reinterpret_cast<filament::TransformManager *>(tTransformManager);
const auto &entity = utils::Entity::import(entityId);
auto transformInstance = transformManager->getInstance(entity);
if (!transformInstance)
{
return;
}
transformManager->setTransform(transformInstance, convert_double4x4_to_mat4(transform));
}
// EMSCRIPTEN_KEEPALIVE void TransformManager_transformToUnitCube(TTransformManager *tTransformManager, EntityId entityId) {
// auto *transformManager = reinterpret_cast<filament::TransformManager*>(tTransformManager);
// const auto &entity = utils::Entity::import(entityId);
// auto transformInstance = transformManager->getInstance(entity);
// if (!transformInstance)
// {
// return;
// }
// auto aabb = instance->getBoundingBox();
// auto center = aabb.center();
// auto halfExtent = aabb.extent();
// auto maxExtent = max(halfExtent) * 2;
// auto scaleFactor = 2.0f / maxExtent;
// auto transform = math::mat4f::scaling(scaleFactor) * math::mat4f::translation(-center);
// tm.setTransform(tm.getInstance(instance->getRoot()), transform);
// }
EMSCRIPTEN_KEEPALIVE void TransformManager_setParent(TTransformManager *tTransformManager, EntityId childId, EntityId parentId, bool preserveScaling)
{
auto tm = reinterpret_cast<TransformManager *>(tTransformManager);
const auto child = Entity::import(childId);
const auto parent = Entity::import(parentId);
const auto &parentInstance = tm->getInstance(parent);
const auto &childInstance = tm->getInstance(child);
if (!parentInstance.isValid())
{
Log("Parent instance is not valid");
return;
}
if (!childInstance.isValid())
{
Log("Child instance is not valid");
return;
}
if (preserveScaling)
{
auto parentTransform = tm->getWorldTransform(parentInstance);
math::float3 parentTranslation;
math::quatf parentRotation;
math::float3 parentScale;
decomposeMatrix(parentTransform, &parentTranslation, &parentRotation, &parentScale);
auto childTransform = tm->getTransform(childInstance);
math::float3 childTranslation;
math::quatf childRotation;
math::float3 childScale;
decomposeMatrix(childTransform, &childTranslation, &childRotation, &childScale);
childScale = childScale * (1 / parentScale);
childTransform = composeMatrix(childTranslation, childRotation, childScale);
tm->setTransform(childInstance, childTransform);
}
tm->setParent(childInstance, parentInstance);
}
EMSCRIPTEN_KEEPALIVE EntityId TransformManager_getParent(TTransformManager *tTransformManager, EntityId childId)
{
auto tm = reinterpret_cast<TransformManager *>(tTransformManager);
const auto child = Entity::import(childId);
const auto &childInstance = tm->getInstance(child);
return Entity::smuggle(tm->getParent(childInstance));
}
EMSCRIPTEN_KEEPALIVE EntityId TransformManager_getAncestor(TTransformManager *tTransformManager, EntityId childEntityId)
{
auto tm = reinterpret_cast<TransformManager *>(tTransformManager);
const auto child = Entity::import(childEntityId);
auto transformInstance = tm->getInstance(child);
Entity parent;
while (true)
{
auto newParent = tm->getParent(transformInstance);
if (newParent.isNull())
{
break;
}
parent = newParent;
transformInstance = tm->getInstance(parent);
}
return Entity::smuggle(parent);
}
}

View File

@@ -0,0 +1,181 @@
#include <filament/View.h>
#include <filament/Viewport.h>
#include <filament/Engine.h>
#include <filament/ToneMapper.h>
#include <filament/ColorGrading.h>
#include <filament/Camera.h>
#include "c_api/ThermionDartApi.h"
#include "c_api/TView.h"
#include "Log.hpp"
#ifdef __cplusplus
namespace thermion {
extern "C"
{
using namespace filament;
#endif
EMSCRIPTEN_KEEPALIVE TViewport View_getViewport(TView *tView)
{
auto view = reinterpret_cast<View *>(tView);
auto & vp = view->getViewport();
TViewport tvp;
tvp.left = vp.left;
tvp.bottom = vp.bottom;
tvp.width = vp.width;
tvp.height = vp.height;
return tvp;
}
EMSCRIPTEN_KEEPALIVE void View_updateViewport(TView *tView, uint32_t width, uint32_t height)
{
auto view = reinterpret_cast<View *>(tView);
view->setViewport({0, 0, width, height});
}
EMSCRIPTEN_KEEPALIVE void View_setRenderTarget(TView *tView, TRenderTarget *tRenderTarget)
{
auto view = reinterpret_cast<View *>(tView);
auto renderTarget = reinterpret_cast<RenderTarget *>(tRenderTarget);
view->setRenderTarget(renderTarget);
}
EMSCRIPTEN_KEEPALIVE void View_setFrustumCullingEnabled(TView *tView, bool enabled)
{
auto view = reinterpret_cast<View *>(tView);
view->setFrustumCullingEnabled(enabled);
}
EMSCRIPTEN_KEEPALIVE void View_setPostProcessing(TView *tView, bool enabled)
{
auto view = reinterpret_cast<View *>(tView);
view->setPostProcessingEnabled(enabled);
}
EMSCRIPTEN_KEEPALIVE void View_setShadowsEnabled(TView *tView, bool enabled)
{
auto view = reinterpret_cast<View *>(tView);
view->setShadowingEnabled(enabled);
}
EMSCRIPTEN_KEEPALIVE void View_setShadowType(TView *tView, int shadowType)
{
auto view = reinterpret_cast<View *>(tView);
view->setShadowType((ShadowType)shadowType);
}
EMSCRIPTEN_KEEPALIVE void View_setSoftShadowOptions(TView *tView, float penumbraScale, float penumbraRatioScale)
{
auto view = reinterpret_cast<View *>(tView);
SoftShadowOptions opts;
opts.penumbraRatioScale = penumbraRatioScale;
opts.penumbraScale = penumbraScale;
view->setSoftShadowOptions(opts);
}
EMSCRIPTEN_KEEPALIVE void View_setBloom(TView *tView, float strength)
{
auto view = reinterpret_cast<View *>(tView);
#ifndef __EMSCRIPTEN__
decltype(view->getBloomOptions()) opts;
opts.enabled = true;
opts.strength = strength;
view->setBloomOptions(opts);
#endif
}
EMSCRIPTEN_KEEPALIVE void View_setToneMapping(TView *tView, TEngine *tEngine, ToneMapping toneMapping)
{
auto view = reinterpret_cast<View *>(tView);
auto engine = reinterpret_cast<Engine *>(tEngine);
ToneMapper *tm;
switch (toneMapping)
{
case ToneMapping::ACES:
Log("Setting tone mapping to ACES");
tm = new ACESToneMapper();
break;
case ToneMapping::LINEAR:
Log("Setting tone mapping to Linear");
tm = new LinearToneMapper();
break;
case ToneMapping::FILMIC:
Log("Setting tone mapping to Filmic");
tm = new FilmicToneMapper();
break;
default:
Log("ERROR: Unsupported tone mapping");
return;
}
auto newColorGrading = ColorGrading::Builder().toneMapper(tm).build(*engine);
auto oldColorGrading = view->getColorGrading();
view->setColorGrading(newColorGrading);
if (oldColorGrading)
{
engine->destroy(oldColorGrading);
}
delete tm;
}
void View_setAntiAliasing(TView *tView, bool msaa, bool fxaa, bool taa)
{
auto view = reinterpret_cast<View *>(tView);
View::MultiSampleAntiAliasingOptions multiSampleAntiAliasingOptions;
multiSampleAntiAliasingOptions.enabled = msaa;
view->setMultiSampleAntiAliasingOptions(multiSampleAntiAliasingOptions);
TemporalAntiAliasingOptions taaOpts;
taaOpts.enabled = taa;
view->setTemporalAntiAliasingOptions(taaOpts);
view->setAntiAliasing(fxaa ? AntiAliasing::FXAA : AntiAliasing::NONE);
}
EMSCRIPTEN_KEEPALIVE void View_setLayerEnabled(TView* tView, int layer, bool enabled) {
auto view = reinterpret_cast<View *>(tView);
view->setLayerEnabled(layer, enabled);
}
EMSCRIPTEN_KEEPALIVE void View_setCamera(TView *tView, TCamera *tCamera) {
auto view = reinterpret_cast<View *>(tView);
auto *camera = reinterpret_cast<Camera *>(tCamera);
view->setCamera(camera);
}
EMSCRIPTEN_KEEPALIVE TScene* View_getScene(TView* tView) {
auto view = reinterpret_cast<View *>(tView);
return reinterpret_cast<TScene*>(view->getScene());
}
EMSCRIPTEN_KEEPALIVE TCamera* View_getCamera(TView *tView) {
auto view = reinterpret_cast<View *>(tView);
return reinterpret_cast<TCamera*>(&(view->getCamera()));
}
EMSCRIPTEN_KEEPALIVE void View_setStencilBufferEnabled(TView *tView, bool enabled) {
auto view = reinterpret_cast<View *>(tView);
view->setStencilBufferEnabled(enabled);
}
EMSCRIPTEN_KEEPALIVE bool View_isStencilBufferEnabled(TView *tView) {
auto view = reinterpret_cast<View *>(tView);
return view->isStencilBufferEnabled();
}
EMSCRIPTEN_KEEPALIVE void View_pick(TView *tView, uint32_t requestId, uint32_t x, uint32_t y, PickCallback callback)
{
auto *view = reinterpret_cast<View *>(tView);
view->pick(x, y, [=](filament::View::PickingQueryResult const &result) {
callback(requestId, utils::Entity::smuggle(result.renderable), result.depth, result.fragCoords.x, result.fragCoords.y, result.fragCoords.z);
});
}
#ifdef __cplusplus
}
}
#endif

View File

@@ -0,0 +1,511 @@
#ifdef _WIN32
#include "ThermionWin32.h"
#endif
#include <thread>
#include <functional>
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
#include "filament/LightManager.h"
#include "ResourceBuffer.hpp"
#include "FilamentViewer.hpp"
#include "Log.hpp"
#include "ThreadPool.hpp"
using namespace thermion;
extern "C"
{
#include "c_api/ThermionDartApi.h"
EMSCRIPTEN_KEEPALIVE TViewer *Viewer_create(const void *context, const void *const loader, void *const platform, const char *uberArchivePath)
{
const auto *loaderImpl = new ResourceLoaderWrapperImpl((ResourceLoaderWrapper *)loader);
auto viewer = new FilamentViewer(context, loaderImpl, platform, uberArchivePath);
return reinterpret_cast<TViewer *>(viewer);
}
EMSCRIPTEN_KEEPALIVE TEngine *Viewer_getEngine(TViewer *viewer)
{
auto *engine = reinterpret_cast<FilamentViewer *>(viewer)->getEngine();
return reinterpret_cast<TEngine *>(engine);
}
EMSCRIPTEN_KEEPALIVE TRenderTarget *Viewer_createRenderTarget(TViewer *tViewer, intptr_t texture, uint32_t width, uint32_t height)
{
auto viewer = reinterpret_cast<FilamentViewer *>(tViewer);
auto renderTarget = viewer->createRenderTarget(texture, width, height);
return reinterpret_cast<TRenderTarget *>(renderTarget);
}
EMSCRIPTEN_KEEPALIVE void Viewer_destroyRenderTarget(TViewer *tViewer, TRenderTarget *tRenderTarget)
{
auto viewer = reinterpret_cast<FilamentViewer *>(tViewer);
auto renderTarget = reinterpret_cast<RenderTarget *>(tRenderTarget);
viewer->destroyRenderTarget(renderTarget);
}
EMSCRIPTEN_KEEPALIVE void Viewer_destroy(TViewer *viewer)
{
delete ((FilamentViewer *)viewer);
}
EMSCRIPTEN_KEEPALIVE void set_background_color(TViewer *viewer, const float r, const float g, const float b, const float a)
{
((FilamentViewer *)viewer)->setBackgroundColor(r, g, b, a);
}
EMSCRIPTEN_KEEPALIVE void clear_background_image(TViewer *viewer)
{
((FilamentViewer *)viewer)->clearBackgroundImage();
}
EMSCRIPTEN_KEEPALIVE void set_background_image(TViewer *viewer, const char *path, bool fillHeight)
{
((FilamentViewer *)viewer)->setBackgroundImage(path, fillHeight, 100, 100);
}
EMSCRIPTEN_KEEPALIVE void set_background_image_position(TViewer *viewer, float x, float y, bool clamp)
{
((FilamentViewer *)viewer)->setBackgroundImagePosition(x, y, clamp, 100, 100);
}
EMSCRIPTEN_KEEPALIVE void load_skybox(TViewer *viewer, const char *skyboxPath)
{
((FilamentViewer *)viewer)->loadSkybox(skyboxPath);
}
EMSCRIPTEN_KEEPALIVE void create_ibl(TViewer *viewer, float r, float g, float b, float intensity)
{
((FilamentViewer *)viewer)->createIbl(r, g, b, intensity);
}
EMSCRIPTEN_KEEPALIVE void Viewer_loadIbl(TViewer *viewer, const char *iblPath, float intensity)
{
((FilamentViewer *)viewer)->loadIbl(iblPath, intensity);
}
EMSCRIPTEN_KEEPALIVE void rotate_ibl(TViewer *viewer, float *rotationMatrix)
{
math::mat3f matrix(rotationMatrix[0], rotationMatrix[1],
rotationMatrix[2],
rotationMatrix[3],
rotationMatrix[4],
rotationMatrix[5],
rotationMatrix[6],
rotationMatrix[7],
rotationMatrix[8]);
((FilamentViewer *)viewer)->rotateIbl(matrix);
}
EMSCRIPTEN_KEEPALIVE void remove_skybox(TViewer *viewer)
{
((FilamentViewer *)viewer)->removeSkybox();
}
EMSCRIPTEN_KEEPALIVE void remove_ibl(TViewer *viewer)
{
((FilamentViewer *)viewer)->removeIbl();
}
EMSCRIPTEN_KEEPALIVE EntityId add_light(
TViewer *viewer,
uint8_t type,
float colour,
float intensity,
float posX,
float posY,
float posZ,
float dirX,
float dirY,
float dirZ,
float falloffRadius,
float spotLightConeInner,
float spotLightConeOuter,
float sunAngularRadius,
float sunHaloSize,
float sunHaloFallof,
bool shadows)
{
return ((FilamentViewer *)viewer)->addLight((LightManager::Type)type, colour, intensity, posX, posY, posZ, dirX, dirY, dirZ, falloffRadius, spotLightConeInner, spotLightConeOuter, sunAngularRadius, sunHaloSize, sunHaloFallof, shadows);
}
EMSCRIPTEN_KEEPALIVE void set_light_position(TViewer *viewer, int32_t entityId, float x, float y, float z)
{
((FilamentViewer *)viewer)->setLightPosition(entityId, x, y, z);
}
EMSCRIPTEN_KEEPALIVE void set_light_direction(TViewer *viewer, int32_t entityId, float x, float y, float z)
{
((FilamentViewer *)viewer)->setLightDirection(entityId, x, y, z);
}
EMSCRIPTEN_KEEPALIVE void remove_light(TViewer *viewer, int32_t entityId)
{
((FilamentViewer *)viewer)->removeLight(entityId);
}
EMSCRIPTEN_KEEPALIVE void clear_lights(TViewer *viewer)
{
((FilamentViewer *)viewer)->clearLights();
}
EMSCRIPTEN_KEEPALIVE int get_instance_count(TSceneManager *sceneManager, EntityId entityId)
{
return ((SceneManager *)sceneManager)->getInstanceCount(entityId);
}
EMSCRIPTEN_KEEPALIVE void get_instances(TSceneManager *sceneManager, EntityId entityId, EntityId *out)
{
return ((SceneManager *)sceneManager)->getInstances(entityId, out);
}
EMSCRIPTEN_KEEPALIVE void Viewer_setMainCamera(TViewer *tViewer, TView *tView)
{
auto *viewer = reinterpret_cast<FilamentViewer *>(tViewer);
auto *view = reinterpret_cast<View *>(tView);
viewer->setMainCamera(view);
}
EMSCRIPTEN_KEEPALIVE EntityId get_main_camera(TViewer *viewer)
{
return ((FilamentViewer *)viewer)->getMainCamera();
}
EMSCRIPTEN_KEEPALIVE float get_camera_fov(TCamera *camera, bool horizontal)
{
auto cam = reinterpret_cast<filament::Camera *>(camera);
return cam->getFieldOfViewInDegrees(horizontal ? Camera::Fov::HORIZONTAL : Camera::Fov::VERTICAL);
}
EMSCRIPTEN_KEEPALIVE double get_camera_focal_length(TCamera *const camera)
{
auto cam = reinterpret_cast<filament::Camera *>(camera);
return cam->getFocalLength();
}
EMSCRIPTEN_KEEPALIVE void set_camera_projection_from_fov(TCamera *camera, double fovInDegrees, double aspect, double near, double far, bool horizontal)
{
auto cam = reinterpret_cast<filament::Camera *>(camera);
cam->setProjection(fovInDegrees, aspect, near, far, horizontal ? Camera::Fov::HORIZONTAL : Camera::Fov::VERTICAL);
}
EMSCRIPTEN_KEEPALIVE TCamera *get_camera(TViewer *viewer, EntityId entity)
{
auto filamentCamera = ((FilamentViewer *)viewer)->getCamera(entity);
return reinterpret_cast<TCamera *>(filamentCamera);
}
EMSCRIPTEN_KEEPALIVE double4x4 get_camera_model_matrix(TCamera *camera)
{
const auto &mat = reinterpret_cast<filament::Camera *>(camera)->getModelMatrix();
return convert_mat4_to_double4x4(mat);
}
EMSCRIPTEN_KEEPALIVE double4x4 get_camera_view_matrix(TCamera *camera)
{
const auto &mat = reinterpret_cast<filament::Camera *>(camera)->getViewMatrix();
return convert_mat4_to_double4x4(mat);
}
EMSCRIPTEN_KEEPALIVE double4x4 get_camera_projection_matrix(TCamera *camera)
{
const auto &mat = reinterpret_cast<filament::Camera *>(camera)->getProjectionMatrix();
return convert_mat4_to_double4x4(mat);
}
EMSCRIPTEN_KEEPALIVE double4x4 get_camera_culling_projection_matrix(TCamera *camera)
{
const auto &mat = reinterpret_cast<filament::Camera *>(camera)->getCullingProjectionMatrix();
return convert_mat4_to_double4x4(mat);
}
EMSCRIPTEN_KEEPALIVE void set_camera_projection_matrix(TCamera *camera, double4x4 matrix, double near, double far)
{
auto cam = reinterpret_cast<filament::Camera *>(camera);
const auto &mat = convert_double4x4_to_mat4(matrix);
cam->setCustomProjection(mat, near, far);
}
EMSCRIPTEN_KEEPALIVE void Camera_setLensProjection(TCamera *camera, double near, double far, double aspect, double focalLength)
{
auto cam = reinterpret_cast<filament::Camera *>(camera);
cam->setLensProjection(focalLength, aspect, near, far);
}
EMSCRIPTEN_KEEPALIVE void Camera_setModelMatrix(TCamera *camera, double4x4 matrix)
{
auto cam = reinterpret_cast<filament::Camera *>(camera);
cam->setModelMatrix(convert_double4x4_to_mat4(matrix));
}
EMSCRIPTEN_KEEPALIVE double get_camera_near(TCamera *camera)
{
auto cam = reinterpret_cast<filament::Camera *>(camera);
return cam->getNear();
}
EMSCRIPTEN_KEEPALIVE double get_camera_culling_far(TCamera *camera)
{
auto cam = reinterpret_cast<filament::Camera *>(camera);
return cam->getCullingFar();
}
EMSCRIPTEN_KEEPALIVE const double *const get_camera_frustum(TCamera *camera)
{
const auto frustum = reinterpret_cast<filament::Camera *>(camera)->getFrustum();
const math::float4 *planes = frustum.getNormalizedPlanes();
double *array = (double *)calloc(24, sizeof(double));
for (int i = 0; i < 6; i++)
{
auto plane = planes[i];
array[i * 4] = double(plane.x);
array[i * 4 + 1] = double(plane.y);
array[i * 4 + 2] = double(plane.z);
array[i * 4 + 3] = double(plane.w);
}
return array;
}
EMSCRIPTEN_KEEPALIVE void set_camera_focus_distance(TCamera *camera, float distance)
{
auto *cam = reinterpret_cast<filament::Camera *>(camera);
cam->setFocusDistance(distance);
}
EMSCRIPTEN_KEEPALIVE void set_camera_exposure(TCamera *camera, float aperture, float shutterSpeed, float sensitivity)
{
auto *cam = reinterpret_cast<filament::Camera *>(camera);
cam->setExposure(aperture, shutterSpeed, sensitivity);
}
EMSCRIPTEN_KEEPALIVE void set_camera_model_matrix(TCamera *camera, double4x4 matrix)
{
auto *cam = reinterpret_cast<filament::Camera *>(camera);
const filament::math::mat4 &mat = convert_double4x4_to_mat4(matrix);
cam->setModelMatrix(mat);
}
EMSCRIPTEN_KEEPALIVE void Viewer_render(
TViewer *tViewer)
{
auto viewer = reinterpret_cast<FilamentViewer *>(tViewer);
viewer->render(0);
}
EMSCRIPTEN_KEEPALIVE void Viewer_setViewRenderable(TViewer *tViewer, TSwapChain *tSwapChain, TView *tView, bool renderable)
{
auto viewer = reinterpret_cast<FilamentViewer *>(tViewer);
auto swapChain = reinterpret_cast<SwapChain *>(tSwapChain);
auto *view = reinterpret_cast<View *>(tView);
viewer->setRenderable(view, swapChain, renderable);
}
EMSCRIPTEN_KEEPALIVE void Viewer_capture(
TViewer *tViewer,
TView *tView,
TSwapChain *tSwapChain,
uint8_t *pixelBuffer,
void (*callback)(void))
{
#ifdef __EMSCRIPTEN__
bool useFence = true;
#else
bool useFence = false;
#endif
auto swapChain = reinterpret_cast<SwapChain *>(tSwapChain);
auto viewer = reinterpret_cast<FilamentViewer *>(tViewer);
auto *view = reinterpret_cast<View *>(tView);
viewer->capture(view, pixelBuffer, useFence, swapChain, callback);
};
EMSCRIPTEN_KEEPALIVE void Viewer_captureRenderTarget(
TViewer *tViewer,
TView *tView,
TSwapChain *tSwapChain,
TRenderTarget *tRenderTarget,
uint8_t *pixelBuffer,
void (*callback)(void))
{
#ifdef __EMSCRIPTEN__
bool useFence = true;
#else
bool useFence = false;
#endif
auto swapChain = reinterpret_cast<SwapChain *>(tSwapChain);
auto renderTarget = reinterpret_cast<RenderTarget *>(tRenderTarget);
auto viewer = reinterpret_cast<FilamentViewer *>(tViewer);
auto *view = reinterpret_cast<View *>(tView);
viewer->capture(view, pixelBuffer, useFence, swapChain, renderTarget, callback);
};
EMSCRIPTEN_KEEPALIVE void set_frame_interval(
TViewer *viewer,
float frameInterval)
{
((FilamentViewer *)viewer)->setFrameInterval(frameInterval);
}
EMSCRIPTEN_KEEPALIVE void Viewer_destroySwapChain(TViewer *tViewer, TSwapChain *tSwapChain)
{
auto viewer = reinterpret_cast<FilamentViewer *>(tViewer);
auto swapChain = reinterpret_cast<SwapChain *>(tSwapChain);
viewer->destroySwapChain(swapChain);
}
EMSCRIPTEN_KEEPALIVE TSwapChain *Viewer_createHeadlessSwapChain(TViewer *tViewer, uint32_t width, uint32_t height)
{
auto viewer = reinterpret_cast<FilamentViewer *>(tViewer);
auto swapChain = viewer->createSwapChain(width, height);
return reinterpret_cast<TSwapChain *>(swapChain);
}
EMSCRIPTEN_KEEPALIVE TSwapChain *Viewer_createSwapChain(TViewer *tViewer, const void *const window)
{
auto viewer = reinterpret_cast<FilamentViewer *>(tViewer);
auto swapChain = viewer->createSwapChain(window);
return reinterpret_cast<TSwapChain *>(swapChain);
}
EMSCRIPTEN_KEEPALIVE TSwapChain *Viewer_getSwapChainAt(TViewer *tViewer, int index)
{
auto viewer = reinterpret_cast<FilamentViewer *>(tViewer);
auto swapChain = viewer->getSwapChainAt(index);
return reinterpret_cast<TSwapChain *>(swapChain);
}
EMSCRIPTEN_KEEPALIVE TView *Viewer_createView(TViewer *tViewer)
{
auto viewer = reinterpret_cast<FilamentViewer *>(tViewer);
auto view = viewer->createView();
return reinterpret_cast<TView *>(view);
}
EMSCRIPTEN_KEEPALIVE TView *Viewer_getViewAt(TViewer *tViewer, int32_t index)
{
auto viewer = reinterpret_cast<FilamentViewer *>(tViewer);
auto view = viewer->getViewAt(index);
return reinterpret_cast<TView *>(view);
}
EMSCRIPTEN_KEEPALIVE TSceneManager *Viewer_getSceneManager(TViewer *tViewer)
{
auto *viewer = reinterpret_cast<FilamentViewer *>(tViewer);
auto *sceneManager = viewer->getSceneManager();
return reinterpret_cast<TSceneManager *>(sceneManager);
}
EMSCRIPTEN_KEEPALIVE void queue_position_update_from_viewport_coords(TSceneManager *sceneManager, TView *tView, EntityId entity, float viewportX, float viewportY)
{
auto *view = reinterpret_cast<View *>(tView);
((SceneManager *)sceneManager)->queueRelativePositionUpdateFromViewportVector(view, entity, viewportX, viewportY);
}
EMSCRIPTEN_KEEPALIVE void ios_dummy()
{
Log("Dummy called");
}
EMSCRIPTEN_KEEPALIVE void thermion_filament_free(void *ptr)
{
free(ptr);
}
EMSCRIPTEN_KEEPALIVE void add_collision_component(TSceneManager *sceneManager, EntityId entityId, void (*onCollisionCallback)(const EntityId entityId1, const EntityId entityId2), bool affectsCollidingTransform)
{
((SceneManager *)sceneManager)->addCollisionComponent(entityId, onCollisionCallback, affectsCollidingTransform);
}
EMSCRIPTEN_KEEPALIVE void remove_collision_component(TSceneManager *sceneManager, EntityId entityId)
{
((SceneManager *)sceneManager)->removeCollisionComponent(entityId);
}
EMSCRIPTEN_KEEPALIVE void test_collisions(TSceneManager *sceneManager, EntityId entity)
{
((SceneManager *)sceneManager)->testCollisions(entity);
}
EMSCRIPTEN_KEEPALIVE Aabb2 get_bounding_box(TSceneManager *sceneManager, TView *tView, EntityId entity)
{
auto view = reinterpret_cast<View *>(tView);
return ((SceneManager *)sceneManager)->getScreenSpaceBoundingBox(view, entity);
}
EMSCRIPTEN_KEEPALIVE void get_bounding_box_to_out(TSceneManager *sceneManager, TView *tView, EntityId entity, float *minX, float *minY, float *maxX, float *maxY)
{
auto view = reinterpret_cast<View *>(tView);
auto box = ((SceneManager *)sceneManager)->getScreenSpaceBoundingBox(view, entity);
*minX = box.minX;
*minY = box.minY;
*maxX = box.maxX;
*maxY = box.maxY;
}
EMSCRIPTEN_KEEPALIVE void thermion_flutter_free(void *ptr)
{
free(ptr);
}
EMSCRIPTEN_KEEPALIVE void unproject_texture(TViewer *viewer, EntityId entity, uint8_t *input, uint32_t inputWidth, uint32_t inputHeight, uint8_t *out, uint32_t outWidth, uint32_t outHeight)
{
((FilamentViewer *)viewer)->unprojectTexture(entity, input, inputWidth, inputHeight, out, outWidth, outHeight);
}
EMSCRIPTEN_KEEPALIVE void *const create_texture(TSceneManager *sceneManager, uint8_t *data, size_t length)
{
return (void *const)((SceneManager *)sceneManager)->createTexture(data, length, "SOMETEXTURE");
}
EMSCRIPTEN_KEEPALIVE void apply_texture_to_material(TSceneManager *sceneManager, EntityId entity, void *const texture, const char *parameterName, int materialIndex)
{
((SceneManager *)sceneManager)->applyTexture(entity, reinterpret_cast<Texture *>(texture), parameterName, materialIndex);
}
EMSCRIPTEN_KEEPALIVE void destroy_texture(TSceneManager *sceneManager, void *const texture)
{
((SceneManager *)sceneManager)->destroyTexture(reinterpret_cast<Texture *>(texture));
}
EMSCRIPTEN_KEEPALIVE TTransformManager *Engine_getTransformManager(TEngine *tEngine)
{
auto *engine = reinterpret_cast<Engine *>(tEngine);
auto &transformManager = engine->getTransformManager();
return reinterpret_cast<TTransformManager *>(&transformManager);
}
EMSCRIPTEN_KEEPALIVE TRenderableManager *Engine_getRenderableManager(TEngine *tEngine)
{
auto *engine = reinterpret_cast<Engine *>(tEngine);
auto &renderableManager = engine->getRenderableManager();
return reinterpret_cast<TRenderableManager *>(&renderableManager);
}
EMSCRIPTEN_KEEPALIVE TCamera *Engine_getCameraComponent(TEngine *tEngine, EntityId entityId)
{
auto *engine = reinterpret_cast<Engine *>(tEngine);
auto *camera = engine->getCameraComponent(utils::Entity::import(entityId));
return reinterpret_cast<TCamera *>(camera);
}
EMSCRIPTEN_KEEPALIVE void Engine_setTransform(TEngine *tEngine, EntityId entity, double4x4 transform)
{
auto *engine = reinterpret_cast<Engine *>(tEngine);
auto &transformManager = engine->getTransformManager();
auto transformInstance = transformManager.getInstance(utils::Entity::import(entity));
if (!transformInstance.isValid())
{
Log("Transform instance not valid");
}
transformManager.setTransform(transformInstance, convert_double4x4_to_mat4(transform));
}
}

View File

@@ -0,0 +1,581 @@
#include <functional>
#include <mutex>
#include <thread>
#include <stdlib.h>
#include <filament/LightManager.h>
#include "c_api/APIBoundaryTypes.h"
#include "c_api/TView.h"
#include "c_api/TSceneAsset.h"
#include "c_api/TSceneManager.h"
#include "c_api/TAnimationManager.h"
#include "c_api/ThermionDartRenderThreadApi.h"
#include "FilamentViewer.hpp"
#include "Log.hpp"
#include "ThreadPool.hpp"
using namespace thermion;
using namespace std::chrono_literals;
#include <time.h>
class RenderLoop
{
public:
explicit RenderLoop()
{
srand(time(NULL));
t = new std::thread([this]()
{ start(); });
}
~RenderLoop()
{
_stop = true;
_cv.notify_one();
t->join();
}
static void mainLoop(void *arg)
{
((RenderLoop *)arg)->iter();
}
static void *startHelper(void *parm)
{
((RenderLoop *)parm)->start();
return nullptr;
}
void start()
{
while (!_stop)
{
iter();
}
}
void requestFrame(void (*callback)())
{
std::unique_lock<std::mutex> lock(_mutex);
this->_requestFrameRenderCallback = callback;
_cv.notify_one();
}
void iter()
{
{
std::unique_lock<std::mutex> lock(_mutex);
if (_requestFrameRenderCallback)
{
doRender();
lock.unlock();
this->_requestFrameRenderCallback();
this->_requestFrameRenderCallback = nullptr;
// Calculate and print FPS
auto currentTime = std::chrono::high_resolution_clock::now();
float deltaTime = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - _lastFrameTime).count();
_lastFrameTime = currentTime;
_frameCount++;
_accumulatedTime += deltaTime;
if (_accumulatedTime >= 1.0f) // Update FPS every second
{
_fps = _frameCount / _accumulatedTime;
// std::cout << "FPS: " << _fps << std::endl;
_frameCount = 0;
_accumulatedTime = 0.0f;
}
}
}
std::unique_lock<std::mutex> taskLock(_taskMutex);
if (!_tasks.empty())
{
auto task = std::move(_tasks.front());
_tasks.pop_front();
taskLock.unlock();
task();
taskLock.lock();
}
_cv.wait_for(taskLock, std::chrono::microseconds(2000), [this]
{ return !_tasks.empty() || _stop; });
}
void createViewer(void *const context,
void *const platform,
const char *uberArchivePath,
const ResourceLoaderWrapper *const loader,
void (*renderCallback)(void *),
void *const owner,
void (*callback)(TViewer *))
{
_renderCallback = renderCallback;
_renderCallbackOwner = owner;
std::packaged_task<void()> lambda([=]() mutable
{
auto viewer = (FilamentViewer *)Viewer_create(context, loader, platform, uberArchivePath);
_viewer = reinterpret_cast<TViewer*>(viewer);
callback(_viewer); });
auto fut = add_task(lambda);
}
void destroyViewer(FilamentViewer *viewer)
{
std::packaged_task<void()> lambda([=]() mutable
{
_viewer = nullptr;
Viewer_destroy(reinterpret_cast<TViewer*>(viewer)); });
auto fut = add_task(lambda);
fut.wait();
}
void doRender()
{
Viewer_render(_viewer);
if (_renderCallback)
{
_renderCallback(_renderCallbackOwner);
}
}
void setFrameIntervalInMilliseconds(float frameIntervalInMilliseconds)
{
_frameIntervalInMicroseconds = static_cast<int>(1000.0f * frameIntervalInMilliseconds);
}
template <class Rt>
auto add_task(std::packaged_task<Rt()> &pt) -> std::future<Rt>
{
std::unique_lock<std::mutex> lock(_taskMutex);
auto ret = pt.get_future();
_tasks.push_back([pt = std::make_shared<std::packaged_task<Rt()>>(
std::move(pt))]
{ (*pt)(); });
_cv.notify_one();
return ret;
}
private:
void (*_requestFrameRenderCallback)() = nullptr;
bool _stop = false;
int _frameIntervalInMicroseconds = 1000000 / 60;
std::mutex _mutex;
std::mutex _taskMutex;
std::condition_variable _cv;
void (*_renderCallback)(void *const) = nullptr;
void *_renderCallbackOwner = nullptr;
std::deque<std::function<void()>> _tasks;
TViewer *_viewer = nullptr;
std::chrono::high_resolution_clock::time_point _lastFrameTime;
int _frameCount = 0;
float _accumulatedTime = 0.0f;
float _fps = 0.0f;
std::thread *t = nullptr;
};
extern "C"
{
static RenderLoop *_rl;
EMSCRIPTEN_KEEPALIVE void Viewer_createOnRenderThread(
void *const context, void *const platform, const char *uberArchivePath,
const void *const loader,
void (*renderCallback)(void *const renderCallbackOwner),
void *const renderCallbackOwner,
void (*callback)(TViewer *))
{
if (!_rl)
{
_rl = new RenderLoop();
}
_rl->createViewer(context, platform, uberArchivePath, (const ResourceLoaderWrapper *const)loader,
renderCallback, renderCallbackOwner, callback);
}
EMSCRIPTEN_KEEPALIVE void Viewer_destroyOnRenderThread(TViewer *viewer)
{
_rl->destroyViewer((FilamentViewer *)viewer);
delete _rl;
_rl = nullptr;
}
EMSCRIPTEN_KEEPALIVE void Viewer_createHeadlessSwapChainRenderThread(TViewer *viewer,
uint32_t width,
uint32_t height,
void (*onComplete)(TSwapChain *))
{
std::packaged_task<void()> lambda(
[=]() mutable
{
auto *swapChain = Viewer_createHeadlessSwapChain(viewer, width, height);
onComplete(swapChain);
});
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void Viewer_createSwapChainRenderThread(TViewer *viewer,
void *const surface,
void (*onComplete)(TSwapChain *))
{
std::packaged_task<void()> lambda(
[=]() mutable
{
auto *swapChain = Viewer_createSwapChain(viewer, surface);
onComplete(swapChain);
});
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void Viewer_destroySwapChainRenderThread(TViewer *viewer, TSwapChain *swapChain, void (*onComplete)())
{
std::packaged_task<void()> lambda(
[=]() mutable
{
Viewer_destroySwapChain(viewer, swapChain);
onComplete();
});
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void Viewer_requestFrameRenderThread(TViewer *viewer, void (*onComplete)())
{
if (!_rl)
{
Log("No render loop!"); // PANIC?
}
else
{
_rl->requestFrame(onComplete);
}
}
EMSCRIPTEN_KEEPALIVE void Viewer_loadIblRenderThread(TViewer *viewer, const char *iblPath, float intensity, void (*onComplete)())
{
std::packaged_task<void()> lambda(
[=]() mutable
{
Viewer_loadIbl(viewer, iblPath, intensity);
onComplete();
});
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void Viewer_createRenderTargetRenderThread(TViewer *viewer, intptr_t texture, uint32_t width, uint32_t height, void (*onComplete)(TRenderTarget *))
{
std::packaged_task<void()> lambda(
[=]() mutable
{
auto renderTarget = Viewer_createRenderTarget(viewer, texture, width, height);
onComplete(renderTarget);
});
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void
set_frame_interval_render_thread(TViewer *viewer, float frameIntervalInMilliseconds)
{
_rl->setFrameIntervalInMilliseconds(frameIntervalInMilliseconds);
std::packaged_task<void()> lambda([=]() mutable
{ ((FilamentViewer *)viewer)->setFrameInterval(frameIntervalInMilliseconds); });
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void Viewer_renderRenderThread(TViewer *viewer, TView *tView, TSwapChain *tSwapChain)
{
std::packaged_task<void()> lambda([=]() mutable
{ _rl->doRender(); });
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void Viewer_captureRenderThread(TViewer *viewer, TView *view, TSwapChain *tSwapChain, uint8_t *pixelBuffer, void (*onComplete)())
{
std::packaged_task<void()> lambda([=]() mutable
{ Viewer_capture(viewer, view, tSwapChain, pixelBuffer, onComplete); });
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void Viewer_captureRenderTargetRenderThread(TViewer *viewer, TView *view, TSwapChain *tSwapChain, TRenderTarget *tRenderTarget, uint8_t *pixelBuffer, void (*onComplete)())
{
std::packaged_task<void()> lambda([=]() mutable
{ Viewer_captureRenderTarget(viewer, view, tSwapChain, tRenderTarget, pixelBuffer, onComplete); });
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void
set_background_color_render_thread(TViewer *viewer, const float r, const float g,
const float b, const float a)
{
std::packaged_task<void()> lambda(
[=]() mutable
{ set_background_color(viewer, r, g, b, a); });
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void SceneManager_loadGltfRenderThread(TSceneManager *sceneManager,
const char *path,
const char *relativeResourcePath,
bool keepData,
void (*callback)(TSceneAsset *))
{
std::packaged_task<void()> lambda([=]() mutable
{
auto entity = SceneManager_loadGltf(sceneManager, path, relativeResourcePath, keepData);
callback(entity); });
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void SceneManager_loadGlbRenderThread(TSceneManager *sceneManager,
const char *path,
int numInstances,
bool keepData,
void (*callback)(TSceneAsset *))
{
std::packaged_task<void()> lambda(
[=]() mutable
{
auto asset = SceneManager_loadGlb(sceneManager, path, numInstances, keepData);
callback(asset);
});
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void SceneManager_createGeometryRenderThread(
TSceneManager *sceneManager,
float *vertices,
int numVertices,
float *normals,
int numNormals,
float *uvs,
int numUvs,
uint16_t *indices,
int numIndices,
int primitiveType,
TMaterialInstance **materialInstances,
int materialInstanceCount,
bool keepData,
void (*callback)(TSceneAsset *))
{
std::packaged_task<void()> lambda(
[=]
{
auto *asset = SceneManager_createGeometry(sceneManager, vertices, numVertices, normals, numNormals, uvs, numUvs, indices, numIndices, primitiveType, materialInstances, materialInstanceCount, keepData);
callback(asset);
});
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void SceneAsset_createInstanceRenderThread(
TSceneAsset *asset, TMaterialInstance **tMaterialInstances,
int materialInstanceCount,
void (*callback)(TSceneAsset *))
{
std::packaged_task<void()> lambda(
[=]
{
auto instanceAsset = SceneAsset_createInstance(asset, tMaterialInstances, materialInstanceCount);
callback(instanceAsset);
});
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void MaterialProvider_createMaterialInstanceRenderThread(TMaterialProvider *tMaterialProvider, TMaterialKey *tKey, void (*callback)(TMaterialInstance *))
{
std::packaged_task<void()> lambda(
[=]
{
auto materialInstance = MaterialProvider_createMaterialInstance(tMaterialProvider, tKey);
callback(materialInstance);
});
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void SceneManager_createUnlitMaterialInstanceRenderThread(TSceneManager *sceneManager, void (*callback)(TMaterialInstance *))
{
std::packaged_task<void()> lambda(
[=]() mutable
{
auto instance = SceneManager_createUnlitMaterialInstance(sceneManager);
callback(instance);
});
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void SceneManager_createUnlitFixedSizeMaterialInstanceRenderThread(TSceneManager *sceneManager, void (*callback)(TMaterialInstance *))
{
std::packaged_task<void()> lambda(
[=]() mutable
{
auto instance = SceneManager_createUnlitFixedSizeMaterialInstance(sceneManager);
callback(instance);
});
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void SceneManager_loadGlbFromBufferRenderThread(TSceneManager *sceneManager,
const uint8_t *const data,
size_t length,
int numInstances,
bool keepData,
int priority,
int layer,
bool loadResourcesAsync,
void (*callback)(TSceneAsset *))
{
std::packaged_task<void()> lambda(
[=]() mutable
{
auto *asset = SceneManager_loadGlbFromBuffer(sceneManager, data, length, keepData, priority, layer, loadResourcesAsync);
callback(asset);
});
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void clear_background_image_render_thread(TViewer *viewer)
{
std::packaged_task<void()> lambda([=]
{ clear_background_image(viewer); });
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void set_background_image_render_thread(TViewer *viewer,
const char *path,
bool fillHeight, void (*callback)())
{
std::packaged_task<void()> lambda(
[=]
{
set_background_image(viewer, path, fillHeight);
callback();
});
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void set_background_image_position_render_thread(TViewer *viewer,
float x, float y,
bool clamp)
{
std::packaged_task<void()> lambda(
[=]
{ set_background_image_position(viewer, x, y, clamp); });
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void load_skybox_render_thread(TViewer *viewer,
const char *skyboxPath,
void (*onComplete)())
{
std::packaged_task<void()> lambda([=]
{
load_skybox(viewer, skyboxPath);
onComplete(); });
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void remove_skybox_render_thread(TViewer *viewer)
{
std::packaged_task<void()> lambda([=]
{ remove_skybox(viewer); });
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void remove_ibl_render_thread(TViewer *viewer)
{
std::packaged_task<void()> lambda([=]
{ remove_ibl(viewer); });
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void View_setToneMappingRenderThread(TView *tView, TEngine *tEngine, thermion::ToneMapping toneMapping)
{
std::packaged_task<void()> lambda(
[=]
{
View_setToneMapping(tView, tEngine, toneMapping);
});
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void View_setBloomRenderThread(TView *tView, double bloom)
{
std::packaged_task<void()> lambda(
[=]
{
View_setBloom(tView, bloom);
});
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void *SceneManager_destroyAllRenderThread(TSceneManager *tSceneManager, void (*callback)())
{
std::packaged_task<void()> lambda(
[=]() mutable
{
SceneManager_destroyAll(tSceneManager);
callback();
});
auto fut = _rl->add_task(lambda);
return nullptr;
}
EMSCRIPTEN_KEEPALIVE void *SceneManager_destroyAssetRenderThread(TSceneManager *tSceneManager, TSceneAsset *tSceneAsset, void (*callback)())
{
std::packaged_task<void()> lambda(
[=]() mutable
{
SceneManager_destroyAsset(tSceneManager, tSceneAsset);
callback();
});
auto fut = _rl->add_task(lambda);
return nullptr;
}
EMSCRIPTEN_KEEPALIVE void unproject_texture_render_thread(TViewer *viewer, EntityId entity, uint8_t *input, uint32_t inputWidth, uint32_t inputHeight, uint8_t *out, uint32_t outWidth, uint32_t outHeight, void (*callback)())
{
std::packaged_task<void()> lambda(
[=]
{
unproject_texture(viewer, entity, input, inputWidth, inputHeight, out, outWidth, outHeight);
callback();
});
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void AnimationManager_updateBoneMatricesRenderThread(
TAnimationManager *tAnimationManager,
TSceneAsset *sceneAsset,
void (*callback)(bool))
{
std::packaged_task<void()> lambda(
[=]() mutable
{
bool result = AnimationManager_updateBoneMatrices(tAnimationManager, sceneAsset);
callback(result);
});
auto fut = _rl->add_task(lambda);
}
EMSCRIPTEN_KEEPALIVE void AnimationManager_setMorphTargetWeightsRenderThread(
TAnimationManager *tAnimationManager,
EntityId entityId,
const float *const morphData,
int numWeights,
void (*callback)(bool))
{
std::packaged_task<void()> lambda(
[=]() mutable
{
bool result = AnimationManager_setMorphTargetWeights(tAnimationManager, entityId, morphData, numWeights);
callback(result);
});
auto fut = _rl->add_task(lambda);
}
}