expose method for retrieving all child entities

This commit is contained in:
Nick Fisher
2024-03-26 00:24:21 +08:00
parent 849ad6c530
commit b357144a79
13 changed files with 101 additions and 21 deletions

View File

@@ -195,7 +195,7 @@ namespace flutter_filament
uint32_t _imageWidth = 0;
mat4f _imageScale;
Texture *_imageTexture = nullptr;
utils::Entity *_imageEntity = nullptr;
utils::Entity _imageEntity;
VertexBuffer *_imageVb = nullptr;
IndexBuffer *_imageIb = nullptr;
Material *_imageMaterial = nullptr;

View File

@@ -188,6 +188,7 @@ extern "C"
FLUTTER_PLUGIN_EXPORT const char *get_name_for_entity(void *const sceneManager, const EntityId entityId);
FLUTTER_PLUGIN_EXPORT EntityId find_child_entity_by_name(void *const sceneManager, const EntityId parent, const char* name);
FLUTTER_PLUGIN_EXPORT int get_entity_count(void *const sceneManager, const EntityId target, bool renderableOnly);
FLUTTER_PLUGIN_EXPORT void get_entities(void *const sceneManager, const EntityId target, bool renderableOnly, EntityId* out);
FLUTTER_PLUGIN_EXPORT const char* get_entity_name_at(void *const sceneManager, const EntityId target, int index, bool renderableOnly);
FLUTTER_PLUGIN_EXPORT void set_recording(void *const viewer, bool recording);
FLUTTER_PLUGIN_EXPORT void set_recording_output_directory(void *const viewer, const char* outputDirectory);

View File

@@ -130,6 +130,7 @@ namespace flutter_filament
EntityId entityId,
const char *entityName);
int getEntityCount(EntityId entity, bool renderableOnly);
void getEntities(EntityId entity, bool renderableOnly, EntityId *out);
const char* getEntityNameAt(EntityId entity, int index, bool renderableOnly);
void addCollisionComponent(EntityId entity, void (*onCollisionCallback)(const EntityId entityId1, const EntityId entityId2), bool affectsCollidingTransform);
void removeCollisionComponent(EntityId entityId);

View File

@@ -238,17 +238,16 @@ namespace flutter_filament
_imageIb->setBuffer(*_engine, {sFullScreenTriangleIndices,
sizeof(sFullScreenTriangleIndices)});
utils::Entity imageEntity = em.create();
_imageEntity = em.create();
RenderableManager::Builder(1)
.boundingBox({{}, {1.0f, 1.0f, 1.0f}})
.material(0, _imageMaterial->getDefaultInstance())
.geometry(0, RenderableManager::PrimitiveType::TRIANGLES, _imageVb,
_imageIb, 0, 3)
.culling(false)
.build(*_engine, imageEntity);
_imageEntity = &imageEntity;
_scene->addEntity(imageEntity);
Log("Added imageEntity %d", imageEntity);
.build(*_engine, _imageEntity);
_scene->addEntity(_imageEntity);
Log("Added imageEntity %d", _imageEntity);
}
void FilamentViewer::setAntiAliasing(bool msaa, bool fxaa, bool taa) {
@@ -335,6 +334,8 @@ namespace flutter_filament
_lights.push_back(light);
auto entityId = Entity::smuggle(light);
auto transformInstance = transformManager.getInstance(light);
transformManager.setTransform(transformInstance, math::mat4::translation(math::float3 { posX, posY, posZ}));
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;
}
@@ -885,7 +886,7 @@ namespace flutter_filament
ResourceBuffer skyboxBuffer = _resourceLoaderWrapper->load(skyboxPath);
// because this will go out of scope before the texture callback is invoked, we need to make a copy to the heap
// because this will go out of scope before the texture callback is invoked, we need to make a copy of the variable itself (not its contents)
ResourceBuffer *skyboxBufferCopy = new ResourceBuffer(skyboxBuffer);
if (skyboxBuffer.size <= 0)
@@ -1449,9 +1450,12 @@ namespace flutter_filament
void FilamentViewer::pick(uint32_t x, uint32_t y, void (*callback)(EntityId entityId, int x, int y))
{
_view->pick(x, y, [=](filament::View::PickingQueryResult const &result)
{
callback(Entity::smuggle(result.renderable), x, y);
if(result.renderable != _imageEntity) {
callback(Entity::smuggle(result.renderable), x, y);
}
});
}

View File

@@ -562,6 +562,10 @@ extern "C"
return ((SceneManager *)sceneManager)->getEntityCount(target, renderableOnly);
}
FLUTTER_PLUGIN_EXPORT void get_entities(void *const sceneManager, const EntityId target, bool renderableOnly, EntityId* out) {
((SceneManager *)sceneManager)->getEntities(target, renderableOnly, out);
}
FLUTTER_PLUGIN_EXPORT const char* get_entity_name_at(void *const sceneManager, const EntityId target, int index, bool renderableOnly) {
return ((SceneManager *)sceneManager)->getEntityNameAt(target, index, renderableOnly);
}

View File

@@ -1529,6 +1529,34 @@ namespace flutter_filament
return instance->getEntityCount();
}
void SceneManager::getEntities(EntityId entityId, bool renderableOnly, EntityId* out) {
const auto *instance = getInstanceByEntityId(entityId);
if(!instance) {
auto asset = getAssetByEntityId(entityId);
if(asset) {
instance = asset->getInstance();
} else {
return;
}
}
if(renderableOnly) {
int count = 0;
const auto& rm = _engine->getRenderableManager();
const Entity *entities = instance->getEntities();
int offset = 0;
for(int i=0; i < instance->getEntityCount(); i++) {
if(rm.hasComponent(entities[i])) {
out[offset] = Entity::smuggle(entities[i]);
}
}
} else {
for(int i=0;i < instance->getEntityCount(); i++) {
out[i] = Entity::smuggle(instance->getEntities()[i]);
}
}
}
const char* SceneManager::getEntityNameAt(EntityId entityId, int index, bool renderableOnly) {
const auto *instance = getInstanceByEntityId(entityId);
if(!instance) {