add (very rough) gizmo, restructure Dart package into library, add EntityListWidget

This commit is contained in:
Nick Fisher
2024-03-25 22:21:37 +08:00
parent 66ae0a4d08
commit 357c585d44
53 changed files with 2913 additions and 568 deletions

View File

@@ -138,7 +138,7 @@ namespace flutter_filament
void setAntiAliasing(bool msaaEnabled, bool fxaaEnabled, bool taaEnabled);
void setDepthOfField();
EntityId createGeometry(float* vertices, uint32_t numVertices, uint16_t* indices, uint32_t numIndices, const char* materialPath);
EntityId createGeometry(float* vertices, uint32_t numVertices, uint16_t* indices, uint32_t numIndices, filament::RenderableManager::PrimitiveType primitiveType = RenderableManager::PrimitiveType::TRIANGLES, const char* materialPath = nullptr);
SceneManager *const getSceneManager()
{

View File

@@ -197,9 +197,11 @@ extern "C"
FLUTTER_PLUGIN_EXPORT void remove_collision_component(void *const sceneManager, EntityId entityId);
FLUTTER_PLUGIN_EXPORT void add_animation_component(void *const sceneManager, EntityId entityId);
FLUTTER_PLUGIN_EXPORT EntityId create_geometry(void *const viewer, float* vertices, int numVertices, uint16_t* indices, int numIndices, const char* materialPath);
FLUTTER_PLUGIN_EXPORT EntityId create_geometry(void *const viewer, float* vertices, int numVertices, uint16_t* indices, int numIndices, int primitiveType, const char* materialPath);
FLUTTER_PLUGIN_EXPORT void set_parent(void *const sceneManager, EntityId child, EntityId parent);
FLUTTER_PLUGIN_EXPORT void test_collisions(void *const sceneManager, EntityId entity);
FLUTTER_PLUGIN_EXPORT void set_priority(void *const sceneManager, EntityId entityId, int priority);
FLUTTER_PLUGIN_EXPORT void get_gizmo(void *const sceneManager, EntityId* out);
#ifdef __cplusplus
}

View File

@@ -115,7 +115,7 @@ extern "C"
FLUTTER_PLUGIN_EXPORT void set_post_processing_ffi(void *const viewer, bool enabled);
FLUTTER_PLUGIN_EXPORT void reset_to_rest_pose_ffi(void *const sceneManager, EntityId entityId);
FLUTTER_PLUGIN_EXPORT void ios_dummy_ffi();
FLUTTER_PLUGIN_EXPORT void create_geometry_ffi(void *const viewer, float *vertices, int numVertices, uint16_t *indices, int numIndices, const char *materialPath, void (*callback)(EntityId));
FLUTTER_PLUGIN_EXPORT void create_geometry_ffi(void *const viewer, float *vertices, int numVertices, uint16_t *indices, int numIndices, int primitiveType, const char *materialPath, void (*callback)(EntityId));
#ifdef __cplusplus
}

View File

@@ -12,6 +12,10 @@
#include <gltfio/FilamentInstance.h>
#include <gltfio/ResourceLoader.h>
#include <filament/IndexBuffer.h>
#include <filament/InstanceBuffer.h>
#include "material/gizmo.h"
#include "utils/NameComponentManager.h"
#include "ResourceBuffer.hpp"
#include "components/CollisionComponentManager.hpp"
@@ -141,6 +145,19 @@ namespace flutter_filament
/// @param entityId
void getInstances(EntityId entityId, EntityId* out);
///
/// Sets the draw priority for the given entity. See RenderableManager.h for more details.
///
void setPriority(EntityId entity, int priority);
/// @brief returns the gizmo entity, used to manipulate the global transform for entities
/// @param out a pointer to an array of three EntityId {x, y, z}
/// @return
///
void getGizmo(EntityId* out);
friend class FilamentViewer;
@@ -154,6 +171,7 @@ namespace flutter_filament
gltfio::TextureProvider *_stbDecoder = nullptr;
gltfio::TextureProvider *_ktxDecoder = nullptr;
std::mutex _mutex;
Material* _gizmoMaterial;
utils::NameComponentManager* _ncm;
@@ -173,5 +191,10 @@ namespace flutter_filament
const gltfio::FilamentInstance* instance,
const char *entityName);
EntityId addGizmo();
utils::Entity _gizmoX;
utils::Entity _gizmoY;
utils::Entity _gizmoZ;
};
}

View File

