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

@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
@@ -129,7 +130,7 @@ class ExampleWidgetState extends State<ExampleWidget> {
),
EntityListWidget(controller: _filamentController),
Positioned(
bottom: 0,
bottom: Platform.isIOS ? 30 : 0,
left: 0,
right: 10,
height: 30,
@@ -164,9 +165,8 @@ class ExampleWidgetState extends State<ExampleWidget> {
),
GestureDetector(
onTap: () async {
await _filamentController!.loadGlb(
'assets/shapes/shapes.glb',
numInstances: 10);
await _filamentController!
.loadGlb('assets/shapes/shapes.glb', numInstances: 1);
},
child: Container(
color: Colors.transparent,

View File

@@ -215,6 +215,13 @@ class _AssetSubmenuState extends State<AssetSubmenu> {
},
child: const Text("Add point light"),
),
MenuItemButton(
onPressed: () async {
await widget.controller
.addLight(3, 6500, 15000000, 0, 1, 0, 0, -1, 0, true);
},
child: const Text("Add spot light"),
),
MenuItemButton(
onPressed: () async {
await widget.controller.clearLights();

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) {

View File

@@ -65,6 +65,9 @@ class Gizmo {
}
void detach() async {
await controller.setParent(x, 0);
await controller.setParent(y, 0);
await controller.setParent(z, 0);
await controller.hide(x, null);
await controller.hide(y, null);
await controller.hide(z, null);

View File

@@ -576,6 +576,12 @@ abstract class FilamentController {
double orbitSpeedY = 0.01,
double zoomSpeed = 0.01});
///
/// Returns all child entities under [parent].
///
Future<List<FilamentEntity>> getChildEntities(
FilamentEntity parent, bool renderableOnly);
///
/// Finds the child entity named [childName] associated with the given parent.
/// Usually, [parent] will be the return value from [loadGlb]/[loadGltf] and [childName] will be the name of a node/mesh.
@@ -584,9 +590,9 @@ abstract class FilamentController {
FilamentEntity parent, String childName);
///
/// List all child entities under the given entity.
/// List the name of all child entities under the given entity.
///
Future<List<String>> getChildEntities(FilamentEntity entity,
Future<List<String>> getChildEntityNames(FilamentEntity entity,
{bool renderableOnly = true});
///

View File

@@ -358,7 +358,7 @@ class FilamentControllerFFI extends FilamentController {
get_gizmo(_sceneManager!, out);
var gizmo = Gizmo(out[0], out[1], out[2], this);
allocator.free(out);
_scene = SceneImpl(gizmo);
_scene = SceneImpl(gizmo, this);
hasViewer.value = true;
_creating = false;
@@ -1504,8 +1504,19 @@ class FilamentControllerFFI extends FilamentController {
return childEntity;
}
Future<List<FilamentEntity>> getChildEntities(
FilamentEntity parent, bool renderableOnly) async {
var count = get_entity_count(_sceneManager!, parent, renderableOnly);
var out = allocator<Int32>(count);
get_entities(_sceneManager!, parent, renderableOnly, out);
var outList =
List.generate(count, (index) => out[index]).cast<FilamentEntity>();
allocator.free(out);
return outList;
}
@override
Future<List<String>> getChildEntities(FilamentEntity entity,
Future<List<String>> getChildEntityNames(FilamentEntity entity,
{bool renderableOnly = false}) async {
var count = get_entity_count(_sceneManager!, entity, renderableOnly);
var names = <String>[];

View File

@@ -885,6 +885,17 @@ external int get_entity_count(
bool renderableOnly,
);
@ffi.Native<
ffi.Void Function(
ffi.Pointer<ffi.Void>, EntityId, ffi.Bool, ffi.Pointer<EntityId>)>(
symbol: 'get_entities', assetId: 'flutter_filament_plugin')
external void get_entities(
ffi.Pointer<ffi.Void> sceneManager,
int target,
bool renderableOnly,
ffi.Pointer<EntityId> out,
);
@ffi.Native<
ffi.Pointer<ffi.Char> Function(
ffi.Pointer<ffi.Void>, EntityId, ffi.Int, ffi.Bool)>(

View File

@@ -9,7 +9,9 @@ class SceneImpl extends Scene {
final Gizmo _gizmo;
Gizmo get gizmo => _gizmo;
SceneImpl(this._gizmo);
FilamentController controller;
SceneImpl(this._gizmo, this.controller);
@override
FilamentEntity? selected;
@@ -35,8 +37,9 @@ class SceneImpl extends Scene {
_onUpdatedController.add(true);
}
void unregisterLight(FilamentEntity entity) {
if (selected == entity) {
void unregisterLight(FilamentEntity entity) async {
var children = await controller.getChildEntities(entity, true);
if (selected == entity || children.contains(selected)) {
selected = null;
_gizmo.detach();
}
@@ -45,8 +48,9 @@ class SceneImpl extends Scene {
_onUpdatedController.add(true);
}
void unregisterEntity(FilamentEntity entity) {
if (selected == entity) {
void unregisterEntity(FilamentEntity entity) async {
var children = await controller.getChildEntities(entity, true);
if (selected == entity || children.contains(selected)) {
selected = null;
_gizmo.detach();
}