add model/view matrix getters & manipulator options

This commit is contained in:
Nick Fisher
2023-11-03 15:20:15 +08:00
parent 83469e93b9
commit 58a9542121
18 changed files with 833 additions and 553 deletions

View File

@@ -1,5 +1,7 @@
#pragma once
#include <mutex>
#include <filament/Scene.h>
#include <gltfio/AssetLoader.h>
@@ -82,6 +84,7 @@ namespace polyvox {
gltfio::ResourceLoader* _gltfResourceLoader = nullptr;
gltfio::TextureProvider* _stbDecoder = nullptr;
gltfio::TextureProvider* _ktxDecoder = nullptr;
std::mutex _animationMutex;
vector<SceneAsset> _assets;
tsl::robin_map<EntityId, int> _entityIdLookup;

View File

@@ -92,6 +92,8 @@ namespace polyvox {
void setCameraExposure(float aperture, float shutterSpeed, float sensitivity);
void setCameraPosition(float x, float y, float z);
void setCameraRotation(float rads, float x, float y, float z);
const math::mat4 getCameraModelMatrix();
const math::mat4 getCameraViewMatrix();
void setCameraModelMatrix(const float* const matrix);
void setCameraFocalLength(float fl);
void setCameraFocusDistance(float focusDistance);
@@ -103,7 +105,7 @@ namespace polyvox {
void scrollUpdate(float x, float y, float delta);
void scrollEnd();
int32_t addLight(LightManager::Type t, float colour, float intensity, float posX, float posY, float posZ, float dirX, float dirY, float dirZ, bool shadows);
EntityId addLight(LightManager::Type t, float colour, float intensity, float posX, float posY, float posZ, float dirX, float dirY, float dirZ, bool shadows);
void removeLight(EntityId entityId);
void clearLights();
void setPostProcessing(bool enabled);

View File

@@ -144,8 +144,12 @@ FLUTTER_PLUGIN_EXPORT void move_camera_to_asset(const void* const viewer, Entity
FLUTTER_PLUGIN_EXPORT void set_view_frustum_culling(const void* const viewer, bool enabled);
FLUTTER_PLUGIN_EXPORT void set_camera_exposure(const void* const viewer, float aperture, float shutterSpeed, float sensitivity);
FLUTTER_PLUGIN_EXPORT void set_camera_position(const void* const viewer, float x, float y, float z);
FLUTTER_PLUGIN_EXPORT void get_camera_position(const void* const viewer);
FLUTTER_PLUGIN_EXPORT void set_camera_rotation(const void* const viewer, float rads, float x, float y, float z);
FLUTTER_PLUGIN_EXPORT void set_camera_model_matrix(const void* const viewer, const float *const matrix);
FLUTTER_PLUGIN_EXPORT const double* const get_camera_model_matrix(const void* const viewer);
FLUTTER_PLUGIN_EXPORT const double* const get_camera_view_matrix(const void* const viewer);
FLUTTER_PLUGIN_EXPORT const double* const get_camera_projection_matrix(const void* const viewer);
FLUTTER_PLUGIN_EXPORT void set_camera_focal_length(const void* const viewer, float focalLength);
FLUTTER_PLUGIN_EXPORT void set_camera_focus_distance(const void* const viewer, float focusDistance);
FLUTTER_PLUGIN_EXPORT int hide_mesh(void* assetManager, EntityId asset, const char* meshName);

View File

@@ -257,7 +257,7 @@ FilamentAsset* AssetManager::getAssetByEntityId(EntityId entityId) {
void AssetManager::updateAnimations() {
std::lock_guard lock(_animationMutex);
RenderableManager &rm = _engine->getRenderableManager();
for (auto& asset : _assets) {
@@ -467,7 +467,8 @@ bool AssetManager::setMorphAnimationBuffer(
int numMorphTargets,
int numFrames,
float frameLengthInMs) {
std::lock_guard lock(_animationMutex);
const auto& pos = _entityIdLookup.find(entityId);
if(pos == _entityIdLookup.end()) {
Log("ERROR: asset not found for entity.");
@@ -543,7 +544,8 @@ bool AssetManager::setBoneAnimationBuffer(
const char** const meshNames,
int numMeshTargets,
float frameLengthInMs) {
std::lock_guard lock(_animationMutex);
const auto& pos = _entityIdLookup.find(entityId);
if(pos == _entityIdLookup.end()) {
Log("ERROR: asset not found for entity.");
@@ -632,6 +634,8 @@ bool AssetManager::setBoneAnimationBuffer(
void AssetManager::playAnimation(EntityId e, int index, bool loop, bool reverse, bool replaceActive, float crossfade) {
std::lock_guard lock(_animationMutex);
if(index < 0) {
Log("ERROR: glTF animation index must be greater than zero.");
return;
@@ -686,6 +690,8 @@ void AssetManager::playAnimation(EntityId e, int index, bool loop, bool reverse,
}
void AssetManager::stopAnimation(EntityId entityId, int index) {
std::lock_guard lock(_animationMutex);
const auto& pos = _entityIdLookup.find(entityId);
if(pos == _entityIdLookup.end()) {
Log("ERROR: asset not found for entity.");

View File

@@ -1108,6 +1108,18 @@ namespace polyvox
cam.setModelMatrix(modelMatrix);
}
const math::mat4 FilamentViewer::getCameraModelMatrix()
{
const auto& cam = _view->getCamera();
return cam.getModelMatrix();
}
const math::mat4 FilamentViewer::getCameraViewMatrix()
{
const auto& cam = _view->getCamera();
return cam.getViewMatrix();
}
void FilamentViewer::_createManipulator()
{
Camera &cam = _view->getCamera();

View File

@@ -84,18 +84,30 @@ extern "C" {
((FilamentViewer*)viewer)->clearLights();
}
EntityId load_glb(void* assetManager, const char* assetPath, bool unlit) {
FLUTTER_PLUGIN_EXPORT EntityId load_glb(void* assetManager, const char* assetPath, bool unlit) {
return ((AssetManager*)assetManager)->loadGlb(assetPath, unlit);
}
EntityId load_gltf(void* assetManager, const char* assetPath, const char* relativePath) {
FLUTTER_PLUGIN_EXPORT EntityId load_gltf(void* assetManager, const char* assetPath, const char* relativePath) {
return ((AssetManager*)assetManager)->loadGltf(assetPath, relativePath);
}
bool set_camera(const void* const viewer, EntityId asset, const char* nodeName) {
FLUTTER_PLUGIN_EXPORT bool set_camera(const void* const viewer, EntityId asset, const char* nodeName) {
return ((FilamentViewer*)viewer)->setCamera(asset, nodeName);
}
const double* const get_camera_model_matrix(const void* const viewer) {
const auto& modelMatrix = ((FilamentViewer*)viewer)->getCameraModelMatrix();
double* array = (double*)calloc(16, sizeof(double));
memcpy(array, modelMatrix.asArray(), 16 * sizeof(double));
return array;
}
const double* const get_camera_view_matrix(const void* const viewer) {
const auto& modelMatrix = ((FilamentViewer*)viewer)->getCameraViewMatrix();
return modelMatrix.asArray();
}
FLUTTER_PLUGIN_EXPORT void set_view_frustum_culling(const void* const viewer, bool enabled) {
((FilamentViewer*)viewer)->setViewFrustumCulling(enabled);
}