-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add API for controlling the scene (#15)
>[!WARNING] > Based on the following PR which should be merged first > - #13 **Goal:** To provide an API from JS that can be used to setup the scene and control the transform of the elements. (Most important) **Changes:** - **Camera:** - Renamed `lookAt` -> `lookAtCameraManipulator` Can be used to make camera look at camera manipulator - New `lookAt` function, which can be used to manually control the position, target and up vector of the camera - New `setLensProjection` & `setProjection` function to Control the lens configuration from JS - **⚠️ Note:** @mrousavy The user would also need to call this when the surface size changes (as we need to pass the aspect ratio to this function). Lets discuss in a quick meeting how to solve this best - **Engine:** - Renamed `createDefaultLight` -> `setIndirectLight`, only sets the indirect light from an FilamentBuffer now (decouples the function from setting a default directional light, which can be now configured by the user) - New `createLightEntity` Function to create a light with the configuration you want - Exposed `transformToUnitCube`. Pass an asset and it will transform the asset to fit into a unit cube - New `setEntityPosition`, `setEntityRotation` and `setEntityScale` to set or update an entities transform - `loadAsset` now returns a `FilamentAsset`. The asset can be used for various operations in the scene. --------- Co-authored-by: Marc Rousavy <me@mrousavy.com>
- Loading branch information
Showing
20 changed files
with
457 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// Created by Marc Rousavy on 22.02.24. | ||
// | ||
|
||
#pragma once | ||
|
||
#include "jsi/EnumMapper.h" | ||
#include <filament/Camera.h> | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include "FilamentAssetWrapper.h" | ||
|
||
#include <utils/Entity.h> | ||
#include <utils/EntityInstance.h> | ||
|
||
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<float> center = aabb.center(); | ||
math::details::TVec3<float> 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<TransformManager> transformInstance = transformManager.getInstance(_asset->getRoot()); | ||
transformManager.setTransform(transformInstance, transform); | ||
} | ||
|
||
std::shared_ptr<EntityWrapper> FilamentAssetWrapper::getRoot() { | ||
Entity rootEntity = _asset->getRoot(); | ||
return std::make_shared<EntityWrapper>(rootEntity); | ||
} | ||
|
||
} // namespace margelo |
Oops, something went wrong.