rename (asset to entity)

This commit is contained in:
Nick Fisher
2024-02-15 15:16:56 +08:00
parent 935b876ce9
commit 2ae3f8c466
15 changed files with 108 additions and 41 deletions

View File

@@ -174,10 +174,10 @@ var entity = await _filamentController.loadGlb("file:///tmp/bob.glb");
The return type `FilamentEntity` is simply an integer handle that be used to manipulate the entity in the scene. For example, to remove the asset: The return type `FilamentEntity` is simply an integer handle that be used to manipulate the entity in the scene. For example, to remove the asset:
``` ```
await _filamentController.removeAsset(entity); await _filamentController.removeEntity(entity);
entity = null; entity = null;
``` ```
> Removing an entity from the scene will invalidate the corresponding `FilamentEntity` handle, so ensure you don't retain any references to it after calling `removeAsset` or `clearAssets`. Removing one `FilamentEntity` does not invalidate/change any other `FilamentEntity` handles; you can continue to safely manipulate these via the `FilamentController`. > Removing an entity from the scene will invalidate the corresponding `FilamentEntity` handle, so ensure you don't retain any references to it after calling `removeEntity` or `clearEntities`. Removing one `FilamentEntity` does not invalidate/change any other `FilamentEntity` handles; you can continue to safely manipulate these via the `FilamentController`.
### Lighting ### Lighting

View File

