This commit is contained in:
Nick Fisher
2023-04-19 18:06:48 +08:00
parent 7f4ca7e69b
commit a162ff2400
30 changed files with 3604 additions and 2293 deletions

777
ios/src/AssetManager.cpp Normal file
View 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

View File

@@ -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();

View File

@@ -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);
}
}

View File

@@ -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

View File

@@ -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
View 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

View File

@@ -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);