namespace change and add camera extraction from GLTF

This commit is contained in:
Nick Fisher
2022-01-20 19:55:41 +08:00
parent 77a68a11de
commit dc3f9e9be8
11 changed files with 90 additions and 28 deletions

View File

@@ -1,3 +1,3 @@
# holovox_filament
# polyvox_filament
3D rendering utilities for the Holovox app.
3D rendering utilities for the Polyvox app.

View File

@@ -11,8 +11,8 @@ static const id VIEW_TYPE = @"holovox.app/filament_view";
@interface FilamentMethodCallHandler : FlutterMethodChannel
- (void)handleMethodCall:(FlutterMethodCall* _Nonnull)call result:( FlutterResult _Nonnull)result;
- (holovox::FilamentViewer*) _viewer;
- (holovox::ResourceBuffer)loadResource:(const char* const)path;
- (polyvox::FilamentViewer*) _viewer;
- (polyvox::ResourceBuffer)loadResource:(const char* const)path;
- (void)freeResource:(void*)mem size:(size_t)size misc:(void*)misc;
- (void)ready;
- (instancetype)initWithController:(FilamentViewController*)controller

View File

@@ -4,7 +4,7 @@
static const FilamentMethodCallHandler* _handler;
static holovox::ResourceBuffer loadResourceGlobal(const char* name) {
static polyvox::ResourceBuffer loadResourceGlobal(const char* name) {
return [_handler loadResource:name];
}
@@ -16,7 +16,7 @@ static void* freeResourceGlobal(void* mem, size_t size, void* misc) {
@implementation FilamentMethodCallHandler {
FilamentViewController *_controller;
FlutterMethodChannel* _channel;
holovox::FilamentViewer* _viewer;
polyvox::FilamentViewer* _viewer;
void* _layer;
NSObject<FlutterPluginRegistrar>* _registrar;
@@ -43,9 +43,9 @@ static void* freeResourceGlobal(void* mem, size_t size, void* misc) {
- (void)handleMethodCall:(FlutterMethodCall* _Nonnull)call result:(FlutterResult _Nonnull )result {
if([@"initialize" isEqualToString:call.method]) {
if(!call.arguments)
_viewer = new holovox::FilamentViewer(_layer, nullptr, nullptr, loadResourceGlobal, freeResourceGlobal);
_viewer = new polyvox::FilamentViewer(_layer, nullptr, nullptr, loadResourceGlobal, freeResourceGlobal);
else
_viewer = new holovox::FilamentViewer(_layer, [call.arguments[0] UTF8String], [call.arguments[1] UTF8String], loadResourceGlobal, freeResourceGlobal);
_viewer = new polyvox::FilamentViewer(_layer, [call.arguments[0] UTF8String], [call.arguments[1] UTF8String], loadResourceGlobal, freeResourceGlobal);
[_controller setViewer:_viewer];
[_controller startDisplayLink];
result(@"OK");
@@ -123,7 +123,7 @@ static void* freeResourceGlobal(void* mem, size_t size, void* misc) {
_viewer->playAnimation([call.arguments intValue]);
result(@"OK");
} else if([@"getTargetNames" isEqualToString:call.method]) {
holovox::StringList list = _viewer->getTargetNames([call.arguments UTF8String]);
polyvox::StringList list = _viewer->getTargetNames([call.arguments UTF8String]);
NSMutableArray* asArray = [NSMutableArray arrayWithCapacity:list.count];
for(int i = 0; i < list.count; i++) {
asArray[i] = [NSString stringWithFormat:@"%s", list.strings[i]];
@@ -152,7 +152,7 @@ static void* freeResourceGlobal(void* mem, size_t size, void* misc) {
}
}
- (holovox::ResourceBuffer)loadResource:(const char* const)path {
- (polyvox::ResourceBuffer)loadResource:(const char* const)path {
NSString* p = [NSString stringWithFormat:@"%s", path];
NSString* key = [_registrar lookupKeyForAsset:p];
NSString* nsPath = [[NSBundle mainBundle] pathForResource:key
@@ -165,7 +165,7 @@ static void* freeResourceGlobal(void* mem, size_t size, void* misc) {
NSData* buffer = [NSData dataWithContentsOfFile:nsPath];
void* cpy = malloc([buffer length]);
memcpy(cpy, [buffer bytes], [buffer length]); // can we avoid this copy somehow?
holovox::ResourceBuffer rbuf(cpy, [buffer length]);
polyvox::ResourceBuffer rbuf(cpy, [buffer length]);
return rbuf;
}

View File

@@ -30,7 +30,7 @@
@implementation FilamentNativeView {
FilamentView* _view;
FilamentViewController* _controller;
holovox::FilamentViewer* _viewer;
polyvox::FilamentViewer* _viewer;
FilamentMethodCallHandler* _handler;
void* _layer;
}

View File

@@ -24,7 +24,7 @@ NS_ASSUME_NONNULL_BEGIN
*
*/
@interface FilamentView : UIView
- (void)setViewer:(holovox::FilamentViewer*)viewer;
- (void)setViewer:(polyvox::FilamentViewer*)viewer;
@end
NS_ASSUME_NONNULL_END

View File

@@ -23,14 +23,14 @@ using namespace std;
@interface FilamentView ()
- (void)initCommon;
- (void)setViewer:(holovox::FilamentViewer*)viewer;
- (void)setViewer:(polyvox::FilamentViewer*)viewer;
@end
@implementation FilamentView {
holovox::FilamentViewer* _viewer;
polyvox::FilamentViewer* _viewer;
}
- (void)setViewer:(holovox::FilamentViewer*)viewer {
- (void)setViewer:(polyvox::FilamentViewer*)viewer {
_viewer = viewer;
_viewer->updateViewportAndCameraProjection(self.bounds.size.width, self.bounds.size.height, self.contentScaleFactor);
}

View File

@@ -23,7 +23,7 @@
@interface FilamentViewController : UIViewController
@property(weak, nonatomic) IBOutlet FilamentView* modelView;
- (void)setViewer:(holovox::FilamentViewer*)viewer;
- (void)setViewer:(polyvox::FilamentViewer*)viewer;
- (void)startDisplayLink;
- (void)stopDisplayLink;

View File

@@ -22,7 +22,7 @@
@implementation FilamentViewController {
CADisplayLink* _displayLink;
NSObject<FlutterPluginRegistrar>* _registrar;
holovox::FilamentViewer* _viewer;
polyvox::FilamentViewer* _viewer;
FilamentView* _view;
}
@@ -35,7 +35,7 @@
return self;
}
- (void)setViewer:(holovox::FilamentViewer*)viewer {
- (void)setViewer:(polyvox::FilamentViewer*)viewer {
_viewer = viewer;
[_view setViewer:_viewer];
}

View File

@@ -83,7 +83,7 @@ namespace gltfio {
filament::math::quatf* rotation, filament::math::float3* scale);
}
namespace holovox {
namespace polyvox {
const double kNearPlane = 0.05; // 5 cm
const double kFarPlane = 1000.0; // 1 km
@@ -212,7 +212,7 @@ void FilamentViewer::releaseSourceAssets() {
void FilamentViewer::animateWeights(float* data, int numWeights, int length, float frameRate) {
transformToUnitCube();
// transformToUnitCube();
morphAnimationBuffer = std::make_unique<MorphAnimationBuffer>(data, numWeights, length / numWeights, 1000 / frameRate );
}
@@ -237,8 +237,12 @@ void FilamentViewer::loadGlb(const char* const uri) {
std::cerr << "Unknown error loading GLB asset." << std::endl;
exit(1);
}
int entityCount = _asset->getEntityCount();
_scene->addEntities(_asset->getEntities(), _asset->getEntityCount());
_scene->addEntities(_asset->getEntities(), entityCount);
std::cerr << "Added " << entityCount << " entities to scene" << std::endl;
_resourceLoader->loadResources(_asset);
_animator = _asset->getAnimator();
@@ -252,7 +256,10 @@ void FilamentViewer::loadGlb(const char* const uri) {
_freeResource((void*)rbuf.data, rbuf.size, nullptr);
transformToUnitCube();
// transformToUnitCube();
setCamera("Camera.001"); // TODO - expose this for external invocation
std::cerr << "Successfully loaded GLB." << std::endl;
}
@@ -280,8 +287,57 @@ void FilamentViewer::loadGltf(const char* const uri, const char* const relativeR
_freeResource((void*)rbuf.data, rbuf.size, nullptr);
transformToUnitCube();
// transformToUnitCube();
setCamera("Camera.001"); // TODO - expose this for external invocation
}
void FilamentViewer::setCamera(const char* cameraName) {
FFilamentAsset* asset = (FFilamentAsset*)_asset;
NodeMap &sourceNodes = asset->isInstanced() ? asset->mInstances[0]->nodeMap
: asset->mNodeMap;
for (auto pair : sourceNodes) {
cgltf_node const *node = pair.first;
if(node->camera) {
std::cout << "Got camera " << node->camera->name << " of type " << node->camera->type << std::endl;
if(strcmp(cameraName, node->camera->name) == 0) {
filament::math::mat4 mat(
node->matrix[0],
node->matrix[1],
node->matrix[2],
node->matrix[3],
node->matrix[4],
node->matrix[5],
node->matrix[6],
node->matrix[7],
node->matrix[8],
node->matrix[9],
node->matrix[10],
node->matrix[11],
node->parent->translation[0],
node->parent->translation[1],
node->parent->translation[2],
1
);
quatf rot1(node->parent->rotation[0],node->parent->rotation[1], node->parent->rotation[2], node->parent->rotation[3]);
quatf rot2(node->rotation[0],node->rotation[1], node->rotation[2], node->rotation[3]);
quatf rot3 = rot1 * rot2;
filament::math::mat4 rotm(rot3);
filament::math::mat4 result = mat * rotm;
_engine->getTransformManager().setTransform(
_engine->getTransformManager().getInstance(_mainCamera->getEntity()), result);
}
}
}
}
StringList FilamentViewer::getTargetNames(const char* meshName) {
@@ -294,6 +350,10 @@ StringList FilamentViewer::getTargetNames(const char* meshName) {
for (auto pair : sourceNodes) {
cgltf_node const *node = pair.first;
cgltf_mesh const *mesh = node->mesh;
if(node->camera) {
std::cout << "Got camera " << node->camera->name << " of type " << node->camera->type << std::endl;
}
if (mesh) {
std::cout << "Mesh : " << mesh->name;
@@ -415,10 +475,10 @@ void FilamentViewer::render() {
return;
}
// Extract the camera basis from the helper and push it to the Filament camera.
math::float3 eye, target, upward;
manipulator->getLookAt(&eye, &target, &upward);
//math::float3 eye, target, upward;
//manipulator->getLookAt(&eye, &target, &upward);
_mainCamera->lookAt(eye, target, upward);
//_mainCamera->lookAt(eye, target, upward);
if(morphAnimationBuffer) {
updateMorphAnimation();

View File

@@ -40,7 +40,7 @@ using namespace utils;
using namespace camutils;
namespace holovox {
namespace polyvox {
typedef std::chrono::time_point<std::chrono::high_resolution_clock> time_point_t;
@@ -117,6 +117,7 @@ namespace holovox {
void cleanup();
void updateMorphAnimation();
void updateEmbeddedAnimation();
void setCamera(const char* cameraName);
// animation flags;
bool isAnimating;

View File

@@ -63,6 +63,7 @@ namespace gltfio {
: asset->mNodeMap;
for (auto pair : sourceNodes) {
cgltf_node const *node = pair.first;
cgltf_mesh const *mesh = node->mesh;