From d3b86fab091b11983d56b1a0737bc82c6195e949 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Tue, 5 Mar 2024 13:25:29 +0100 Subject: [PATCH] Revert "feat: Add API for controlling the scene (#15)" This reverts commit 9dde746d4a92e8e64deba4bec848253fb343ebd7. --- package/android/CMakeLists.txt | 1 - package/cpp/core/CameraFovEnum.h | 35 ------- package/cpp/core/CameraWrapper.cpp | 26 +---- package/cpp/core/CameraWrapper.h | 6 +- package/cpp/core/EngineWrapper.cpp | 110 ++++++++-------------- package/cpp/core/EngineWrapper.h | 34 ++----- package/cpp/core/FilamentAssetWrapper.cpp | 33 ------- package/cpp/core/FilamentAssetWrapper.h | 27 ------ package/cpp/core/LightEnum.h | 51 ---------- package/cpp/core/ViewWrapper.cpp | 9 +- package/cpp/core/ViewWrapper.h | 1 - package/cpp/core/utils/Converter.h | 21 ----- package/cpp/jsi/EnumMapper.h | 23 +---- package/cpp/jsi/JSIConverter.h | 2 +- package/src/FilamentView.tsx | 89 +++++++---------- package/src/types/Camera.ts | 35 +------ package/src/types/Engine.ts | 36 +------ package/src/types/FilamentAsset.ts | 5 - package/src/types/View.ts | 1 - package/src/types/float3.ts | 4 - 20 files changed, 92 insertions(+), 457 deletions(-) delete mode 100644 package/cpp/core/CameraFovEnum.h delete mode 100644 package/cpp/core/FilamentAssetWrapper.cpp delete mode 100644 package/cpp/core/FilamentAssetWrapper.h delete mode 100644 package/cpp/core/LightEnum.h delete mode 100644 package/cpp/core/utils/Converter.h delete mode 100644 package/src/types/FilamentAsset.ts delete mode 100644 package/src/types/float3.ts diff --git a/package/android/CMakeLists.txt b/package/android/CMakeLists.txt index a97a215d..0c545e10 100644 --- a/package/android/CMakeLists.txt +++ b/package/android/CMakeLists.txt @@ -36,7 +36,6 @@ add_library( ../cpp/core/CameraWrapper.cpp ../cpp/core/ViewWrapper.cpp ../cpp/core/SwapChainWrapper.cpp - ../cpp/core/FilamentAssetWrapper.cpp # Filament Utils ../cpp/core/utils/EntityWrapper.cpp diff --git a/package/cpp/core/CameraFovEnum.h b/package/cpp/core/CameraFovEnum.h deleted file mode 100644 index 3883d72d..00000000 --- a/package/cpp/core/CameraFovEnum.h +++ /dev/null @@ -1,35 +0,0 @@ -// -// Created by Marc Rousavy on 22.02.24. -// - -#pragma once - -#include "jsi/EnumMapper.h" -#include - -namespace margelo { -using namespace filament; - -namespace EnumMapper { - static void convertJSUnionToEnum(const std::string& inUnion, Camera::Fov* outEnum) { - if (inUnion == "horizontal") - *outEnum = Camera::Fov::HORIZONTAL; - else if (inUnion == "vertical") - *outEnum = Camera::Fov::VERTICAL; - else - throw invalidUnion(inUnion); - } - static void convertEnumToJSUnion(Camera::Fov inEnum, std::string* outUnion) { - switch (inEnum) { - case filament::Camera::Fov::HORIZONTAL: - *outUnion = "horizontal"; - break; - case filament::Camera::Fov::VERTICAL: - *outUnion = "vertical"; - break; - default: - throw invalidEnum(inEnum); - } - } -} // namespace EnumMapper -} // namespace margelo \ No newline at end of file diff --git a/package/cpp/core/CameraWrapper.cpp b/package/cpp/core/CameraWrapper.cpp index 7c47d654..e10d417b 100644 --- a/package/cpp/core/CameraWrapper.cpp +++ b/package/cpp/core/CameraWrapper.cpp @@ -1,14 +1,10 @@ #include "CameraWrapper.h" -#include "CameraFovEnum.h" void margelo::CameraWrapper::loadHybridMethods() { - registerHybridMethod("lookAtCameraManipulator", &CameraWrapper::lookAtCameraManipulator, this); registerHybridMethod("lookAt", &CameraWrapper::lookAt, this); - registerHybridMethod("setLensProjection", &CameraWrapper::setLensProjection, this); - registerHybridMethod("setProjection", &CameraWrapper::setProjection, this); } -void margelo::CameraWrapper::lookAtCameraManipulator(std::shared_ptr cameraManipulator) { +void margelo::CameraWrapper::lookAt(std::shared_ptr cameraManipulator) { if (!cameraManipulator) { throw std::invalid_argument("CameraManipulator is null"); } @@ -17,23 +13,3 @@ void margelo::CameraWrapper::lookAtCameraManipulator(std::shared_ptrgetManipulator()->getLookAt(&eye, ¢er, &up); _camera->lookAt(eye, center, up); } - -void margelo::CameraWrapper::lookAt(std::vector eye, std::vector center, std::vector up) { - math::float3 eyeVec = {static_cast(eye[0]), static_cast(eye[1]), static_cast(eye[2])}; - math::float3 centerVec = {static_cast(center[0]), static_cast(center[1]), static_cast(center[2])}; - math::float3 upVec = {static_cast(up[0]), static_cast(up[1]), static_cast(up[2])}; - _camera->lookAt(eyeVec, centerVec, upVec); -} - -void margelo::CameraWrapper::setLensProjection(double fov, double aspect, double near, double far) { - _camera->setLensProjection(static_cast(fov), static_cast(aspect), static_cast(near), static_cast(far)); -} - -void margelo::CameraWrapper::setProjection(double fovInDegrees, double aspect, double near, double far, - std::string directionStr = "vertical") { - Camera::Fov direction; - EnumMapper::convertJSUnionToEnum(directionStr, &direction); - - _camera->setProjection(static_cast(fovInDegrees), static_cast(aspect), static_cast(near), static_cast(far), - direction); -} diff --git a/package/cpp/core/CameraWrapper.h b/package/cpp/core/CameraWrapper.h index 574604f0..b2c9bc88 100644 --- a/package/cpp/core/CameraWrapper.h +++ b/package/cpp/core/CameraWrapper.h @@ -22,11 +22,7 @@ class CameraWrapper : public HybridObject { std::shared_ptr _camera; private: - void lookAt(std::vector eye, std::vector center, std::vector up); - void setLensProjection(double fov, double aspect, double near, double far); - // TODO(Hanno): Add directionStr , Camera::Fov directionStr = Camera::Fov::VERTICAL - void setProjection(double fovInDegrees, double aspect, double near, double far, std::string directionStr); // Convenience methods - void lookAtCameraManipulator(std::shared_ptr cameraManipulator); + void lookAt(std::shared_ptr cameraManipulator); }; } // namespace margelo \ No newline at end of file diff --git a/package/cpp/core/EngineWrapper.cpp b/package/cpp/core/EngineWrapper.cpp index 31a19317..5ada38cf 100644 --- a/package/cpp/core/EngineWrapper.cpp +++ b/package/cpp/core/EngineWrapper.cpp @@ -4,10 +4,7 @@ #include "EngineWrapper.h" -#include "LightEnum.h" #include "References.h" -#include "utils/Converter.h" - #include #include #include @@ -81,7 +78,7 @@ EngineWrapper::EngineWrapper(std::shared_ptr choreographer) { void EngineWrapper::loadHybridMethods() { registerHybridMethod("setSurfaceProvider", &EngineWrapper::setSurfaceProvider, this); registerHybridMethod("setRenderCallback", &EngineWrapper::setRenderCallback, this); - registerHybridMethod("setIndirectLight", &EngineWrapper::setIndirectLight, this); + registerHybridMethod("createDefaultLight", &EngineWrapper::createDefaultLight, this); registerHybridMethod("loadAsset", &EngineWrapper::loadAsset, this); @@ -91,11 +88,6 @@ void EngineWrapper::loadHybridMethods() { registerHybridMethod("getView", &EngineWrapper::getView, this); registerHybridMethod("getCamera", &EngineWrapper::getCamera, this); registerHybridMethod("getCameraManipulator", &EngineWrapper::getCameraManipulator, this); - registerHybridMethod("createLightEntity", &EngineWrapper::createLightEntity, this); - registerHybridMethod("transformToUnitCube", &EngineWrapper::transformToUnitCube, this); - registerHybridMethod("setEntityPosition", &EngineWrapper::setEntityPosition, this); - registerHybridMethod("setEntityRotation", &EngineWrapper::setEntityRotation, this); - registerHybridMethod("setEntityScale", &EngineWrapper::setEntityScale, this); } void EngineWrapper::setSurfaceProvider(std::shared_ptr surfaceProvider) { @@ -140,8 +132,7 @@ void EngineWrapper::surfaceSizeChanged(int width, int height) { _view->setViewport(0, 0, width, height); } - // TODO: when the surface resizes we need to update the camera projection, but that one is owned by JS now. - // updateCameraProjection(); + updateCameraProjection(); } void EngineWrapper::destroySurface() { @@ -232,8 +223,8 @@ std::shared_ptr EngineWrapper::createCamera() { return std::make_shared(camera); } -std::shared_ptr EngineWrapper::loadAsset(std::shared_ptr modelBuffer) { - gltfio::FilamentAsset* asset = _assetLoader->createAsset(modelBuffer->getData(), modelBuffer->getSize()); +void EngineWrapper::loadAsset(std::shared_ptr modelBuffer) { + filament::gltfio::FilamentAsset* asset = _assetLoader->createAsset(modelBuffer->getData(), modelBuffer->getSize()); if (asset == nullptr) { throw std::runtime_error("Failed to load asset"); } @@ -251,14 +242,11 @@ std::shared_ptr EngineWrapper::loadAsset(std::shared_ptrgetInstance()->getAnimator(); asset->releaseSourceData(); - auto sharedPtr = std::shared_ptr(asset, [](gltfio::FilamentAsset* asset) { - // TODO: destroy the asset - }); - return std::make_shared(sharedPtr); + transformToUnitCube(asset); } // Default light is a directional light for shadows + a default IBL -void EngineWrapper::setIndirectLight(std::shared_ptr iblBuffer) { +void EngineWrapper::createDefaultLight(std::shared_ptr iblBuffer) { if (!_scene) { throw std::runtime_error("Scene not initialized"); } @@ -286,23 +274,17 @@ void EngineWrapper::setIndirectLight(std::shared_ptr iblBuffer) IndirectLight::Builder().reflections(cubemap).irradiance(3, harmonics).intensity(30000.0f).build(*_engine); _scene->getScene()->setIndirectLight(_indirectLight); -} -std::shared_ptr EngineWrapper::createLightEntity(std::string lightTypeStr, double colorFahrenheit, double intensity, - double directionX, double directionY, double directionZ, bool castShadows) { + // Add directional light for supporting shadows auto lightEntity = _engine->getEntityManager().create(); - - // TODO(Marc): Fix enum converter - LightManager::Type lightType; - EnumMapper::convertJSUnionToEnum(lightTypeStr, &lightType); - - LightManager::Builder(lightType) - .color(Color::cct(static_cast(colorFahrenheit))) - .intensity(static_cast(intensity)) - .direction({directionX, directionY, directionZ}) - .castShadows(castShadows) + LightManager::Builder(LightManager::Type::DIRECTIONAL) + .color(Color::cct(6500.0f)) + .intensity(10000) + .direction({0, -1, 0}) + .castShadows(true) .build(*_engine, lightEntity); - return std::make_shared(lightEntity); + + _scene->getScene()->addEntity(lightEntity); } std::shared_ptr EngineWrapper::createCameraManipulator(int width, int height) { @@ -319,9 +301,31 @@ std::shared_ptr EngineWrapper::createCameraManipulator(int w /** * Sets up a root transform on the current model to make it fit into a unit cube. */ -void EngineWrapper::transformToUnitCube(std::shared_ptr asset) { +void EngineWrapper::transformToUnitCube(filament::gltfio::FilamentAsset* asset) { TransformManager& tm = _engine->getTransformManager(); - asset->transformToUnitCube(tm); + Aabb aabb = asset->getBoundingBox(); + math::details::TVec3 center = aabb.center(); + math::details::TVec3 halfExtent = aabb.extent(); + float maxExtent = max(halfExtent) * 2.0f; + float scaleFactor = 2.0f / maxExtent; + math::mat4f transform = math::mat4f::scaling(scaleFactor) * math::mat4f::translation(-center); + EntityInstance transformInstance = tm.getInstance(asset->getRoot()); + tm.setTransform(transformInstance, transform); +} + +void EngineWrapper::updateCameraProjection() { + if (!_view) { + throw std::runtime_error("View not initialized"); + } + if (!_camera) { + throw std::runtime_error("Camera not initialized"); + } + + const double aspect = (double)_view->getView()->getViewport().width / _view->getView()->getViewport().height; + double focalLength = 28.0; + double near = 0.05; // 5cm + double far = 1000.0; // 1km + _camera->getCamera()->setLensProjection(focalLength, aspect, near, far); } void EngineWrapper::synchronizePendingFrames() { @@ -336,42 +340,4 @@ void EngineWrapper::synchronizePendingFrames() { _engine->destroy(fence); } -/** - * Internal method that will help updating the transform of an entity. - * @param transform The transform matrix to apply - * @param entity The entity to apply the transform to - * @param multiplyCurrent If true, the current transform will be multiplied with the new transform, otherwise it will be replaced - */ -void EngineWrapper::updateTransform(math::mat4 transform, std::shared_ptr entity, bool multiplyCurrent) { - if (!entity) { - throw std::invalid_argument("Entity is null"); - } - - TransformManager& tm = _engine->getTransformManager(); - EntityInstance entityInstance = tm.getInstance(entity->getEntity()); - auto currentTransform = tm.getTransform(entityInstance); - auto newTransform = multiplyCurrent ? (currentTransform * transform) : transform; - tm.setTransform(entityInstance, newTransform); -} - -// TODO(Marc): Ideally i want to do this in the entity wrapper, but i dont have access to the transform manager there -void EngineWrapper::setEntityPosition(std::shared_ptr entity, std::vector positionVec, bool multiplyCurrent) { - math::float3 position = Converter::VecToFloat3(positionVec); - auto translationMatrix = math::mat4::translation(position); - updateTransform(translationMatrix, entity, multiplyCurrent); -} - -void EngineWrapper::setEntityRotation(std::shared_ptr entity, double angleRadians, std::vector axisVec, - bool multiplyCurrent) { - math::float3 axis = Converter::VecToFloat3(axisVec); - auto rotationMatrix = math::mat4::rotation(angleRadians, axis); - updateTransform(rotationMatrix, entity, multiplyCurrent); -} - -void EngineWrapper::setEntityScale(std::shared_ptr entity, std::vector scaleVec, bool multiplyCurrent) { - math::float3 scale = Converter::VecToFloat3(scaleVec); - auto scaleMatrix = math::mat4::scaling(scale); - updateTransform(scaleMatrix, entity, multiplyCurrent); -} - } // namespace margelo diff --git a/package/cpp/core/EngineWrapper.h b/package/cpp/core/EngineWrapper.h index a62ef9e3..6d7421d4 100644 --- a/package/cpp/core/EngineWrapper.h +++ b/package/cpp/core/EngineWrapper.h @@ -4,24 +4,10 @@ #pragma once -#include "jsi/HybridObject.h" - -#include "CameraWrapper.h" -#include "Choreographer.h" -#include "FilamentAssetWrapper.h" -#include "FilamentBuffer.h" -#include "RendererWrapper.h" -#include "SceneWrapper.h" #include "Surface.h" #include "SurfaceProvider.h" -#include "SwapChainWrapper.h" -#include "ViewWrapper.h" -#include "core/utils/EntityWrapper.h" -#include "core/utils/ManipulatorWrapper.h" - -#include +#include #include -#include #include #include #include @@ -62,20 +48,12 @@ class EngineWrapper : public HybridObject { void setRenderCallback(std::function)> callback); void renderFrame(double timestamp); - void transformToUnitCube(std::shared_ptr asset); - std::shared_ptr loadAsset(std::shared_ptr modelBuffer); - void setIndirectLight(std::shared_ptr modelBuffer); - + void transformToUnitCube(gltfio::FilamentAsset* asset); + void loadAsset(std::shared_ptr modelBuffer); + void createDefaultLight(std::shared_ptr modelBuffer); + void updateCameraProjection(); void synchronizePendingFrames(); - std::shared_ptr createLightEntity(std::string lightTypeStr, double colorFahrenheit, double intensity, double directionX, - double directionY, double directionZ, bool castShadows); - - void updateTransform(math::mat4 transform, std::shared_ptr entity, bool multiplyCurrent); - void setEntityPosition(std::shared_ptr entity, std::vector positionVec, bool multiplyCurrent); - void setEntityRotation(std::shared_ptr entity, double angleRadians, std::vector axisVec, bool multiplyCurrent); - void setEntityScale(std::shared_ptr entity, std::vector scaleVec, bool multiplyCurrent); - private: std::shared_ptr _engine; std::shared_ptr _surfaceProvider; @@ -93,7 +71,7 @@ class EngineWrapper : public HybridObject { std::shared_ptr _resourceLoader; const math::float3 defaultObjectPosition = {0.0f, 0.0f, 0.0f}; - const math::float3 defaultCameraPosition = {0.0f, 0.0f, 0.0f}; + const math::float3 defaultCameraPosition = {0.0f, 0.0f, 5.0f}; private: // Internals we create, but share the access with the user diff --git a/package/cpp/core/FilamentAssetWrapper.cpp b/package/cpp/core/FilamentAssetWrapper.cpp deleted file mode 100644 index e49b125c..00000000 --- a/package/cpp/core/FilamentAssetWrapper.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "FilamentAssetWrapper.h" - -#include -#include - -namespace margelo { - -using namespace utils; - -void FilamentAssetWrapper::loadHybridMethods() { - registerHybridMethod("getRoot", &FilamentAssetWrapper::getRoot, this); -} - -/** - * Sets up a root transform on the current model to make it fit into a unit cube. - */ -void FilamentAssetWrapper::transformToUnitCube(TransformManager& transformManager) { - Aabb aabb = _asset->getBoundingBox(); - math::details::TVec3 center = aabb.center(); - math::details::TVec3 halfExtent = aabb.extent(); - float maxExtent = max(halfExtent) * 2.0f; - float scaleFactor = 2.0f / maxExtent; - math::mat4f transform = math::mat4f::scaling(scaleFactor) * math::mat4f::translation(-center); - EntityInstance transformInstance = transformManager.getInstance(_asset->getRoot()); - transformManager.setTransform(transformInstance, transform); -} - -std::shared_ptr FilamentAssetWrapper::getRoot() { - Entity rootEntity = _asset->getRoot(); - return std::make_shared(rootEntity); -} - -} // namespace margelo \ No newline at end of file diff --git a/package/cpp/core/FilamentAssetWrapper.h b/package/cpp/core/FilamentAssetWrapper.h deleted file mode 100644 index dd63f9f8..00000000 --- a/package/cpp/core/FilamentAssetWrapper.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include "core/utils/EntityWrapper.h" -#include "jsi/HybridObject.h" -#include -#include - -namespace margelo { - -using namespace filament; - -class FilamentAssetWrapper : public HybridObject { -public: - explicit FilamentAssetWrapper(std::shared_ptr asset) : _asset(asset) {} - - void loadHybridMethods() override; - - void transformToUnitCube(TransformManager& transformManager); - -private: - std::shared_ptr getRoot(); - -private: - std::shared_ptr _asset; -}; - -} // namespace margelo diff --git a/package/cpp/core/LightEnum.h b/package/cpp/core/LightEnum.h deleted file mode 100644 index c0d0e80e..00000000 --- a/package/cpp/core/LightEnum.h +++ /dev/null @@ -1,51 +0,0 @@ -// -// Created by Hanno Gödecke on 29.02.24. -// - -#pragma once - -#include "jsi/EnumMapper.h" -#include - -namespace margelo { - -namespace EnumMapper { - using namespace filament; - - static void convertJSUnionToEnum(const std::string& inUnion, LightManager::Type* outEnum) { - if (inUnion == "directional") - *outEnum = LightManager::Type::DIRECTIONAL; - else if (inUnion == "spot") - *outEnum = LightManager::Type::SPOT; - else if (inUnion == "point") - *outEnum = LightManager::Type::POINT; - else if (inUnion == "focused_sport") - *outEnum = LightManager::Type::FOCUSED_SPOT; - else if (inUnion == "sun") - *outEnum = LightManager::Type::SUN; - else - throw invalidUnion(inUnion); - } - static void convertEnumToJSUnion(LightManager::Type inEnum, std::string* outUnion) { - switch (inEnum) { - case LightManager::Type::DIRECTIONAL: - *outUnion = "directional"; - break; - case LightManager::Type::SPOT: - *outUnion = "spot"; - break; - case LightManager::Type::POINT: - *outUnion = "point"; - break; - case LightManager::Type::FOCUSED_SPOT: - *outUnion = "focused_sport"; - break; - case LightManager::Type::SUN: - *outUnion = "sun"; - break; - default: - throw invalidEnum(inEnum); - } - } -} // namespace EnumMapper -} // namespace margelo diff --git a/package/cpp/core/ViewWrapper.cpp b/package/cpp/core/ViewWrapper.cpp index 36793d0d..4551443c 100644 --- a/package/cpp/core/ViewWrapper.cpp +++ b/package/cpp/core/ViewWrapper.cpp @@ -8,7 +8,6 @@ void ViewWrapper::loadHybridMethods() { registerHybridSetter("camera", &ViewWrapper::setCamera, this); registerHybridGetter("camera", &ViewWrapper::getCamera, this); registerHybridMethod("setViewport", &ViewWrapper::setViewport, this); - registerHybridGetter("aspectRatio", &ViewWrapper::getAspectRatio, this); } void ViewWrapper::setScene(std::shared_ptr scene) { @@ -42,11 +41,11 @@ void ViewWrapper::setViewport(int x, int y, int width, int height) { throw std::invalid_argument("Invalid viewport size"); } - _view->setViewport({x, y, static_cast(width), static_cast(height)}); -} + if (!_view) { + throw std::invalid_argument("View is null"); + } -double ViewWrapper::getAspectRatio() { - return (double)_view->getViewport().width / _view->getViewport().height; + _view->setViewport({x, y, static_cast(width), static_cast(height)}); } } // namespace margelo diff --git a/package/cpp/core/ViewWrapper.h b/package/cpp/core/ViewWrapper.h index 816e4d66..01d60cf0 100644 --- a/package/cpp/core/ViewWrapper.h +++ b/package/cpp/core/ViewWrapper.h @@ -27,7 +27,6 @@ class ViewWrapper : public HybridObject { std::shared_ptr getScene(); void setCamera(std::shared_ptr camera); std::shared_ptr getCamera(); - double getAspectRatio(); private: std::shared_ptr _view; diff --git a/package/cpp/core/utils/Converter.h b/package/cpp/core/utils/Converter.h deleted file mode 100644 index de9541f8..00000000 --- a/package/cpp/core/utils/Converter.h +++ /dev/null @@ -1,21 +0,0 @@ -// -// Created by Hanno Gödecke on 29.02.24. -// - -#pragma once - -#include - -namespace margelo { - -class Converter { -public: - static math::float3 VecToFloat3(std::vector vec) { - if (vec.size() != 3) { - throw std::invalid_argument("Point must have 3 elements"); - } - - return math::float3(vec[0], vec[1], vec[2]); - } -}; -} // namespace margelo diff --git a/package/cpp/jsi/EnumMapper.h b/package/cpp/jsi/EnumMapper.h index 67153bf3..7f2a7513 100644 --- a/package/cpp/jsi/EnumMapper.h +++ b/package/cpp/jsi/EnumMapper.h @@ -17,32 +17,17 @@ namespace EnumMapper { static std::runtime_error invalidUnion(const std::string& passedUnion) { return std::runtime_error("Cannot convert JS Value to Enum: Invalid Union value passed! (\"" + std::string(passedUnion) + "\")"); } - template static std::runtime_error invalidEnum(T passedEnum) { return std::runtime_error("Cannot convert Enum to JS Value: Invalid Enum passed! (Value #" + std::to_string(static_cast(passedEnum)) + ")"); } - // Trait to check if a convertJSUnionToEnum function for enum type T exists - template struct has_js_union_to_enum : std::false_type {}; - template - struct has_js_union_to_enum(), std::declval()))>> - : std::true_type {}; - - // Trait to check if a convertEnumToJSUnion function for enum type T exists - template struct has_enum_to_js_union : std::false_type {}; - template - struct has_enum_to_js_union(), std::declval()))>> - : std::true_type {}; - - template static void convertJSUnionToEnum(const std::string&, TEnum*) { - static_assert(has_js_union_to_enum::value, - "Cannot convert a JS union to this enum type. Did you implement EnumMapper::convertJSUnionToEnum(...)?"); + static void convertJSUnionToEnum(const std::string& inUnion, int*) { + throw invalidUnion(inUnion); } - template static void convertEnumToJSUnion(TEnum, std::string*) { - static_assert(has_enum_to_js_union::value, - "Cannot convert this enum type to a JS union. Did you implement EnumMapper::convertEnumToJSUnion(...)?"); + static void convertEnumToJSUnion(int inEnum, std::string*) { + throw invalidEnum(inEnum); } } // namespace EnumMapper diff --git a/package/cpp/jsi/JSIConverter.h b/package/cpp/jsi/JSIConverter.h index 036f4329..20094973 100644 --- a/package/cpp/jsi/JSIConverter.h +++ b/package/cpp/jsi/JSIConverter.h @@ -108,7 +108,7 @@ template struct JSIConverter & Readonly -const penguModelPath = Platform.select({ - android: 'custom/pengu.glb', - ios: 'pengu.glb', -})! - -const indirectLightPath = Platform.select({ - android: 'custom/default_env_ibl.ktx', - ios: 'default_env_ibl.ktx', -})! - export class FilamentView extends React.PureComponent { private readonly ref: React.RefObject - private readonly engine = FilamentProxy.createEngine() + private readonly choreographer = FilamentProxy.createChoreographer() + private choreographerListener: Listener | null = null constructor(props: FilamentViewProps) { super(props) this.ref = React.createRef() - - this.setup3dScene() } // TODO: Does this also work for Fabric? @@ -42,59 +30,48 @@ export class FilamentView extends React.PureComponent { componentDidMount() { // TODO: lets get rid of this timeout setTimeout(() => { - this.setupSurface() + this.setup3dScene() }, 100) } - setup3dScene = () => { - // Load a model into the scene: - const modelBuffer = FilamentProxy.getAssetByteBuffer(penguModelPath) - const penguAsset = this.engine.loadAsset(modelBuffer) - // By default all assets get added to the origin at 0,0,0, - // we transform it to fit into a unit cube at the origin using this utility: - this.engine.transformToUnitCube(penguAsset) - - // We can also change the pengus position, rotation and scale: - const penguEntity = penguAsset.getRoot() - this.engine.setEntityPosition(penguEntity, [0, 2, 0], true) // Move the pengu up by 2 units - - // Create a default light: - const indirectLightBuffer = FilamentProxy.getAssetByteBuffer(indirectLightPath) - this.engine.setIndirectLight(indirectLightBuffer) - - // Create a directional light for supporting shadows - const light = this.engine.createLightEntity('directional', 6500, 10000, 0, -1, 0, true) - this.engine.getScene().addEntity(light) - } - - renderCallback = () => { - const cameraPosition: Float3 = [0, 0, 5] - const cameraTarget: Float3 = [0, 0, 0] - const cameraUp: Float3 = [0, 1, 0] - - this.engine.getCamera().lookAt(cameraPosition, cameraTarget, cameraUp) + componentWillUnmount(): void { + this.choreographer.stop() + if (this.choreographerListener != null) { + this.choreographerListener.remove() + } } - setupSurface = () => { + setup3dScene = () => { // Get Surface: const fView = FilamentProxy.findFilamentView(this.handle) const surfaceProvider = fView.getSurfaceProvider() - // Link the surface with the engine: - this.engine.setSurfaceProvider(surfaceProvider) + // Create engine: + const engine = FilamentProxy.createEngine() - // Configure camera lens (Important, do so after linking the surface) - const view = this.engine.getView() - const aspectRatio = view.aspectRatio - const focalLengthInMillimeters = 28 - const near = 0.1 - const far = 1000 - this.engine.getCamera().setLensProjection(focalLengthInMillimeters, aspectRatio, near, far) - console.log('aspectRatio', aspectRatio) - // Alternatively setProjection can be used + // Load a model into the scene: + const modelPath = Platform.select({ + android: 'custom/pengu.glb', + ios: 'pengu.glb', + }) + const modelBuffer = FilamentProxy.getAssetByteBuffer(modelPath!) + engine.loadAsset(modelBuffer) + + // Create a default light: + const indirectLightPath = Platform.select({ + android: 'custom/default_env_ibl.ktx', + ios: 'default_env_ibl.ktx', + }) + const indirectLightBuffer = FilamentProxy.getAssetByteBuffer(indirectLightPath!) + engine.createDefaultLight(indirectLightBuffer) + + // Link the surface with the engine: + engine.setSurfaceProvider(surfaceProvider) // Callback for rendering every frame - this.engine.setRenderCallback(this.renderCallback) + engine.setRenderCallback(() => { + engine.getCamera().lookAt(engine.getCameraManipulator()) + }) } /** @internal */ diff --git a/package/src/types/Camera.ts b/package/src/types/Camera.ts index 88666040..1a6f4903 100644 --- a/package/src/types/Camera.ts +++ b/package/src/types/Camera.ts @@ -1,5 +1,4 @@ import { Manipulator } from './Manipulator' -import { Float3 } from './float3' /** * Camera represents the eye through which the scene is viewed. @@ -80,36 +79,6 @@ import { Float3 } from './float3' */ export interface Camera { - // Convenience method - lookAtCameraManipulator(cameraManipulator: Manipulator): void - /** - * - * @param eye The position of the camera in space - * @param center The target position to look at - * @param up The up vector of the camera (Usually (0, 1, 0)) - */ - lookAt(eye: Float3, center: Float3, up: Float3): void - - /** Utility to set the projection matrix from the focal length. - * - * @param focalLengthInMillimeters lens's focal length in millimeters.focalLength > 0. - * @param aspect aspect ratio (You can use view.aspectRatio) - * @param near distance in world units from the camera to the near plane. near > 0. - * @param far distance in world units from the camera to the far plane. far > near. - */ - setLensProjection(focalLengthInMillimeters: number, aspect: number, near: number, far: number): void - - //TODO(Hanno): This also accepts a last parameter called direction. Implement once custom enums are fixed - /** - * Utility to set the projection matrix from the field-of-view. - * - * @param fovInDegrees full field-of-view in degrees. 0 < \p fov < 180. - * @param aspect aspect ratio \f$ \frac{width}{height} \f$. \p aspect > 0. - * @param near distance in world units from the camera to the near plane. \p near > 0. - * @param far distance in world units from the camera to the far plane. \p far > \p near. - * @param direction direction of the \p fovInDegrees parameter. - * - * @see Fov. - */ - setProjection(fov: number, aspect: number, near: number, far: number): void + // Convenience method. The original method works slightly different, this is a simplification, so we don't have to deal with out params. + lookAt(cameraManipulator: Manipulator): void } diff --git a/package/src/types/Engine.ts b/package/src/types/Engine.ts index 599a3b3f..b97c62b3 100644 --- a/package/src/types/Engine.ts +++ b/package/src/types/Engine.ts @@ -5,49 +5,17 @@ import { Renderer } from './Renderer' import { Scene } from './Scene' import { View } from './View' import { FilamentBuffer } from '../native/FilamentBuffer' -import { Entity } from './Entity' -import { FilamentAsset } from './FilamentAsset' -import { Float3 } from './float3' export interface Engine { setSurfaceProvider(surfaceProvider: SurfaceProvider): void setRenderCallback(callback: (engine: Engine) => void): void - /** - * Given a @see FilamentBuffer (e.g. from a .glb file), load the asset into the engine. - * This will by default add all entities from the asset to the attached default scene. - */ - loadAsset(buffer: FilamentBuffer): FilamentAsset - - /** - * Set the indirect light for the scene. - * @param iblBuffer A buffer containing the IBL data (e.g. from a .ktx file) - */ - setIndirectLight(iblBuffer: FilamentBuffer): void + loadAsset(buffer: FilamentBuffer): void + createDefaultLight(iblBuffer: FilamentBuffer): void getRenderer(): Renderer getScene(): Scene getCamera(): Camera getView(): View getCameraManipulator(): Manipulator - - createLightEntity( - type: 'directional' | 'spot' | 'point' | 'focused_point' | 'sun', - colorFahrenheit: number, - intensity: number, - directionX: number, - directionY: number, - directionZ: number, - castShadows: boolean, - ): Entity - - /** - * Transforms the given entity to fit into a unit cube at the origin (0,0,0). - * @param entity The entity to transform - */ - transformToUnitCube(entity: FilamentAsset): void - - setEntityPosition(entity: Entity, position: Float3, multiplyCurrent: boolean): void - setEntityRotation(entity: Entity, angleRadians: number, axis: Float3, multiplyCurrent: boolean): void - setEntityScale(entity: Entity, scale: Float3, multiplyCurrent: boolean): void } diff --git a/package/src/types/FilamentAsset.ts b/package/src/types/FilamentAsset.ts deleted file mode 100644 index f3d11e03..00000000 --- a/package/src/types/FilamentAsset.ts +++ /dev/null @@ -1,5 +0,0 @@ -import type { Entity } from './Entity' - -export interface FilamentAsset { - getRoot(): Entity -} diff --git a/package/src/types/View.ts b/package/src/types/View.ts index 25eece32..ec7ce907 100644 --- a/package/src/types/View.ts +++ b/package/src/types/View.ts @@ -23,6 +23,5 @@ import { Scene } from './Scene' export interface View { camera: Camera scene: Scene - aspectRatio: number setViewport(x: number, y: number, width: number, height: number): void } diff --git a/package/src/types/float3.ts b/package/src/types/float3.ts deleted file mode 100644 index fb7f75d5..00000000 --- a/package/src/types/float3.ts +++ /dev/null @@ -1,4 +0,0 @@ -/** - * A 3D float vector (x, y, z). - */ -export type Float3 = [number, number, number]