@@ -115,6 +115,8 @@ namespace flutter_filament {
{
auto now = high_resolution_clock::now();
// Log("animation component count : %d", )
for(auto it = begin(); it < end(); it++) {
const auto& entity = getEntity(it);

View File

@@ -1,95 +0,0 @@
#ifndef FILE_MATERIAL_PROVIDER
#define FILE_MATERIAL_PROVIDER
#include <filament/gltfio/MaterialProvider.h>
#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>
#include "Log.hpp"
namespace flutter_filament {
class FileMaterialProvider : public filament::gltfio::MaterialProvider {
filament::Material* _m;
const filament::Material* _ms[1];
filament::Texture* mDummyTexture = nullptr;
public:
FileMaterialProvider(filament::Engine* engine, const void* const data, const size_t size) {
_m = filament::Material::Builder()
.package(data, size)
.build(*engine);
_ms[0] = _m;
unsigned char texels[4] = {};
mDummyTexture = filament::Texture::Builder()
.width(1).height(1)
.format(filament::Texture::InternalFormat::RGBA8)
.build(*engine);
filament::Texture::PixelBufferDescriptor pbd(texels, sizeof(texels), filament::Texture::Format::RGBA,
filament::Texture::Type::UBYTE);
mDummyTexture->setImage(*engine, 0, std::move(pbd));
}
filament::MaterialInstance* createMaterialInstance(filament::gltfio::MaterialKey* config, filament::gltfio::UvMap* uvmap,
const char* label = "material", const char* extras = nullptr) {
auto getUvIndex = [uvmap](uint8_t srcIndex, bool hasTexture) -> int {
return hasTexture ? int(uvmap->at(srcIndex)) - 1 : -1;
};
auto instance = _m->createInstance();
filament::math::mat3f identity;
instance->setParameter("baseColorUvMatrix", identity);
instance->setParameter("normalUvMatrix", identity);
instance->setParameter("baseColorIndex", getUvIndex(config->baseColorUV, config->hasBaseColorTexture));
instance->setParameter("normalIndex", getUvIndex(config->normalUV, config->hasNormalTexture));
if(config->hasNormalTexture) {
filament::TextureSampler sampler;
instance->setParameter("normalMap", mDummyTexture, sampler);
instance->setParameter("baseColorMap", mDummyTexture, sampler);
} else {
Log("No normal texture for specified material.");
}
return instance;
}
/**
* Creates or fetches a compiled Filament material corresponding to the given config.
*/
virtual filament::Material* getMaterial(filament::gltfio::MaterialKey* config, filament::gltfio::UvMap* uvmap, const char* label = "material") {
return _m;
}
/**
* Gets a weak reference to the array of cached materials.
*/
const filament::Material* const* getMaterials() const noexcept {
return _ms;
}
/**
* Gets the number of cached materials.
*/
size_t getMaterialsCount() const noexcept {
return (size_t)1;
}
void destroyMaterials() {
}
bool needsDummyData(filament::VertexAttribute attrib) const noexcept {
return true;
}
};
}
#endif

View File

@@ -0,0 +1,12 @@
.global GIZMO_GIZMO_OFFSET;
.global GIZMO_GIZMO_SIZE;
.global GIZMO_PACKAGE
.section .rodata
GIZMO_PACKAGE:
.incbin "gizmo.bin"
GIZMO_GIZMO_OFFSET:
.int 0
GIZMO_GIZMO_SIZE:
.int 36751

View File

@@ -0,0 +1,12 @@
.global _GIZMO_GIZMO_OFFSET;
.global _GIZMO_GIZMO_SIZE;
.global _GIZMO_PACKAGE
.section __TEXT,__const
_GIZMO_PACKAGE:
.incbin "gizmo.bin"
_GIZMO_GIZMO_OFFSET:
.int 0
_GIZMO_GIZMO_SIZE:
.int 36751

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:bbb413c69ac6f77243a02049d2ccf494a1f20ea88fb8ca159b22c5bc0a4fdd95
size 36751

1847
ios/include/material/gizmo.c Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,13 @@
#ifndef GIZMO_H_
#define GIZMO_H_
#include <stdint.h>
extern "C" {
extern const uint8_t GIZMO_PACKAGE[];
extern int GIZMO_GIZMO_OFFSET;
extern int GIZMO_GIZMO_SIZE;
}
#define GIZMO_GIZMO_DATA (GIZMO_PACKAGE + GIZMO_GIZMO_OFFSET)
#endif

View File

@@ -248,6 +248,7 @@ namespace flutter_filament
.build(*_engine, imageEntity);
_imageEntity = &imageEntity;
_scene->addEntity(imageEntity);
Log("Added imageEntity %d", imageEntity);
}
void FilamentViewer::setAntiAliasing(bool msaa, bool fxaa, bool taa) {
@@ -320,6 +321,9 @@ namespace flutter_filament
int32_t FilamentViewer::addLight(LightManager::Type t, float colour, float intensity, float posX, float posY, float posZ, float dirX, float dirY, float dirZ, bool shadows)
{
auto light = EntityManager::get().create();
auto& transformManager = _engine->getTransformManager();
transformManager.create(light);
auto parent = transformManager.getInstance(light);
auto builder = LightManager::Builder(t)
.color(Color::cct(colour))
.intensity(intensity)
@@ -329,6 +333,7 @@ namespace flutter_filament
.build(*_engine, light);
_scene->addEntity(light);
_lights.push_back(light);
auto entityId = Entity::smuggle(light);
Log("Added light under entity ID %d of type %d with colour %f intensity %f at (%f, %f, %f) with direction (%f, %f, %f) with shadows %d", entityId, t, colour, intensity, posX, posY, posZ, dirX, dirY, dirZ, shadows);
return entityId;
@@ -1450,7 +1455,7 @@ namespace flutter_filament
});
}
EntityId FilamentViewer::createGeometry(float *vertices, uint32_t numVertices, uint16_t *indices, uint32_t numIndices, const char* materialPath)
EntityId FilamentViewer::createGeometry(float *vertices, uint32_t numVertices, uint16_t *indices, uint32_t numIndices, RenderableManager::PrimitiveType primitiveType, const char* materialPath)
{
float *verticesCopy = (float*)malloc(numVertices * sizeof(float));
@@ -1499,7 +1504,7 @@ namespace flutter_filament
RenderableManager::Builder builder = RenderableManager::Builder(1);
builder
.boundingBox({{minX, minY, minZ}, {maxX, maxY, maxZ}})
.geometry(0, RenderableManager::PrimitiveType::TRIANGLES,
.geometry(0, primitiveType,
vb, ib, 0, numIndices)
.culling(false)
.receiveShadows(false)
@@ -1510,7 +1515,8 @@ namespace flutter_filament
auto result = builder.build(*_engine, renderable);
_scene->addEntity(renderable);
Log("Created geometry with result %d", result);
Log("Created geometry with primitive type %d (result %d)", primitiveType, result);
return Entity::smuggle(renderable);
}

View File

@@ -597,8 +597,15 @@ extern "C"
((SceneManager*)sceneManager)->addAnimationComponent(entityId);
}
FLUTTER_PLUGIN_EXPORT EntityId create_geometry(void *const viewer, float* vertices, int numVertices, uint16_t* indices, int numIndices, const char* materialPath) {
return ((FilamentViewer*)viewer)->createGeometry(vertices, (uint32_t)numVertices, indices, numIndices, materialPath);
FLUTTER_PLUGIN_EXPORT EntityId create_geometry(void *const viewer, float* vertices, int numVertices, uint16_t* indices, int numIndices, int primitiveType, const char* materialPath) {
return ((FilamentViewer*)viewer)->createGeometry(
vertices,
(uint32_t)numVertices,
indices,
numIndices,
(filament::RenderableManager::PrimitiveType)primitiveType,
materialPath
);
}
FLUTTER_PLUGIN_EXPORT EntityId find_child_entity_by_name(void *const sceneManager, const EntityId parent, const char* name) {
@@ -614,4 +621,12 @@ extern "C"
((SceneManager*)sceneManager)->testCollisions(entity);
}
FLUTTER_PLUGIN_EXPORT void set_priority(void *const sceneManager, EntityId entity, int priority) {
((SceneManager*)sceneManager)->setPriority(entity, priority);
}
FLUTTER_PLUGIN_EXPORT void get_gizmo(void *const sceneManager, EntityId* out) {
return ((SceneManager*)sceneManager)->getGizmo(out);
}
}

View File

@@ -40,7 +40,6 @@ public:
_t = new std::thread([this]() {
auto last = std::chrono::high_resolution_clock::now();
while (!_stop) {
last = std::chrono::high_resolution_clock::now();
if (_rendering) {
// auto frameStart = std::chrono::high_resolution_clock::now();
@@ -48,6 +47,8 @@ public:
// auto frameEnd = std::chrono::high_resolution_clock::now();
}
last = std::chrono::high_resolution_clock::now();
auto now = std::chrono::high_resolution_clock::now();
float elapsed = float(std::chrono::duration_cast<std::chrono::milliseconds>(now - last).count());
@@ -58,7 +59,6 @@ public:
if(_tasks.empty()) {
_cond.wait_for(lock, std::chrono::duration<float, std::milli>(1));
continue;
}
while(!_tasks.empty()) {
task = std::move(_tasks.front());
@@ -571,10 +571,10 @@ FLUTTER_PLUGIN_EXPORT void add_bone_animation_ffi(
FLUTTER_PLUGIN_EXPORT void ios_dummy_ffi() { Log("Dummy called"); }
FLUTTER_PLUGIN_EXPORT void create_geometry_ffi(void* const viewer, float* vertices, int numVertices, uint16_t* indices, int numIndices, const char* materialPath, void (*callback)(EntityId) ) {
FLUTTER_PLUGIN_EXPORT void create_geometry_ffi(void* const viewer, float* vertices, int numVertices, uint16_t* indices, int numIndices, int primitiveType, const char* materialPath, void (*callback)(EntityId) ) {
std::packaged_task<EntityId()> lambda(
[=] {
auto entity = create_geometry(viewer, vertices, numVertices, indices, numIndices, materialPath);
auto entity = create_geometry(viewer, vertices, numVertices, indices, numIndices, primitiveType, materialPath);
callback(entity);
return entity;
});

View File

@@ -23,7 +23,6 @@
#include "Log.hpp"
#include "SceneManager.hpp"
#include "material/FileMaterialProvider.hpp"
#include "gltfio/materials/uberarchive.h"
extern "C"
@@ -88,6 +87,8 @@ namespace flutter_filament
_collisionComponentManager = new CollisionComponentManager(tm);
_animationComponentManager = new AnimationComponentManager(tm, _engine->getRenderableManager());
addGizmo();
}
SceneManager::~SceneManager()
@@ -307,8 +308,10 @@ namespace flutter_filament
if(asset) {
instance = asset->getInstance();
} else {
return false;
}
// Log("Failed to find glTF instance under entityID %d, hiding as regular entity", entityId);
_scene->remove(Entity::import(entityId));
return true;
}
}
utils::Entity entity;
@@ -342,7 +345,9 @@ namespace flutter_filament
if(asset) {
instance = asset->getInstance();
} else {
return false;
// Log("Failed to find glTF instance under entityID %d, revealing as regular entity", entityId);
_scene->addEntity(Entity::import(entityId));
return true;
}
}
@@ -1563,4 +1568,161 @@ namespace flutter_filament
return _ncm->getName(inst);
}
void SceneManager::setPriority(EntityId entityId, int priority) {
auto& rm = _engine->getRenderableManager();
auto renderableInstance = rm.getInstance(Entity::import(entityId));
if(!renderableInstance.isValid()) {
Log("Error: invalid renderable, did you pass the correct entity?", priority);
return;
}
rm.setPriority(renderableInstance, priority);
Log("Set instance renderable priority to %d", priority);
}
EntityId SceneManager::addGizmo() {
auto mat = _resourceLoaderWrapper->load("file:///Users/nickfisher/Documents/polyvox/flutter/flutter_filament/materials/gizmo.filamat");
_gizmoMaterial =
Material::Builder()
.package(mat.data, mat.size)
// .package(GIZMO_GIZMO_DATA, GIZMO_GIZMO_SIZE)
.build(*_engine);
auto vertexCount = 9;
float* vertices = new float[vertexCount * 3] {
-0.05, 0.0f, 0.05f,
0.05f, 0.0f, 0.05f,
0.05f, 0.0f, -0.05f,
-0.05f, 0.0f, -0.05f,
-0.05f, 1.0f, 0.05f,
0.05f, 1.0f, 0.05f,
0.05f, 1.0f, -0.05f,
-0.05f, 1.0f, -0.05f,
0.00f, 1.1f, 0.0f
};
VertexBuffer::BufferDescriptor::Callback vertexCallback = [](void *buf, size_t,
void *data)
{
free((void*)buf);
};
auto indexCount = 42;
uint16_t* indices = new uint16_t[indexCount] {
//bottom quad
0,1,2,
0,2,3,
// top "cone"
4,5,8,
5,6,8,
4,7,8,
6,7,8,
// front
0,1,4,
1,5,4,
// right
1,2,5,
2,6,5,
// back
2,6,7,
7,3,2,
// left
0,4,7,
7,3,0
};
IndexBuffer::BufferDescriptor::Callback indexCallback = [](void *buf, size_t,
void *data)
{
free((void*)buf);
};
auto vb = VertexBuffer::Builder()
.vertexCount(vertexCount)
.bufferCount(1)
.attribute(
VertexAttribute::POSITION, 0, VertexBuffer::AttributeType::FLOAT3)
.build(*_engine);
vb->setBufferAt(
*_engine,
0,
VertexBuffer::BufferDescriptor(vertices, vb->getVertexCount() * sizeof(filament::math::float3), 0, vertexCallback)
);
auto ib = IndexBuffer::Builder().indexCount(indexCount).bufferType(IndexBuffer::IndexType::USHORT).build(*_engine);
ib->setBuffer(*_engine, IndexBuffer::BufferDescriptor(indices, ib->getIndexCount() * sizeof(uint16_t), 0, indexCallback));
auto &entityManager = EntityManager::get();
_gizmoY = entityManager.create();
auto materialY = _gizmoMaterial->createInstance();
materialY->setParameter("color", math::float3 { 1.0f, 0.0f, 0.0f });
RenderableManager::Builder(1)
.boundingBox({{}, {1.0f, 1.0f, 1.0f}})
.material(0, materialY)
.geometry(0, RenderableManager::PrimitiveType::TRIANGLES, vb,
ib, 0, indexCount)
.culling(false)
.build(*_engine, _gizmoY);
_gizmoX = entityManager.create();
auto materialX = _gizmoMaterial->createInstance();
materialX->setParameter("color", math::float3 { 0.0f, 1.0f, 0.0f });
auto xTransform = math::mat4f::translation(math::float3 { 0.0f, 0.05f, -0.05f}) * math::mat4f::rotation(-math::F_PI_2, math::float3 { 0, 0, 1 });
auto* instanceBufferX = InstanceBuffer::Builder(1).localTransforms(&xTransform).build(*_engine);
RenderableManager::Builder(1)
.boundingBox({{}, {1.0f, 1.0f, 1.0f}})
.instances(1, instanceBufferX)
.material(0, materialX)
.geometry(0, RenderableManager::PrimitiveType::TRIANGLES, vb,
ib, 0, indexCount)
.culling(false)
.build(*_engine, _gizmoX);
_gizmoZ = entityManager.create();
auto materialZ = _gizmoMaterial->createInstance();
materialZ->setParameter("color", math::float3 { 0.0f, 0.0f, 1.0f });
auto zTransform = math::mat4f::translation(math::float3 { 0.0f, 0.05f, -0.05f}) * math::mat4f::rotation(3 * math::F_PI_2, math::float3 { 1, 0, 0 });
auto* instanceBufferZ = InstanceBuffer::Builder(1).localTransforms(&zTransform).build(*_engine);
RenderableManager::Builder(1)
.boundingBox({{}, {1.0f, 1.0f, 1.0f}})
.instances(1, instanceBufferZ)
.material(0, materialZ)
.geometry(0, RenderableManager::PrimitiveType::TRIANGLES, vb,
ib, 0, indexCount)
.culling(false)
.build(*_engine, _gizmoZ);
// auto localTransforms = math::mat4f[3] {
// math::mat4f(),
// math::mat4f::translation(math::float3 { 0.0f, 0.05f, -0.05f}) * math::mat4f::rotation(3 * math::F_PI_2, math::float3 { 1, 0, 0 }) ,
// math::mat4f::translation(math::float3 { 0.0f, 0.05f, -0.05f}) * math::mat4f::rotation(math::F_PI_2, math::float3 { 0, 0, 1 })
// };
// RenderableManager::Builder(1)
// .boundingBox({{}, {1.0f, 1.0f, 1.0f}})
// .instances(3, instanceBuffer)
// .material(0, _gizmoMaterial->getDefaultInstance())
// .geometry(0, RenderableManager::PrimitiveType::TRIANGLES, vb,
// ib, 0, indexCount)
// .culling(false)
// .build(*_engine, _gizmo);
auto& rm = _engine->getRenderableManager();
rm.setPriority(rm.getInstance(_gizmoX), 7);
rm.setPriority(rm.getInstance(_gizmoY), 7);
rm.setPriority(rm.getInstance(_gizmoZ), 7);
return Entity::smuggle(_gizmoX);
}
void SceneManager::getGizmo(EntityId* out) {
out[0] = Entity::smuggle(_gizmoX);
out[1] = Entity::smuggle(_gizmoY);
out[2] = Entity::smuggle(_gizmoZ);
}
} // namespace flutter_filament