@@ -148,7 +148,7 @@ class ExampleWidgetState extends State<ExampleWidget> {
color: _controlled == entity ? Colors.green : Colors.black)), color: _controlled == entity ? Colors.green : Colors.black)),
IconButton( IconButton(
onPressed: () async { onPressed: () async {
await _filamentController!.removeAsset(entity); await _filamentController!.removeEntity(entity);
assets.remove(entity); assets.remove(entity);
setState(() {}); setState(() {});
}, },

View File

@@ -216,7 +216,8 @@ class _AssetSubmenuState extends State<AssetSubmenu> {
ExampleWidgetState.animations = await widget.controller ExampleWidgetState.animations = await widget.controller
.getAnimationNames(ExampleWidgetState.assets.last); .getAnimationNames(ExampleWidgetState.assets.last);
} else { } else {
await widget.controller.removeAsset(ExampleWidgetState.buster!); await widget.controller
.removeEntity(ExampleWidgetState.buster!);
ExampleWidgetState.buster = null; ExampleWidgetState.buster = null;
} }
}, },
@@ -232,7 +233,7 @@ class _AssetSubmenuState extends State<AssetSubmenu> {
force: true); force: true);
} else { } else {
await widget.controller await widget.controller
.removeAsset(ExampleWidgetState.flightHelmet!); .removeEntity(ExampleWidgetState.flightHelmet!);
ExampleWidgetState.flightHelmet = null; ExampleWidgetState.flightHelmet = null;
} }
}, },
@@ -281,7 +282,7 @@ class _AssetSubmenuState extends State<AssetSubmenu> {
child: const Text("Request permissions (tests inactive->resume)")), child: const Text("Request permissions (tests inactive->resume)")),
MenuItemButton( MenuItemButton(
onPressed: () async { onPressed: () async {
await widget.controller.clearAssets(); await widget.controller.clearEntities();
ExampleWidgetState.flightHelmet = null; ExampleWidgetState.flightHelmet = null;
ExampleWidgetState.buster = null; ExampleWidgetState.buster = null;
ExampleWidgetState.assets.clear(); ExampleWidgetState.assets.clear();

View File

@@ -70,8 +70,8 @@ namespace polyvox
void rotateIbl(const math::mat3f & matrix); void rotateIbl(const math::mat3f & matrix);
void removeAsset(EntityId asset); void removeEntity(EntityId asset);
void clearAssets(); void clearEntities();
void updateViewportAndCameraProjection(int height, int width, float scaleFactor); void updateViewportAndCameraProjection(int height, int width, float scaleFactor);
void render( void render(

View File

@@ -142,8 +142,8 @@ extern "C"
FLUTTER_PLUGIN_EXPORT float get_animation_duration(void *assetManager, EntityId asset, int index); FLUTTER_PLUGIN_EXPORT float get_animation_duration(void *assetManager, EntityId asset, int index);
FLUTTER_PLUGIN_EXPORT void get_morph_target_name(void *assetManager, EntityId asset, const char *meshName, char *const outPtr, int index); FLUTTER_PLUGIN_EXPORT void get_morph_target_name(void *assetManager, EntityId asset, const char *meshName, char *const outPtr, int index);
FLUTTER_PLUGIN_EXPORT int get_morph_target_name_count(void *assetManager, EntityId asset, const char *meshName); FLUTTER_PLUGIN_EXPORT int get_morph_target_name_count(void *assetManager, EntityId asset, const char *meshName);
FLUTTER_PLUGIN_EXPORT void remove_asset(const void *const viewer, EntityId asset); FLUTTER_PLUGIN_EXPORT void remove_entity(const void *const viewer, EntityId asset);
FLUTTER_PLUGIN_EXPORT void clear_assets(const void *const viewer); FLUTTER_PLUGIN_EXPORT void clear_entities(const void *const viewer);
FLUTTER_PLUGIN_EXPORT bool set_material_color(void *assetManager, EntityId asset, const char *meshName, int materialIndex, const float r, const float g, const float b, const float a); FLUTTER_PLUGIN_EXPORT bool set_material_color(void *assetManager, EntityId asset, const char *meshName, int materialIndex, const float r, const float g, const float b, const float a);
FLUTTER_PLUGIN_EXPORT void transform_to_unit_cube(void *assetManager, EntityId asset); FLUTTER_PLUGIN_EXPORT void transform_to_unit_cube(void *assetManager, EntityId asset);
FLUTTER_PLUGIN_EXPORT void queue_position_update(void *assetManager, EntityId asset, float x, float y, float z, bool relative); FLUTTER_PLUGIN_EXPORT void queue_position_update(void *assetManager, EntityId asset, float x, float y, float z, bool relative);

View File

@@ -40,8 +40,8 @@ FLUTTER_PLUGIN_EXPORT void remove_light_ffi(void* const viewer, EntityId entityI
FLUTTER_PLUGIN_EXPORT void clear_lights_ffi(void* const viewer); FLUTTER_PLUGIN_EXPORT void clear_lights_ffi(void* const viewer);
FLUTTER_PLUGIN_EXPORT EntityId load_glb_ffi(void* const assetManager, const char *assetPath, bool unlit); FLUTTER_PLUGIN_EXPORT EntityId load_glb_ffi(void* const assetManager, const char *assetPath, bool unlit);
FLUTTER_PLUGIN_EXPORT EntityId load_gltf_ffi(void* const assetManager, const char *assetPath, const char *relativePath); FLUTTER_PLUGIN_EXPORT EntityId load_gltf_ffi(void* const assetManager, const char *assetPath, const char *relativePath);
FLUTTER_PLUGIN_EXPORT void remove_asset_ffi(void* const viewer, EntityId asset); FLUTTER_PLUGIN_EXPORT void remove_entity_ffi(void* const viewer, EntityId asset);
FLUTTER_PLUGIN_EXPORT void clear_assets_ffi(void* const viewer); FLUTTER_PLUGIN_EXPORT void clear_entities_ffi(void* const viewer);
FLUTTER_PLUGIN_EXPORT bool set_camera_ffi(void* const viewer, EntityId asset, const char *nodeName); FLUTTER_PLUGIN_EXPORT bool set_camera_ffi(void* const viewer, EntityId asset, const char *nodeName);
FLUTTER_PLUGIN_EXPORT void apply_weights_ffi( FLUTTER_PLUGIN_EXPORT void apply_weights_ffi(
void* const assetManager, void* const assetManager,

View File

@@ -103,7 +103,7 @@ public:
* *
* This removes all the asset entities from the Scene, but does not destroy them. * This removes all the asset entities from the Scene, but does not destroy them.
*/ */
void removeAsset(); void removeEntity();
/** /**
* Sets or changes the current scene's IBL to allow the UI manipulate it. * Sets or changes the current scene's IBL to allow the UI manipulate it.

View File

@@ -234,6 +234,9 @@ namespace polyvox
inst->getAnimator()->updateBoneMatrices(); inst->getAnimator()->updateBoneMatrices();
inst->recomputeBoundingBoxes(); inst->recomputeBoundingBoxes();
auto box = inst->getBoundingBox();
auto verts = box.extent();
Log("AABB extent for %s is %f %f %f", uri, verts.x, verts.y, verts.z);
asset->releaseSourceData(); asset->releaseSourceData();

View File

@@ -639,7 +639,7 @@ namespace polyvox
FilamentViewer::~FilamentViewer() FilamentViewer::~FilamentViewer()
{ {
clearAssets(); clearEntities();
delete _assetManager; delete _assetManager;
for (auto it : _lights) for (auto it : _lights)
@@ -725,7 +725,7 @@ namespace polyvox
_engine->flushAndWait(); _engine->flushAndWait();
} }
void FilamentViewer::clearAssets() void FilamentViewer::clearEntities()
{ {
Log("Clearing all assets"); Log("Clearing all assets");
if (_mainCamera) if (_mainCamera)
@@ -738,7 +738,7 @@ namespace polyvox
Log("Cleared all assets"); Log("Cleared all assets");
} }
void FilamentViewer::removeAsset(EntityId asset) void FilamentViewer::removeEntity(EntityId asset)
{ {
Log("Removing asset from scene"); Log("Removing asset from scene");

View File

@@ -450,14 +450,14 @@ extern "C"
strcpy(outPtr, name.c_str()); strcpy(outPtr, name.c_str());
} }
FLUTTER_PLUGIN_EXPORT void remove_asset(const void *const viewer, EntityId asset) FLUTTER_PLUGIN_EXPORT void remove_entity(const void *const viewer, EntityId asset)
{ {
((FilamentViewer *)viewer)->removeAsset(asset); ((FilamentViewer *)viewer)->removeEntity(asset);
} }
FLUTTER_PLUGIN_EXPORT void clear_assets(const void *const viewer) FLUTTER_PLUGIN_EXPORT void clear_entities(const void *const viewer)
{ {
((FilamentViewer *)viewer)->clearAssets(); ((FilamentViewer *)viewer)->clearEntities();
} }
bool set_material_color(void *assetManager, EntityId asset, const char *meshName, int materialIndex, const float r, const float g, const float b, const float a) bool set_material_color(void *assetManager, EntityId asset, const char *meshName, int materialIndex, const float r, const float g, const float b, const float a)

View File

@@ -366,14 +366,14 @@ FLUTTER_PLUGIN_EXPORT void clear_lights_ffi(void *const viewer) {
fut.wait(); fut.wait();
} }
FLUTTER_PLUGIN_EXPORT void remove_asset_ffi(void *const viewer, FLUTTER_PLUGIN_EXPORT void remove_entity_ffi(void *const viewer,
EntityId asset) { EntityId asset) {
std::packaged_task<void()> lambda([&] { remove_asset(viewer, asset); }); std::packaged_task<void()> lambda([&] { remove_entity(viewer, asset); });
auto fut = _rl->add_task(lambda); auto fut = _rl->add_task(lambda);
fut.wait(); fut.wait();
} }
FLUTTER_PLUGIN_EXPORT void clear_assets_ffi(void *const viewer) { FLUTTER_PLUGIN_EXPORT void clear_entities_ffi(void *const viewer) {
std::packaged_task<void()> lambda([&] { clear_assets(viewer); }); std::packaged_task<void()> lambda([&] { clear_entities(viewer); });
auto fut = _rl->add_task(lambda); auto fut = _rl->add_task(lambda);
fut.wait(); fut.wait();
} }

54
ios/src/Untitled-1.cpp Normal file
View File

@@ -0,0 +1,54 @@
// we know there's been a collision, but we want to adjust the direction vector to continue movement in the non-colliding direction
// first, we need to find the AABB plane that we have collided with
auto vertices = targetBox.getCorners().vertices;
// each entry here is a plane from the target bounding box
// (we drop the fourth vertex because it's mathematically not necessary to define the plane)
std::vector<std::vector<filament::math::float3>> planes = {
{
vertices[0],vertices[2],vertices[4] // bottom
},
{
vertices[1],vertices[3],vertices[5] // top
},
{
vertices[0],vertices[1],vertices[4] // back
},
{
vertices[0],vertices[1],vertices[2] // left
},
{
vertices[4],vertices[5],vertices[6] // right
},
{
vertices[2],vertices[3],vertices[6] //front
},
};
// now, iterate over each plane and project the intersecting source vertex onto it
// the smallest value will be the closest plane
auto sourceVertex = sourceCorners.vertices[i];
int planeIndex = -1;
int minDist = 999999.0f;
filament::math::float3 projection;
for(int j = 0; j < 6; j++) {
// translate the plane so the intersecting source vertex is at the origin
auto plane = std::vector<filament::math::float3>{ planes[j][0] - sourceVertex, planes[j][1] - sourceVertex, planes[j][2] - sourceVertex };
// cross product of the two known co-planar vectors to find the normal
auto normal = normalize(cross(plane[1] - plane[0], plane[2] - plane[1]));
// project the normal onto the original (untranslated) plane vector
auto dist = dot(planes[j][0], normal) / norm(planes[j][0]);
Log("Dist : %f", dist);
if(dist < minDist) {
minDist = dist;
planeIndex = j;
}
}
Log("Collision with plane index %d", planeIndex);
auto sourceNormal = normalize(cross(planes[planeIndex][1] - planes[planeIndex][0], planes[planeIndex][2] - planes[planeIndex][1]));
projection = direction - (sourceNormal * dot(sourceNormal, direction));

View File

@@ -35,7 +35,7 @@ abstract class FilamentController {
Stream<FilamentEntity> get onLoad; Stream<FilamentEntity> get onLoad;
/// ///
/// A Stream containing every FilamentEntity removed from the scene (i.e. via [removeAsset], [clearAssets], [removeLight] or [clearLights]). /// A Stream containing every FilamentEntity removed from the scene (i.e. via [removeEntity], [clearEntities], [removeLight] or [clearLights]).
Stream<FilamentEntity> get onUnload; Stream<FilamentEntity> get onUnload;
@@ -294,13 +294,13 @@ abstract class FilamentController {
/// Removes/destroys the specified entity from the scene. /// Removes/destroys the specified entity from the scene.
/// [entity] will no longer be a valid handle after this method is called; ensure you immediately discard all references once this method is complete. /// [entity] will no longer be a valid handle after this method is called; ensure you immediately discard all references once this method is complete.
/// ///
Future removeAsset(FilamentEntity entity); Future removeEntity(FilamentEntity entity);
/// ///
/// Removes/destroys all renderable entities from the scene (including cameras). /// Removes/destroys all renderable entities from the scene (including cameras).
/// All [FilamentEntity] handles will no longer be valid after this method is called; ensure you immediately discard all references to all entities once this method is complete. /// All [FilamentEntity] handles will no longer be valid after this method is called; ensure you immediately discard all references to all entities once this method is complete.
/// ///
Future clearAssets(); Future clearEntities();
/// ///
/// Called by `FilamentGestureDetector`. You probably don't want to call this yourself. /// Called by `FilamentGestureDetector`. You probably don't want to call this yourself.

View File

@@ -817,21 +817,21 @@ class FilamentControllerFFI extends FilamentController {
} }
@override @override
Future removeAsset(FilamentEntity entity) async { Future removeEntity(FilamentEntity entity) async {
if (_viewer == null) { if (_viewer == null) {
throw Exception("No viewer available, ignoring"); throw Exception("No viewer available, ignoring");
} }
_entities.remove(entity); _entities.remove(entity);
remove_asset_ffi(_viewer!, entity); remove_entity_ffi(_viewer!, entity);
_onUnloadController.add(entity); _onUnloadController.add(entity);
} }
@override @override
Future clearAssets() async { Future clearEntities() async {
if (_viewer == null) { if (_viewer == null) {
throw Exception("No viewer available, ignoring"); throw Exception("No viewer available, ignoring");
} }
clear_assets_ffi(_viewer!); clear_entities_ffi(_viewer!);
for (final entity in _entities) { for (final entity in _entities) {
_onUnloadController.add(entity); _onUnloadController.add(entity);

View File

@@ -499,15 +499,15 @@ external int get_morph_target_name_count(
); );
@ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Void>, EntityId)>( @ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Void>, EntityId)>(
symbol: 'remove_asset', assetId: 'flutter_filament_plugin') symbol: 'remove_entity', assetId: 'flutter_filament_plugin')
external void remove_asset( external void remove_entity(
ffi.Pointer<ffi.Void> viewer, ffi.Pointer<ffi.Void> viewer,
int asset, int asset,
); );
@ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Void>)>( @ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Void>)>(
symbol: 'clear_assets', assetId: 'flutter_filament_plugin') symbol: 'clear_entities', assetId: 'flutter_filament_plugin')
external void clear_assets( external void clear_entities(
ffi.Pointer<ffi.Void> viewer, ffi.Pointer<ffi.Void> viewer,
); );
@@ -852,14 +852,15 @@ external void flutter_filament_free(
ffi.Void Function( ffi.Void Function(
ffi.Pointer<ffi.Void>, ffi.Pointer<ffi.Void>,
EntityId, EntityId,
ffi.Pointer< ffi.Pointer<ffi.NativeFunction<ffi.Void Function(EntityId entityId)>>,
ffi.NativeFunction<ffi.Void Function(EntityId entityId)>>)>( ffi.Bool)>(
symbol: 'add_collision_component', assetId: 'flutter_filament_plugin') symbol: 'add_collision_component', assetId: 'flutter_filament_plugin')
external void add_collision_component( external void add_collision_component(
ffi.Pointer<ffi.Void> assetManager, ffi.Pointer<ffi.Void> assetManager,
int entityId, int entityId,
ffi.Pointer<ffi.NativeFunction<ffi.Void Function(EntityId entityId)>> ffi.Pointer<ffi.NativeFunction<ffi.Void Function(EntityId entityId)>>
callback, callback,
bool affectsCollidingTransform,
); );
@ffi.Native< @ffi.Native<
@@ -875,6 +876,14 @@ external int create_geometry(
ffi.Pointer<ffi.Char> materialPath, ffi.Pointer<ffi.Char> materialPath,
); );
@ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Void>, EntityId, EntityId)>(
symbol: 'set_parent', assetId: 'flutter_filament_plugin')
external void set_parent(
ffi.Pointer<ffi.Void> assetManager,
int child,
int parent,
);
@ffi.Native< @ffi.Native<
ffi.Pointer<ffi.Void> Function( ffi.Pointer<ffi.Void> Function(
ffi.Pointer<ffi.Void>, ffi.Pointer<ffi.Void>,
@@ -1114,15 +1123,15 @@ external int load_gltf_ffi(
); );
@ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Void>, EntityId)>( @ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Void>, EntityId)>(
symbol: 'remove_asset_ffi', assetId: 'flutter_filament_plugin') symbol: 'remove_entity_ffi', assetId: 'flutter_filament_plugin')
external void remove_asset_ffi( external void remove_entity_ffi(
ffi.Pointer<ffi.Void> viewer, ffi.Pointer<ffi.Void> viewer,
int asset, int asset,
); );
@ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Void>)>( @ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Void>)>(
symbol: 'clear_assets_ffi', assetId: 'flutter_filament_plugin') symbol: 'clear_entities_ffi', assetId: 'flutter_filament_plugin')
external void clear_assets_ffi( external void clear_entities_ffi(
ffi.Pointer<ffi.Void> viewer, ffi.Pointer<ffi.Void> viewer,
); );