refactor
This commit is contained in:
@@ -13,7 +13,7 @@ add_library(
|
||||
../ios/src/ktxreader/Ktx1Reader.cpp
|
||||
src/main/cpp/StbProvider.cpp
|
||||
src/main/cpp/JobSystem.cpp
|
||||
../ios/src/SceneAssetLoader.cpp
|
||||
../ios/src/AssetManager.cpp
|
||||
../ios/src/FilamentViewer.cpp
|
||||
../ios/src/PolyvoxFilamentApi.cpp
|
||||
../ios/src/StreamBufferAdapter.cpp
|
||||
|
||||
@@ -19,12 +19,12 @@ class MyApp extends StatefulWidget {
|
||||
State<MyApp> createState() => _MyAppState();
|
||||
}
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
final FilamentController _filamentController = PolyvoxFilamentController();
|
||||
class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
|
||||
late FilamentController _filamentController;
|
||||
|
||||
FilamentAsset? _cube;
|
||||
FilamentAsset? _flightHelmet;
|
||||
FilamentLight? _light;
|
||||
FilamentEntity? _cube;
|
||||
FilamentEntity? _flightHelmet;
|
||||
FilamentEntity? _light;
|
||||
|
||||
final weights = List.filled(255, 0.0);
|
||||
List<String> _targetNames = [];
|
||||
@@ -37,6 +37,7 @@ class _MyAppState extends State<MyApp> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_filamentController = FilamentController(this);
|
||||
}
|
||||
|
||||
void onClick(int index) async {
|
||||
@@ -45,7 +46,7 @@ class _MyAppState extends State<MyApp> {
|
||||
await _filamentController.initialize();
|
||||
break;
|
||||
case -2:
|
||||
await _filamentController.render();
|
||||
_filamentController.render();
|
||||
break;
|
||||
case -4:
|
||||
setState(() {
|
||||
@@ -61,55 +62,48 @@ class _MyAppState extends State<MyApp> {
|
||||
break;
|
||||
|
||||
case 0:
|
||||
await _filamentController.setBackgroundImage('assets/background.ktx');
|
||||
_filamentController.setBackgroundImage('assets/background.ktx');
|
||||
break;
|
||||
case 1:
|
||||
await _filamentController
|
||||
_filamentController
|
||||
.loadSkybox('assets/default_env/default_env_skybox.ktx');
|
||||
break;
|
||||
case -3:
|
||||
await _filamentController
|
||||
.loadIbl('assets/default_env/default_env_ibl.ktx');
|
||||
_filamentController.loadIbl('assets/default_env/default_env_ibl.ktx');
|
||||
break;
|
||||
case 2:
|
||||
await _filamentController.removeSkybox();
|
||||
_filamentController.removeSkybox();
|
||||
break;
|
||||
case 3:
|
||||
_cube = await _filamentController.loadGlb('assets/cube.glb');
|
||||
|
||||
_animationNames = await _filamentController.getAnimationNames(_cube!);
|
||||
_cube = _filamentController.loadGlb('assets/cube.glb');
|
||||
_animationNames = _filamentController.getAnimationNames(_cube!);
|
||||
break;
|
||||
|
||||
case 4:
|
||||
if (_cube != null) {
|
||||
await _filamentController.removeAsset(_cube!);
|
||||
_filamentController.removeAsset(_cube!);
|
||||
}
|
||||
_cube =
|
||||
await _filamentController.loadGltf('assets/cube.gltf', 'assets');
|
||||
print(await _filamentController.getAnimationNames(_cube!));
|
||||
_cube = _filamentController.loadGltf('assets/cube.gltf', 'assets');
|
||||
break;
|
||||
case 5:
|
||||
if (_flightHelmet == null) {
|
||||
_flightHelmet = await _filamentController.loadGltf(
|
||||
'assets/FlightHelmet/FlightHelmet.gltf', 'assets/FlightHelmet');
|
||||
}
|
||||
_flightHelmet ??= _filamentController.loadGltf(
|
||||
'assets/FlightHelmet/FlightHelmet.gltf', 'assets/FlightHelmet');
|
||||
break;
|
||||
case 6:
|
||||
await _filamentController.removeAsset(_cube!);
|
||||
_filamentController.removeAsset(_cube!);
|
||||
break;
|
||||
|
||||
case 7:
|
||||
await _filamentController.setMorphTargetWeights(
|
||||
_cube!, List.filled(8, 1.0));
|
||||
_filamentController.setMorphTargetWeights(_cube!, List.filled(8, 1.0));
|
||||
break;
|
||||
case 8:
|
||||
await _filamentController.setMorphTargetWeights(
|
||||
_cube!, List.filled(8, 0));
|
||||
_filamentController.setMorphTargetWeights(_cube!, List.filled(8, 0));
|
||||
break;
|
||||
case 9:
|
||||
_filamentController.playAnimations(
|
||||
_cube!, List.generate(_animationNames.length, (i) => i),
|
||||
loop: _loop);
|
||||
for (int i = 0; i < _animationNames.length; i++) {
|
||||
_filamentController.playAnimation(_cube!, i, loop: _loop);
|
||||
}
|
||||
|
||||
break;
|
||||
case 10:
|
||||
_filamentController.stopAnimation(_cube!, 0);
|
||||
@@ -145,39 +139,38 @@ class _MyAppState extends State<MyApp> {
|
||||
// _filamentController.setAnimation(_cube!, animation);
|
||||
break;
|
||||
case 16:
|
||||
_targetNames =
|
||||
await _filamentController.getMorphTargetNames(_cube!, "Cube");
|
||||
_targetNames = _filamentController.getMorphTargetNames(_cube!, "Cube");
|
||||
setState(() {});
|
||||
break;
|
||||
case 17:
|
||||
_animationNames = await _filamentController.getAnimationNames(_cube!);
|
||||
_animationNames = _filamentController.getAnimationNames(_cube!);
|
||||
setState(() {});
|
||||
|
||||
break;
|
||||
case 18:
|
||||
await _filamentController.panStart(1, 1);
|
||||
await _filamentController.panUpdate(1, 2);
|
||||
await _filamentController.panEnd();
|
||||
_filamentController.panStart(1, 1);
|
||||
_filamentController.panUpdate(1, 2);
|
||||
_filamentController.panEnd();
|
||||
break;
|
||||
case 19:
|
||||
await _filamentController.panStart(1, 1);
|
||||
await _filamentController.panUpdate(0, 0);
|
||||
await _filamentController.panEnd();
|
||||
_filamentController.panStart(1, 1);
|
||||
_filamentController.panUpdate(0, 0);
|
||||
_filamentController.panEnd();
|
||||
break;
|
||||
case 20:
|
||||
await _filamentController.clearAssets();
|
||||
_filamentController.clearAssets();
|
||||
break;
|
||||
case 21:
|
||||
await _filamentController.setTexture(_cube!, "assets/background.png");
|
||||
_filamentController.setTexture(_cube!, "assets/background.png");
|
||||
break;
|
||||
case 22:
|
||||
await _filamentController.transformToUnitCube(_cube!);
|
||||
_filamentController.transformToUnitCube(_cube!);
|
||||
break;
|
||||
case 23:
|
||||
await _filamentController.setPosition(_cube!, 1.0, 1.0, -1.0);
|
||||
_filamentController.setPosition(_cube!, 1.0, 1.0, -1.0);
|
||||
break;
|
||||
case 24:
|
||||
await _filamentController.setRotation(_cube!, pi / 2, 0.0, 1.0, 0.0);
|
||||
_filamentController.setRotation(_cube!, pi / 2, 0.0, 1.0, 0.0);
|
||||
break;
|
||||
case 25:
|
||||
setState(() {
|
||||
@@ -185,35 +178,34 @@ class _MyAppState extends State<MyApp> {
|
||||
});
|
||||
break;
|
||||
case 26:
|
||||
await _filamentController.setCameraPosition(0, 0, 3);
|
||||
await _filamentController.setCameraRotation(0, 0, 1, 0);
|
||||
_filamentController.setCameraPosition(0, 0, 3);
|
||||
_filamentController.setCameraRotation(0, 0, 1, 0);
|
||||
break;
|
||||
case 27:
|
||||
_framerate = _framerate == 60 ? 30 : 60;
|
||||
await _filamentController.setFrameRate(_framerate);
|
||||
_filamentController.setFrameRate(_framerate);
|
||||
break;
|
||||
case 28:
|
||||
await _filamentController.setBackgroundImagePosition(25, 25);
|
||||
_filamentController.setBackgroundImagePosition(25, 25);
|
||||
break;
|
||||
case 29:
|
||||
_light = await _filamentController.addLight(
|
||||
_light = _filamentController.addLight(
|
||||
1, 6500, 15000000, 0, 1, 0, 0, -1, 0, true);
|
||||
_light = await _filamentController.addLight(
|
||||
_light = _filamentController.addLight(
|
||||
2, 6500, 15000000, 0, 0, 1, 0, 0, -1, true);
|
||||
break;
|
||||
case 30:
|
||||
if (_light != null) {
|
||||
await _filamentController.removeLight(_light!);
|
||||
_filamentController.removeLight(_light!);
|
||||
}
|
||||
break;
|
||||
case 31:
|
||||
await _filamentController.clearLights();
|
||||
_filamentController.clearLights();
|
||||
break;
|
||||
case 32:
|
||||
await _filamentController
|
||||
.setCameraModelMatrix(List<double>.filled(16, 1.0));
|
||||
_filamentController.setCameraModelMatrix(List<double>.filled(16, 1.0));
|
||||
|
||||
// await _filamentController.setBoneTransform(
|
||||
// _filamentController.setBoneTransform(
|
||||
// _cube!,
|
||||
// "Bone.001",
|
||||
// "Cube.001",
|
||||
|
||||
@@ -41,7 +41,7 @@ endif()
|
||||
# of modifying this function.
|
||||
function(APPLY_STANDARD_SETTINGS TARGET)
|
||||
target_compile_features(${TARGET} PUBLIC cxx_std_14)
|
||||
target_compile_options(${TARGET} PRIVATE -Wall -Werror -Wno-unused-function)
|
||||
target_compile_options(${TARGET} PRIVATE -Wall -Werror -Wno-unused-function -Wno-unused-variable)
|
||||
target_compile_options(${TARGET} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:-O3>")
|
||||
target_compile_definitions(${TARGET} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:NDEBUG>")
|
||||
endfunction()
|
||||
|
||||
95
ios/include/AssetManager.hpp
Normal file
95
ios/include/AssetManager.hpp
Normal file
@@ -0,0 +1,95 @@
|
||||
#pragma once
|
||||
|
||||
#include <filament/Scene.h>
|
||||
|
||||
#include <gltfio/AssetLoader.h>
|
||||
#include <gltfio/FilamentAsset.h>
|
||||
#include <gltfio/ResourceLoader.h>
|
||||
|
||||
#include "ResourceManagement.hpp"
|
||||
#include "SceneAsset.hpp"
|
||||
#include "ResourceBuffer.hpp"
|
||||
|
||||
typedef int32_t EntityId;
|
||||
|
||||
namespace polyvox {
|
||||
using namespace filament;
|
||||
using namespace filament::gltfio;
|
||||
|
||||
class AssetManager {
|
||||
public:
|
||||
AssetManager(LoadResource loadResource,
|
||||
FreeResource freeResource,
|
||||
NameComponentManager *ncm,
|
||||
Engine *engine,
|
||||
Scene *scene);
|
||||
~AssetManager();
|
||||
EntityId loadGltf(const char* uri, const char* relativeResourcePath);
|
||||
EntityId loadGlb(const char* uri, bool unlit);
|
||||
FilamentAsset* getAssetByEntityId(EntityId entityId);
|
||||
void remove(EntityId entity);
|
||||
void destroyAll();
|
||||
unique_ptr<vector<string>> getAnimationNames(EntityId entity);
|
||||
unique_ptr<vector<string>> getMorphTargetNames(EntityId entity, const char *meshName);
|
||||
void transformToUnitCube(EntityId e);
|
||||
inline void updateTransform(EntityId e);
|
||||
void setScale(EntityId e, float scale);
|
||||
void setPosition(EntityId e, float x, float y, float z);
|
||||
void setRotation(EntityId e, float rads, float x, float y, float z);
|
||||
const utils::Entity *getCameraEntities(EntityId e);
|
||||
size_t getCameraEntityCount(EntityId e);
|
||||
const utils::Entity* getLightEntities(EntityId e) const noexcept;
|
||||
size_t getLightEntityCount(EntityId e) const noexcept;
|
||||
void updateAnimations();
|
||||
|
||||
bool setBoneAnimationBuffer(
|
||||
EntityId entity,
|
||||
int length,
|
||||
const char** const boneNames,
|
||||
const char** const meshNames,
|
||||
const float* const frameData,
|
||||
int numFrames,
|
||||
float frameLengthInMs);
|
||||
bool setMorphAnimationBuffer(
|
||||
EntityId entity,
|
||||
const char* entityName,
|
||||
const float* const morphData,
|
||||
int numMorphWeights,
|
||||
int numFrames,
|
||||
float frameLengthInMs);
|
||||
void playAnimation(EntityId e, int index, bool loop, bool reverse);
|
||||
void stopAnimation(EntityId e, int index);
|
||||
void setMorphTargetWeights(const char* const entityName, float *weights, int count);
|
||||
void loadTexture(EntityId entity, const char* resourcePath, int renderableIndex);
|
||||
void setAnimationFrame(EntityId entity, int animationIndex, int animationFrame);
|
||||
|
||||
private:
|
||||
LoadResource _loadResource;
|
||||
FreeResource _freeResource;
|
||||
AssetLoader* _assetLoader = nullptr;
|
||||
ResourceLoader* _resourceLoader = nullptr;
|
||||
NameComponentManager* _ncm = nullptr;
|
||||
Engine* _engine;
|
||||
Scene* _scene;
|
||||
MaterialProvider* _unlitProvider = nullptr;
|
||||
MaterialProvider* _ubershaderProvider = nullptr;
|
||||
gltfio::ResourceLoader* _gltfResourceLoader = nullptr;
|
||||
gltfio::TextureProvider* _stbDecoder = nullptr;
|
||||
tsl::robin_map<EntityId, SceneAsset> _assets;
|
||||
|
||||
void setBoneTransform(
|
||||
FilamentInstance* instance,
|
||||
vector<BoneAnimationData> animations,
|
||||
int frameNumber
|
||||
);
|
||||
|
||||
utils::Entity findEntityByName(
|
||||
SceneAsset asset,
|
||||
const char* entityName
|
||||
);
|
||||
|
||||
inline void updateTransform(SceneAsset asset);
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
@@ -31,17 +31,16 @@
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
|
||||
#include "SceneAssetLoader.hpp"
|
||||
#include "SceneAsset.hpp"
|
||||
#include "AssetManager.hpp"
|
||||
#include "ResourceManagement.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace filament;
|
||||
using namespace filament::math;
|
||||
using namespace gltfio;
|
||||
using namespace utils;
|
||||
using namespace camutils;
|
||||
|
||||
typedef int32_t EntityId;
|
||||
|
||||
namespace polyvox {
|
||||
class FilamentViewer {
|
||||
@@ -56,9 +55,7 @@ namespace polyvox {
|
||||
void loadIbl(const char* const iblUri, float intensity);
|
||||
void removeIbl();
|
||||
|
||||
SceneAsset* loadGlb(const char* const uri, bool unlit);
|
||||
SceneAsset* loadGltf(const char* const uri, const char* relativeResourcePath);
|
||||
void removeAsset(SceneAsset* asset);
|
||||
void removeAsset(EntityId asset);
|
||||
// removes all add assets from the current scene
|
||||
void clearAssets();
|
||||
|
||||
@@ -66,8 +63,7 @@ namespace polyvox {
|
||||
void render(uint64_t frameTimeInNanos);
|
||||
void setFrameInterval(float interval);
|
||||
|
||||
bool setFirstCamera(SceneAsset* asset);
|
||||
bool setCamera(SceneAsset* asset, const char* nodeName);
|
||||
bool setCamera(EntityId asset, const char* nodeName);
|
||||
|
||||
void createSwapChain(void* surface, uint32_t width, uint32_t height);
|
||||
void destroySwapChain();
|
||||
@@ -95,9 +91,13 @@ namespace polyvox {
|
||||
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);
|
||||
void removeLight(int32_t entityId);
|
||||
void removeLight(EntityId entityId);
|
||||
void clearLights();
|
||||
|
||||
AssetManager* const getAssetManager() {
|
||||
return (AssetManager* const) _assetManager;
|
||||
}
|
||||
|
||||
private:
|
||||
void createImageRenderable();
|
||||
void loadResources(std::string relativeResourcePath);
|
||||
@@ -124,24 +124,18 @@ namespace polyvox {
|
||||
|
||||
SwapChain* _swapChain = nullptr;
|
||||
|
||||
vector<SceneAsset*> _assets;
|
||||
|
||||
SceneAssetLoader* _ubershaderAssetLoader;
|
||||
SceneAssetLoader* _unlitAssetLoader;
|
||||
NameComponentManager* _ncm;
|
||||
AssetManager* _assetManager = nullptr;
|
||||
|
||||
NameComponentManager* _ncm = nullptr;
|
||||
|
||||
std::mutex mtx; // mutex to ensure thread safety when removing assets
|
||||
|
||||
vector<Entity> _lights;
|
||||
vector<utils::Entity> _lights;
|
||||
Texture* _skyboxTexture = nullptr;
|
||||
Skybox* _skybox = nullptr;
|
||||
Texture* _iblTexture = nullptr;
|
||||
IndirectLight* _indirectLight = nullptr;
|
||||
|
||||
MaterialProvider* _ubershaderProvider = nullptr;
|
||||
MaterialProvider* _unlitProvider = nullptr;
|
||||
|
||||
gltfio::ResourceLoader* _resourceLoader = nullptr;
|
||||
gltfio::TextureProvider* _stbDecoder = nullptr;
|
||||
bool _recomputeAabb = false;
|
||||
|
||||
bool _actualSize = false;
|
||||
@@ -156,7 +150,7 @@ namespace polyvox {
|
||||
uint32_t _imageWidth = 0;
|
||||
mat4f _imageScale;
|
||||
Texture* _imageTexture = nullptr;
|
||||
Entity* _imageEntity = nullptr;
|
||||
utils::Entity* _imageEntity = nullptr;
|
||||
VertexBuffer* _imageVb = nullptr;
|
||||
IndexBuffer* _imageIb = nullptr;
|
||||
Material* _imageMaterial = nullptr;
|
||||
|
||||
@@ -7,111 +7,104 @@
|
||||
|
||||
typedef struct ResourceBuffer ResourceBuffer;
|
||||
|
||||
///
|
||||
/// Frame data for animating multiples bones for multiple meshes.
|
||||
/// [data]
|
||||
typedef int32_t EntityId;
|
||||
|
||||
///
|
||||
/// struct to facilitate passing bone animation frame data between Dart/native.
|
||||
///
|
||||
|
||||
struct BoneAnimation {
|
||||
const char* const* const boneNames;
|
||||
const char* const* const meshNames;
|
||||
const float* const data;
|
||||
size_t numBones;
|
||||
size_t numMeshTargets;
|
||||
};
|
||||
void* create_filament_viewer(void *context, ResourceBuffer (*loadResource)(const char *), void (*freeResource)(uint32_t));
|
||||
void delete_filament_viewer(void *viewer);
|
||||
void* get_asset_manager(void* viewer);
|
||||
void create_render_target(void *viewer, uint32_t textureId, uint32_t width, uint32_t height);
|
||||
void clear_background_image(void *viewer);
|
||||
void set_background_image(void *viewer, const char *path);
|
||||
void set_background_image_position(void *viewer, float x, float y, bool clamp);
|
||||
void set_background_color(void *viewer, const float r, const float g, const float b, const float a);
|
||||
void load_skybox(void *viewer, const char *skyboxPath);
|
||||
void load_ibl(void *viewer, const char *iblPath, float intensity);
|
||||
void remove_skybox(void *viewer);
|
||||
void remove_ibl(void *viewer);
|
||||
EntityId add_light(void *viewer, uint8_t type, float colour, float intensity, float posX, float posY, float posZ, float dirX, float dirY, float dirZ, bool shadows);
|
||||
void remove_light(void *viewer, EntityId entityId);
|
||||
void clear_lights(void *viewer);
|
||||
EntityId load_glb(void *assetManager, const char *assetPath, bool unlit);
|
||||
EntityId load_gltf(void *assetManager, const char *assetPath, const char *relativePath);
|
||||
bool set_camera(void *viewer, EntityId asset, const char *nodeName);
|
||||
void render(void *viewer, uint64_t frameTimeInNanos);
|
||||
void create_swap_chain(void *viewer, void *surface, uint32_t width, uint32_t height);
|
||||
void destroy_swap_chain(void *viewer);
|
||||
void set_frame_interval(void *viewer, float interval);
|
||||
void* get_renderer(void *viewer);
|
||||
void update_viewport_and_camera_projection(void *viewer, int width, int height, float scaleFactor);
|
||||
void scroll_begin(void *viewer);
|
||||
void scroll_update(void *viewer, float x, float y, float z);
|
||||
void scroll_end(void *viewer);
|
||||
|
||||
typedef struct BoneAnimation BoneAnimation;
|
||||
void grab_begin(void *viewer, float x, float y, bool pan);
|
||||
void grab_update(void *viewer, float x, float y);
|
||||
void grab_end(void *viewer);
|
||||
|
||||
|
||||
void* filament_viewer_new(void* context, ResourceBuffer (*loadResource)(const char*), void (*freeResource)(uint32_t));
|
||||
void filament_viewer_delete(void* viewer);
|
||||
void create_render_target(void* viewer, uint32_t textureId, uint32_t width, uint32_t height);
|
||||
void clear_background_image(void* viewer);
|
||||
void set_background_image(void* viewer, const char* path);
|
||||
void set_background_image_position(void* viewer, float x, float y, bool clamp);
|
||||
void set_background_color(void* viewer, const float r, const float g, const float b, const float a);
|
||||
|
||||
void load_skybox(void* viewer, const char* skyboxPath);
|
||||
void load_ibl(void* viewer, const char* iblPath, float intensity);
|
||||
void remove_skybox(void* viewer);
|
||||
void remove_ibl(void* viewer);
|
||||
int32_t add_light(void* viewer, uint8_t type, float colour, float intensity, float posX, float posY, float posZ, float dirX, float dirY, float dirZ, bool shadows);
|
||||
void remove_light(void* viewer, int32_t entityId);
|
||||
void clear_lights(void* viewer);
|
||||
void* load_glb(void* viewer, const char* assetPath, bool unlit);
|
||||
void* load_gltf(void* viewer, const char* assetPath, const char* relativePath);
|
||||
bool set_camera(void* viewer, void* asset, const char* nodeName);
|
||||
void render(void* viewer, uint64_t frameTimeInNanos);
|
||||
void create_swap_chain(void* viewer, void* surface, uint32_t width, uint32_t height);
|
||||
void destroy_swap_chain(void* viewer);
|
||||
void set_frame_interval(void* viewer, float interval);
|
||||
void* get_renderer(void* viewer);
|
||||
void update_viewport_and_camera_projection(void* viewer, int width, int height, float scaleFactor);
|
||||
|
||||
void scroll_begin(void* viewer);
|
||||
void scroll_update(void* viewer, float x, float y , float z);
|
||||
void scroll_end(void* viewer);
|
||||
|
||||
void grab_begin(void* viewer, float x, float y, bool pan);
|
||||
void grab_update(void* viewer, float x, float y);
|
||||
void grab_end(void* viewer);
|
||||
|
||||
void apply_weights(void* asset, const char* const entityName, float* const weights, int count);
|
||||
|
||||
void set_animation(
|
||||
void* asset,
|
||||
const char* const entityName,
|
||||
const float* const morphData,
|
||||
int numMorphWeights,
|
||||
const BoneAnimation* const boneAnimations,
|
||||
int numBoneAnimations,
|
||||
int numFrames,
|
||||
float frameLengthInMs
|
||||
void apply_weights(
|
||||
void* assetManager,
|
||||
EntityId asset,
|
||||
const char *const entityName,
|
||||
float *const weights,
|
||||
int count
|
||||
);
|
||||
|
||||
void set_morph_animation(
|
||||
void* assetManager,
|
||||
EntityId asset,
|
||||
const char *const entityName,
|
||||
const float *const morphData,
|
||||
int numMorphWeights,
|
||||
int numFrames,
|
||||
float frameLengthInMs);
|
||||
|
||||
void set_bone_animation(
|
||||
void* assetManager,
|
||||
EntityId asset,
|
||||
int length,
|
||||
const char** const boneNames,
|
||||
const char** const meshNames,
|
||||
const float* const frameData,
|
||||
int numFrames,
|
||||
float frameLengthInMs);
|
||||
|
||||
// void set_bone_transform(
|
||||
// void* asset,
|
||||
// const char* boneName,
|
||||
// EntityId asset,
|
||||
// const char* boneName,
|
||||
// const char* entityName,
|
||||
// float transX,
|
||||
// float transY,
|
||||
// float transZ,
|
||||
// float transX,
|
||||
// float transY,
|
||||
// float transZ,
|
||||
// float quatX,
|
||||
// float quatY,
|
||||
// float quatZ,
|
||||
// float quatW
|
||||
// );
|
||||
|
||||
void play_animation(void* asset, int index, bool loop, bool reverse);
|
||||
void set_animation_frame(void* asset, int animationIndex, int animationFrame);
|
||||
void stop_animation(void* asset, int index);
|
||||
|
||||
int get_animation_count(void* asset);
|
||||
|
||||
void get_animation_name(void* asset, char* const outPtr, int index);
|
||||
void play_animation(void* assetManager, EntityId asset, int index, bool loop, bool reverse);
|
||||
void set_animation_frame(void* assetManager, EntityId asset, int animationIndex, int animationFrame);
|
||||
void stop_animation(void* assetManager, EntityId asset, int index);
|
||||
int get_animation_count(void* assetManager, EntityId asset);
|
||||
void get_animation_name(void* assetManager, EntityId asset, char *const outPtr, int index);
|
||||
void get_morph_target_name(void* assetManager, EntityId asset, const char *meshName, char *const outPtr, int index);
|
||||
int get_morph_target_name_count(void* assetManager, EntityId asset, const char *meshName);
|
||||
void remove_asset(void *viewer, EntityId asset);
|
||||
void clear_assets(void *viewer);
|
||||
void load_texture(void* assetManager, EntityId asset, const char *assetPath, int renderableIndex);
|
||||
void set_texture(void* assetManager, EntityId asset);
|
||||
void transform_to_unit_cube(void* assetManager, EntityId asset);
|
||||
void set_position(void* assetManager, EntityId asset, float x, float y, float z);
|
||||
void set_rotation(void* assetManager, EntityId asset, float rads, float x, float y, float z);
|
||||
void set_scale(void* assetManager, EntityId asset, float scale);
|
||||
void set_camera_exposure(void *viewer, float aperture, float shutterSpeed, float sensitivity);
|
||||
void set_camera_position(void *viewer, float x, float y, float z);
|
||||
void set_camera_rotation(void *viewer, float rads, float x, float y, float z);
|
||||
void set_camera_model_matrix(void *viewer, const float *const matrix);
|
||||
void set_camera_focal_length(void *viewer, float focalLength);
|
||||
void set_camera_focus_distance(void *viewer, float focusDistance);
|
||||
|
||||
void get_morph_target_name(void* asset, const char* meshName, char* const outPtr, int index );
|
||||
|
||||
int get_morph_target_name_count(void* asset, const char* meshName);
|
||||
|
||||
void remove_asset(void* viewer, void* asset);
|
||||
|
||||
void clear_assets(void* viewer);
|
||||
|
||||
void load_texture(void* asset, const char* assetPath, int renderableIndex);
|
||||
void set_texture(void* asset);
|
||||
|
||||
void transform_to_unit_cube(void* asset);
|
||||
|
||||
void set_position(void* asset, float x, float y, float z);
|
||||
void set_rotation(void* asset, float rads, float x, float y, float z);
|
||||
void set_scale(void* asset, float scale);
|
||||
|
||||
void set_camera_exposure(void* viewer, float aperture, float shutterSpeed, float sensitivity);
|
||||
void set_camera_position(void* viewer, float x, float y, float z);
|
||||
void set_camera_rotation(void* viewer, float rads, float x, float y, float z);
|
||||
void set_camera_model_matrix(void* viewer, const float* const matrix);
|
||||
void set_camera_focal_length(void* viewer, float focalLength);
|
||||
void set_camera_focus_distance(void* viewer, float focusDistance);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
void* filament_viewer_new_ios(void* texture, void* loadResource, void* freeResource, void* resources);
|
||||
void* create_filament_viewer_ios(void* texture, void* loadResource, void* freeResource, void* resources);
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace polyvox {
|
||||
//
|
||||
// Typedef for a function that loads a resource into a ResourceBuffer from an asset URI.
|
||||
//
|
||||
using LoadResource = function<ResourceBuffer(const char* uri)>;
|
||||
using LoadResource = function<ResourceBuffer(const char* uri)>;
|
||||
|
||||
//
|
||||
// Typedef for a function that frees an ID associated with a ResourceBuffer.
|
||||
|
||||
@@ -11,14 +11,12 @@
|
||||
#include <math/mat3.h>
|
||||
#include <math/norm.h>
|
||||
|
||||
#include <gltfio/Animator.h>
|
||||
#include <gltfio/AssetLoader.h>
|
||||
#include <gltfio/FilamentAsset.h>
|
||||
#include <gltfio/ResourceLoader.h>
|
||||
|
||||
#include <utils/NameComponentManager.h>
|
||||
|
||||
#include "ResourceManagement.hpp"
|
||||
#include "SceneAssetAnimation.hpp"
|
||||
|
||||
extern "C" {
|
||||
#include "PolyvoxFilamentApi.h"
|
||||
@@ -30,121 +28,88 @@ namespace polyvox {
|
||||
using namespace utils;
|
||||
using namespace std;
|
||||
|
||||
class SceneAsset {
|
||||
friend class SceneAssetLoader;
|
||||
public:
|
||||
SceneAsset(FilamentAsset* asset, Engine* engine, NameComponentManager* ncm, LoadResource loadResource, FreeResource freeResource);
|
||||
~SceneAsset();
|
||||
|
||||
unique_ptr<vector<string>> getMorphTargetNames(const char* meshName);
|
||||
unique_ptr<vector<string>> getAnimationNames();
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
void loadTexture(const char* resourcePath, int renderableIndex);
|
||||
void setTexture();
|
||||
|
||||
///
|
||||
/// Update the bone/morph target animations to reflect the current frame (if applicable).
|
||||
///
|
||||
void updateAnimations();
|
||||
|
||||
///
|
||||
/// Immediately stop the animation at the specified index. Noop if no animation is playing.
|
||||
///
|
||||
void stopAnimation(int index);
|
||||
|
||||
///
|
||||
/// Play the embedded animation (i.e. animation node embedded in the GLTF asset) under the specified index. If [loop] is true, the animation will repeat indefinitely.
|
||||
///
|
||||
void playAnimation(int index, bool loop, bool reverse);
|
||||
|
||||
void setAnimationFrame(int animationIndex, int animationFrame);
|
||||
|
||||
///
|
||||
/// Set the weights for all [count] morph targets in this asset's entity named [inst] to [weights].
|
||||
/// See [setAnimation] if you want to do the same across a number of frames (and extended to bone transforms).
|
||||
///
|
||||
void setMorphTargetWeights(const char* const entityName, float* weights, int count);
|
||||
|
||||
///
|
||||
/// Animates the asset's morph targets/bone transforms according to the frame weights/transforms specified in [morphData]/[boneData].
|
||||
/// The duration of each "frame" is specified by [frameLengthInMs] (i.e. this is not the framerate of the renderer).
|
||||
/// [morphData] is a contiguous chunk of floats whose length will be (numMorphWeights * numFrames).
|
||||
/// [boneData] is a contiguous chunk of floats whose length will be (numBones * 7 * numFrames) (where 7 is 3 floats for translation, 4 for quat rotation).
|
||||
/// [morphData] and [boneData] will both be copied, so remember to free these after calling this function.
|
||||
///
|
||||
void setAnimation(
|
||||
const char* entityName,
|
||||
const float* const morphData,
|
||||
int numMorphWeights,
|
||||
const BoneAnimation* const targets,
|
||||
int numBoneAnimations,
|
||||
int numFrames,
|
||||
float frameLengthInMs
|
||||
);
|
||||
|
||||
size_t getBoneIndex(const char* name);
|
||||
|
||||
Entity getNode(const char* name);
|
||||
|
||||
void transformToUnitCube();
|
||||
|
||||
void setScale(float scale);
|
||||
|
||||
void setPosition(float x, float y, float z);
|
||||
|
||||
void setRotation(float rads, float x, float y, float z);
|
||||
|
||||
const utils::Entity* getCameraEntities();
|
||||
|
||||
size_t getCameraEntityCount();
|
||||
|
||||
const Entity* getLightEntities() const noexcept;
|
||||
|
||||
size_t getLightEntityCount() const noexcept;
|
||||
|
||||
|
||||
private:
|
||||
|
||||
FilamentAsset* _asset = nullptr;
|
||||
Engine* _engine = nullptr;
|
||||
NameComponentManager* _ncm;
|
||||
|
||||
void setBoneTransform(
|
||||
uint8_t skinIndex,
|
||||
const vector<uint8_t>& boneIndices,
|
||||
const vector<Entity>& targets,
|
||||
const vector<float> data,
|
||||
int frameNumber
|
||||
);
|
||||
|
||||
void updateRuntimeAnimation();
|
||||
|
||||
void updateEmbeddedAnimations();
|
||||
|
||||
Animator* _animator;
|
||||
|
||||
// animation flags;
|
||||
unique_ptr<RuntimeAnimation> _runtimeAnimationBuffer;
|
||||
vector<GLTFAnimation> _embeddedAnimationStatus;
|
||||
|
||||
LoadResource _loadResource;
|
||||
FreeResource _freeResource;
|
||||
|
||||
// a slot to preload textures
|
||||
filament::Texture* _texture = nullptr;
|
||||
|
||||
// initialized to identity
|
||||
math::mat4f _position;
|
||||
|
||||
// initialized to identity
|
||||
math::mat4f _rotation;
|
||||
|
||||
float _scale = 1;
|
||||
|
||||
void updateTransform();
|
||||
typedef std::chrono::time_point<std::chrono::high_resolution_clock> time_point_t;
|
||||
|
||||
struct AnimationStatus {
|
||||
time_point_t mStart = time_point_t::max();
|
||||
bool mLoop = false;
|
||||
bool mReverse = false;
|
||||
float mDuration = 0;
|
||||
int mFrameNumber = -1;
|
||||
};
|
||||
|
||||
//
|
||||
// Use this to manually construct a buffer of frame data for morph animations.
|
||||
//
|
||||
struct MorphAnimationBuffer {
|
||||
utils::EntityInstance<RenderableManager>* mInstance = nullptr;
|
||||
int mNumFrames = -1;
|
||||
float mFrameLengthInMs = 0;
|
||||
vector<float> mFrameData;
|
||||
int mNumMorphWeights = 0;
|
||||
};
|
||||
|
||||
///
|
||||
/// Frame data for the bones/meshes specified by [mBoneIndices] and [mMeshTargets].
|
||||
/// This is mainly used as a wrapper for animation data being transferred from the Dart to the native side.
|
||||
///
|
||||
struct BoneAnimationData {
|
||||
size_t skinIndex = 0;
|
||||
uint8_t mBoneIndex;
|
||||
utils::Entity mMeshTarget;
|
||||
vector<float> mFrameData;
|
||||
};
|
||||
|
||||
//
|
||||
// Use this to manually construct a buffer of frame data for bone animations.
|
||||
//
|
||||
struct BoneAnimationBuffer {
|
||||
int mNumFrames = -1;
|
||||
float mFrameLengthInMs = 0;
|
||||
vector<BoneAnimationData> mAnimations;
|
||||
};
|
||||
|
||||
struct SceneAsset {
|
||||
|
||||
FilamentAsset* mAsset = nullptr;
|
||||
Animator* mAnimator = nullptr;
|
||||
|
||||
// animation flags;
|
||||
bool mAnimating = false;
|
||||
|
||||
// fixed-sized vector containing the status of the morph, bone and GLTF animations.
|
||||
// entries 0 and 1 are the morph/bone animations.
|
||||
// subsequent entries are the GLTF animations.
|
||||
vector<AnimationStatus> mAnimations;
|
||||
|
||||
MorphAnimationBuffer mMorphAnimationBuffer;
|
||||
BoneAnimationBuffer mBoneAnimationBuffer;
|
||||
|
||||
// a slot to preload textures
|
||||
filament::Texture* mTexture = nullptr;
|
||||
|
||||
// initialized to identity
|
||||
math::mat4f mPosition;
|
||||
|
||||
// initialized to identity
|
||||
math::mat4f mRotation;
|
||||
|
||||
float mScale = 1;
|
||||
|
||||
SceneAsset(
|
||||
FilamentAsset* asset
|
||||
) : mAsset(asset) {
|
||||
|
||||
mAnimator = mAsset->getInstance()->getAnimator();
|
||||
|
||||
mAnimations.resize(2 + mAnimator->getAnimationCount());
|
||||
|
||||
for(int i=2; i < mAnimations.size(); i++) {
|
||||
mAnimations[i].mDuration = mAnimator->getAnimationDuration(i-2);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,118 +0,0 @@
|
||||
#ifndef SCENE_ASSET_ANIMATION_H_
|
||||
#define SCENE_ASSET_ANIMATION_H_
|
||||
|
||||
#include "utils/Entity.h"
|
||||
#include <filament/RenderableManager.h>
|
||||
|
||||
namespace polyvox {
|
||||
|
||||
using namespace std;
|
||||
|
||||
using Instance = utils::EntityInstance<filament::RenderableManager>;
|
||||
|
||||
typedef std::chrono::time_point<std::chrono::high_resolution_clock> time_point_t;
|
||||
|
||||
//
|
||||
// Holds the current state of a GLTF animation.
|
||||
// Whenever a SceneAsset is created, an instance of GLTFAnimation will be created for every embedded animation.
|
||||
// On each frame loop, we check if [play] is true, and if so, advance the animation to the correct frame based on [startedAt].
|
||||
// The [GLTFAnimation] will persist for the lifetime of the SceneAsset.
|
||||
//
|
||||
struct GLTFAnimation {
|
||||
GLTFAnimation(bool loop, bool reverse) : loop(loop), reverse(reverse) {}
|
||||
|
||||
//
|
||||
// A flag that is checked each frame to determine whether or not the animation should play.
|
||||
//
|
||||
bool play = false;
|
||||
|
||||
//
|
||||
// If [play] is true, this flag will be checked when the animation is complete. If true, the animation will restart.
|
||||
//
|
||||
bool loop = false;
|
||||
|
||||
//
|
||||
// If true, the animation will be played in reverse.
|
||||
//
|
||||
bool reverse = false;
|
||||
|
||||
//
|
||||
// If [play] is true, this flag will be set to true when the animation is started.
|
||||
//
|
||||
bool started = false;
|
||||
|
||||
//
|
||||
// The index of the animation in the GLTF asset.
|
||||
//
|
||||
int animationIndex = -1;
|
||||
|
||||
//
|
||||
// The time point at which this animation was last started.
|
||||
// This is used to calculate the "animation time offset" that is passed to the Animator.
|
||||
//
|
||||
time_point_t startedAt;
|
||||
|
||||
};
|
||||
|
||||
///
|
||||
/// Holds a single set of frame data that may be used to animate multiple bones/meshes.
|
||||
///
|
||||
struct BoneTransformTarget {
|
||||
|
||||
size_t skinIndex = 0;
|
||||
unique_ptr<vector<uint8_t>> mBoneIndices;
|
||||
unique_ptr<vector<utils::Entity>> mMeshTargets;
|
||||
unique_ptr<vector<float>> mBoneData;
|
||||
|
||||
BoneTransformTarget(
|
||||
unique_ptr<vector<uint8_t>>& boneIndices,
|
||||
unique_ptr<vector<utils::Entity>>& meshTargets,
|
||||
unique_ptr<vector<float>>& boneData) : mBoneIndices(move(boneIndices)), mMeshTargets(move(meshTargets)), mBoneData(move(boneData)) {}
|
||||
|
||||
};
|
||||
|
||||
//
|
||||
// An animation created by manually passing frame data for morph weights/bone transforms.
|
||||
//
|
||||
struct RuntimeAnimation {
|
||||
|
||||
Instance mInstance;
|
||||
|
||||
int frameNumber = -1;
|
||||
int mNumFrames = -1;
|
||||
float mFrameLengthInMs = 0;
|
||||
time_point_t startTime;
|
||||
|
||||
|
||||
float* mMorphFrameData = nullptr;
|
||||
int mNumMorphWeights = 0;
|
||||
|
||||
unique_ptr<vector<BoneTransformTarget>> mTargets;
|
||||
|
||||
RuntimeAnimation(Instance instance,
|
||||
const float* const morphData,
|
||||
int numMorphWeights,
|
||||
unique_ptr<vector<BoneTransformTarget>>& targets,
|
||||
int numFrames,
|
||||
float frameLengthInMs) :
|
||||
mInstance(instance),
|
||||
mNumFrames(numFrames),
|
||||
mFrameLengthInMs(frameLengthInMs),
|
||||
mNumMorphWeights(numMorphWeights),
|
||||
mTargets(move(targets)) {
|
||||
|
||||
if(numMorphWeights > 0) {
|
||||
size_t morphSize = numMorphWeights * mNumFrames * sizeof(float);
|
||||
mMorphFrameData = (float*)malloc(morphSize);
|
||||
memcpy(mMorphFrameData, morphData, morphSize);
|
||||
}
|
||||
}
|
||||
|
||||
~RuntimeAnimation() {
|
||||
delete(mMorphFrameData);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,47 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <filament/Scene.h>
|
||||
|
||||
#include <gltfio/AssetLoader.h>
|
||||
#include <gltfio/FilamentAsset.h>
|
||||
#include <gltfio/ResourceLoader.h>
|
||||
|
||||
#include "ResourceManagement.hpp"
|
||||
#include "SceneAsset.hpp"
|
||||
#include "ResourceBuffer.hpp"
|
||||
|
||||
namespace polyvox {
|
||||
using namespace filament;
|
||||
using namespace filament::gltfio;
|
||||
using namespace utils;
|
||||
|
||||
class SceneAssetLoader {
|
||||
public:
|
||||
SceneAssetLoader(
|
||||
LoadResource loadResource,
|
||||
FreeResource freeResource,
|
||||
MaterialProvider* materialProvider,
|
||||
EntityManager* entityManager,
|
||||
ResourceLoader* resourceLoader,
|
||||
NameComponentManager* ncm,
|
||||
Engine* engine,
|
||||
Scene* scene);
|
||||
~SceneAssetLoader();
|
||||
SceneAsset* fromGltf(const char* uri, const char* relativeResourcePath);
|
||||
SceneAsset* fromGlb(const char* uri);
|
||||
void remove(SceneAsset* asset);
|
||||
void destroyAll();
|
||||
|
||||
private:
|
||||
LoadResource _loadResource;
|
||||
FreeResource _freeResource;
|
||||
AssetLoader* _assetLoader;
|
||||
ResourceLoader* _resourceLoader;
|
||||
NameComponentManager* _ncm;
|
||||
Engine* _engine;
|
||||
Scene* _scene;
|
||||
|
||||
vector<SceneAsset*> _assets;
|
||||
|
||||
};
|
||||
}
|
||||
32
ios/include/TimeIt.hpp
Normal file
32
ios/include/TimeIt.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef TIMEIT_H_
|
||||
#define TIMEIT_H_
|
||||
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus <= 199711L
|
||||
#include <ctime>
|
||||
#else
|
||||
#include <chrono>
|
||||
#endif
|
||||
|
||||
class Timer
|
||||
{
|
||||
public:
|
||||
|
||||
Timer() { reset(); }
|
||||
void reset();
|
||||
double elapsed();
|
||||
|
||||
private:
|
||||
|
||||
#if __cplusplus <= 199711L
|
||||
timespec beg_, end_;
|
||||
#else
|
||||
typedef std::chrono::high_resolution_clock clock_;
|
||||
typedef std::chrono::duration<double, std::ratio<1> > second_;
|
||||
std::chrono::time_point<clock_> beg_;
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
#endif // TIMEIT_H_
|
||||
@@ -4,6 +4,10 @@
|
||||
#include <filament/Texture.h>
|
||||
#include <filament/TextureSampler.h>
|
||||
#include <math/mat4.h>
|
||||
#include <math/vec3.h>
|
||||
#include <math/vec4.h>
|
||||
#include <math/mat3.h>
|
||||
#include <math/norm.h>
|
||||
|
||||
namespace polyvox {
|
||||
class FileMaterialProvider : public MaterialProvider {
|
||||
@@ -36,9 +40,8 @@ namespace polyvox {
|
||||
return hasTexture ? int(uvmap->at(srcIndex)) - 1 : -1;
|
||||
};
|
||||
|
||||
Log("CREATING MATERIAL INSTANCE");
|
||||
auto instance = _m->createInstance();
|
||||
mat3f identity;
|
||||
math::mat3f identity;
|
||||
instance->setParameter("baseColorUvMatrix", identity);
|
||||
instance->setParameter("normalUvMatrix", identity);
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
#ifndef UNLIT_MATERIAL_PROVIDER
|
||||
#define UNLIT_MATERIAL_PROVIDER
|
||||
|
||||
#include "material/unlit_opaque.h"
|
||||
|
||||
namespace polyvox {
|
||||
class UnlitMaterialProvider : public MaterialProvider {
|
||||
|
||||
|
||||
777
ios/src/AssetManager.cpp
Normal file
777
ios/src/AssetManager.cpp
Normal file
@@ -0,0 +1,777 @@
|
||||
#include "AssetManager.hpp"
|
||||
#include "Log.hpp"
|
||||
|
||||
#include <filament/Engine.h>
|
||||
#include <filament/TransformManager.h>
|
||||
#include <filament/Texture.h>
|
||||
#include <filament/RenderableManager.h>
|
||||
|
||||
#include <gltfio/Animator.h>
|
||||
#include <gltfio/AssetLoader.h>
|
||||
#include <gltfio/FilamentAsset.h>
|
||||
#include <gltfio/ResourceLoader.h>
|
||||
#include <gltfio/TextureProvider.h>
|
||||
|
||||
#include <imageio/ImageDecoder.h>
|
||||
|
||||
#include "StreamBufferAdapter.hpp"
|
||||
#include "SceneAsset.hpp"
|
||||
#include "Log.hpp"
|
||||
#include "ResourceManagement.hpp"
|
||||
#include "material/UnlitMaterialProvider.hpp"
|
||||
#include "material/FileMaterialProvider.hpp"
|
||||
#include "gltfio/materials/uberarchive.h"
|
||||
|
||||
extern "C" {
|
||||
#include "material/image.h"
|
||||
#include "material/unlit_opaque.h"
|
||||
}
|
||||
|
||||
namespace polyvox {
|
||||
|
||||
using namespace std;
|
||||
using namespace std::chrono;
|
||||
using namespace image;
|
||||
using namespace utils;
|
||||
using namespace filament;
|
||||
using namespace filament::gltfio;
|
||||
|
||||
AssetManager::AssetManager(LoadResource loadResource,
|
||||
FreeResource freeResource,
|
||||
NameComponentManager *ncm,
|
||||
Engine *engine,
|
||||
Scene *scene)
|
||||
: _loadResource(loadResource),
|
||||
_freeResource(freeResource),
|
||||
_ncm(ncm),
|
||||
_engine(engine),
|
||||
_scene(scene) {
|
||||
|
||||
_stbDecoder = createStbProvider(_engine);
|
||||
|
||||
_gltfResourceLoader = new ResourceLoader({.engine = _engine,
|
||||
.normalizeSkinningWeights = true });
|
||||
_ubershaderProvider = gltfio::createUbershaderProvider(
|
||||
_engine, UBERARCHIVE_DEFAULT_DATA, UBERARCHIVE_DEFAULT_SIZE);
|
||||
EntityManager &em = EntityManager::get();
|
||||
_assetLoader = AssetLoader::create({_engine, _ubershaderProvider, _ncm, &em });
|
||||
_unlitProvider = new UnlitMaterialProvider(_engine);
|
||||
|
||||
_gltfResourceLoader->addTextureProvider("image/png", _stbDecoder);
|
||||
_gltfResourceLoader->addTextureProvider("image/jpeg", _stbDecoder);
|
||||
}
|
||||
|
||||
AssetManager::~AssetManager() {
|
||||
_gltfResourceLoader->asyncCancelLoad();
|
||||
_ubershaderProvider->destroyMaterials();
|
||||
_unlitProvider->destroyMaterials();
|
||||
destroyAll();
|
||||
AssetLoader::destroy(&_assetLoader);
|
||||
|
||||
}
|
||||
|
||||
EntityId AssetManager::loadGltf(const char *uri,
|
||||
const char *relativeResourcePath) {
|
||||
ResourceBuffer rbuf = _loadResource(uri);
|
||||
|
||||
// Parse the glTF file and create Filament entities.
|
||||
FilamentAsset *asset =
|
||||
_assetLoader->createAsset((uint8_t *)rbuf.data, rbuf.size);
|
||||
|
||||
if (!asset) {
|
||||
Log("Unable to parse asset");
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *const *const resourceUris = asset->getResourceUris();
|
||||
const size_t resourceUriCount = asset->getResourceUriCount();
|
||||
|
||||
for (size_t i = 0; i < resourceUriCount; i++) {
|
||||
string uri =
|
||||
string(relativeResourcePath) + string("/") + string(resourceUris[i]);
|
||||
ResourceBuffer buf = _loadResource(uri.c_str());
|
||||
|
||||
// using FunctionCallback = std::function<void(void*, unsigned int, void
|
||||
// *)>; auto cb = [&] (void * ptr, unsigned int len, void * misc) {
|
||||
// };
|
||||
// FunctionCallback fcb = cb;
|
||||
|
||||
ResourceLoader::BufferDescriptor b(buf.data, buf.size);
|
||||
_gltfResourceLoader->addResourceData(resourceUris[i], std::move(b));
|
||||
_freeResource(buf.id);
|
||||
}
|
||||
|
||||
_gltfResourceLoader->loadResources(asset);
|
||||
const utils::Entity *entities = asset->getEntities();
|
||||
RenderableManager &rm = _engine->getRenderableManager();
|
||||
for (int i = 0; i < asset->getEntityCount(); i++) {
|
||||
auto inst = rm.getInstance(entities[i]);
|
||||
rm.setCulling(inst, false);
|
||||
}
|
||||
|
||||
FilamentInstance* inst = asset->getInstance();
|
||||
inst->getAnimator()->updateBoneMatrices();
|
||||
inst->recomputeBoundingBoxes();
|
||||
|
||||
_scene->addEntities(asset->getEntities(), asset->getEntityCount());
|
||||
|
||||
asset->releaseSourceData();
|
||||
|
||||
Log("Load complete for GLTF at URI %s", uri);
|
||||
SceneAsset sceneAsset(asset);
|
||||
utils::Entity e = EntityManager::get().create();
|
||||
|
||||
EntityId eid = Entity::smuggle(e);
|
||||
|
||||
_assets.emplace(eid, sceneAsset);
|
||||
|
||||
return eid;
|
||||
}
|
||||
|
||||
EntityId AssetManager::loadGlb(const char *uri, bool unlit) {
|
||||
|
||||
Log("Loading GLB at URI %s", uri);
|
||||
_loadResource("BLORTY");
|
||||
Log("blorty");
|
||||
|
||||
ResourceBuffer rbuf = _loadResource(uri);
|
||||
|
||||
FilamentAsset *asset = _assetLoader->createAsset(
|
||||
(const uint8_t *)rbuf.data, rbuf.size);
|
||||
|
||||
if (!asset) {
|
||||
Log("Unknown error loading GLB asset.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int entityCount = asset->getEntityCount();
|
||||
|
||||
_scene->addEntities(asset->getEntities(), entityCount);
|
||||
|
||||
_gltfResourceLoader->loadResources(asset);
|
||||
|
||||
// const Entity *entities = asset->getEntities();
|
||||
|
||||
// RenderableManager &rm = _engine->getRenderableManager();
|
||||
|
||||
// MaterialKey config;
|
||||
// auto mi_new = _materialProvider->createMaterialInstance(&config, nullptr);
|
||||
|
||||
// for (int i = 0; i < asset->getEntityCount(); i++) {
|
||||
// auto entityInstance = rm.getInstance(entities[i]);
|
||||
// auto mi = rm.getMaterialInstanceAt(entityInstance, 0);
|
||||
// // auto m = mi->getMaterial();
|
||||
// // auto shading = m->getShading();
|
||||
// // Log("Shading %d", shading);
|
||||
// }
|
||||
|
||||
auto lights = asset->getLightEntities();
|
||||
_scene->addEntities(lights, asset->getLightEntityCount());
|
||||
|
||||
FilamentInstance* inst = asset->getInstance();
|
||||
|
||||
inst->getAnimator()->updateBoneMatrices();
|
||||
|
||||
inst->recomputeBoundingBoxes();
|
||||
|
||||
asset->releaseSourceData();
|
||||
|
||||
_freeResource(rbuf.id);
|
||||
|
||||
SceneAsset sceneAsset(asset);
|
||||
|
||||
utils::Entity e = EntityManager::get().create();
|
||||
EntityId eid = Entity::smuggle(e);
|
||||
|
||||
_assets.emplace(eid, sceneAsset);
|
||||
|
||||
return eid;
|
||||
}
|
||||
|
||||
void AssetManager::destroyAll() {
|
||||
for (auto kp : _assets) {
|
||||
auto asset = kp.second;
|
||||
_scene->removeEntities(asset.mAsset->getEntities(),
|
||||
asset.mAsset->getEntityCount());
|
||||
|
||||
_scene->removeEntities(asset.mAsset->getLightEntities(),
|
||||
asset.mAsset->getLightEntityCount());
|
||||
|
||||
_gltfResourceLoader->evictResourceData();
|
||||
_assetLoader->destroyAsset(asset.mAsset);
|
||||
}
|
||||
_assets.clear();
|
||||
}
|
||||
|
||||
FilamentAsset* AssetManager::getAssetByEntityId(EntityId entityId) {
|
||||
const auto& pos = _assets.find(entityId);
|
||||
if(pos == _assets.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return pos->second.mAsset;
|
||||
}
|
||||
|
||||
|
||||
void AssetManager::updateAnimations() {
|
||||
auto now = high_resolution_clock::now();
|
||||
|
||||
RenderableManager &rm = _engine->getRenderableManager();
|
||||
|
||||
for (auto kp : _assets) {
|
||||
auto asset = kp.second;
|
||||
if(asset.mAnimating) {
|
||||
|
||||
asset.mAnimating = false;
|
||||
|
||||
// morph animation
|
||||
AnimationStatus morphAnimation = asset.mAnimations[0];
|
||||
auto elapsed = (now - morphAnimation.mStart).count();
|
||||
|
||||
int lengthInFrames = static_cast<int>(morphAnimation.mDuration / asset.mMorphAnimationBuffer.mFrameLengthInMs);
|
||||
|
||||
if(elapsed >= morphAnimation.mDuration) {
|
||||
if(morphAnimation.mLoop) {
|
||||
morphAnimation.mStart = now;
|
||||
if(morphAnimation.mReverse) {
|
||||
morphAnimation.mFrameNumber = lengthInFrames;
|
||||
}
|
||||
asset.mAnimating = true;
|
||||
} else {
|
||||
morphAnimation.mStart = time_point_t::max();
|
||||
}
|
||||
} else {
|
||||
asset.mAnimating = true;
|
||||
}
|
||||
|
||||
int frameNumber = static_cast<int>(elapsed / asset.mMorphAnimationBuffer.mFrameLengthInMs);
|
||||
if(frameNumber < lengthInFrames) {
|
||||
if(morphAnimation.mReverse) {
|
||||
frameNumber = lengthInFrames - frameNumber;
|
||||
}
|
||||
rm.setMorphWeights(
|
||||
*(asset.mMorphAnimationBuffer.mInstance),
|
||||
asset.mMorphAnimationBuffer.mFrameData.data() + (morphAnimation.mFrameNumber * asset.mMorphAnimationBuffer.mNumMorphWeights),
|
||||
asset.mMorphAnimationBuffer.mNumMorphWeights);
|
||||
}
|
||||
|
||||
// bone animation
|
||||
AnimationStatus boneAnimation = asset.mAnimations[1];
|
||||
elapsed = (now - boneAnimation.mStart).count();
|
||||
|
||||
lengthInFrames = static_cast<int>(boneAnimation.mDuration / asset.mBoneAnimationBuffer.mFrameLengthInMs);
|
||||
|
||||
if(elapsed >= boneAnimation.mDuration) {
|
||||
if(boneAnimation.mLoop) {
|
||||
boneAnimation.mStart = now;
|
||||
if(boneAnimation.mReverse) {
|
||||
boneAnimation.mFrameNumber = lengthInFrames;
|
||||
}
|
||||
asset.mAnimating = true;
|
||||
} else {
|
||||
boneAnimation.mStart = time_point_t::max();
|
||||
}
|
||||
} else {
|
||||
asset.mAnimating = true;
|
||||
}
|
||||
|
||||
frameNumber = static_cast<int>(elapsed / asset.mBoneAnimationBuffer.mFrameLengthInMs);
|
||||
if(frameNumber < lengthInFrames) {
|
||||
if(boneAnimation.mReverse) {
|
||||
frameNumber = lengthInFrames - frameNumber;
|
||||
}
|
||||
boneAnimation.mFrameNumber = frameNumber;
|
||||
setBoneTransform(
|
||||
asset.mAsset->getInstance(),
|
||||
asset.mBoneAnimationBuffer.mAnimations,
|
||||
frameNumber
|
||||
);
|
||||
}
|
||||
|
||||
// GLTF animations
|
||||
|
||||
Animator* animator = asset.mAnimator;
|
||||
|
||||
for(int j = 2; j < asset.mAnimations.size(); j++) {
|
||||
|
||||
AnimationStatus anim = asset.mAnimations[j];
|
||||
|
||||
elapsed = (now - anim.mStart).count();
|
||||
|
||||
if(elapsed < anim.mDuration) {
|
||||
if(anim.mLoop) {
|
||||
animator->applyAnimation(j-2, anim.mDuration - elapsed);
|
||||
} else {
|
||||
animator->applyAnimation(j-2, elapsed);
|
||||
}
|
||||
asset.mAnimating = true;
|
||||
} else if(anim.mLoop) {
|
||||
animator->applyAnimation(j-2, float(elapsed) ); //% anim.mDuration
|
||||
asset.mAnimating = true;
|
||||
} else if(elapsed - anim.mDuration < 0.3) {
|
||||
// cross-fade
|
||||
animator->applyCrossFade(j-2, anim.mDuration - 0.05, elapsed / 0.3);
|
||||
asset.mAnimating = true;
|
||||
} else {
|
||||
// stop
|
||||
anim.mStart = time_point_t::max();
|
||||
}
|
||||
}
|
||||
asset.mAnimator->updateBoneMatrices();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AssetManager::remove(EntityId entityId) {
|
||||
const auto& pos = _assets.find(entityId);
|
||||
if(pos == _assets.end()) {
|
||||
Log("Couldn't find asset under specified entity id.");
|
||||
return;
|
||||
}
|
||||
auto sceneAsset = pos->second;
|
||||
|
||||
_scene->removeEntities(sceneAsset.mAsset->getEntities(),
|
||||
sceneAsset.mAsset->getEntityCount());
|
||||
|
||||
_scene->removeEntities(sceneAsset.mAsset->getLightEntities(),
|
||||
sceneAsset.mAsset->getLightEntityCount());
|
||||
|
||||
_assetLoader->destroyAsset(sceneAsset.mAsset);
|
||||
|
||||
if(sceneAsset.mTexture) {
|
||||
_engine->destroy(sceneAsset.mTexture);
|
||||
}
|
||||
EntityManager& em = EntityManager::get();
|
||||
em.destroy(Entity::import(entityId));
|
||||
_assets.erase(entityId);
|
||||
}
|
||||
|
||||
void AssetManager::setMorphTargetWeights(const char* const entityName, float *weights, int count) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
utils::Entity AssetManager::findEntityByName(SceneAsset asset, const char* entityName) {
|
||||
utils::Entity entity;
|
||||
for (size_t i = 0, c = asset.mAsset->getEntityCount(); i != c; ++i) {
|
||||
auto entity = asset.mAsset->getEntities()[i];
|
||||
auto name = _ncm->getName(_ncm->getInstance(entity));
|
||||
|
||||
if(strcmp(entityName,name)==0) {
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
|
||||
|
||||
bool AssetManager::setMorphAnimationBuffer(
|
||||
EntityId entityId,
|
||||
const char* entityName,
|
||||
const float* const morphData,
|
||||
int numMorphWeights,
|
||||
int numFrames,
|
||||
float frameLengthInMs) {
|
||||
|
||||
const auto& pos = _assets.find(entityId);
|
||||
if(pos == _assets.end()) {
|
||||
Log("ERROR: asset not found for entity.");
|
||||
return false;
|
||||
}
|
||||
auto asset = pos->second;
|
||||
|
||||
auto entity = findEntityByName(asset, entityName);
|
||||
if(!entity) {
|
||||
Log("Warning: failed to find entity %s", entityName);
|
||||
return false;
|
||||
}
|
||||
RenderableManager &rm = _engine->getRenderableManager();
|
||||
auto inst = rm.getInstance(entity);
|
||||
|
||||
asset.mMorphAnimationBuffer.mInstance = &inst;
|
||||
asset.mMorphAnimationBuffer.mNumFrames = numFrames;
|
||||
asset.mMorphAnimationBuffer.mFrameLengthInMs = frameLengthInMs;
|
||||
asset.mMorphAnimationBuffer.mFrameData.clear();
|
||||
asset.mMorphAnimationBuffer.mFrameData.insert(
|
||||
asset.mMorphAnimationBuffer.mFrameData.begin(),
|
||||
morphData,
|
||||
morphData + (numFrames * numMorphWeights)
|
||||
);
|
||||
|
||||
asset.mMorphAnimationBuffer.mNumMorphWeights = numMorphWeights;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AssetManager::setBoneAnimationBuffer(
|
||||
EntityId entity,
|
||||
int length,
|
||||
const char** const boneNames,
|
||||
const char** const meshNames,
|
||||
const float* const frameData,
|
||||
int numFrames,
|
||||
float frameLengthInMs) {
|
||||
|
||||
const auto& pos = _assets.find(entity);
|
||||
if(pos == _assets.end()) {
|
||||
Log("ERROR: asset not found for entity.");
|
||||
return false;
|
||||
}
|
||||
auto asset = pos->second;
|
||||
|
||||
auto filamentInstance = asset.mAsset->getInstance();
|
||||
|
||||
size_t skinCount = filamentInstance->getSkinCount();
|
||||
|
||||
if(skinCount > 1) {
|
||||
Log("WARNING - skin count > 1 not currently implemented. This will probably not work");
|
||||
}
|
||||
|
||||
int skinIndex = 0;
|
||||
const utils::Entity* joints = filamentInstance->getJointsAt(skinIndex);
|
||||
size_t numJoints = filamentInstance->getJointCountAt(skinIndex);
|
||||
vector<int> boneIndices;
|
||||
for(int i = 0; i < length; i++) {
|
||||
for(int j = 0; j < numJoints; j++) {
|
||||
const char* jointName = _ncm->getName(_ncm->getInstance(joints[j]));
|
||||
if(strcmp(jointName, boneNames[i]) == 0) {
|
||||
boneIndices.push_back(j);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(boneIndices.size() != length) {
|
||||
Log("Failed to find one or more bone indices");
|
||||
return false;
|
||||
}
|
||||
|
||||
asset.mBoneAnimationBuffer.mAnimations.clear();
|
||||
|
||||
for(int i = 0; i < length; i++) {
|
||||
BoneAnimationData boneAnimationData;
|
||||
boneAnimationData.mBoneIndex = boneIndices[i];
|
||||
|
||||
auto entity = findEntityByName(asset, meshNames[i]);
|
||||
|
||||
if(!entity) {
|
||||
Log("Mesh target %s for bone animation could not be found", meshNames[i]);
|
||||
return false;
|
||||
}
|
||||
|
||||
boneAnimationData.mMeshTarget = entity;
|
||||
|
||||
boneAnimationData.mFrameData.insert(
|
||||
boneAnimationData.mFrameData.begin(),
|
||||
frameData[i * numFrames * 7 * sizeof(float)], // 7 == x, y, z, w + three euler angles
|
||||
frameData[(i+1) * numFrames * 7 * sizeof(float)]
|
||||
);
|
||||
|
||||
asset.mBoneAnimationBuffer.mAnimations.push_back(boneAnimationData);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void AssetManager::setBoneTransform(
|
||||
FilamentInstance* filamentInstance,
|
||||
vector<BoneAnimationData> animations,
|
||||
int frameNumber) {
|
||||
|
||||
RenderableManager &rm = _engine->getRenderableManager();
|
||||
|
||||
TransformManager &transformManager = _engine->getTransformManager();
|
||||
|
||||
auto frameDataOffset = frameNumber * 7;
|
||||
|
||||
int skinIndex = 0;
|
||||
|
||||
for(auto& animation : animations) {
|
||||
|
||||
math::mat4f inverseGlobalTransform = inverse(
|
||||
transformManager.getWorldTransform(
|
||||
transformManager.getInstance(animation.mMeshTarget)
|
||||
)
|
||||
);
|
||||
|
||||
utils::Entity joint = filamentInstance->getJointsAt(skinIndex)[animation.mBoneIndex];
|
||||
|
||||
math::mat4f localTransform(math::quatf{
|
||||
animation.mFrameData[frameDataOffset+6],
|
||||
animation.mFrameData[frameDataOffset+3],
|
||||
animation.mFrameData[frameDataOffset+4],
|
||||
animation.mFrameData[frameDataOffset+5]
|
||||
});
|
||||
|
||||
const math::mat4f& inverseBindMatrix = filamentInstance->getInverseBindMatricesAt(animation.skinIndex)[animation.mBoneIndex];
|
||||
auto jointInstance = transformManager.getInstance(joint);
|
||||
math::mat4f globalJointTransform = transformManager.getWorldTransform(jointInstance);
|
||||
|
||||
math::mat4f boneTransform = inverseGlobalTransform * globalJointTransform * localTransform * inverseBindMatrix;
|
||||
auto renderable = rm.getInstance(animation.mMeshTarget);
|
||||
rm.setBones(
|
||||
renderable,
|
||||
&boneTransform,
|
||||
1,
|
||||
animation.mBoneIndex
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void AssetManager::playAnimation(EntityId e, int index, bool loop, bool reverse) {
|
||||
const auto& pos = _assets.find(e);
|
||||
if(pos == _assets.end()) {
|
||||
Log("ERROR: asset not found for entity.");
|
||||
return;
|
||||
}
|
||||
auto asset = pos->second;
|
||||
|
||||
asset.mAnimations[index+2].mStart = high_resolution_clock::now();
|
||||
asset.mAnimations[index+2].mLoop = loop;
|
||||
asset.mAnimations[index+2].mReverse = reverse;
|
||||
}
|
||||
|
||||
void AssetManager::stopAnimation(EntityId entityId, int index) {
|
||||
const auto& pos = _assets.find(entityId);
|
||||
if(pos == _assets.end()) {
|
||||
Log("ERROR: asset not found for entity.");
|
||||
return;
|
||||
}
|
||||
auto asset = pos->second;
|
||||
asset.mAnimations[index+2].mStart = time_point_t::max();
|
||||
}
|
||||
|
||||
void AssetManager::loadTexture(EntityId entity, const char* resourcePath, int renderableIndex) {
|
||||
|
||||
const auto& pos = _assets.find(entity);
|
||||
if(pos == _assets.end()) {
|
||||
Log("ERROR: asset not found for entity.");
|
||||
return;
|
||||
}
|
||||
auto asset = pos->second;
|
||||
|
||||
Log("Loading texture at %s for renderableIndex %d", resourcePath, renderableIndex);
|
||||
|
||||
string rp(resourcePath);
|
||||
|
||||
if(asset.mTexture) {
|
||||
_engine->destroy(asset.mTexture);
|
||||
asset.mTexture = nullptr;
|
||||
}
|
||||
|
||||
ResourceBuffer imageResource = _loadResource(rp.c_str());
|
||||
|
||||
StreamBufferAdapter sb((char *)imageResource.data, (char *)imageResource.data + imageResource.size);
|
||||
|
||||
istream *inputStream = new std::istream(&sb);
|
||||
|
||||
LinearImage *image = new LinearImage(ImageDecoder::decode(
|
||||
*inputStream, rp.c_str(), ImageDecoder::ColorSpace::SRGB));
|
||||
|
||||
if (!image->isValid()) {
|
||||
Log("Invalid image : %s", rp.c_str());
|
||||
delete inputStream;
|
||||
_freeResource(imageResource.id);
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t channels = image->getChannels();
|
||||
uint32_t w = image->getWidth();
|
||||
uint32_t h = image->getHeight();
|
||||
asset.mTexture = Texture::Builder()
|
||||
.width(w)
|
||||
.height(h)
|
||||
.levels(0xff)
|
||||
.format(channels == 3 ? Texture::InternalFormat::RGB16F
|
||||
: Texture::InternalFormat::RGBA16F)
|
||||
.sampler(Texture::Sampler::SAMPLER_2D)
|
||||
.build(*_engine);
|
||||
|
||||
Texture::PixelBufferDescriptor::Callback freeCallback = [](void *buf, size_t,
|
||||
void *data) {
|
||||
delete reinterpret_cast<LinearImage *>(data);
|
||||
};
|
||||
|
||||
Texture::PixelBufferDescriptor buffer(
|
||||
image->getPixelRef(), size_t(w * h * channels * sizeof(float)),
|
||||
channels == 3 ? Texture::Format::RGB : Texture::Format::RGBA,
|
||||
Texture::Type::FLOAT, freeCallback);
|
||||
|
||||
asset.mTexture->setImage(*_engine, 0, std::move(buffer));
|
||||
MaterialInstance* const* inst = asset.mAsset->getInstance()->getMaterialInstances();
|
||||
size_t mic = asset.mAsset->getInstance()->getMaterialInstanceCount();
|
||||
Log("Material instance count : %d", mic);
|
||||
|
||||
auto sampler = TextureSampler();
|
||||
inst[0]->setParameter("baseColorIndex",0);
|
||||
inst[0]->setParameter("baseColorMap",asset.mTexture,sampler);
|
||||
delete inputStream;
|
||||
|
||||
_freeResource(imageResource.id);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void AssetManager::setAnimationFrame(EntityId entity, int animationIndex, int animationFrame) {
|
||||
const auto& pos = _assets.find(entity);
|
||||
if(pos == _assets.end()) {
|
||||
Log("ERROR: asset not found for entity.");
|
||||
return;
|
||||
}
|
||||
auto asset = pos->second;
|
||||
auto offset = 60 * animationFrame * 1000; // TODO - don't hardcore 60fps framerate
|
||||
asset.mAnimator->applyAnimation(animationIndex, offset);
|
||||
asset.mAnimator->updateBoneMatrices();
|
||||
}
|
||||
|
||||
unique_ptr<vector<string>> AssetManager::getAnimationNames(EntityId entity) {
|
||||
|
||||
const auto& pos = _assets.find(entity);
|
||||
|
||||
unique_ptr<vector<string>> names = make_unique<vector<string>>();
|
||||
|
||||
if(pos == _assets.end()) {
|
||||
Log("ERROR: asset not found for entity id.");
|
||||
return names;
|
||||
}
|
||||
auto asset = pos->second;
|
||||
|
||||
size_t count = asset.mAnimator->getAnimationCount();
|
||||
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
names->push_back(asset.mAnimator->getAnimationName(i));
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
unique_ptr<vector<string>> AssetManager::getMorphTargetNames(EntityId entity, const char *meshName) {
|
||||
|
||||
unique_ptr<vector<string>> names = make_unique<vector<string>>();
|
||||
|
||||
const auto& pos = _assets.find(entity);
|
||||
if(pos == _assets.end()) {
|
||||
Log("ERROR: asset not found for entity.");
|
||||
return names;
|
||||
}
|
||||
auto asset = pos->second;
|
||||
|
||||
const utils::Entity *entities = asset.mAsset->getEntities();
|
||||
|
||||
for (int i = 0; i < asset.mAsset->getEntityCount(); i++) {
|
||||
utils::Entity e = entities[i];
|
||||
auto inst = _ncm->getInstance(e);
|
||||
const char *name = _ncm->getName(inst);
|
||||
|
||||
if (strcmp(name, meshName) == 0) {
|
||||
size_t count = asset.mAsset->getMorphTargetCountAt(e);
|
||||
for (int j = 0; j < count; j++) {
|
||||
const char *morphName = asset.mAsset->getMorphTargetNameAt(e, j);
|
||||
names->push_back(morphName);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
void AssetManager::transformToUnitCube(EntityId entity) {
|
||||
const auto& pos = _assets.find(entity);
|
||||
if(pos == _assets.end()) {
|
||||
Log("ERROR: asset not found for entity.");
|
||||
return;
|
||||
}
|
||||
auto asset = pos->second;
|
||||
|
||||
Log("Transforming asset to unit cube.");
|
||||
auto &tm = _engine->getTransformManager();
|
||||
FilamentInstance* inst = asset.mAsset->getInstance();
|
||||
auto aabb = inst->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(inst->getRoot()), transform);
|
||||
}
|
||||
|
||||
void AssetManager::updateTransform(SceneAsset asset) {
|
||||
auto &tm = _engine->getTransformManager();
|
||||
auto transform =
|
||||
asset.mPosition * asset.mRotation * math::mat4f::scaling(asset.mScale);
|
||||
tm.setTransform(tm.getInstance(asset.mAsset->getRoot()), transform);
|
||||
}
|
||||
|
||||
void AssetManager::setScale(EntityId entity, float scale) {
|
||||
const auto& pos = _assets.find(entity);
|
||||
if(pos == _assets.end()) {
|
||||
Log("ERROR: asset not found for entity.");
|
||||
return;
|
||||
}
|
||||
auto asset = pos->second;
|
||||
asset.mScale = scale;
|
||||
updateTransform(asset);
|
||||
}
|
||||
|
||||
void AssetManager::setPosition(EntityId entity, float x, float y, float z) {
|
||||
const auto& pos = _assets.find(entity);
|
||||
if(pos == _assets.end()) {
|
||||
Log("ERROR: asset not found for entity.");
|
||||
return;
|
||||
}
|
||||
auto asset = pos->second;
|
||||
asset.mPosition = math::mat4f::translation(math::float3(x,y,z));
|
||||
updateTransform(asset);
|
||||
}
|
||||
|
||||
void AssetManager::setRotation(EntityId entity, float rads, float x, float y, float z) {
|
||||
const auto& pos = _assets.find(entity);
|
||||
if(pos == _assets.end()) {
|
||||
Log("ERROR: asset not found for entity.");
|
||||
return;
|
||||
}
|
||||
auto asset = pos->second;
|
||||
asset.mRotation = math::mat4f::rotation(rads, math::float3(x,y,z));
|
||||
updateTransform(asset);
|
||||
}
|
||||
|
||||
const utils::Entity *AssetManager::getCameraEntities(EntityId entity) {
|
||||
const auto& pos = _assets.find(entity);
|
||||
if(pos == _assets.end()) {
|
||||
Log("ERROR: asset not found for entity.");
|
||||
return nullptr;
|
||||
}
|
||||
auto asset = pos->second;
|
||||
return asset.mAsset->getCameraEntities();
|
||||
}
|
||||
|
||||
size_t AssetManager::getCameraEntityCount(EntityId entity) {
|
||||
const auto& pos = _assets.find(entity);
|
||||
if(pos == _assets.end()) {
|
||||
Log("ERROR: asset not found for entity.");
|
||||
return 0;
|
||||
}
|
||||
auto asset = pos->second;
|
||||
return asset.mAsset->getCameraEntityCount();
|
||||
}
|
||||
|
||||
const utils::Entity* AssetManager::getLightEntities(EntityId entity) const noexcept {
|
||||
const auto& pos = _assets.find(entity);
|
||||
if(pos == _assets.end()) {
|
||||
Log("ERROR: asset not found for entity.");
|
||||
return nullptr;
|
||||
}
|
||||
auto asset = pos->second;
|
||||
return asset.mAsset->getLightEntities();
|
||||
}
|
||||
|
||||
size_t AssetManager::getLightEntityCount(EntityId entity) const noexcept {
|
||||
const auto& pos = _assets.find(entity);
|
||||
if(pos == _assets.end()) {
|
||||
Log("ERROR: asset not found for entity.");
|
||||
return 0;
|
||||
}
|
||||
auto asset = pos->second;
|
||||
return asset.mAsset->getLightEntityCount();
|
||||
}
|
||||
|
||||
|
||||
} // namespace polyvox
|
||||
@@ -70,17 +70,11 @@
|
||||
#include <mutex>
|
||||
|
||||
#include "Log.hpp"
|
||||
#include "ResourceManagement.hpp"
|
||||
|
||||
extern "C" {
|
||||
#include "material/image.h"
|
||||
#include "material/unlit_opaque.h"
|
||||
}
|
||||
|
||||
#include "FilamentViewer.hpp"
|
||||
#include "StreamBufferAdapter.hpp"
|
||||
#include "material/UnlitMaterialProvider.hpp"
|
||||
#include "material/FileMaterialProvider.hpp"
|
||||
#include "material/image.h"
|
||||
#include "TimeIt.hpp"
|
||||
|
||||
using namespace filament;
|
||||
using namespace filament::math;
|
||||
@@ -116,15 +110,13 @@ static const uint16_t sFullScreenTriangleIndices[3] = {0, 1, 2};
|
||||
FilamentViewer::FilamentViewer(void* context, LoadResource loadResource,
|
||||
FreeResource freeResource)
|
||||
: _loadResource(loadResource), _freeResource(freeResource) {
|
||||
Log("Creating FilamentViewer");
|
||||
|
||||
#if TARGET_OS_IPHONE
|
||||
_engine = Engine::create(Engine::Backend::METAL);
|
||||
#else
|
||||
_engine = Engine::create(Engine::Backend::OPENGL, nullptr, context, nullptr);
|
||||
#endif
|
||||
|
||||
Log("Engine created");
|
||||
|
||||
_renderer = _engine->createRenderer();
|
||||
|
||||
float fr = 60.0f;
|
||||
@@ -138,7 +130,7 @@ FilamentViewer::FilamentViewer(void* context, LoadResource loadResource,
|
||||
|
||||
Log("Scene created");
|
||||
|
||||
Entity camera = EntityManager::get().create();
|
||||
utils::Entity camera = EntityManager::get().create();
|
||||
|
||||
_mainCamera = _engine->createCamera(camera);
|
||||
|
||||
@@ -191,38 +183,17 @@ FilamentViewer::FilamentViewer(void* context, LoadResource loadResource,
|
||||
// Log("Loaded resource of size %d", materialRb.size);
|
||||
// _materialProvider = new FileMaterialProvider(_engine, (void*) materialRb.data, (size_t)materialRb.size);
|
||||
|
||||
_unlitProvider = new UnlitMaterialProvider(_engine);
|
||||
_ubershaderProvider = gltfio::createUbershaderProvider(
|
||||
_engine, UBERARCHIVE_DEFAULT_DATA, UBERARCHIVE_DEFAULT_SIZE);
|
||||
|
||||
EntityManager &em = EntityManager::get();
|
||||
|
||||
_ncm = new NameComponentManager(em);
|
||||
|
||||
_assetManager = new AssetManager(
|
||||
_loadResource,
|
||||
_freeResource,
|
||||
_ncm,
|
||||
_engine,
|
||||
_scene);
|
||||
|
||||
_resourceLoader = new ResourceLoader({.engine = _engine,
|
||||
.normalizeSkinningWeights = true });
|
||||
_stbDecoder = createStbProvider(_engine);
|
||||
_resourceLoader->addTextureProvider("image/png", _stbDecoder);
|
||||
_resourceLoader->addTextureProvider("image/jpeg", _stbDecoder);
|
||||
_ubershaderAssetLoader = new SceneAssetLoader(_loadResource,
|
||||
_freeResource,
|
||||
_ubershaderProvider,
|
||||
&em,
|
||||
_resourceLoader,
|
||||
_ncm,
|
||||
_engine,
|
||||
_scene);
|
||||
|
||||
_unlitAssetLoader = new SceneAssetLoader(_loadResource,
|
||||
_freeResource,
|
||||
_unlitProvider,
|
||||
&em,
|
||||
_resourceLoader,
|
||||
|
||||
_ncm,
|
||||
_engine,
|
||||
_scene);
|
||||
|
||||
|
||||
_imageTexture = Texture::Builder()
|
||||
.width(1)
|
||||
.height(1)
|
||||
@@ -261,7 +232,7 @@ FilamentViewer::FilamentViewer(void* context, LoadResource loadResource,
|
||||
_imageIb->setBuffer(*_engine, {sFullScreenTriangleIndices,
|
||||
sizeof(sFullScreenTriangleIndices)});
|
||||
|
||||
Entity imageEntity = em.create();
|
||||
utils::Entity imageEntity = em.create();
|
||||
RenderableManager::Builder(1)
|
||||
.boundingBox({{}, {1.0f, 1.0f, 1.0f}})
|
||||
.material(0, _imageMaterial->getDefaultInstance())
|
||||
@@ -432,7 +403,6 @@ void FilamentViewer::clearBackgroundImage() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void FilamentViewer::setBackgroundImage(const char *resourcePath) {
|
||||
|
||||
string resourcePathString(resourcePath);
|
||||
@@ -535,11 +505,7 @@ void FilamentViewer::setBackgroundImagePosition(float x, float y, bool clamp=fal
|
||||
|
||||
FilamentViewer::~FilamentViewer() {
|
||||
clearAssets();
|
||||
delete _ubershaderAssetLoader;
|
||||
delete _unlitAssetLoader;
|
||||
_resourceLoader->asyncCancelLoad();
|
||||
_ubershaderProvider->destroyMaterials();
|
||||
_unlitProvider->destroyMaterials();
|
||||
delete _assetManager;
|
||||
|
||||
for(auto it : _lights) {
|
||||
_engine->destroy(it);
|
||||
@@ -616,36 +582,6 @@ void FilamentViewer::destroySwapChain() {
|
||||
}
|
||||
}
|
||||
|
||||
SceneAsset *FilamentViewer::loadGlb(const char *const uri, bool unlit) {
|
||||
SceneAsset *asset;
|
||||
if(unlit) {
|
||||
asset = _unlitAssetLoader->fromGlb(uri);
|
||||
} else {
|
||||
asset = _ubershaderAssetLoader->fromGlb(uri);
|
||||
}
|
||||
if (!asset) {
|
||||
Log("Unknown error loading asset.");
|
||||
} else {
|
||||
_assets.push_back(asset);
|
||||
Log("GLB loaded, asset at index %d", _assets.size() - 1);
|
||||
}
|
||||
|
||||
return asset;
|
||||
}
|
||||
|
||||
SceneAsset *FilamentViewer::loadGltf(const char *const uri,
|
||||
const char *const relativeResourcePath) {
|
||||
Log("Loading GLTF at URI %s with relativeResourcePath %s", uri,
|
||||
relativeResourcePath);
|
||||
SceneAsset *asset = _ubershaderAssetLoader->fromGltf(uri, relativeResourcePath);
|
||||
if (!asset) {
|
||||
Log("Unknown error loading asset.");
|
||||
} else {
|
||||
_assets.push_back(asset);
|
||||
}
|
||||
return asset;
|
||||
}
|
||||
|
||||
void FilamentViewer::clearAssets() {
|
||||
Log("Clearing all assets");
|
||||
if(_mainCamera) {
|
||||
@@ -657,21 +593,18 @@ void FilamentViewer::clearAssets() {
|
||||
_manipulator = nullptr;
|
||||
}
|
||||
|
||||
_ubershaderAssetLoader->destroyAll();
|
||||
_unlitAssetLoader->destroyAll();
|
||||
|
||||
_assets.clear();
|
||||
_assetManager->destroyAll();
|
||||
|
||||
Log("Cleared all assets");
|
||||
}
|
||||
|
||||
void FilamentViewer::removeAsset(SceneAsset *asset) {
|
||||
void FilamentViewer::removeAsset(EntityId asset) {
|
||||
Log("Removing asset from scene");
|
||||
|
||||
mtx.lock();
|
||||
// todo - what if we are using a camera from this asset?
|
||||
_view->setCamera(_mainCamera);
|
||||
_ubershaderAssetLoader->remove(asset);
|
||||
_unlitAssetLoader->remove(asset);
|
||||
_assetManager->remove(asset);
|
||||
mtx.unlock();
|
||||
}
|
||||
|
||||
@@ -704,65 +637,58 @@ void FilamentViewer::setCameraFocusDistance(float focusDistance) {
|
||||
}
|
||||
|
||||
///
|
||||
/// Sets the active camera to the first GLTF camera node found in the hierarchy.
|
||||
/// Useful when your asset only has one camera.
|
||||
///
|
||||
bool FilamentViewer::setFirstCamera(SceneAsset *asset) {
|
||||
size_t count = asset->getCameraEntityCount();
|
||||
if (count == 0) {
|
||||
Log("Failed, no cameras found in current asset.");
|
||||
return false;
|
||||
}
|
||||
const utils::Entity *cameras = asset->getCameraEntities();
|
||||
Log("%zu cameras found in asset", count);
|
||||
auto inst = _ncm->getInstance(cameras[0]);
|
||||
const char *name = _ncm->getName(inst);
|
||||
return setCamera(asset, name);
|
||||
}
|
||||
|
||||
///
|
||||
/// Sets the active camera to the GLTF camera node specified by [name].
|
||||
/// Sets the active camera to the GLTF camera node specified by [name] (or if null, the first camera found under that node).
|
||||
/// N.B. Blender will generally export a three-node hierarchy -
|
||||
/// Camera1->Camera_Orientation->Camera2. The correct name will be the Camera_Orientation.
|
||||
///
|
||||
bool FilamentViewer::setCamera(SceneAsset *asset, const char *cameraName) {
|
||||
Log("Attempting to set camera to %s.", cameraName);
|
||||
bool FilamentViewer::setCamera(EntityId entityId, const char *cameraName) {
|
||||
|
||||
auto asset = _assetManager->getAssetByEntityId(entityId);
|
||||
if(!asset) {
|
||||
Log("Failed to find asset attached to specified entity id.");
|
||||
}
|
||||
size_t count = asset->getCameraEntityCount();
|
||||
if (count == 0) {
|
||||
Log("Failed, no cameras found in current asset.");
|
||||
return false;
|
||||
}
|
||||
|
||||
const utils::Entity *cameras = asset->getCameraEntities();
|
||||
Log("%zu cameras found in asset", count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
const utils::Entity* cameras = asset->getCameraEntities();
|
||||
|
||||
auto inst = _ncm->getInstance(cameras[i]);
|
||||
const char *name = _ncm->getName(inst);
|
||||
Log("Camera %d : %s", i, name);
|
||||
if (strcmp(name, cameraName) == 0) {
|
||||
const utils::Entity target;
|
||||
|
||||
Camera *camera = _engine->getCameraComponent(cameras[i]);
|
||||
_view->setCamera(camera);
|
||||
int i = -1;
|
||||
|
||||
const Viewport &vp = _view->getViewport();
|
||||
const double aspect = (double)vp.width / vp.height;
|
||||
|
||||
const float aperture = camera->getAperture();
|
||||
const float shutterSpeed = camera->getShutterSpeed();
|
||||
const float sens = camera->getSensitivity();
|
||||
|
||||
// camera->setExposure(1.0f);
|
||||
|
||||
Log("Camera focal length : %f aspect %f aperture %f shutter %f sensitivity %f", camera->getFocalLength(),
|
||||
aspect, aperture, shutterSpeed, sens);
|
||||
camera->setScaling({1.0 / aspect, 1.0});
|
||||
Log("Successfully set camera.");
|
||||
return true;
|
||||
if(!cameraName) {
|
||||
i = 0;
|
||||
} else {
|
||||
for (int j = 0; j < count; j++) {
|
||||
auto inst = _ncm->getInstance(cameras[j]);
|
||||
const char *name = _ncm->getName(inst);
|
||||
if (strcmp(name, cameraName) == 0) {
|
||||
i = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(i == -1) {
|
||||
Log("Unable to locate camera under name %s ", cameraName);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Log("Unable to locate camera under name %s ", cameraName);
|
||||
return false;
|
||||
|
||||
Camera *camera = _engine->getCameraComponent(target);
|
||||
_view->setCamera(camera);
|
||||
|
||||
const Viewport &vp = _view->getViewport();
|
||||
const double aspect = (double)vp.width / vp.height;
|
||||
|
||||
// const float aperture = camera->getAperture();
|
||||
// const float shutterSpeed = camera->getShutterSpeed();
|
||||
// const float sens = camera->getSensitivity();
|
||||
// camera->setExposure(1.0f);
|
||||
|
||||
camera->setScaling({1.0 / aspect, 1.0});
|
||||
return true;
|
||||
}
|
||||
|
||||
void FilamentViewer::loadSkybox(const char *const skyboxPath) {
|
||||
@@ -792,9 +718,6 @@ void FilamentViewer::loadSkybox(const char *const skyboxPath) {
|
||||
|
||||
_scene->setSkybox(_skybox);
|
||||
_freeResource(skyboxBuffer.id);
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -856,6 +779,9 @@ void FilamentViewer::loadIbl(const char *const iblPath, float intensity) {
|
||||
}
|
||||
}
|
||||
|
||||
double _elapsed = 0;
|
||||
int _frameCount = 0;
|
||||
|
||||
void FilamentViewer::render(uint64_t frameTimeInNanos) {
|
||||
|
||||
if (!_view || !_mainCamera || !_swapChain) {
|
||||
@@ -863,10 +789,19 @@ void FilamentViewer::render(uint64_t frameTimeInNanos) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto &asset : _assets) {
|
||||
asset->updateAnimations();
|
||||
if(_frameCount == 60) {
|
||||
Log("1 sec average for asset animation update %f", _elapsed);
|
||||
_elapsed = 0;
|
||||
_frameCount = 0;
|
||||
}
|
||||
|
||||
Timer tmr;
|
||||
|
||||
_assetManager->updateAnimations();
|
||||
|
||||
_elapsed += tmr.elapsed();
|
||||
_frameCount++;
|
||||
|
||||
if(_manipulator) {
|
||||
math::float3 eye, target, upward;
|
||||
Camera& cam =_view->getCamera();
|
||||
|
||||
@@ -6,180 +6,210 @@
|
||||
|
||||
using namespace polyvox;
|
||||
|
||||
#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default")))
|
||||
|
||||
extern "C" {
|
||||
|
||||
#include "PolyvoxFilamentApi.h"
|
||||
|
||||
void* filament_viewer_new(void* context, ResourceBuffer (*loadResource)(char const*), void (*freeResource)(unsigned int)) {
|
||||
FLUTTER_PLUGIN_EXPORT void* create_filament_viewer(void* context, ResourceBuffer (*loadResource)(char const*), void (*freeResource)(unsigned int)) {
|
||||
FilamentViewer* v = new FilamentViewer(context, loadResource, freeResource);
|
||||
return (void*)v;
|
||||
}
|
||||
|
||||
void create_render_target(void* viewer, uint32_t textureId, uint32_t width, uint32_t height) {
|
||||
FLUTTER_PLUGIN_EXPORT void create_render_target(void* viewer, uint32_t textureId, uint32_t width, uint32_t height) {
|
||||
((FilamentViewer*)viewer)->createRenderTarget(textureId, width, height);
|
||||
}
|
||||
|
||||
void filament_viewer_delete(void* viewer) {
|
||||
FLUTTER_PLUGIN_EXPORT void delete_filament_viewer(void* viewer) {
|
||||
delete((FilamentViewer*)viewer);
|
||||
}
|
||||
|
||||
void set_background_color(void* viewer, const float r, const float g, const float b, const float a) {
|
||||
FLUTTER_PLUGIN_EXPORT void set_background_color(void* viewer, const float r, const float g, const float b, const float a) {
|
||||
((FilamentViewer*)viewer)->setBackgroundColor(r, g, b, a);
|
||||
}
|
||||
|
||||
void clear_background_image(void* viewer) {
|
||||
FLUTTER_PLUGIN_EXPORT void clear_background_image(void* viewer) {
|
||||
((FilamentViewer*)viewer)->clearBackgroundImage();
|
||||
}
|
||||
|
||||
void set_background_image(void* viewer, const char* path) {
|
||||
FLUTTER_PLUGIN_EXPORT void set_background_image(void* viewer, const char* path) {
|
||||
((FilamentViewer*)viewer)->setBackgroundImage(path);
|
||||
}
|
||||
|
||||
void set_background_image_position(void* viewer, float x, float y, bool clamp) {
|
||||
FLUTTER_PLUGIN_EXPORT void set_background_image_position(void* viewer, float x, float y, bool clamp) {
|
||||
((FilamentViewer*)viewer)->setBackgroundImagePosition(x, y, clamp);
|
||||
}
|
||||
|
||||
void load_skybox(void* viewer, const char* skyboxPath) {
|
||||
FLUTTER_PLUGIN_EXPORT void load_skybox(void* viewer, const char* skyboxPath) {
|
||||
((FilamentViewer*)viewer)->loadSkybox(skyboxPath);
|
||||
}
|
||||
|
||||
void load_ibl(void* viewer, const char* iblPath, float intensity) {
|
||||
FLUTTER_PLUGIN_EXPORT void load_ibl(void* viewer, const char* iblPath, float intensity) {
|
||||
((FilamentViewer*)viewer)->loadIbl(iblPath, intensity);
|
||||
}
|
||||
|
||||
void remove_skybox(void* viewer) {
|
||||
FLUTTER_PLUGIN_EXPORT void remove_skybox(void* viewer) {
|
||||
((FilamentViewer*)viewer)->removeSkybox();
|
||||
}
|
||||
|
||||
void remove_ibl(void* viewer) {
|
||||
FLUTTER_PLUGIN_EXPORT void remove_ibl(void* viewer) {
|
||||
((FilamentViewer*)viewer)->removeIbl();
|
||||
}
|
||||
|
||||
int32_t add_light(void* viewer, uint8_t type, float colour, float intensity, float posX, float posY, float posZ, float dirX, float dirY, float dirZ, bool shadows) {
|
||||
FLUTTER_PLUGIN_EXPORT int32_t add_light(void* viewer, uint8_t type, float colour, float intensity, float posX, float posY, float posZ, float dirX, float dirY, float dirZ, bool shadows) {
|
||||
return ((FilamentViewer*)viewer)->addLight((LightManager::Type)type, colour, intensity, posX, posY, posZ, dirX, dirY, dirZ, shadows);
|
||||
}
|
||||
|
||||
void remove_light(void* viewer, int32_t entityId) {
|
||||
FLUTTER_PLUGIN_EXPORT void remove_light(void* viewer, int32_t entityId) {
|
||||
((FilamentViewer*)viewer)->removeLight(entityId);
|
||||
}
|
||||
|
||||
void clear_lights(void* viewer) {
|
||||
FLUTTER_PLUGIN_EXPORT void clear_lights(void* viewer) {
|
||||
((FilamentViewer*)viewer)->clearLights();
|
||||
}
|
||||
|
||||
void* load_glb(void* viewer, const char* assetPath, bool unlit) {
|
||||
return ((FilamentViewer*)viewer)->loadGlb(assetPath, unlit);
|
||||
FLUTTER_PLUGIN_EXPORT EntityId load_glb(void* assetManager, const char* assetPath, bool unlit) {
|
||||
return ((AssetManager*)assetManager)->loadGlb(assetPath, unlit);
|
||||
}
|
||||
|
||||
void* load_gltf(void* viewer, const char* assetPath, const char* relativePath) {
|
||||
return ((FilamentViewer*)viewer)->loadGltf(assetPath, relativePath);
|
||||
FLUTTER_PLUGIN_EXPORT EntityId load_gltf(void* assetManager, const char* assetPath, const char* relativePath) {
|
||||
return ((AssetManager*)assetManager)->loadGltf(assetPath, relativePath);
|
||||
}
|
||||
|
||||
bool set_camera(void* viewer, void* asset, const char* nodeName) {
|
||||
return ((FilamentViewer*)viewer)->setCamera((SceneAsset*)asset, nodeName);
|
||||
FLUTTER_PLUGIN_EXPORT bool set_camera(void* viewer, EntityId asset, const char* nodeName) {
|
||||
return ((FilamentViewer*)viewer)->setCamera(asset, nodeName);
|
||||
}
|
||||
|
||||
void set_camera_exposure(void* viewer, float aperture, float shutterSpeed, float sensitivity) {
|
||||
FLUTTER_PLUGIN_EXPORT void set_camera_exposure(void* viewer, float aperture, float shutterSpeed, float sensitivity) {
|
||||
((FilamentViewer*)viewer)->setCameraExposure(aperture, shutterSpeed, sensitivity);
|
||||
}
|
||||
|
||||
void set_camera_position(void* viewer, float x, float y, float z) {
|
||||
FLUTTER_PLUGIN_EXPORT void set_camera_position(void* viewer, float x, float y, float z) {
|
||||
((FilamentViewer*)viewer)->setCameraPosition(x, y, z);
|
||||
}
|
||||
|
||||
void set_camera_rotation(void* viewer, float rads, float x, float y, float z) {
|
||||
FLUTTER_PLUGIN_EXPORT void set_camera_rotation(void* viewer, float rads, float x, float y, float z) {
|
||||
((FilamentViewer*)viewer)->setCameraRotation(rads, x, y, z);
|
||||
}
|
||||
|
||||
void set_camera_model_matrix(void* viewer, const float* const matrix) {
|
||||
FLUTTER_PLUGIN_EXPORT void set_camera_model_matrix(void* viewer, const float* const matrix) {
|
||||
((FilamentViewer*)viewer)->setCameraModelMatrix(matrix);
|
||||
}
|
||||
|
||||
void set_camera_focal_length(void* viewer, float focalLength) {
|
||||
FLUTTER_PLUGIN_EXPORT void set_camera_focal_length(void* viewer, float focalLength) {
|
||||
((FilamentViewer*)viewer)->setCameraFocalLength(focalLength);
|
||||
}
|
||||
|
||||
void render(
|
||||
FLUTTER_PLUGIN_EXPORT void render(
|
||||
void* viewer,
|
||||
uint64_t frameTimeInNanos
|
||||
) {
|
||||
((FilamentViewer*)viewer)->render(frameTimeInNanos);
|
||||
}
|
||||
|
||||
void set_frame_interval(
|
||||
FLUTTER_PLUGIN_EXPORT void set_frame_interval(
|
||||
void* viewer,
|
||||
float frameInterval
|
||||
) {
|
||||
((FilamentViewer*)viewer)->setFrameInterval(frameInterval);
|
||||
}
|
||||
|
||||
void destroy_swap_chain(void* viewer) {
|
||||
FLUTTER_PLUGIN_EXPORT void destroy_swap_chain(void* viewer) {
|
||||
((FilamentViewer*)viewer)->destroySwapChain();
|
||||
}
|
||||
|
||||
void create_swap_chain(void* viewer, void* surface=nullptr, uint32_t width=0, uint32_t height=0) {
|
||||
FLUTTER_PLUGIN_EXPORT void create_swap_chain(void* viewer, void* surface=nullptr, uint32_t width=0, uint32_t height=0) {
|
||||
((FilamentViewer*)viewer)->createSwapChain(surface, width, height);
|
||||
}
|
||||
|
||||
void* get_renderer(void* viewer) {
|
||||
FLUTTER_PLUGIN_EXPORT void* get_renderer(void* viewer) {
|
||||
return ((FilamentViewer*)viewer)->getRenderer();
|
||||
}
|
||||
|
||||
void update_viewport_and_camera_projection(void* viewer, int width, int height, float scaleFactor) {
|
||||
FLUTTER_PLUGIN_EXPORT void update_viewport_and_camera_projection(void* viewer, int width, int height, float scaleFactor) {
|
||||
return ((FilamentViewer*)viewer)->updateViewportAndCameraProjection(width, height, scaleFactor);
|
||||
}
|
||||
|
||||
void scroll_update(void* viewer, float x, float y, float delta) {
|
||||
FLUTTER_PLUGIN_EXPORT void scroll_update(void* viewer, float x, float y, float delta) {
|
||||
((FilamentViewer*)viewer)->scrollUpdate(x, y, delta);
|
||||
}
|
||||
|
||||
void scroll_begin(void* viewer) {
|
||||
FLUTTER_PLUGIN_EXPORT void scroll_begin(void* viewer) {
|
||||
((FilamentViewer*)viewer)->scrollBegin();
|
||||
}
|
||||
|
||||
void scroll_end(void* viewer) {
|
||||
FLUTTER_PLUGIN_EXPORT void scroll_end(void* viewer) {
|
||||
((FilamentViewer*)viewer)->scrollEnd();
|
||||
}
|
||||
|
||||
void grab_begin(void* viewer, float x, float y, bool pan) {
|
||||
FLUTTER_PLUGIN_EXPORT void grab_begin(void* viewer, float x, float y, bool pan) {
|
||||
((FilamentViewer*)viewer)->grabBegin(x, y, pan);
|
||||
}
|
||||
|
||||
void grab_update(void* viewer, float x, float y) {
|
||||
FLUTTER_PLUGIN_EXPORT void grab_update(void* viewer, float x, float y) {
|
||||
((FilamentViewer*)viewer)->grabUpdate(x, y);
|
||||
}
|
||||
|
||||
void grab_end(void* viewer) {
|
||||
FLUTTER_PLUGIN_EXPORT void grab_end(void* viewer) {
|
||||
((FilamentViewer*)viewer)->grabEnd();
|
||||
}
|
||||
|
||||
void apply_weights(void* asset, const char* const entityName, float* const weights, int count) {
|
||||
((SceneAsset*)asset)->setMorphTargetWeights(entityName, weights, count);
|
||||
FLUTTER_PLUGIN_EXPORT void* get_asset_manager(void* viewer) {
|
||||
return (void*)((FilamentViewer*)viewer)->getAssetManager();
|
||||
}
|
||||
|
||||
void set_animation(
|
||||
void* asset,
|
||||
FLUTTER_PLUGIN_EXPORT void apply_weights(
|
||||
void* assetManager,
|
||||
EntityId asset,
|
||||
const char* const entityName,
|
||||
float* const weights,
|
||||
int count) {
|
||||
// ((AssetManager*)assetManager)->setMorphTargetWeights(asset, entityName, weights, count);
|
||||
}
|
||||
|
||||
FLUTTER_PLUGIN_EXPORT void set_morph_animation(
|
||||
void* assetManager,
|
||||
EntityId asset,
|
||||
const char* const entityName,
|
||||
const float* const morphData,
|
||||
int numMorphWeights,
|
||||
const BoneAnimation* const boneAnimations,
|
||||
int numBoneAnimations,
|
||||
int numFrames,
|
||||
float frameLengthInMs) {
|
||||
((SceneAsset*)asset)->setAnimation(
|
||||
((AssetManager*)assetManager)->setMorphAnimationBuffer(
|
||||
asset,
|
||||
entityName,
|
||||
morphData,
|
||||
numMorphWeights,
|
||||
boneAnimations,
|
||||
numBoneAnimations,
|
||||
numFrames,
|
||||
frameLengthInMs
|
||||
);
|
||||
}
|
||||
|
||||
FLUTTER_PLUGIN_EXPORT void set_bone_animation(
|
||||
void* assetManager,
|
||||
EntityId asset,
|
||||
int length,
|
||||
const char** const boneNames,
|
||||
const char** const meshNames,
|
||||
const float* const frameData,
|
||||
int numFrames,
|
||||
float frameLengthInMs) {
|
||||
((AssetManager*)assetManager)->setBoneAnimationBuffer(
|
||||
asset,
|
||||
length,
|
||||
boneNames,
|
||||
meshNames,
|
||||
frameData,
|
||||
numFrames,
|
||||
frameLengthInMs
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// void set_bone_transform(
|
||||
// void* asset,
|
||||
// EntityId asset,
|
||||
// const char* boneName,
|
||||
// const char* entityName,
|
||||
// float transX,
|
||||
@@ -190,7 +220,7 @@ extern "C" {
|
||||
// float quatZ,
|
||||
// float quatW
|
||||
// ) {
|
||||
// ((SceneAsset*)asset)->setBoneTransform(
|
||||
// ((AssetManager*)assetManager)->setBoneTransform(
|
||||
// boneName,
|
||||
// entityName,
|
||||
// transX,
|
||||
@@ -206,70 +236,86 @@ extern "C" {
|
||||
// }
|
||||
|
||||
|
||||
void play_animation(void* asset, int index, bool loop, bool reverse) {
|
||||
((SceneAsset*)asset)->playAnimation(index, loop, reverse);
|
||||
FLUTTER_PLUGIN_EXPORT void play_animation(
|
||||
void* assetManager,
|
||||
EntityId asset,
|
||||
int index,
|
||||
bool loop,
|
||||
bool reverse) {
|
||||
((AssetManager*)assetManager)->playAnimation(asset, index, loop, reverse);
|
||||
}
|
||||
|
||||
void set_animation_frame(void* asset, int animationIndex, int animationFrame) {
|
||||
((SceneAsset*)asset)->setAnimationFrame(animationIndex, animationFrame);
|
||||
FLUTTER_PLUGIN_EXPORT void set_animation_frame(
|
||||
void* assetManager,
|
||||
EntityId asset,
|
||||
int animationIndex,
|
||||
int animationFrame) {
|
||||
// ((AssetManager*)assetManager)->setAnimationFrame(asset, animationIndex, animationFrame);
|
||||
}
|
||||
|
||||
int get_animation_count(void* asset) {
|
||||
auto names = ((SceneAsset*)asset)->getAnimationNames();
|
||||
FLUTTER_PLUGIN_EXPORT int get_animation_count(
|
||||
void* assetManager,
|
||||
EntityId asset) {
|
||||
auto names = ((AssetManager*)assetManager)->getAnimationNames(asset);
|
||||
return names->size();
|
||||
}
|
||||
|
||||
void get_animation_name(void* asset, char* const outPtr, int index) {
|
||||
auto names = ((SceneAsset*)asset)->getAnimationNames();
|
||||
FLUTTER_PLUGIN_EXPORT void get_animation_name(
|
||||
void* assetManager,
|
||||
EntityId asset,
|
||||
char* const outPtr,
|
||||
int index
|
||||
) {
|
||||
auto names = ((AssetManager*)assetManager)->getAnimationNames(asset);
|
||||
string name = names->at(index);
|
||||
strcpy(outPtr, name.c_str());
|
||||
}
|
||||
|
||||
int get_morph_target_name_count(void* asset, const char* meshName) {
|
||||
unique_ptr<vector<string>> names = ((SceneAsset*)asset)->getMorphTargetNames(meshName);
|
||||
FLUTTER_PLUGIN_EXPORT int get_morph_target_name_count(void* assetManager, EntityId asset, const char* meshName) {
|
||||
unique_ptr<vector<string>> names = ((AssetManager*)assetManager)->getMorphTargetNames(asset, meshName);
|
||||
return names->size();
|
||||
}
|
||||
|
||||
void get_morph_target_name(void* asset, const char* meshName, char* const outPtr, int index ) {
|
||||
unique_ptr<vector<string>> names = ((SceneAsset*)asset)->getMorphTargetNames(meshName);
|
||||
FLUTTER_PLUGIN_EXPORT void get_morph_target_name(void* assetManager, EntityId asset, const char* meshName, char* const outPtr, int index ) {
|
||||
unique_ptr<vector<string>> names = ((AssetManager*)assetManager)->getMorphTargetNames(asset, meshName);
|
||||
string name = names->at(index);
|
||||
strcpy(outPtr, name.c_str());
|
||||
}
|
||||
|
||||
void remove_asset(void* viewer, void* asset) {
|
||||
((FilamentViewer*)viewer)->removeAsset((SceneAsset*)asset);
|
||||
FLUTTER_PLUGIN_EXPORT void remove_asset(void* viewer, EntityId asset) {
|
||||
((FilamentViewer*)viewer)->removeAsset(asset);
|
||||
}
|
||||
|
||||
void clear_assets(void* viewer) {
|
||||
FLUTTER_PLUGIN_EXPORT void clear_assets(void* viewer) {
|
||||
((FilamentViewer*)viewer)->clearAssets();
|
||||
}
|
||||
|
||||
void load_texture(void* asset, const char* assetPath, int renderableIndex) {
|
||||
((SceneAsset*)asset)->loadTexture(assetPath, renderableIndex);
|
||||
FLUTTER_PLUGIN_EXPORT void load_texture(void* assetManager, EntityId asset, const char* assetPath, int renderableIndex) {
|
||||
// ((AssetManager*)assetManager)->loadTexture(assetPath, renderableIndex);
|
||||
}
|
||||
|
||||
void set_texture(void* asset) {
|
||||
((SceneAsset*)asset)->setTexture();
|
||||
FLUTTER_PLUGIN_EXPORT void set_texture(void* assetManager, EntityId asset) {
|
||||
// ((AssetManager*)assetManager)->setTexture();
|
||||
}
|
||||
|
||||
void transform_to_unit_cube(void* asset) {
|
||||
((SceneAsset*)asset)->transformToUnitCube();
|
||||
FLUTTER_PLUGIN_EXPORT void transform_to_unit_cube(void* assetManager, EntityId asset) {
|
||||
((AssetManager*)assetManager)->transformToUnitCube(asset);
|
||||
}
|
||||
|
||||
void set_position(void* asset, float x, float y, float z) {
|
||||
((SceneAsset*)asset)->setPosition(x, y, z);
|
||||
FLUTTER_PLUGIN_EXPORT void set_position(void* assetManager, EntityId asset, float x, float y, float z) {
|
||||
((AssetManager*)assetManager)->setPosition(asset, x, y, z);
|
||||
}
|
||||
|
||||
void set_rotation(void* asset, float rads, float x, float y, float z) {
|
||||
((SceneAsset*)asset)->setRotation(rads, x, y, z);
|
||||
FLUTTER_PLUGIN_EXPORT void set_rotation(void* assetManager, EntityId asset, float rads, float x, float y, float z) {
|
||||
((AssetManager*)assetManager)->setRotation(asset, rads, x, y, z);
|
||||
}
|
||||
|
||||
void set_scale(void* asset, float scale) {
|
||||
((SceneAsset*)asset)->setScale(scale);
|
||||
}
|
||||
FLUTTER_PLUGIN_EXPORT void set_scale(void* assetManager, EntityId asset, float scale) {
|
||||
((AssetManager*)assetManager)->setScale(asset, scale);
|
||||
}
|
||||
|
||||
void stop_animation(void* asset, int index) {
|
||||
((SceneAsset*)asset)->stopAnimation(index);
|
||||
FLUTTER_PLUGIN_EXPORT void stop_animation(void* assetManager, EntityId asset, int index) {
|
||||
((AssetManager*)assetManager)->stopAnimation(asset, index);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,517 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
|
||||
#include <filament/Engine.h>
|
||||
#include <filament/TransformManager.h>
|
||||
#include <filament/Texture.h>
|
||||
#include <filament/RenderableManager.h>
|
||||
|
||||
#include <gltfio/Animator.h>
|
||||
#include <gltfio/AssetLoader.h>
|
||||
#include <gltfio/FilamentAsset.h>
|
||||
#include <gltfio/ResourceLoader.h>
|
||||
#include <gltfio/TextureProvider.h>
|
||||
|
||||
#include <imageio/ImageDecoder.h>
|
||||
|
||||
#include "StreamBufferAdapter.hpp"
|
||||
#include "SceneAsset.hpp"
|
||||
#include "Log.hpp"
|
||||
#include "ResourceManagement.hpp"
|
||||
#include "SceneAssetAnimation.hpp"
|
||||
|
||||
using namespace std::chrono;
|
||||
|
||||
namespace polyvox {
|
||||
|
||||
using namespace std;
|
||||
using namespace filament;
|
||||
using namespace filament::gltfio;
|
||||
using namespace image;
|
||||
using namespace utils;
|
||||
|
||||
SceneAsset::SceneAsset(FilamentAsset *asset, Engine *engine,
|
||||
NameComponentManager *ncm, LoadResource loadResource, FreeResource freeResource)
|
||||
: _asset(asset), _engine(engine), _ncm(ncm), _loadResource(loadResource), _freeResource(freeResource) {
|
||||
_animator = _asset->getInstance()->getAnimator();
|
||||
for (int i = 0; i < _animator->getAnimationCount(); i++) {
|
||||
_embeddedAnimationStatus.push_back(
|
||||
GLTFAnimation(false,false));
|
||||
}
|
||||
Log("Created animation buffers for %d", _embeddedAnimationStatus.size());
|
||||
}
|
||||
|
||||
SceneAsset::~SceneAsset() {
|
||||
// most other destructor work is deferred to SceneAssetLoader so we don't need to do anything here
|
||||
if(_texture) {
|
||||
_engine->destroy(_texture);
|
||||
_texture = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void SceneAsset::setMorphTargetWeights(const char* const entityName, float *weights, int count) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
void SceneAsset::setAnimation(
|
||||
const char* entityName,
|
||||
const float* const morphData,
|
||||
int numMorphWeights,
|
||||
const BoneAnimation* const boneAnimations,
|
||||
int numBoneAnimations,
|
||||
int numFrames,
|
||||
float frameLengthInMs) {
|
||||
|
||||
auto filamentInstance = _asset->getInstance();
|
||||
|
||||
size_t skinCount = filamentInstance->getSkinCount();
|
||||
|
||||
if(skinCount > 1) {
|
||||
Log("WARNING - skin count > 1 not currently implemented. This will probably not work");
|
||||
}
|
||||
|
||||
auto transforms = make_unique<vector<BoneTransformTarget>>();
|
||||
|
||||
auto numFloats = numFrames * 7;
|
||||
|
||||
for(int i = 0; i < numBoneAnimations; i++) {
|
||||
|
||||
auto boneIndices = make_unique<vector<uint8_t>>();
|
||||
boneIndices->resize(boneAnimations[i].numBones);
|
||||
for(int j = 0; j < boneAnimations[i].numBones; j++) {
|
||||
boneIndices->at(j) = getBoneIndex(boneAnimations[i].boneNames[j]);
|
||||
}
|
||||
|
||||
auto meshTargets = make_unique<vector<Entity>>();
|
||||
for(int j = 0; j < _asset->getEntityCount(); j++) {
|
||||
for(int k = 0; k < boneAnimations[i].numMeshTargets;k++) {
|
||||
auto meshName = boneAnimations[i].meshNames[k];
|
||||
auto entity = _asset->getEntities()[j];
|
||||
auto nameInstance = _ncm->getInstance(entity);
|
||||
if(strcmp(meshName,_ncm->getName(nameInstance))==0) {
|
||||
meshTargets->push_back(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto frameData = make_unique<vector<float>>(
|
||||
boneAnimations[i].data,
|
||||
boneAnimations[i].data + (numFloats * sizeof(float))
|
||||
);
|
||||
|
||||
transforms->push_back(BoneTransformTarget(
|
||||
boneIndices,
|
||||
meshTargets,
|
||||
frameData
|
||||
));
|
||||
}
|
||||
|
||||
RenderableManager &rm = _engine->getRenderableManager();
|
||||
Instance inst;
|
||||
for (size_t i = 0, c = _asset->getEntityCount(); i != c; ++i) {
|
||||
auto entity = _asset->getEntities()[i];
|
||||
auto name = _ncm->getName(_ncm->getInstance(entity));
|
||||
|
||||
if(strcmp(entityName,name)==0) {
|
||||
inst = rm.getInstance(_asset->getEntities()[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if(!inst) {
|
||||
Log("Warning: failed to find Renderable instance for entity %s", entityName);
|
||||
} else {
|
||||
|
||||
_runtimeAnimationBuffer = std::make_unique<RuntimeAnimation>(
|
||||
inst,
|
||||
morphData,
|
||||
numMorphWeights,
|
||||
transforms,
|
||||
numFrames,
|
||||
frameLengthInMs
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void SceneAsset::updateAnimations() {
|
||||
updateRuntimeAnimation();
|
||||
updateEmbeddedAnimations();
|
||||
}
|
||||
|
||||
void SceneAsset::updateRuntimeAnimation() {
|
||||
|
||||
if (!_runtimeAnimationBuffer) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_runtimeAnimationBuffer->frameNumber == -1) {
|
||||
_runtimeAnimationBuffer->startTime = high_resolution_clock::now();
|
||||
}
|
||||
|
||||
duration<double, std::milli> dur =
|
||||
high_resolution_clock::now() - _runtimeAnimationBuffer->startTime;
|
||||
int frameNumber =
|
||||
static_cast<int>(dur.count() / _runtimeAnimationBuffer->mFrameLengthInMs);
|
||||
|
||||
// if the animation has finished, return early
|
||||
if (frameNumber >= _runtimeAnimationBuffer->mNumFrames) {
|
||||
_runtimeAnimationBuffer = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
RenderableManager &rm = _engine->getRenderableManager();
|
||||
if (frameNumber > _runtimeAnimationBuffer->frameNumber) {
|
||||
_runtimeAnimationBuffer->frameNumber = frameNumber;
|
||||
if(_runtimeAnimationBuffer->mMorphFrameData) {
|
||||
auto morphFramePtrOffset = frameNumber * _runtimeAnimationBuffer->mNumMorphWeights;
|
||||
rm.setMorphWeights(
|
||||
_runtimeAnimationBuffer->mInstance,
|
||||
_runtimeAnimationBuffer->mMorphFrameData + morphFramePtrOffset,
|
||||
_runtimeAnimationBuffer->mNumMorphWeights);
|
||||
}
|
||||
|
||||
if(_runtimeAnimationBuffer->mTargets->size() > 0) {
|
||||
for(auto& target : *(_runtimeAnimationBuffer->mTargets)) {
|
||||
|
||||
setBoneTransform(
|
||||
target.skinIndex,
|
||||
*(target.mBoneIndices),
|
||||
*(target.mMeshTargets),
|
||||
*(target.mBoneData),
|
||||
frameNumber
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t SceneAsset::getBoneIndex(const char* name) {
|
||||
|
||||
auto filamentInstance = _asset->getInstance();
|
||||
|
||||
int skinIndex = 0;
|
||||
const utils::Entity* joints = filamentInstance->getJointsAt(skinIndex);
|
||||
size_t numJoints = filamentInstance->getJointCountAt(skinIndex);
|
||||
|
||||
int boneIndex = -1;
|
||||
for(int i =0; i < numJoints; i++) {
|
||||
const char* jointName = _ncm->getName(_ncm->getInstance(joints[i]));
|
||||
if(strcmp(jointName, name) == 0) {
|
||||
boneIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(boneIndex == -1) {
|
||||
Log("Failed to find bone index %d for bone %s", name);
|
||||
}
|
||||
return boneIndex;
|
||||
}
|
||||
|
||||
void SceneAsset::setBoneTransform(
|
||||
uint8_t skinIndex,
|
||||
const vector<uint8_t>& boneIndices,
|
||||
const vector<Entity>& targets,
|
||||
const vector<float> data,
|
||||
int frameNumber) {
|
||||
|
||||
auto filamentInstance = _asset->getInstance();
|
||||
|
||||
RenderableManager &rm = _engine->getRenderableManager();
|
||||
TransformManager &transformManager = _engine->getTransformManager();
|
||||
|
||||
auto frameDataOffset = frameNumber * 7;
|
||||
|
||||
for(auto& target : targets) {
|
||||
|
||||
auto renderable = rm.getInstance(target);
|
||||
|
||||
math::mat4f inverseGlobalTransform = inverse(
|
||||
transformManager.getWorldTransform(
|
||||
transformManager.getInstance(target)
|
||||
)
|
||||
);
|
||||
|
||||
for(auto boneIndex : boneIndices) {
|
||||
|
||||
utils::Entity joint = filamentInstance->getJointsAt(skinIndex)[boneIndex];
|
||||
|
||||
math::mat4f localTransform(math::quatf{
|
||||
data[frameDataOffset+6],
|
||||
data[frameDataOffset+3],
|
||||
data[frameDataOffset+4],
|
||||
data[frameDataOffset+5]
|
||||
});
|
||||
|
||||
const math::mat4f& inverseBindMatrix = filamentInstance->getInverseBindMatricesAt(skinIndex)[boneIndex];
|
||||
auto jointInstance = transformManager.getInstance(joint);
|
||||
math::mat4f globalJointTransform = transformManager.getWorldTransform(jointInstance);
|
||||
|
||||
math::mat4f boneTransform = inverseGlobalTransform * globalJointTransform * localTransform * inverseBindMatrix;
|
||||
|
||||
rm.setBones(
|
||||
renderable,
|
||||
&boneTransform,
|
||||
1, boneIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SceneAsset::playAnimation(int index, bool loop, bool reverse) {
|
||||
if (index > _animator->getAnimationCount() - 1) {
|
||||
Log("Asset does not contain an animation at index %d", index);
|
||||
} else {
|
||||
const char* name = _animator->getAnimationName(index);
|
||||
Log("Playing animation %d : %s", index, name);
|
||||
if (_embeddedAnimationStatus[index].started) {
|
||||
Log("Animation already playing, call stop first.");
|
||||
} else {
|
||||
Log("Starting animation at index %d with loop : %d and reverse %d ", index, loop, reverse);
|
||||
_embeddedAnimationStatus[index].play = true;
|
||||
_embeddedAnimationStatus[index].loop = loop;
|
||||
_embeddedAnimationStatus[index].reverse = reverse;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SceneAsset::stopAnimation(int index) {
|
||||
Log("Stopping animation %d", index);
|
||||
// TODO - does this need to be threadsafe?
|
||||
_embeddedAnimationStatus[index].play = false;
|
||||
_embeddedAnimationStatus[index].started = false;
|
||||
}
|
||||
|
||||
void SceneAsset::loadTexture(const char* resourcePath, int renderableIndex) {
|
||||
|
||||
Log("Loading texture at %s for renderableIndex %d", resourcePath, renderableIndex);
|
||||
|
||||
string rp(resourcePath);
|
||||
|
||||
if(_texture) {
|
||||
_engine->destroy(_texture);
|
||||
_texture = nullptr;
|
||||
}
|
||||
|
||||
ResourceBuffer imageResource = _loadResource(rp.c_str());
|
||||
|
||||
StreamBufferAdapter sb((char *)imageResource.data, (char *)imageResource.data + imageResource.size);
|
||||
|
||||
istream *inputStream = new std::istream(&sb);
|
||||
|
||||
LinearImage *image = new LinearImage(ImageDecoder::decode(
|
||||
*inputStream, rp.c_str(), ImageDecoder::ColorSpace::SRGB));
|
||||
|
||||
if (!image->isValid()) {
|
||||
Log("Invalid image : %s", rp.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t channels = image->getChannels();
|
||||
uint32_t w = image->getWidth();
|
||||
uint32_t h = image->getHeight();
|
||||
_texture = Texture::Builder()
|
||||
.width(w)
|
||||
.height(h)
|
||||
.levels(0xff)
|
||||
.format(channels == 3 ? Texture::InternalFormat::RGB16F
|
||||
: Texture::InternalFormat::RGBA16F)
|
||||
.sampler(Texture::Sampler::SAMPLER_2D)
|
||||
.build(*_engine);
|
||||
|
||||
Texture::PixelBufferDescriptor::Callback freeCallback = [](void *buf, size_t,
|
||||
void *data) {
|
||||
delete reinterpret_cast<LinearImage *>(data);
|
||||
};
|
||||
|
||||
Texture::PixelBufferDescriptor buffer(
|
||||
image->getPixelRef(), size_t(w * h * channels * sizeof(float)),
|
||||
channels == 3 ? Texture::Format::RGB : Texture::Format::RGBA,
|
||||
Texture::Type::FLOAT, freeCallback);
|
||||
|
||||
_texture->setImage(*_engine, 0, std::move(buffer));
|
||||
setTexture();
|
||||
delete inputStream;
|
||||
|
||||
_freeResource(imageResource.id);
|
||||
|
||||
}
|
||||
|
||||
void SceneAsset::setTexture() {
|
||||
|
||||
MaterialInstance* const* inst = _asset->getInstance()->getMaterialInstances();
|
||||
size_t mic = _asset->getInstance()->getMaterialInstanceCount();
|
||||
Log("Material instance count : %d", mic);
|
||||
|
||||
auto sampler = TextureSampler();
|
||||
inst[0]->setParameter("baseColorIndex",0);
|
||||
inst[0]->setParameter("baseColorMap",_texture,sampler);
|
||||
|
||||
}
|
||||
|
||||
void SceneAsset::setAnimationFrame(int animationIndex, int animationFrame) {
|
||||
auto offset = 60 * animationFrame * 1000; // TODO - don't hardcore 60fps framerate
|
||||
_animator->applyAnimation(animationIndex, offset);
|
||||
_animator->updateBoneMatrices();
|
||||
}
|
||||
|
||||
void SceneAsset::updateEmbeddedAnimations() {
|
||||
auto now = high_resolution_clock::now();
|
||||
|
||||
bool needsUpdate = false;
|
||||
for (int animationIndex = 0; animationIndex < _embeddedAnimationStatus.size(); animationIndex++) {
|
||||
auto &status = _embeddedAnimationStatus[animationIndex];
|
||||
if (status.play == false) {
|
||||
continue;
|
||||
}
|
||||
needsUpdate = true;
|
||||
|
||||
float animationLength = _animator->getAnimationDuration(animationIndex);
|
||||
|
||||
duration<double> elapsed =
|
||||
duration_cast<duration<double>>(now - status.startedAt);
|
||||
float animationTimeOffset = 0;
|
||||
bool finished = false;
|
||||
bool fading = false;
|
||||
|
||||
// if the animation hasn't started yet, start the animation at time zero
|
||||
if (!status.started) {
|
||||
status.started = true;
|
||||
status.startedAt = now;
|
||||
|
||||
// if the animation has finished
|
||||
} else if (elapsed.count() >= animationLength) {
|
||||
// if we aren't looping, just mark the animation as finished
|
||||
if(!status.loop) {
|
||||
finished = true;
|
||||
// otherwise, cross-fade between the end of the animation and the start frame over 1 second
|
||||
} else {
|
||||
// if 1 second has elapsed,
|
||||
if(elapsed.count() >= animationLength + 0.3) {
|
||||
// reset the start time to zero
|
||||
status.startedAt = now;
|
||||
// otherwise, apply the first frame of the animation, then cross-fade with the last frame over 1 second
|
||||
} else {
|
||||
fading = true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
animationTimeOffset = elapsed.count();
|
||||
}
|
||||
|
||||
if (finished) {
|
||||
Log("Animation %d finished", animationIndex);
|
||||
status.play = false;
|
||||
status.started = false;
|
||||
} else {
|
||||
if(status.reverse) {
|
||||
animationTimeOffset = _animator->getAnimationDuration(animationIndex) - animationTimeOffset;
|
||||
}
|
||||
_animator->applyAnimation(animationIndex, animationTimeOffset);
|
||||
if(fading) {
|
||||
// Log("Fading at %0f offset %f", elapsed.count() - animationLength, animationTimeOffset);
|
||||
_animator->applyCrossFade(animationIndex, animationLength - 0.05, (elapsed.count() - animationLength) / 0.3);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(needsUpdate) {
|
||||
_animator->updateBoneMatrices();
|
||||
}
|
||||
needsUpdate = false;
|
||||
}
|
||||
|
||||
unique_ptr<vector<string>> SceneAsset::getAnimationNames() {
|
||||
size_t count = _animator->getAnimationCount();
|
||||
|
||||
unique_ptr<vector<string>> names = make_unique<vector<string>>();
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
names->push_back(_animator->getAnimationName(i));
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
unique_ptr<vector<string>> SceneAsset::getMorphTargetNames(const char *meshName) {
|
||||
if (!_asset) {
|
||||
Log("No asset, ignoring call.");
|
||||
return nullptr;
|
||||
}
|
||||
// Log("Retrieving morph target names for mesh %s", meshName);
|
||||
unique_ptr<vector<string>> names = make_unique<vector<string>>();
|
||||
const Entity *entities = _asset->getEntities();
|
||||
|
||||
for (int i = 0; i < _asset->getEntityCount(); i++) {
|
||||
Entity e = entities[i];
|
||||
auto inst = _ncm->getInstance(e);
|
||||
const char *name = _ncm->getName(inst);
|
||||
|
||||
if (strcmp(name, meshName) == 0) {
|
||||
size_t count = _asset->getMorphTargetCountAt(e);
|
||||
for (int j = 0; j < count; j++) {
|
||||
const char *morphName = _asset->getMorphTargetNameAt(e, j);
|
||||
names->push_back(morphName);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
void SceneAsset::transformToUnitCube() {
|
||||
if (!_asset) {
|
||||
Log("No asset, cannot transform.");
|
||||
return;
|
||||
}
|
||||
Log("Transforming asset to unit cube.");
|
||||
auto &tm = _engine->getTransformManager();
|
||||
FilamentInstance* inst = _asset->getInstance();
|
||||
auto aabb = inst->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(inst->getRoot()), transform);
|
||||
}
|
||||
|
||||
void SceneAsset::updateTransform() {
|
||||
auto &tm = _engine->getTransformManager();
|
||||
auto transform =
|
||||
_position * _rotation * math::mat4f::scaling(_scale);
|
||||
tm.setTransform(tm.getInstance(_asset->getRoot()), transform);
|
||||
}
|
||||
|
||||
void SceneAsset::setScale(float scale) {
|
||||
_scale = scale;
|
||||
updateTransform();
|
||||
}
|
||||
|
||||
void SceneAsset::setPosition(float x, float y, float z) {
|
||||
Log("Setting position to %f %f %f", x, y, z);
|
||||
_position = math::mat4f::translation(math::float3(x,y,z));
|
||||
updateTransform();
|
||||
}
|
||||
|
||||
void SceneAsset::setRotation(float rads, float x, float y, float z) {
|
||||
Log("Rotating %f radians around axis %f %f %f", rads, x, y, z);
|
||||
_rotation = math::mat4f::rotation(rads, math::float3(x,y,z));
|
||||
updateTransform();
|
||||
}
|
||||
|
||||
const utils::Entity *SceneAsset::getCameraEntities() {
|
||||
return _asset->getCameraEntities();
|
||||
}
|
||||
|
||||
size_t SceneAsset::getCameraEntityCount() {
|
||||
return _asset->getCameraEntityCount();
|
||||
}
|
||||
|
||||
const Entity* SceneAsset::getLightEntities() const noexcept {
|
||||
return _asset->getLightEntities();
|
||||
}
|
||||
|
||||
size_t SceneAsset::getLightEntityCount() const noexcept {
|
||||
return _asset->getLightEntityCount();
|
||||
}
|
||||
|
||||
|
||||
} // namespace polyvox
|
||||
@@ -1,196 +0,0 @@
|
||||
#include "SceneAssetLoader.hpp"
|
||||
#include "Log.hpp"
|
||||
|
||||
#include <gltfio/Animator.h>
|
||||
|
||||
namespace polyvox {
|
||||
|
||||
using namespace filament;
|
||||
using namespace filament::gltfio;
|
||||
|
||||
SceneAssetLoader::SceneAssetLoader(LoadResource loadResource,
|
||||
FreeResource freeResource,
|
||||
MaterialProvider* materialProvider,
|
||||
EntityManager* entityManager,
|
||||
ResourceLoader *resourceLoader,
|
||||
NameComponentManager *ncm,
|
||||
Engine *engine,
|
||||
Scene *scene)
|
||||
: _loadResource(loadResource), _freeResource(freeResource), _resourceLoader(resourceLoader), _ncm(ncm),
|
||||
_engine(engine), _scene(scene) {
|
||||
_assetLoader = AssetLoader::create({_engine, materialProvider, _ncm, entityManager});
|
||||
}
|
||||
|
||||
SceneAssetLoader::~SceneAssetLoader() {
|
||||
destroyAll();
|
||||
AssetLoader::destroy(&_assetLoader);
|
||||
}
|
||||
|
||||
SceneAsset *SceneAssetLoader::fromGltf(const char *uri,
|
||||
const char *relativeResourcePath) {
|
||||
ResourceBuffer rbuf = _loadResource(uri);
|
||||
|
||||
// Parse the glTF file and create Filament entities.
|
||||
Log("Creating asset from JSON");
|
||||
FilamentAsset *asset =
|
||||
_assetLoader->createAsset((uint8_t *)rbuf.data, rbuf.size);
|
||||
Log("Created asset from JSON");
|
||||
|
||||
if (!asset) {
|
||||
Log("Unable to parse asset");
|
||||
return nullptr;
|
||||
}
|
||||
Log("Loading relative resources");
|
||||
|
||||
const char *const *const resourceUris = asset->getResourceUris();
|
||||
const size_t resourceUriCount = asset->getResourceUriCount();
|
||||
|
||||
Log("Loading %d resources for asset", resourceUriCount);
|
||||
|
||||
for (size_t i = 0; i < resourceUriCount; i++) {
|
||||
string uri =
|
||||
string(relativeResourcePath) + string("/") + string(resourceUris[i]);
|
||||
Log("Creating resource buffer for resource at %s", uri.c_str());
|
||||
ResourceBuffer buf = _loadResource(uri.c_str());
|
||||
|
||||
// using FunctionCallback = std::function<void(void*, unsigned int, void
|
||||
// *)>; auto cb = [&] (void * ptr, unsigned int len, void * misc) {
|
||||
// };
|
||||
// FunctionCallback fcb = cb;
|
||||
|
||||
ResourceLoader::BufferDescriptor b(buf.data, buf.size);
|
||||
_resourceLoader->addResourceData(resourceUris[i], std::move(b));
|
||||
_freeResource(buf.id);
|
||||
}
|
||||
|
||||
_resourceLoader->loadResources(asset);
|
||||
const Entity *entities = asset->getEntities();
|
||||
RenderableManager &rm = _engine->getRenderableManager();
|
||||
for (int i = 0; i < asset->getEntityCount(); i++) {
|
||||
Entity e = entities[i];
|
||||
auto inst = rm.getInstance(e);
|
||||
rm.setCulling(inst, false);
|
||||
}
|
||||
|
||||
FilamentInstance* inst = asset->getInstance();
|
||||
inst->getAnimator()->updateBoneMatrices();
|
||||
|
||||
inst->recomputeBoundingBoxes();
|
||||
|
||||
_scene->addEntities(asset->getEntities(), asset->getEntityCount());
|
||||
|
||||
Log("Loaded relative resources");
|
||||
asset->releaseSourceData();
|
||||
|
||||
Log("Load complete for GLTF at URI %s", uri);
|
||||
return new SceneAsset(asset, _engine, _ncm, _loadResource,_freeResource);
|
||||
}
|
||||
|
||||
SceneAsset *SceneAssetLoader::fromGlb(const char *uri) {
|
||||
Log("Loading GLB at URI %s", uri);
|
||||
|
||||
ResourceBuffer rbuf = _loadResource(uri);
|
||||
|
||||
FilamentAsset *asset = _assetLoader->createAsset(
|
||||
(const uint8_t *)rbuf.data, rbuf.size);
|
||||
|
||||
|
||||
if (!asset) {
|
||||
Log("Unknown error loading GLB asset.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int entityCount = asset->getEntityCount();
|
||||
|
||||
_scene->addEntities(asset->getEntities(), entityCount);
|
||||
|
||||
Log("Added %d entities to scene", entityCount);
|
||||
|
||||
_resourceLoader->loadResources(asset);
|
||||
|
||||
Log("Resources loaded.");
|
||||
|
||||
// const Entity *entities = asset->getEntities();
|
||||
|
||||
// RenderableManager &rm = _engine->getRenderableManager();
|
||||
|
||||
// MaterialKey config;
|
||||
// auto mi_new = _materialProvider->createMaterialInstance(&config, nullptr);
|
||||
|
||||
// for (int i = 0; i < asset->getEntityCount(); i++) {
|
||||
// auto entityInstance = rm.getInstance(entities[i]);
|
||||
// auto mi = rm.getMaterialInstanceAt(entityInstance, 0);
|
||||
// // auto m = mi->getMaterial();
|
||||
// // auto shading = m->getShading();
|
||||
// // Log("Shading %d", shading);
|
||||
// }
|
||||
|
||||
auto lights = asset->getLightEntities();
|
||||
_scene->addEntities(lights, asset->getLightEntityCount());
|
||||
|
||||
Log("Added %d lights to scene from asset", asset->getLightEntityCount());
|
||||
|
||||
FilamentInstance* inst = asset->getInstance();
|
||||
|
||||
|
||||
inst->getAnimator()->updateBoneMatrices();
|
||||
|
||||
inst->recomputeBoundingBoxes();
|
||||
|
||||
asset->releaseSourceData();
|
||||
Log("Source data released.");
|
||||
|
||||
_freeResource(rbuf.id);
|
||||
|
||||
Log("Successfully loaded GLB.");
|
||||
SceneAsset* sceneAsset = new SceneAsset(asset, _engine, _ncm, _loadResource, _freeResource);
|
||||
_assets.push_back(sceneAsset);
|
||||
|
||||
|
||||
|
||||
|
||||
return sceneAsset;
|
||||
}
|
||||
|
||||
void SceneAssetLoader::destroyAll() {
|
||||
for (auto asset : _assets) {
|
||||
_scene->removeEntities(asset->_asset->getEntities(),
|
||||
asset->_asset->getEntityCount());
|
||||
|
||||
_scene->removeEntities(asset->getLightEntities(),
|
||||
asset->getLightEntityCount());
|
||||
|
||||
_resourceLoader->evictResourceData();
|
||||
_assetLoader->destroyAsset(asset->_asset);
|
||||
delete asset;
|
||||
}
|
||||
_assets.clear();
|
||||
}
|
||||
|
||||
void SceneAssetLoader::remove(SceneAsset *asset) {
|
||||
bool erased = false;
|
||||
for (auto it = _assets.begin(); it != _assets.end();++it) {
|
||||
if (*it == asset) {
|
||||
_assets.erase(it);
|
||||
erased = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!erased) {
|
||||
Log("Error removing asset from scene : not found");
|
||||
return;
|
||||
}
|
||||
|
||||
Log("Removing asset and all associated entities/lights.");
|
||||
|
||||
_scene->removeEntities(asset->_asset->getEntities(),
|
||||
asset->_asset->getEntityCount());
|
||||
|
||||
_scene->removeEntities(asset->getLightEntities(),
|
||||
asset->getLightEntityCount());
|
||||
|
||||
_resourceLoader->evictResourceData();
|
||||
_assetLoader->destroyAsset(asset->_asset);
|
||||
delete asset;
|
||||
}
|
||||
} // namespace polyvox
|
||||
30
ios/src/TimeIt.cpp
Normal file
30
ios/src/TimeIt.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include "TimeIt.hpp"
|
||||
|
||||
#if __cplusplus <= 199711L
|
||||
|
||||
void Timer::reset()
|
||||
{
|
||||
clock_gettime(CLOCK_REALTIME, &beg_);
|
||||
}
|
||||
|
||||
double Timer::elapsed()
|
||||
{
|
||||
clock_gettime(CLOCK_REALTIME, &end_);
|
||||
return end_.tv_sec - beg_.tv_sec +
|
||||
(end_.tv_nsec - beg_.tv_nsec) / 1000000000.;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void Timer::reset()
|
||||
{
|
||||
beg_ = clock_::now();
|
||||
}
|
||||
|
||||
double Timer::elapsed()
|
||||
{
|
||||
return std::chrono::duration_cast<second_>
|
||||
(clock_::now() - beg_).count();
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -11,7 +11,7 @@ extern "C" {
|
||||
using RawLoadType = ResourceBuffer(const char*, void* resource);
|
||||
using RawFreeType = void(uint32_t, void*);
|
||||
|
||||
void* filament_viewer_new_ios(void* pb, void* loadResource, void* freeResource, void* resources) {
|
||||
void* create_filament_viewer_ios(void* pb, void* loadResource, void* freeResource, void* resources) {
|
||||
|
||||
FreeResource _freeResource = [=](uint32_t rid) {
|
||||
reinterpret_cast<RawFreeType*>(freeResource)(rid, resources);
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import 'animations.dart';
|
||||
import 'package:polyvox_filament/animations/animations.dart';
|
||||
import 'package:tuple/tuple.dart';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:vector_math/vector_math.dart';
|
||||
|
||||
class AnimationBuilder {
|
||||
BoneAnimation? boneAnimation;
|
||||
DartBoneAnimation? dartBoneAnimation;
|
||||
double _frameLengthInMs = 0;
|
||||
double _duration = 0;
|
||||
int _numMorphWeights = 0;
|
||||
@@ -13,9 +15,10 @@ class AnimationBuilder {
|
||||
double? _interpMorphStartValue;
|
||||
double? _interpMorphEndValue;
|
||||
|
||||
List<BoneAnimation>? _boneAnimations = null;
|
||||
List<DartBoneAnimation>? _dartBoneAnimations = null;
|
||||
|
||||
Animation build(String meshName, List<String> morphNames) {
|
||||
Tuple2<MorphAnimation, List<DartBoneAnimation>> build(
|
||||
String meshName, List<String> morphNames) {
|
||||
if (_numMorphWeights == 0 || _duration == 0 || _frameLengthInMs == 0)
|
||||
throw Exception();
|
||||
|
||||
@@ -39,8 +42,8 @@ class AnimationBuilder {
|
||||
var morphAnimation =
|
||||
MorphAnimation(meshName, morphData, morphNames, _frameLengthInMs);
|
||||
|
||||
return Animation(
|
||||
morphAnimation: morphAnimation, boneAnimations: _boneAnimations);
|
||||
return Tuple2<MorphAnimation, List<DartBoneAnimation>>(
|
||||
morphAnimation, _dartBoneAnimations!);
|
||||
}
|
||||
|
||||
AnimationBuilder setFramerate(int framerate) {
|
||||
@@ -107,16 +110,18 @@ class AnimationBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
var boneFrameData = BoneTransformFrameData(translations, quats);
|
||||
throw Exception();
|
||||
|
||||
_boneAnimations ??= <BoneAnimation>[];
|
||||
// var boneFrameData = BoneTransformFrameData(translations, quats);
|
||||
|
||||
var frameData = List<List<double>>.generate(
|
||||
numFrames, (index) => boneFrameData.getFrameData(index).toList());
|
||||
// _DartBoneAnimations ??= <DartBoneAnimation>[];
|
||||
|
||||
var animData = Float32List.fromList(frameData.expand((x) => x).toList());
|
||||
// var frameData = List<List<double>>.generate(
|
||||
// numFrames, (index) => boneFrameData.getFrameData(index).toList());
|
||||
|
||||
_boneAnimations!.add(BoneAnimation([boneName], [meshName], animData));
|
||||
// var animData = Float32List.fromList(frameData.expand((x) => x).toList());
|
||||
|
||||
// _DartBoneAnimations!.add(DartDartBoneAnimation([boneName], [meshName], animData));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -2,16 +2,13 @@ import 'dart:typed_data';
|
||||
|
||||
import 'package:vector_math/vector_math.dart';
|
||||
|
||||
class BoneAnimation {
|
||||
final List<String> boneNames;
|
||||
final List<String> meshNames;
|
||||
class DartBoneAnimation {
|
||||
final String boneName;
|
||||
final String meshName;
|
||||
final Float32List frameData;
|
||||
|
||||
BoneAnimation(this.boneNames, this.meshNames, this.frameData);
|
||||
|
||||
List<List> toList() {
|
||||
return [boneNames, meshNames, frameData];
|
||||
}
|
||||
double frameLengthInMs;
|
||||
DartBoneAnimation(
|
||||
this.boneName, this.meshName, this.frameData, this.frameLengthInMs);
|
||||
}
|
||||
|
||||
//
|
||||
@@ -24,25 +21,18 @@ class MorphAnimation {
|
||||
final String meshName;
|
||||
final List<String> morphNames;
|
||||
|
||||
late final Float32List morphData;
|
||||
final Float32List data;
|
||||
|
||||
MorphAnimation(
|
||||
this.meshName, this.morphData, this.morphNames, this.frameLengthInMs);
|
||||
this.meshName, this.data, this.morphNames, this.frameLengthInMs);
|
||||
|
||||
int get numMorphWeights => morphNames.length;
|
||||
|
||||
int get numFrames => morphData.length ~/ numMorphWeights;
|
||||
int get numFrames => data.length ~/ numMorphWeights;
|
||||
|
||||
final double frameLengthInMs;
|
||||
}
|
||||
|
||||
class Animation {
|
||||
final MorphAnimation? morphAnimation;
|
||||
final List<BoneAnimation>? boneAnimations;
|
||||
|
||||
Animation({this.morphAnimation, this.boneAnimations});
|
||||
}
|
||||
|
||||
class BoneTransformFrameData {
|
||||
final List<Vector3> translations;
|
||||
final List<Quaternion> quaternions;
|
||||
@@ -67,18 +57,3 @@ class BoneTransformFrameData {
|
||||
yield quaternions[frame].w;
|
||||
}
|
||||
}
|
||||
|
||||
// Animation.from(
|
||||
// {required this.meshName,
|
||||
// required List<List<double>> morphData,
|
||||
// required this.numMorphWeights,
|
||||
// this.boneAnimations,
|
||||
// required this.numFrames,
|
||||
// required this.frameLengthInMs}) {
|
||||
// if (morphData.length != numFrames) {
|
||||
// throw Exception("Mismatched animation data with frame length");
|
||||
// }
|
||||
// }
|
||||
|
||||
// not directly used, the list of morph targets animated by this [Animation], and may be a subset of the actual morph targets in the asset (and may also be ordered differently).
|
||||
// // When passed to a [FilamentController], these will be re-mapped appropriately (and any morph targets not provided will be set to zero at each frame).
|
||||
@@ -1,115 +1,27 @@
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
import 'dart:ui';
|
||||
import 'dart:ffi';
|
||||
import 'dart:io';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:ffi/ffi.dart';
|
||||
import 'package:flutter/animation.dart';
|
||||
import 'package:flutter/scheduler.dart';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:polyvox_filament/generated_bindings.dart';
|
||||
|
||||
import 'animations/animation_builder.dart';
|
||||
import 'animations/animations.dart';
|
||||
|
||||
// this is confusing - "FilamentAsset" actually defines a pointer to a SceneAsset, whereas FilamentLight is an Entity ID.
|
||||
// should make this consistent
|
||||
typedef FilamentAsset = int;
|
||||
typedef FilamentLight = int;
|
||||
const FilamentAsset FILAMENT_ASSET_ERROR = 0;
|
||||
typedef AssetManager = Pointer<Void>;
|
||||
typedef FilamentViewer = Pointer<Void>;
|
||||
typedef FilamentEntity = int;
|
||||
const FilamentEntity FILAMENT_ASSET_ERROR = 0;
|
||||
|
||||
abstract class FilamentController {
|
||||
Size get size;
|
||||
late Stream<int?> textureId;
|
||||
Future get initialized;
|
||||
Stream get onInitializationRequested;
|
||||
Future initialize();
|
||||
Future createTextureViewer(int width, int height);
|
||||
Future setFrameRate(int framerate);
|
||||
Future setRendering(bool render);
|
||||
Future render();
|
||||
void setPixelRatio(double ratio);
|
||||
Future resize(int width, int height, {double contentScaleFactor = 1});
|
||||
Future setBackgroundColor(Color color);
|
||||
Future clearBackgroundImage();
|
||||
Future setBackgroundImage(String path);
|
||||
Future setBackgroundImagePosition(double x, double y, {bool clamp = false});
|
||||
Future loadSkybox(String skyboxPath);
|
||||
Future removeSkybox();
|
||||
Future loadIbl(String path, {double intensity = 30000});
|
||||
Future removeIbl();
|
||||
|
||||
// copied from LightManager.h
|
||||
// enum class Type : uint8_t {
|
||||
// SUN, //!< Directional light that also draws a sun's disk in the sky.
|
||||
// DIRECTIONAL, //!< Directional light, emits light in a given direction.
|
||||
// POINT, //!< Point light, emits light from a position, in all directions.
|
||||
// FOCUSED_SPOT, //!< Physically correct spot light.
|
||||
// SPOT, //!< Spot light with coupling of outer cone and illumination disabled.
|
||||
// };
|
||||
Future<FilamentLight> addLight(
|
||||
int type,
|
||||
double colour,
|
||||
double intensity,
|
||||
double posX,
|
||||
double posY,
|
||||
double posZ,
|
||||
double dirX,
|
||||
double dirY,
|
||||
double dirZ,
|
||||
bool castShadows);
|
||||
Future removeLight(FilamentLight light);
|
||||
Future clearLights();
|
||||
Future<FilamentAsset> loadGlb(String path, {bool unlit = false});
|
||||
Future<FilamentAsset> loadGltf(String path, String relativeResourcePath);
|
||||
Future zoomBegin();
|
||||
Future zoomUpdate(double z);
|
||||
Future zoomEnd();
|
||||
Future panStart(double x, double y);
|
||||
Future panUpdate(double x, double y);
|
||||
Future panEnd();
|
||||
Future rotateStart(double x, double y);
|
||||
Future rotateUpdate(double x, double y);
|
||||
Future rotateEnd();
|
||||
Future setMorphTargetWeights(FilamentAsset asset, List<double> weights);
|
||||
Future<List<String>> getMorphTargetNames(
|
||||
FilamentAsset asset, String meshName);
|
||||
Future<List<String>> getAnimationNames(FilamentAsset asset);
|
||||
Future removeAsset(FilamentAsset asset);
|
||||
Future clearAssets();
|
||||
Future setAnimationFrame(
|
||||
FilamentAsset asset, int animationIndex, int animationFrame);
|
||||
Future playAnimation(FilamentAsset asset, int index,
|
||||
{bool loop = false, bool reverse = false});
|
||||
Future playAnimations(FilamentAsset asset, List<int> indices,
|
||||
{bool loop = false, bool reverse = false});
|
||||
Future stopAnimation(FilamentAsset asset, int index);
|
||||
Future setCamera(FilamentAsset asset, String name);
|
||||
Future setTexture(FilamentAsset asset, String assetPath,
|
||||
{int renderableIndex = 0});
|
||||
Future transformToUnitCube(FilamentAsset asset);
|
||||
Future setPosition(FilamentAsset asset, double x, double y, double z);
|
||||
Future setRotation(
|
||||
FilamentAsset asset, double rads, double x, double y, double z);
|
||||
// Future setBoneTransform(FilamentAsset asset, String boneName, String meshName,
|
||||
// BoneTransform transform);
|
||||
Future setScale(FilamentAsset asset, double scale);
|
||||
Future setCameraExposure(
|
||||
double aperture, double shutterSpeed, double sensitivity);
|
||||
Future setCameraFocalLength(double focalLength);
|
||||
Future setCameraFocusDistance(double focusDistance);
|
||||
Future setCameraPosition(double x, double y, double z);
|
||||
Future setCameraRotation(double rads, double x, double y, double z);
|
||||
Future setCameraModelMatrix(List<double> matrix);
|
||||
|
||||
///
|
||||
/// Animates morph target weights/bone transforms (where each frame requires a duration of [frameLengthInMs].
|
||||
/// [morphWeights] is a list of doubles in frame-major format.
|
||||
/// Each frame is [numWeights] in length, and each entry is the weight to be applied to the morph target located at that index in the mesh primitive at that frame.
|
||||
///
|
||||
Future setAnimation(FilamentAsset asset, Animation animation);
|
||||
}
|
||||
|
||||
class PolyvoxFilamentController extends FilamentController {
|
||||
class FilamentController {
|
||||
late MethodChannel _channel = MethodChannel("app.polyvox.filament/event");
|
||||
|
||||
double _pixelRatio = 1.0;
|
||||
Size size = Size(0, 0);
|
||||
ui.Size size = ui.Size.zero;
|
||||
|
||||
int? _textureId;
|
||||
final _textureIdController = StreamController<int?>.broadcast();
|
||||
@@ -121,15 +33,27 @@ class PolyvoxFilamentController extends FilamentController {
|
||||
final _initialized = Completer();
|
||||
Future get initialized => _initialized.future;
|
||||
|
||||
PolyvoxFilamentController() {
|
||||
late NativeLibrary _nativeLibrary;
|
||||
|
||||
late FilamentViewer _viewer;
|
||||
late AssetManager _assetManager;
|
||||
|
||||
final TickerProvider _tickerProvider;
|
||||
Ticker? _ticker;
|
||||
bool _rendering = false;
|
||||
|
||||
FilamentController(this._tickerProvider) {
|
||||
_channel.setMethodCallHandler((call) async {
|
||||
print("Received Filament method channel call : ${call.method}");
|
||||
throw Exception("Unknown method channel invocation ${call.method}");
|
||||
});
|
||||
|
||||
_textureIdController.onListen = () {
|
||||
_textureIdController.add(_textureId);
|
||||
};
|
||||
|
||||
_nativeLibrary = NativeLibrary(Platform.isAndroid || Platform.isLinux
|
||||
? DynamicLibrary.open("libpolyvox_filament_plugin.so")
|
||||
: DynamicLibrary.process());
|
||||
}
|
||||
|
||||
Future initialize() async {
|
||||
@@ -138,89 +62,126 @@ class PolyvoxFilamentController extends FilamentController {
|
||||
}
|
||||
|
||||
Future setRendering(bool render) async {
|
||||
await _channel.invokeMethod("setRendering", render);
|
||||
_rendering = render;
|
||||
}
|
||||
|
||||
Future render() async {
|
||||
await _channel.invokeMethod("render");
|
||||
void render() {
|
||||
_nativeLibrary.render(_viewer, 0);
|
||||
_channel.invokeMethod("onFrameAvailable");
|
||||
}
|
||||
|
||||
Future setFrameRate(int framerate) async {
|
||||
await _channel.invokeMethod("setFrameInterval", 1 / framerate);
|
||||
_nativeLibrary.set_frame_interval(_viewer, 1 / framerate);
|
||||
}
|
||||
|
||||
void setPixelRatio(double ratio) {
|
||||
print("Set pixel ratio to $ratio");
|
||||
_pixelRatio = ratio;
|
||||
}
|
||||
|
||||
Future createTextureViewer(int width, int height) async {
|
||||
size = Size(width * _pixelRatio, height * _pixelRatio);
|
||||
print("Creating texture of size $size");
|
||||
size = ui.Size(width * _pixelRatio, height * _pixelRatio);
|
||||
_textureId =
|
||||
await _channel.invokeMethod("initialize", [size.width, size.height]);
|
||||
await _channel.invokeMethod("createTexture", [size.width, size.height]);
|
||||
_textureIdController.add(_textureId);
|
||||
|
||||
var glContext =
|
||||
Pointer<Void>.fromAddress(await _channel.invokeMethod("getContext"));
|
||||
|
||||
final loadResource = Pointer<
|
||||
NativeFunction<ResourceBuffer Function(Pointer<Char>)>>.fromAddress(
|
||||
await _channel.invokeMethod("getLoadResourceFn"));
|
||||
|
||||
print("got $loadResource loadResource");
|
||||
var freeResource =
|
||||
Pointer<NativeFunction<Void Function(Uint32)>>.fromAddress(
|
||||
await _channel.invokeMethod("getFreeResourceFn"));
|
||||
|
||||
_viewer = _nativeLibrary.create_filament_viewer(
|
||||
glContext, loadResource, freeResource);
|
||||
|
||||
// don't pass a surface to the SwapChain as we are effectively creating a headless SwapChain that will render into a RenderTarget associated with a texture
|
||||
_nativeLibrary.create_swap_chain(
|
||||
_viewer, nullptr, size.width.toInt(), size.height.toInt());
|
||||
|
||||
var glTextureId = await _channel.invokeMethod("getGlTextureId");
|
||||
|
||||
_nativeLibrary.create_render_target(
|
||||
_viewer, glTextureId, size.width.toInt(), size.height.toInt());
|
||||
_nativeLibrary.update_viewport_and_camera_projection(
|
||||
_viewer, size.width.toInt(), size.height.toInt(), 1.0);
|
||||
|
||||
_initialized.complete(true);
|
||||
_assetManager = _nativeLibrary.get_asset_manager(_viewer);
|
||||
print("got asset maanger $_assetManager");
|
||||
|
||||
_ticker = _tickerProvider.createTicker((elapsed) {
|
||||
if (_rendering) {
|
||||
render();
|
||||
}
|
||||
});
|
||||
_ticker!.start();
|
||||
}
|
||||
|
||||
Future resize(int width, int height,
|
||||
{double contentScaleFactor = 1.0}) async {
|
||||
size = Size(width * _pixelRatio, height * _pixelRatio);
|
||||
size = ui.Size(width * _pixelRatio, height * _pixelRatio);
|
||||
|
||||
_textureId = await _channel.invokeMethod("resize",
|
||||
[width * _pixelRatio, height * _pixelRatio, contentScaleFactor]);
|
||||
print("Resized to $size with texutre Id $textureId");
|
||||
|
||||
_textureIdController.add(_textureId);
|
||||
}
|
||||
|
||||
@override
|
||||
Future clearBackgroundImage() async {
|
||||
await _channel.invokeMethod("clearBackgroundImage");
|
||||
void clearBackgroundImage() async {
|
||||
_nativeLibrary.clear_background_image(_viewer);
|
||||
}
|
||||
|
||||
@override
|
||||
Future setBackgroundImage(String path) async {
|
||||
await _channel.invokeMethod("setBackgroundImage", path);
|
||||
void setBackgroundImage(String path) async {
|
||||
_nativeLibrary.set_background_image(
|
||||
_viewer, path.toNativeUtf8().cast<Char>());
|
||||
}
|
||||
|
||||
@override
|
||||
Future setBackgroundColor(Color color) async {
|
||||
await _channel.invokeMethod("setBackgroundColor", [
|
||||
color.red.toDouble() / 255.0,
|
||||
color.green.toDouble() / 255.0,
|
||||
color.blue.toDouble() / 255.0,
|
||||
color.alpha.toDouble() / 255.0
|
||||
]);
|
||||
void setBackgroundColor(Color color) async {
|
||||
_nativeLibrary.set_background_color(
|
||||
_viewer,
|
||||
color.red.toDouble() / 255.0,
|
||||
color.green.toDouble() / 255.0,
|
||||
color.blue.toDouble() / 255.0,
|
||||
color.alpha.toDouble() / 255.0);
|
||||
}
|
||||
|
||||
@override
|
||||
Future setBackgroundImagePosition(double x, double y,
|
||||
void setBackgroundImagePosition(double x, double y,
|
||||
{bool clamp = false}) async {
|
||||
await _channel.invokeMethod("setBackgroundImagePosition", [x, y, clamp]);
|
||||
_nativeLibrary.set_background_image_position(_viewer, x, y, clamp ? 1 : 0);
|
||||
}
|
||||
|
||||
@override
|
||||
Future loadSkybox(String skyboxPath) async {
|
||||
await _channel.invokeMethod("loadSkybox", skyboxPath);
|
||||
void loadSkybox(String skyboxPath) async {
|
||||
_nativeLibrary.load_skybox(_viewer, skyboxPath.toNativeUtf8().cast<Char>());
|
||||
}
|
||||
|
||||
@override
|
||||
Future loadIbl(String lightingPath, {double intensity = 30000}) async {
|
||||
await _channel.invokeMethod("loadIbl", [lightingPath, intensity]);
|
||||
void loadIbl(String lightingPath, {double intensity = 30000}) async {
|
||||
_nativeLibrary.load_ibl(
|
||||
_viewer, lightingPath.toNativeUtf8().cast<Char>(), intensity);
|
||||
}
|
||||
|
||||
@override
|
||||
Future removeSkybox() async {
|
||||
await _channel.invokeMethod("removeSkybox");
|
||||
void removeSkybox() async {
|
||||
_nativeLibrary.remove_skybox(_viewer);
|
||||
}
|
||||
|
||||
@override
|
||||
Future removeIbl() async {
|
||||
await _channel.invokeMethod("removeIbl");
|
||||
void removeIbl() async {
|
||||
_nativeLibrary.remove_ibl(_viewer);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<FilamentLight> addLight(
|
||||
// copied from LightManager.h
|
||||
// enum class Type : uint8_t {
|
||||
// SUN, //!< Directional light that also draws a sun's disk in the sky.
|
||||
// DIRECTIONAL, //!< Directional light, emits light in a given direction.
|
||||
// POINT, //!< Point light, emits light from a position, in all directions.
|
||||
// FOCUSED_SPOT, //!< Physically correct spot light.
|
||||
// SPOT, //!< Spot light with coupling of outer cone and illumination disabled.
|
||||
// };
|
||||
|
||||
FilamentEntity addLight(
|
||||
int type,
|
||||
double colour,
|
||||
double intensity,
|
||||
@@ -230,221 +191,241 @@ class PolyvoxFilamentController extends FilamentController {
|
||||
double dirX,
|
||||
double dirY,
|
||||
double dirZ,
|
||||
bool castShadows) async {
|
||||
var entityId = await _channel.invokeMethod("addLight", [
|
||||
type,
|
||||
colour,
|
||||
intensity,
|
||||
posX,
|
||||
posY,
|
||||
posZ,
|
||||
dirX,
|
||||
dirY,
|
||||
dirZ,
|
||||
castShadows
|
||||
]);
|
||||
return entityId as FilamentLight;
|
||||
bool castShadows) {
|
||||
return _nativeLibrary.add_light(_viewer, type, colour, intensity, posX,
|
||||
posY, posZ, dirX, dirY, dirZ, castShadows ? 1 : 0);
|
||||
}
|
||||
|
||||
@override
|
||||
Future removeLight(FilamentLight light) {
|
||||
return _channel.invokeMethod("removeLight", light);
|
||||
void removeLight(FilamentEntity light) async {
|
||||
_nativeLibrary.remove_light(_viewer, light);
|
||||
}
|
||||
|
||||
@override
|
||||
Future clearLights() {
|
||||
return _channel.invokeMethod("clearLights");
|
||||
void clearLights() async {
|
||||
_nativeLibrary.clear_lights(_viewer);
|
||||
}
|
||||
|
||||
Future<FilamentAsset> loadGlb(String path, {bool unlit = false}) async {
|
||||
print("Loading GLB at $path ");
|
||||
var asset = await _channel.invokeMethod("loadGlb", [path, unlit]);
|
||||
FilamentEntity loadGlb(String path, {bool unlit = false}) {
|
||||
var asset = _nativeLibrary.load_glb(
|
||||
_assetManager, path.toNativeUtf8().cast<Char>(), unlit ? 1 : 0);
|
||||
if (asset == FILAMENT_ASSET_ERROR) {
|
||||
throw Exception("An error occurred loading the asset at $path");
|
||||
}
|
||||
return asset as FilamentAsset;
|
||||
return asset;
|
||||
}
|
||||
|
||||
Future<FilamentAsset> loadGltf(
|
||||
String path, String relativeResourcePath) async {
|
||||
print(
|
||||
"Loading GLTF at $path with relative resource path $relativeResourcePath");
|
||||
var asset =
|
||||
await _channel.invokeMethod("loadGltf", [path, relativeResourcePath]);
|
||||
return asset as FilamentAsset;
|
||||
FilamentEntity loadGltf(String path, String relativeResourcePath) {
|
||||
return _nativeLibrary.load_gltf(
|
||||
_assetManager,
|
||||
path.toNativeUtf8().cast<Char>(),
|
||||
relativeResourcePath.toNativeUtf8().cast<Char>());
|
||||
}
|
||||
|
||||
Future panStart(double x, double y) async {
|
||||
await _channel.invokeMethod("panStart", [x * _pixelRatio, y * _pixelRatio]);
|
||||
void panStart(double x, double y) async {
|
||||
_nativeLibrary.grab_begin(_viewer, x * _pixelRatio, y * _pixelRatio, 1);
|
||||
}
|
||||
|
||||
Future panUpdate(double x, double y) async {
|
||||
await _channel
|
||||
.invokeMethod("panUpdate", [x * _pixelRatio, y * _pixelRatio]);
|
||||
void panUpdate(double x, double y) async {
|
||||
_nativeLibrary.grab_update(_viewer, x * _pixelRatio, y * _pixelRatio);
|
||||
}
|
||||
|
||||
Future panEnd() async {
|
||||
await _channel.invokeMethod("panEnd");
|
||||
void panEnd() async {
|
||||
_nativeLibrary.grab_end(_viewer);
|
||||
}
|
||||
|
||||
Future rotateStart(double x, double y) async {
|
||||
await _channel
|
||||
.invokeMethod("rotateStart", [x * _pixelRatio, y * _pixelRatio]);
|
||||
void rotateStart(double x, double y) async {
|
||||
_nativeLibrary.grab_begin(_viewer, x * _pixelRatio, y * _pixelRatio, 0);
|
||||
}
|
||||
|
||||
Future rotateUpdate(double x, double y) async {
|
||||
await _channel
|
||||
.invokeMethod("rotateUpdate", [x * _pixelRatio, y * _pixelRatio]);
|
||||
void rotateUpdate(double x, double y) async {
|
||||
_nativeLibrary.grab_update(_viewer, x * _pixelRatio, y * _pixelRatio);
|
||||
}
|
||||
|
||||
Future rotateEnd() async {
|
||||
await _channel.invokeMethod("rotateEnd");
|
||||
void rotateEnd() async {
|
||||
_nativeLibrary.grab_end(_viewer);
|
||||
}
|
||||
|
||||
Future setMorphTargetWeights(
|
||||
FilamentAsset asset, List<double> weights) async {
|
||||
await _channel.invokeMethod(
|
||||
"setMorphTargetWeights", [asset, Float32List.fromList(weights)]);
|
||||
void setMorphTargetWeights(FilamentEntity asset, List<double> weights) {
|
||||
throw Exception("TODO");
|
||||
// _nativeLibrary.set_morph_target_weights(_assetManager, asset, Float32List.fromList(weights));
|
||||
}
|
||||
|
||||
Future<List<String>> getMorphTargetNames(
|
||||
FilamentAsset asset, String meshName) async {
|
||||
var result =
|
||||
(await _channel.invokeMethod("getMorphTargetNames", [asset, meshName]))
|
||||
.cast<String>();
|
||||
return result;
|
||||
List<String> getMorphTargetNames(FilamentEntity asset, String meshName) {
|
||||
var meshNamePtr = meshName.toNativeUtf8().cast<Char>();
|
||||
var count = _nativeLibrary.get_morph_target_name_count(
|
||||
_assetManager, asset, meshNamePtr);
|
||||
var names = <String>[];
|
||||
for (int i = 0; i < count; i++) {
|
||||
var outPtr = calloc<Char>(255);
|
||||
_nativeLibrary.get_morph_target_name(
|
||||
_assetManager, asset, meshNamePtr, outPtr, i);
|
||||
names.add(outPtr.cast<Utf8>().toDartString());
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
Future<List<String>> getAnimationNames(FilamentAsset asset) async {
|
||||
var result = (await _channel.invokeMethod("getAnimationNames", asset))
|
||||
.cast<String>();
|
||||
return result;
|
||||
List<String> getAnimationNames(FilamentEntity asset) {
|
||||
var count = _nativeLibrary.get_animation_count(_assetManager, asset);
|
||||
var names = <String>[];
|
||||
for (int i = 0; i < count; i++) {
|
||||
var outPtr = calloc<Char>(255);
|
||||
_nativeLibrary.get_animation_name(_assetManager, asset, outPtr, i);
|
||||
names.add(outPtr.cast<Utf8>().toDartString());
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
Future setAnimation(FilamentAsset asset, Animation animation) async {
|
||||
await _channel.invokeMethod("setAnimation", [
|
||||
asset,
|
||||
animation.morphAnimation!.meshName,
|
||||
animation.morphAnimation!.morphData,
|
||||
animation.morphAnimation!.numMorphWeights,
|
||||
animation.boneAnimations?.map((a) => a.toList()).toList() ?? [],
|
||||
animation.morphAnimation!.numFrames,
|
||||
animation.morphAnimation!.frameLengthInMs
|
||||
]);
|
||||
///
|
||||
/// Animates morph target weights/bone transforms (where each frame requires a duration of [frameLengthInMs].
|
||||
/// [morphWeights] is a list of doubles in frame-major format.
|
||||
/// Each frame is [numWeights] in length, and each entry is the weight to be applied to the morph target located at that index in the mesh primitive at that frame.
|
||||
///
|
||||
void setMorphAnimation(FilamentEntity asset, MorphAnimation animation) async {
|
||||
var data = calloc<Float>(animation.data.length);
|
||||
for (int i = 0; i < animation.data.length; i++) {
|
||||
data.elementAt(i).value = animation.data[i];
|
||||
}
|
||||
_nativeLibrary.set_morph_animation(
|
||||
_assetManager,
|
||||
asset,
|
||||
animation.meshName.toNativeUtf8().cast<Char>(),
|
||||
data,
|
||||
animation.numMorphWeights,
|
||||
animation.numFrames,
|
||||
animation.frameLengthInMs);
|
||||
calloc.free(data);
|
||||
}
|
||||
|
||||
Future removeAsset(FilamentAsset asset) async {
|
||||
print("Removing asset : $asset");
|
||||
await _channel.invokeMethod("removeAsset", asset);
|
||||
///
|
||||
/// Animates morph target weights/bone transforms (where each frame requires a duration of [frameLengthInMs].
|
||||
/// [morphWeights] is a list of doubles in frame-major format.
|
||||
/// Each frame is [numWeights] in length, and each entry is the weight to be applied to the morph target located at that index in the mesh primitive at that frame.
|
||||
///
|
||||
void setBoneAnimation(
|
||||
FilamentEntity asset, List<DartBoneAnimation> animations) async {
|
||||
var data =
|
||||
calloc<Float>(animations.length * animations.first.frameData.length);
|
||||
int offset = 0;
|
||||
var numFrames = animations.first.frameData.length;
|
||||
var meshNames = calloc<Pointer<Char>>(animations.length);
|
||||
var boneNames = calloc<Pointer<Char>>(animations.length);
|
||||
int animIdx = 0;
|
||||
for (var animation in animations) {
|
||||
if (animation.frameData.length != numFrames) {
|
||||
throw Exception(
|
||||
"All bone animations must share the same animation frame data length.");
|
||||
}
|
||||
for (int i = 0; i < animation.frameData.length; i++) {
|
||||
data.elementAt(offset).value = animation.frameData[i];
|
||||
offset += 1;
|
||||
}
|
||||
meshNames.elementAt(animIdx).value =
|
||||
animation.meshName.toNativeUtf8().cast<Char>();
|
||||
boneNames.elementAt(animIdx).value =
|
||||
animation.boneName.toNativeUtf8().cast<Char>();
|
||||
}
|
||||
|
||||
_nativeLibrary.set_bone_animation(
|
||||
_assetManager,
|
||||
asset,
|
||||
animations.length,
|
||||
boneNames,
|
||||
meshNames,
|
||||
data,
|
||||
numFrames,
|
||||
animations.first.frameLengthInMs);
|
||||
calloc.free(data);
|
||||
}
|
||||
|
||||
Future clearAssets() async {
|
||||
await _channel.invokeMethod("clearAssets");
|
||||
void removeAsset(FilamentEntity asset) async {
|
||||
_nativeLibrary.remove_asset(_viewer, asset);
|
||||
}
|
||||
|
||||
Future zoomBegin() async {
|
||||
await _channel.invokeMethod("zoomBegin");
|
||||
void clearAssets() async {
|
||||
_nativeLibrary.clear_assets(_viewer);
|
||||
}
|
||||
|
||||
Future zoomUpdate(double z) async {
|
||||
await _channel.invokeMethod("zoomUpdate", [0.0, 0.0, z]);
|
||||
void zoomBegin() async {
|
||||
_nativeLibrary.scroll_begin(_viewer);
|
||||
}
|
||||
|
||||
Future zoomEnd() async {
|
||||
await _channel.invokeMethod("zoomEnd");
|
||||
void zoomUpdate(double z) async {
|
||||
_nativeLibrary.scroll_update(_viewer, 0.0, 0.0, z);
|
||||
}
|
||||
|
||||
Future playAnimation(FilamentAsset asset, int index,
|
||||
void zoomEnd() async {
|
||||
_nativeLibrary.scroll_end(_viewer);
|
||||
}
|
||||
|
||||
void playAnimation(FilamentEntity asset, int index,
|
||||
{bool loop = false, bool reverse = false}) async {
|
||||
await _channel.invokeMethod("playAnimation", [asset, index, loop, reverse]);
|
||||
_nativeLibrary.play_animation(
|
||||
_assetManager, asset, index, loop ? 1 : 0, reverse ? 1 : 0);
|
||||
}
|
||||
|
||||
Future setAnimationFrame(
|
||||
FilamentAsset asset, int index, int animationFrame) async {
|
||||
await _channel
|
||||
.invokeMethod("setAnimationFrame", [asset, index, animationFrame]);
|
||||
void setAnimationFrame(
|
||||
FilamentEntity asset, int index, int animationFrame) async {
|
||||
_nativeLibrary.set_animation_frame(
|
||||
_assetManager, asset, index, animationFrame);
|
||||
}
|
||||
|
||||
Future playAnimations(FilamentAsset asset, List<int> indices,
|
||||
{bool loop = false, bool reverse = false}) async {
|
||||
return Future.wait(indices.map((index) {
|
||||
return _channel
|
||||
.invokeMethod("playAnimation", [asset, index, loop, reverse]);
|
||||
}));
|
||||
void stopAnimation(FilamentEntity asset, int animationIndex) async {
|
||||
_nativeLibrary.stop_animation(_assetManager, asset, animationIndex);
|
||||
}
|
||||
|
||||
Future stopAnimation(FilamentAsset asset, int animationIndex) async {
|
||||
await _channel.invokeMethod("stopAnimation", [asset, animationIndex]);
|
||||
void setCamera(FilamentEntity asset, String name) async {
|
||||
_nativeLibrary.set_camera(_viewer, asset, name.toNativeUtf8().cast<Char>());
|
||||
}
|
||||
|
||||
Future setCamera(FilamentAsset asset, String name) async {
|
||||
await _channel.invokeMethod("setCamera", [asset, name]);
|
||||
void setCameraFocalLength(double focalLength) async {
|
||||
_nativeLibrary.set_camera_focal_length(_viewer, focalLength);
|
||||
}
|
||||
|
||||
Future setCameraFocalLength(double focalLength) async {
|
||||
await _channel.invokeMethod("setCameraFocalLength", focalLength);
|
||||
void setCameraFocusDistance(double focusDistance) async {
|
||||
_nativeLibrary.set_camera_focus_distance(_viewer, focusDistance);
|
||||
}
|
||||
|
||||
Future setCameraFocusDistance(double focusDistance) async {
|
||||
await _channel.invokeMethod("setCameraFocusDistance", focusDistance);
|
||||
void setCameraPosition(double x, double y, double z) async {
|
||||
_nativeLibrary.set_camera_position(_viewer, x, y, z);
|
||||
}
|
||||
|
||||
Future setCameraPosition(double x, double y, double z) async {
|
||||
await _channel.invokeMethod("setCameraPosition", [x, y, z]);
|
||||
}
|
||||
|
||||
Future setCameraExposure(
|
||||
void setCameraExposure(
|
||||
double aperture, double shutterSpeed, double sensitivity) async {
|
||||
await _channel.invokeMethod(
|
||||
"setCameraExposure", [aperture, shutterSpeed, sensitivity]);
|
||||
_nativeLibrary.set_camera_exposure(
|
||||
_viewer, aperture, shutterSpeed, sensitivity);
|
||||
}
|
||||
|
||||
Future setCameraRotation(double rads, double x, double y, double z) async {
|
||||
await _channel.invokeMethod("setCameraRotation", [rads, x, y, z]);
|
||||
void setCameraRotation(double rads, double x, double y, double z) async {
|
||||
_nativeLibrary.set_camera_rotation(_viewer, rads, x, y, z);
|
||||
}
|
||||
|
||||
Future setCameraModelMatrix(List<double> matrix) async {
|
||||
void setCameraModelMatrix(List<double> matrix) async {
|
||||
assert(matrix.length == 16);
|
||||
await _channel.invokeMethod(
|
||||
"setCameraModelMatrix", Float32List.fromList(matrix));
|
||||
var ptr = calloc<Float>(16);
|
||||
for (int i = 0; i < 16; i++) {
|
||||
ptr.elementAt(i).value = matrix[i];
|
||||
}
|
||||
_nativeLibrary.set_camera_model_matrix(_viewer, ptr);
|
||||
}
|
||||
|
||||
Future setTexture(FilamentAsset asset, String assetPath,
|
||||
void setTexture(FilamentEntity asset, String assetPath,
|
||||
{int renderableIndex = 0}) async {
|
||||
await _channel
|
||||
.invokeMethod("setTexture", [asset, assetPath, renderableIndex]);
|
||||
_nativeLibrary.set_texture(_assetManager, asset);
|
||||
}
|
||||
|
||||
Future transformToUnitCube(FilamentAsset asset) async {
|
||||
await _channel.invokeMethod("transformToUnitCube", asset);
|
||||
void transformToUnitCube(FilamentEntity asset) async {
|
||||
_nativeLibrary.transform_to_unit_cube(_assetManager, asset);
|
||||
}
|
||||
|
||||
Future setPosition(FilamentAsset asset, double x, double y, double z) async {
|
||||
await _channel.invokeMethod("setPosition", [asset, x, y, z]);
|
||||
void setPosition(FilamentEntity asset, double x, double y, double z) async {
|
||||
_nativeLibrary.set_position(_assetManager, asset, x, y, z);
|
||||
}
|
||||
|
||||
// Future setBoneTransform(FilamentAsset asset, String boneName, String meshName,
|
||||
// BoneTransform transform) async {
|
||||
// await _channel.invokeMethod("setBoneTransform", [
|
||||
// asset,
|
||||
// boneName,
|
||||
// meshName,
|
||||
// transform.translations[0].x,
|
||||
// transform.translations[0].y,
|
||||
// transform.translations[0].z,
|
||||
// transform.quaternions[0].x,
|
||||
// transform.quaternions[0].y,
|
||||
// transform.quaternions[0].z,
|
||||
// transform.quaternions[0].w
|
||||
// ]);
|
||||
// }
|
||||
|
||||
Future setScale(FilamentAsset asset, double scale) async {
|
||||
await _channel.invokeMethod("setScale", [asset, scale]);
|
||||
void setScale(FilamentEntity asset, double scale) async {
|
||||
_nativeLibrary.set_scale(_assetManager, asset, scale);
|
||||
}
|
||||
|
||||
Future setRotation(
|
||||
FilamentAsset asset, double rads, double x, double y, double z) async {
|
||||
await _channel.invokeMethod("setRotation", [asset, rads, x, y, z]);
|
||||
void setRotation(
|
||||
FilamentEntity asset, double rads, double x, double y, double z) async {
|
||||
_nativeLibrary.set_rotation(_assetManager, asset, rads, x, y, z);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,9 +41,9 @@ class _FilamentGestureDetectorState extends State<FilamentGestureDetector> {
|
||||
// to avoid duplicating code for pan/rotate (panStart, panUpdate, panEnd, rotateStart, rotateUpdate etc)
|
||||
// we have only a single function for start/update/end.
|
||||
// when the gesture type is changed, these properties are updated to point to the correct function.
|
||||
late Future Function(double x, double y) _functionStart;
|
||||
late Future Function(double x, double y) _functionUpdate;
|
||||
late Future Function() _functionEnd;
|
||||
late Function(double x, double y) _functionStart;
|
||||
late Function(double x, double y) _functionUpdate;
|
||||
late Function() _functionEnd;
|
||||
|
||||
double _lastScale = 0;
|
||||
|
||||
@@ -104,8 +104,8 @@ class _FilamentGestureDetectorState extends State<FilamentGestureDetector> {
|
||||
: (d) async {
|
||||
_scaling = true;
|
||||
if (d.pointerCount == 2) {
|
||||
await widget.controller.zoomEnd();
|
||||
await widget.controller.zoomBegin();
|
||||
widget.controller.zoomEnd();
|
||||
widget.controller.zoomBegin();
|
||||
}
|
||||
},
|
||||
onScaleEnd: !widget.enableControls
|
||||
@@ -114,7 +114,7 @@ class _FilamentGestureDetectorState extends State<FilamentGestureDetector> {
|
||||
_scaling = false;
|
||||
if (d.pointerCount == 2) {
|
||||
_lastScale = 0;
|
||||
await widget.controller.zoomEnd();
|
||||
widget.controller.zoomEnd();
|
||||
}
|
||||
},
|
||||
onScaleUpdate: !widget.enableControls
|
||||
@@ -122,7 +122,7 @@ class _FilamentGestureDetectorState extends State<FilamentGestureDetector> {
|
||||
: (d) async {
|
||||
if (d.pointerCount == 2) {
|
||||
if (_lastScale != 0) {
|
||||
await widget.controller.zoomUpdate(Platform.isIOS
|
||||
widget.controller.zoomUpdate(Platform.isIOS
|
||||
? 1000 * (_lastScale - d.scale)
|
||||
: 100 * (_lastScale - d.scale));
|
||||
}
|
||||
@@ -138,8 +138,8 @@ class _FilamentGestureDetectorState extends State<FilamentGestureDetector> {
|
||||
// scroll-wheel zoom on desktop
|
||||
if (pointerSignal is PointerScrollEvent) {
|
||||
_scrollTimer?.cancel();
|
||||
await widget.controller.zoomBegin();
|
||||
await widget.controller.zoomUpdate(
|
||||
widget.controller.zoomBegin();
|
||||
widget.controller.zoomUpdate(
|
||||
pointerSignal.scrollDelta.dy > 0 ? 10 : -10);
|
||||
_scrollTimer =
|
||||
Timer(Duration(milliseconds: 100), () {
|
||||
@@ -154,10 +154,10 @@ class _FilamentGestureDetectorState extends State<FilamentGestureDetector> {
|
||||
? null
|
||||
: (d) async {
|
||||
if (d.buttons == kTertiaryButton || _rotating) {
|
||||
await widget.controller.rotateStart(
|
||||
widget.controller.rotateStart(
|
||||
d.localPosition.dx, d.localPosition.dy);
|
||||
} else {
|
||||
await _functionStart(
|
||||
_functionStart(
|
||||
d.localPosition.dx, d.localPosition.dy);
|
||||
}
|
||||
},
|
||||
@@ -165,10 +165,10 @@ class _FilamentGestureDetectorState extends State<FilamentGestureDetector> {
|
||||
? null
|
||||
: (d) async {
|
||||
if (d.buttons == kTertiaryButton || _rotating) {
|
||||
await widget.controller.rotateUpdate(
|
||||
widget.controller.rotateUpdate(
|
||||
d.localPosition.dx, d.localPosition.dy);
|
||||
} else {
|
||||
await _functionUpdate(
|
||||
_functionUpdate(
|
||||
d.localPosition.dx, d.localPosition.dy);
|
||||
}
|
||||
},
|
||||
@@ -176,9 +176,9 @@ class _FilamentGestureDetectorState extends State<FilamentGestureDetector> {
|
||||
? null
|
||||
: (d) async {
|
||||
if (d.buttons == kTertiaryButton || _rotating) {
|
||||
await widget.controller.rotateEnd();
|
||||
widget.controller.rotateEnd();
|
||||
} else {
|
||||
await _functionEnd();
|
||||
_functionEnd();
|
||||
}
|
||||
},
|
||||
child: widget.child))),
|
||||
|
||||
1343
lib/generated_bindings.dart
Normal file
1343
lib/generated_bindings.dart
Normal file
File diff suppressed because it is too large
Load Diff
@@ -29,11 +29,11 @@ add_library(${PLUGIN_NAME} SHARED
|
||||
"polyvox_filament_plugin.cc"
|
||||
"filament_texture.cc"
|
||||
"filament_pb_texture.cc"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../ios/src/SceneAssetLoader.cpp"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../ios/src/SceneAsset.cpp"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../ios/src/AssetManager.cpp"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../ios/src/FilamentViewer.cpp"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../ios/src/PolyvoxFilamentApi.cpp"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../ios/src/StreamBufferAdapter.cpp"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../ios/src/TimeIt.cpp"
|
||||
)
|
||||
|
||||
# Apply a standard set of build settings that are configured in the
|
||||
|
||||
@@ -19,6 +19,8 @@ static uint32_t _i = 0;
|
||||
|
||||
ResourceBuffer loadResource(const char* name) {
|
||||
|
||||
std::cout << "LOADING RESOURCE" << std::endl;
|
||||
|
||||
char cwd[PATH_MAX];
|
||||
if (getcwd(cwd, sizeof(cwd)) != NULL) {
|
||||
std::cout << "Current working dir: " << cwd << std::endl;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -12,12 +12,20 @@ dependencies:
|
||||
sdk: flutter
|
||||
vector_math: ^2.1.2
|
||||
plugin_platform_interface: ^2.0.0
|
||||
ffi:
|
||||
tuple:
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
flutter_lints: ^1.0.0
|
||||
ffigen:
|
||||
|
||||
ffigen:
|
||||
output: 'lib/generated_bindings.dart'
|
||||
headers:
|
||||
entry-points:
|
||||
- 'ios/include/PolyvoxFilamentApi.h'
|
||||
|
||||
flutter:
|
||||
plugin:
|
||||
|
||||
Reference in New Issue
Block a user