initial work to re-implement FFI with background thread render loop

This commit is contained in:
Nick Fisher
2023-09-29 13:54:04 +08:00
parent 1b49706eca
commit a6506e6346
39 changed files with 6819 additions and 53973 deletions

View File

@@ -17,11 +17,11 @@ open class ResourceBuffer: Structure(), Structure.ByValue {
}
}
interface LoadResourceFromOwner : Callback {
interface LoadFilamentResourceFromOwner : Callback {
fun loadResourceFromOwner(resourceName: String?, owner: Pointer?): ResourceBuffer
}
interface FreeResourceFromOwner : Callback {
interface FreeFilamentResourceFromOwner : Callback {
fun freeResourceFromOwner(rb: ResourceBuffer, owner: Pointer?)
}
interface FilamentInterop : Library {
@@ -29,7 +29,7 @@ interface FilamentInterop : Library {
fun create_filament_viewer(context:Pointer, loader:Pointer) : Pointer;
fun create_swap_chain(viewer: Pointer, window:Pointer?, width:Int, height:Int);
fun get_native_window_from_surface(surface:Object, env:JNIEnv) : Pointer;
fun delete_filament_viewer(viewer: Pointer)
fun destroy_filament_viewer(viewer: Pointer)
fun get_asset_manager(viewer: Pointer?): Pointer?
fun create_render_target(viewer: Pointer, texture_id: Int, width: Int, height: Int)
fun clear_background_image(viewer: Pointer)
@@ -89,6 +89,6 @@ fun set_camera_focus_distance(viewer: Pointer, focus_distance: Float)
fun hide_mesh(asset_manager: Pointer, asset: EntityId, mesh_name: String): Int
fun reveal_mesh(asset_manager: Pointer, asset: EntityId, mesh_name: String): Int
fun ios_dummy()
fun make_resource_loader(loadResourceFromOwner: LoadResourceFromOwner, freeResource: FreeResourceFromOwner, owner:Pointer?) : Pointer;
fun make_resource_loader(loadResourceFromOwner: LoadFilamentResourceFromOwner, freeResource: FreeFilamentResourceFromOwner, owner:Pointer?) : Pointer;
}

View File

@@ -32,7 +32,7 @@ import java.util.concurrent.Executors
typealias EntityId = Int
/** PolyvoxFilamentPlugin */
class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware, LoadResourceFromOwner, FreeResourceFromOwner {
class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware, LoadFilamentResourceFromOwner, FreeFilamentResourceFromOwner {
private val lock = Object()
@@ -221,7 +221,7 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware, Lo
"destroyViewer" -> {
if (_viewer != null) {
_lib.destroy_swap_chain(_viewer!!)
_lib.delete_filament_viewer(_viewer!!)
_lib.destroy_filament_viewer(_viewer!!)
_viewer = null
}
result.success(true)
@@ -249,7 +249,7 @@ class PolyvoxFilamentPlugin: FlutterPlugin, MethodCallHandler, ActivityAware, Lo
"createFilamentViewer" -> {
if (_viewer != null) {
_lib.destroy_swap_chain(_viewer!!)
_lib.delete_filament_viewer(_viewer!!)
_lib.destroy_filament_viewer(_viewer!!)
_viewer = null
}
val resourceLoader = _lib.make_resource_loader(this, this, Pointer(0))

View File

@@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:polyvox_filament/filament_controller.dart';
import 'package:polyvox_filament/animations/bone_animation_data.dart';
import 'package:polyvox_filament/filament_controller_ffi.dart';
import 'package:polyvox_filament/filament_gesture_detector.dart';
import 'package:polyvox_filament/filament_widget.dart';
import 'package:polyvox_filament/animations/animation_builder.dart';
@@ -36,7 +37,7 @@ class ExampleWidget extends StatefulWidget {
}
class _ExampleWidgetState extends State<ExampleWidget> {
final _filamentController = FilamentController();
final _filamentController = FilamentControllerFFI();
FilamentEntity? _cube;
FilamentEntity? _flightHelmet;
@@ -132,9 +133,6 @@ class _ExampleWidgetState extends State<ExampleWidget> {
_cube =
await _filamentController.loadGltf('assets/cube.gltf', 'assets');
}, 'load cube GLTF'),
_item(() async {
_filamentController.setTexture(_cube!, "assets/background.png");
}, 'swap cube texture'),
_item(() async {
_filamentController.transformToUnitCube(_cube!);
}, 'transform to unit cube'),

View File

@@ -204,7 +204,7 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
case "destroyViewer":
if(viewer != nil) {
destroy_swap_chain(viewer)
delete_filament_viewer(viewer)
destroy_filament_viewer(viewer)
viewer = nil
}
result(true)
@@ -227,7 +227,7 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
print("createFilamentViewer")
if(viewer != nil) {
destroy_swap_chain(viewer)
delete_filament_viewer(viewer)
destroy_filament_viewer(viewer)
viewer = nil
}
let callback = make_resource_loader(loadResource, freeResource, Unmanaged.passUnretained(self).toOpaque())

View File

@@ -81,6 +81,8 @@ namespace polyvox {
void setBackgroundImage(const char* resourcePath, bool fillHeight);
void clearBackgroundImage();
void setBackgroundImagePosition(float x, float y, bool clamp);
void setViewFrustumCulling(bool enabled);
void moveCameraToAsset(EntityId entityId);
void setCameraExposure(float aperture, float shutterSpeed, float sensitivity);

View File

@@ -6,8 +6,8 @@
typedef int32_t EntityId;
const void* create_filament_viewer(const void* const context, const ResourceLoaderWrapper* const loader);
ResourceLoaderWrapper* make_resource_loader(LoadResourceFromOwner loadFn, FreeResourceFromOwner freeFn, void* owner);
void delete_filament_viewer(const void* const viewer);
ResourceLoaderWrapper* make_resource_loader(LoadFilamentResourceFromOwner loadFn, FreeFilamentResourceFromOwner freeFn, void* owner);
void destroy_filament_viewer(const void* const viewer);
void* get_asset_manager(const void* const viewer);
void create_render_target(const void* const viewer, uint32_t textureId, uint32_t width, uint32_t height);
void clear_background_image(const void* const viewer);
@@ -26,6 +26,7 @@ void clear_lights(const void* const viewer);
EntityId load_glb(void *assetManager, const char *assetPath, bool unlit);
EntityId load_gltf(void *assetManager, const char *assetPath, const char *relativePath);
bool set_camera(const void* const viewer, EntityId asset, const char *nodeName);
void set_view_frustum_culling(const void* const viewer, bool enabled);
void render(const void* const viewer, uint64_t frameTimeInNanos);
void create_swap_chain(const void* const viewer, const void* const window, uint32_t width, uint32_t height);
void destroy_swap_chain(const void* const viewer);
@@ -82,8 +83,6 @@ void get_morph_target_name(void* assetManager, EntityId asset, const char *meshN
int get_morph_target_name_count(void* assetManager, EntityId asset, const char *meshName);
void remove_asset(const void* const viewer, EntityId asset);
void clear_assets(const void* const viewer);
void load_texture(void* assetManager, EntityId asset, const char *assetPath, int renderableIndex);
void set_texture(void* assetManager, EntityId asset);
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);
void transform_to_unit_cube(void* assetManager, EntityId asset);
void set_position(void* assetManager, EntityId asset, float x, float y, float z);

View File

@@ -0,0 +1,83 @@
#ifndef _POLYVOX_FILAMENT_FFI_API_H
#define _POLYVOX_FILAMENT_FFI_API_H
#include "PolyvoxFilamentApi.h"
///
/// This header replicates most of the methods in PolyvoxFilamentApi.h, and is only intended to be used to generate client FFI bindings.
/// The intention is that calling one of these methods will call its respective method in PolyvoxFilamentApi.h, but wrapped in some kind of thread runner to ensure thread safety.
///
typedef int32_t EntityId;
const void* create_filament_viewer_ffi(void* const context, const ResourceLoaderWrapper* const loader);
void destroy_filament_viewer_ffi(void* const viewer);
void render_ffi(void* const viewer);
void set_rendering_ffi(void* const viewer, bool rendering);
void set_frame_interval_ffi(float frameInterval);
void update_viewport_and_camera_projection_ffi(void* const viewer, const uint32_t width, const uint32_t height, const float scaleFactor);
void create_render_target_ffi(void* const viewer, uint32_t textureId, uint32_t width, uint32_t height);
void set_background_color_ffi(void* const viewer, const float r, const float g, const float b, const float a);
void clear_background_image_ffi(void* const viewer);
void set_background_image_ffi(void* const viewer, const char *path, bool fillHeight);
void set_background_image_position_ffi(void* const viewer, float x, float y, bool clamp);
void set_background_color_ffi(void* const viewer, const float r, const float g, const float b, const float a);
void set_tone_mapping_ffi(void* const viewer, int toneMapping);
void set_bloom_ffi(void* const viewer, float strength);
void load_skybox_ffi(void* const viewer, const char *skyboxPath);
void load_ibl_ffi(void* const viewer, const char *iblPath, float intensity);
void remove_skybox_ffi(void* const viewer);
void remove_ibl_ffi(void* const viewer);
EntityId add_light_ffi(void* const viewer, uint8_t type, float colour, float intensity, float posX, float posY, float posZ, float dirX, float dirY, float dirZ, bool shadows);
void remove_light_ffi(void* const viewer, EntityId entityId);
void clear_lights_ffi(void* const viewer);
EntityId load_glb_ffi(void* const assetManager, const char *assetPath, bool unlit);
EntityId load_gltf_ffi(void* const assetManager, const char *assetPath, const char *relativePath);
void remove_asset_ffi(const void* const viewer, EntityId asset);
void clear_assets_ffi(const void* const viewer);
bool set_camera_ffi(void* const viewer, EntityId asset, const char *nodeName);
void apply_weights_ffi(
void* assetManager,
EntityId asset,
const char *const entityName,
float *const weights,
int count
);
void set_morph_target_weights_ffi(
void* assetManager,
EntityId asset,
const char *const entityName,
const float *const morphData,
int numWeights
);
bool set_morph_animation_ffi(
void* assetManager,
EntityId asset,
const char *const entityName,
const float *const morphData,
const int* const morphIndices,
int numMorphTargets,
int numFrames,
float frameLengthInMs);
void set_bone_animation_ffi(
void* assetManager,
EntityId asset,
const float* const frameData,
int numFrames,
int numBones,
const char** const boneNames,
const char** const meshName,
int numMeshTargets,
float frameLengthInMs);
void play_animation_ffi(void* assetManager, EntityId asset, int index, bool loop, bool reverse, bool replaceActive, float crossfade);
void set_animation_frame_ffi(void* assetManager, EntityId asset, int animationIndex, int animationFrame);
void stop_animation_ffi(void* assetManager, EntityId asset, int index);
int get_animation_count_ffi(void* assetManager, EntityId asset);
void get_animation_name_ffi(void* assetManager, EntityId asset, char *const outPtr, int index);
float get_animation_duration_ffi(void* assetManager, EntityId asset, int index);
void get_morph_target_name_ffi(void* assetManager, EntityId asset, const char *meshName, char *const outPtr, int index);
int get_morph_target_name_count_ffi(void* assetManager, EntityId asset, const char *meshName);
#endif // _POLYVOX_FILAMENT_FFI_API_H

View File

@@ -3,7 +3,6 @@
#include <stdint.h>
#if defined(__cplusplus)
#include "Log.hpp"
extern "C" {
#endif
//
@@ -28,6 +27,10 @@ extern "C" {
struct ResourceBuffer {
#if defined(__cplusplus)
ResourceBuffer(const void* const data, const uint32_t size, const uint32_t id) : data(data), size(size), id(id) {};
ResourceBuffer(const ResourceBuffer& rb) : data(rb.data), size(rb.size), id(rb.id) { };
ResourceBuffer(const ResourceBuffer&& rb) : data(rb.data), size(rb.size), id(rb.id) { };
ResourceBuffer& operator=(const ResourceBuffer& other) = delete;
#endif
const void * const data;
const uint32_t size;
@@ -35,41 +38,41 @@ extern "C" {
};
typedef struct ResourceBuffer ResourceBuffer;
typedef ResourceBuffer (*LoadResource)(const char* uri);
typedef ResourceBuffer (*LoadResourceFromOwner)(const char* const, void* const owner);
typedef void (*FreeResource)(ResourceBuffer);
typedef void (*FreeResourceFromOwner)(ResourceBuffer, void* const owner);
typedef ResourceBuffer (*LoadFilamentResource)(const char* uri);
typedef ResourceBuffer (*LoadFilamentResourceFromOwner)(const char* const, void* const owner);
typedef void (*FreeFilamentResource)(ResourceBuffer);
typedef void (*FreeFilamentResourceFromOwner)(ResourceBuffer, void* const owner);
// this may be compiled as either C or C++, depending on which compiler is being invoked (e.g. binding to Swift will compile as C).
// the former does not allow default initialization to be specified inline), so we need to explicitly set the unused members to nullptr
struct ResourceLoaderWrapper {
#if defined(__cplusplus)
ResourceLoaderWrapper(LoadResource loader, FreeResource freeResource) : mLoadResource(loader), mFreeResource(freeResource), mLoadResourceFromOwner(nullptr), mFreeResourceFromOwner(nullptr),
ResourceLoaderWrapper(LoadFilamentResource loader, FreeFilamentResource freeResource) : mLoadFilamentResource(loader), mFreeFilamentResource(freeResource), mLoadFilamentResourceFromOwner(nullptr), mFreeFilamentResourceFromOwner(nullptr),
mOwner(nullptr) {}
ResourceLoaderWrapper(LoadResourceFromOwner loader, FreeResourceFromOwner freeResource, void* const owner) : mLoadResource(nullptr), mFreeResource(nullptr), mLoadResourceFromOwner(loader), mFreeResourceFromOwner(freeResource), mOwner(owner) {
ResourceLoaderWrapper(LoadFilamentResourceFromOwner loader, FreeFilamentResourceFromOwner freeResource, void* const owner) : mLoadFilamentResource(nullptr), mFreeFilamentResource(nullptr), mLoadFilamentResourceFromOwner(loader), mFreeFilamentResourceFromOwner(freeResource), mOwner(owner) {
};
ResourceBuffer load(const char* uri) const {
if(mLoadResourceFromOwner) {
return mLoadResourceFromOwner(uri, mOwner);
if(mLoadFilamentResourceFromOwner) {
return mLoadFilamentResourceFromOwner(uri, mOwner);
}
return mLoadResource(uri);
return mLoadFilamentResource(uri);
}
void free(ResourceBuffer rb) const {
if(mFreeResourceFromOwner) {
mFreeResourceFromOwner(rb, mOwner);
if(mFreeFilamentResourceFromOwner) {
mFreeFilamentResourceFromOwner(rb, mOwner);
} else {
mFreeResource(rb);
mFreeFilamentResource(rb);
}
}
#endif
LoadResource mLoadResource;
FreeResource mFreeResource;
LoadResourceFromOwner mLoadResourceFromOwner;
FreeResourceFromOwner mFreeResourceFromOwner;
LoadFilamentResource mLoadFilamentResource;
FreeFilamentResource mFreeFilamentResource;
LoadFilamentResourceFromOwner mLoadFilamentResourceFromOwner;
FreeFilamentResourceFromOwner mFreeFilamentResourceFromOwner;
void* mOwner;
};
typedef struct ResourceLoaderWrapper ResourceLoaderWrapper;

View File

@@ -54,12 +54,12 @@ _scene(scene) {
_gltfResourceLoader = new ResourceLoader({.engine = _engine,
.normalizeSkinningWeights = true });
auto uberdata = resourceLoaderWrapper->load("packages/polyvox_filament/assets/materials_ios_arm64.uberz");
// auto uberdata = resourceLoaderWrapper->load("packages/polyvox_filament/assets/materials_ios_arm64.uberz");
_ubershaderProvider = gltfio::createUbershaderProvider(
_engine, uberdata.data, uberdata.size);
// _ubershaderProvider = gltfio::createUbershaderProvider(
// _engine, UBERARCHIVE_DEFAULT_DATA, UBERARCHIVE_DEFAULT_SIZE);
// _engine, uberdata.data, uberdata.size);
_ubershaderProvider = gltfio::createUbershaderProvider(
_engine, UBERARCHIVE_DEFAULT_DATA, UBERARCHIVE_DEFAULT_SIZE);
// _ubershaderProvider = gltfio::createJitShaderProvider(_engine, true);
EntityManager &em = EntityManager::get();

View File

@@ -870,9 +870,12 @@ void FilamentViewer::setCameraPosition(float x, float y, float z) {
cam.setModelMatrix(_cameraPosition * _cameraRotation);
}
void FilamentViewer::moveCameraToAsset(EntityId entityId) {
void FilamentViewer::setViewFrustumCulling(bool enabled) {
_view->setFrustumCullingEnabled(enabled);
}
auto asset = _assetManager->getAssetByEntityId(entityId);
void FilamentViewer::moveCameraToAsset(EntityId entityId) {
auto asset = _assetManager->getAssetByEntityId(entityId);
if(!asset) {
Log("Failed to find asset attached to specified entity id.");
return;

View File

@@ -17,10 +17,10 @@ extern "C" {
#include "PolyvoxFilamentApi.h"
FLUTTER_PLUGIN_EXPORT const void* create_filament_viewer(const void* context, const ResourceLoaderWrapper* const loader) {
return (void*) new FilamentViewer(context, loader);
return (const void*) new FilamentViewer(context, loader);
}
FLUTTER_PLUGIN_EXPORT ResourceLoaderWrapper* make_resource_loader(LoadResourceFromOwner loadFn, FreeResourceFromOwner freeFn, void* const owner) {
FLUTTER_PLUGIN_EXPORT ResourceLoaderWrapper* make_resource_loader(LoadFilamentResourceFromOwner loadFn, FreeFilamentResourceFromOwner freeFn, void* const owner) {
return new ResourceLoaderWrapper(loadFn, freeFn, owner);
}
@@ -28,12 +28,12 @@ extern "C" {
((FilamentViewer*)viewer)->createRenderTarget(textureId, width, height);
}
FLUTTER_PLUGIN_EXPORT void delete_filament_viewer(const void* const viewer) {
FLUTTER_PLUGIN_EXPORT void destroy_filament_viewer(const void* const viewer) {
delete((FilamentViewer*)viewer);
}
FLUTTER_PLUGIN_EXPORT void set_background_color(const void* const viewer, const float r, const float g, const float b, const float a) {
((FilamentViewer*)viewer)->setBackgroundColor(r, g, b, a);
((FilamentViewer*)viewer)->setBackgroundColor(r, g, b, a);
}
FLUTTER_PLUGIN_EXPORT void clear_background_image(const void* const viewer) {
@@ -97,6 +97,10 @@ extern "C" {
return ((FilamentViewer*)viewer)->setCamera(asset, nodeName);
}
FLUTTER_PLUGIN_EXPORT void set_view_frustum_culling(const void* const viewer, bool enabled) {
((FilamentViewer*)viewer)->setViewFrustumCulling(enabled);
}
FLUTTER_PLUGIN_EXPORT void move_camera_to_asset(const void* const viewer, EntityId asset) {
((FilamentViewer*)viewer)->moveCameraToAsset(asset);
}
@@ -344,14 +348,6 @@ extern "C" {
((FilamentViewer*)viewer)->clearAssets();
}
FLUTTER_PLUGIN_EXPORT void load_texture(void* assetManager, EntityId asset, const char* assetPath, int renderableIndex) {
// ((AssetManager*)assetManager)->loadTexture(assetPath, renderableIndex);
}
FLUTTER_PLUGIN_EXPORT void set_texture(void* assetManager, EntityId asset) {
// ((AssetManager*)assetManager)->setTexture();
}
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) {
return ((AssetManager*)assetManager)->setMaterialColor(asset, meshName, materialIndex, r, g, b, a);
}
@@ -366,7 +362,7 @@ extern "C" {
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);
}
}
FLUTTER_PLUGIN_EXPORT void set_scale(void* assetManager, EntityId asset, float scale) {
((AssetManager*)assetManager)->setScale(asset, scale);

View File

@@ -0,0 +1,119 @@
#include "ResourceBuffer.hpp"
#include "FilamentViewer.hpp"
#include "filament/LightManager.h"
#include "Log.hpp"
#include "ThreadPool.hpp"
#include <thread>
#include <functional>
using namespace polyvox;
#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default")))
class RenderLoop {
public:
explicit RenderLoop() {
_t = new std::thread([this]() {
while(!_stop) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(_access);
if(_tasks.empty()) {
_cond.wait_for(lock, std::chrono::duration<int, std::milli>(5));
continue;
}
task = std::move(_tasks.front());
_tasks.pop_front();
std::this_thread::sleep_for(
std::chrono::milliseconds(_frameIntervalInMilliseconds));
}
task();
}
});
}
~RenderLoop() {
_stop = true;
_t->join();
}
void setRendering(bool rendering) {
_rendering = rendering;
}
template<class Rt>
auto add_task(std::packaged_task<Rt()>& pt) -> std::future<Rt> {
std::unique_lock<std::mutex> lock(_access);
auto ret = pt.get_future();
_tasks.push_back([pt=std::make_shared<std::packaged_task<Rt()>>(std::move(pt))]{ (*pt)();});
_cond.notify_one();
return ret;
}
private:
bool _stop = false;
bool _rendering = false;
int _frameIntervalInMilliseconds = 1000 / 60;
std::mutex _access;
FilamentViewer* _viewer = nullptr;
std::thread* _t = nullptr;
std::condition_variable _cond;
std::deque<std::function<void()>> _tasks;
};
extern "C" {
#include "PolyvoxFilamentApi.h"
static RenderLoop* _rl;
FLUTTER_PLUGIN_EXPORT const void* create_filament_viewer_ffi(const void* context, const ResourceLoaderWrapper* const loader) {
if(!_rl) {
_rl = new RenderLoop();
}
std::packaged_task<const void*()> lambda([&]() mutable {
return (const void*) new FilamentViewer(context, loader);
});
auto fut = _rl->add_task(lambda);
fut.wait();
return fut.get();
}
FLUTTER_PLUGIN_EXPORT void update_viewport_and_camera_projection_ffi(void* const viewer, const uint32_t width, const uint32_t height, const float scaleFactor);
std::packaged_task<void()> lambda([&]() mutable {
update_viewport_and_camera_projection(viewer, width, height, scaleFactor);
});
auto fut = _rl->add_task(lambda);
fut.wait();
}
FLUTTER_PLUGIN_EXPORT bool set_rendering(bool rendering) {
if(!_rl) {
return false;
}
_rl->setRendering(rendering);
return true;
}
FLUTTER_PLUGIN_EXPORT void render_ffi(void* const viewer) {
std::packaged_task<void()> lambda([&]() mutable {
render(viewer, 0);
});
auto fut = _rl->add_task(lambda);
fut.wait();
}
FLUTTER_PLUGIN_EXPORT void set_background_color_ffi(const void* const viewer, const float r, const float g, const float b, const float a) {
std::packaged_task<void()> lambda([&]() mutable {
set_background_color(viewer, r, g,b, a);
});
auto fut = _rl->add_task(lambda);
fut.wait();
}
}

View File

@@ -63,7 +63,7 @@ class PolyvoxFilamentPluginWeb {
case "createFilamentViewer":
// if(viewer != nil) {
// destroy_swap_chain(viewer)
// delete_filament_viewer(viewer)
// destroy_filament_viewer(viewer)
// viewer = nil
// }
// let callback = make_resource_loader(loadResource, freeResource, Unmanaged.passUnretained(self).toOpaque())

View File

@@ -1,5 +1,5 @@
import 'package:polyvox_filament/animations/morph_animation_data.dart';
import 'package:polyvox_filament/filament_controller.dart';
import 'package:polyvox_filament/filament_controller_method_channel.dart';
import 'package:flutter/foundation.dart';
import 'package:vector_math/vector_math.dart';

View File

@@ -1,11 +1,10 @@
//
// Frame weights for the morph targets specified in [morphNames] attached to mesh [meshName].
// morphData is laid out as numFrames x numMorphTargets
// where the weights are in the same order as [morphNames].
// [morphNames] must be provided but is not used directly; this is only used to check that the eventual asset being animated contains the same morph targets in the same order.
// A wrapper for morph target animation data.
// [data] is laid out as numFrames x numMorphTargets (where each morph target is ordered according to [animatedMorphNames]).
// [data] frame data for the morph weights used to animate the morph targets [animatedMorphNames] in mesh [meshName].
// the morph targets specified in [morphNames] attached to mesh [meshName].
// [animatedMorphNames] must be provided but is not used directly; this is only used to check that the eventual asset being animated contains the same morph targets in the same order.
//
import 'dart:typed_data';
class MorphAnimationData {
final String meshName;
final List<String> animatedMorphNames;

View File

@@ -1,187 +1,50 @@
import 'dart:async';
import 'dart:ffi';
import 'dart:io';
import 'dart:ui' as ui;
import 'package:flutter/services.dart';
import 'package:polyvox_filament/animations/bone_animation_data.dart';
import 'package:polyvox_filament/animations/morph_animation_data.dart';
import 'package:polyvox_filament/generated_bindings_web.dart';
import 'package:polyvox_filament/generated_bindings.dart';
typedef AssetManager = int;
typedef FilamentEntity = int;
const FilamentEntity FILAMENT_ASSET_ERROR = 0;
enum ToneMapper { ACES, FILMIC, LINEAR }
class FilamentController {
late MethodChannel _channel = MethodChannel("app.polyvox.filament/event");
double _pixelRatio = 1.0;
ui.Size size = ui.Size.zero;
int? _textureId;
final _textureIdController = StreamController<int?>.broadcast();
Stream<int?> get textureId => _textureIdController.stream;
Completer _isReadyForScene = Completer();
Future get isReadyForScene => _isReadyForScene.future;
late AssetManager _assetManager;
int? _viewer;
abstract class FilamentController {
Stream<int?> get textureId;
Future get isReadyForScene;
Future setRendering(bool render);
Future render();
Future setFrameRate(int framerate);
void setPixelRatio(double ratio);
Future destroyViewer();
Future destroyTexture();
///
/// This controller uses platform channels to bridge Dart with the C/C++ code for the Filament API.
/// Setting up the context/texture (since this is platform-specific) and the render ticker are platform-specific; all other methods are passed through by the platform channel to the methods specified in PolyvoxFilamentApi.h.
///
FilamentController() {
_channel.setMethodCallHandler((call) async {
throw Exception("Unknown method channel invocation ${call.method}");
});
}
Future setRendering(bool render) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
return _channel.invokeMethod("setRendering", render);
}
Future render() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("render");
}
Future setFrameRate(int framerate) async {
await _channel.invokeMethod("setFrameInterval", 1.0 / framerate);
}
void setPixelRatio(double ratio) {
_pixelRatio = ratio;
}
Future destroyViewer() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_viewer = null;
await _channel.invokeMethod("destroyViewer");
_isReadyForScene = Completer();
}
Future destroyTexture() async {
await _channel.invokeMethod("destroyTexture");
_textureId = null;
_assetManager = 0;
_textureIdController.add(null);
}
///
/// The process for creating/initializing the Filament layer is as follows:
/// You can insert a Filament viewport into the Flutter rendering hierarchy as follows:
/// 1) Create a FilamentController
/// 2) Insert a FilamentWidget into the rendering tree
/// 3) Initially, this widget will only contain an empty Container. After the first frame is rendered, the widget itself will automatically call [createViewer] with the width/height from its constraints
/// 4) The FilamentWidget will replace the empty Container with the Texture widget.
/// 2) Insert a FilamentWidget into the rendering tree, passing this instance of FilamentController
/// 3) Initially, the FilamentWidget will only contain an empty Container (by default, with a solid red background).
/// This widget will render a single frame to get its actual size, then will itself call [createViewer]. You do not need to call [createViewer] yourself.
/// This will dispatch a request to the native platform to create a hardware texture (Metal on iOS, OpenGL on Linux, GLES on Android and Windows) and a FilamentViewer (the main interface for manipulating the 3D scene) .
/// 4) The FilamentController will notify FilamentWidget that a texture is available
/// 5) The FilamentWidget will replace the empty Container with a Texture widget
/// If you need to wait until a FilamentViewer has been created, [await] the [isReadyForScene] Future.
///
Future createViewer(int width, int height) async {
if (_viewer != null) {
throw Exception(
"Viewer already exists, make sure you call destroyViewer first");
}
if (_isReadyForScene.isCompleted) {
throw Exception(
"Do not call createViewer when a viewer has already been created without calling destroyViewer");
}
size = ui.Size(width * _pixelRatio, height * _pixelRatio);
Future createViewer(int width, int height);
Future resize(int width, int height, {double contentScaleFactor = 1.0});
_textureId =
await _channel.invokeMethod("createTexture", [size.width, size.height]);
Future clearBackgroundImage();
Future setBackgroundImage(String path, {bool fillHeight = false});
_viewer = await _channel
.invokeMethod("createFilamentViewer", [size.width, size.height]);
await _channel.invokeMethod("updateViewportAndCameraProjection",
[size.width.toInt(), size.height.toInt(), 1.0]);
_assetManager = await _channel.invokeMethod("getAssetManager");
_textureIdController.add(_textureId);
_isReadyForScene.complete(true);
}
bool _resizing = false;
Future resize(int width, int height,
{double contentScaleFactor = 1.0}) async {
_resizing = true;
_textureId = await _channel.invokeMethod("resize",
[width * _pixelRatio, height * _pixelRatio, contentScaleFactor]);
_textureIdController.add(_textureId);
_resizing = false;
}
Future clearBackgroundImage() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("clearBackgroundImage");
}
Future setBackgroundImage(String path, {bool fillHeight = false}) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setBackgroundImage", [path, fillHeight]);
}
Future setBackgroundColor(Color color) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setBackgroundColor", [
color.red.toDouble() / 255.0,
color.green.toDouble() / 255.0,
color.blue.toDouble() / 255.0,
color.alpha.toDouble() / 255.0
]);
}
Future setBackgroundImagePosition(double x, double y,
{bool clamp = false}) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel
.invokeMethod("setBackgroundImagePosition", [x, y, clamp ? 1 : 0]);
}
Future loadSkybox(String skyboxPath) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("loadSkybox", skyboxPath);
}
Future loadIbl(String lightingPath, {double intensity = 30000}) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("loadIbl", [lightingPath, intensity]);
}
Future removeSkybox() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("removeSkybox");
}
Future removeIbl() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("removeIbl");
}
Future setBackgroundColor(Color color);
Future setBackgroundImagePosition(double x, double y, {bool clamp = false});
Future loadSkybox(String skyboxPath);
Future loadIbl(String lightingPath, {double intensity = 30000});
Future removeSkybox();
Future removeIbl();
// copied from LightManager.h
// enum class Type : uint8_t {
@@ -201,169 +64,45 @@ class FilamentController {
double dirX,
double dirY,
double dirZ,
bool castShadows) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var entity = await _channel.invokeMethod("addLight", [
type,
colour,
intensity,
posX,
posY,
posZ,
dirX,
dirY,
dirZ,
castShadows ? 1 : 0
]);
return entity as FilamentEntity;
}
bool castShadows);
Future removeLight(FilamentEntity light);
Future removeLight(FilamentEntity light) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("removeLight", light);
}
Future clearLights();
Future clearLights() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("clearLights");
}
Future<FilamentEntity> loadGlb(String path, {bool unlit = false});
Future<FilamentEntity> loadGlb(String path, {bool unlit = false}) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var asset =
await _channel.invokeMethod("loadGlb", [_assetManager, path, unlit]);
if (asset == FILAMENT_ASSET_ERROR) {
throw Exception("An error occurred loading the asset at $path");
}
return asset;
}
Future<FilamentEntity> loadGltf(String path, String relativeResourcePath);
Future<FilamentEntity> loadGltf(
String path, String relativeResourcePath) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var entity = await _channel
.invokeMethod("loadGltf", [_assetManager, path, relativeResourcePath]);
return entity as FilamentEntity;
}
Future panStart(double x, double y);
Future panUpdate(double x, double y);
Future panEnd();
Future panStart(double x, double y) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel
.invokeMethod("grabBegin", [x * _pixelRatio, y * _pixelRatio, 1]);
}
Future rotateStart(double x, double y);
Future panUpdate(double x, double y) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel
.invokeMethod("grabUpdate", [x * _pixelRatio, y * _pixelRatio]);
}
Future rotateUpdate(double x, double y);
Future panEnd() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("grabEnd");
}
Future rotateStart(double x, double y) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel
.invokeMethod("grabBegin", [x * _pixelRatio, y * _pixelRatio, 0]);
}
Future rotateUpdate(double x, double y) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel
.invokeMethod("grabUpdate", [x * _pixelRatio, y * _pixelRatio]);
}
Future rotateEnd() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("grabEnd");
}
Future rotateEnd();
Future setMorphTargetWeights(
FilamentEntity asset, String meshName, List<double> weights) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setMorphTargetWeights",
[_assetManager, asset, meshName, weights, weights.length]);
}
FilamentEntity asset, String meshName, List<double> weights);
Future<List<String>> getMorphTargetNames(
FilamentEntity asset, String meshName) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var names = await _channel
.invokeMethod("getMorphTargetNames", [_assetManager, asset, meshName]);
return names.cast<String>();
}
FilamentEntity asset, String meshName);
Future<List<String>> getAnimationNames(FilamentEntity asset) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var names = await _channel
.invokeMethod("getAnimationNames", [_assetManager, asset]);
return names.cast<String>();
}
Future<List<String>> getAnimationNames(FilamentEntity asset);
///
/// Returns the length (in seconds) of the animation at the given index.
///
Future<double> getAnimationDuration(
FilamentEntity asset, int animationIndex) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var duration = await _channel.invokeMethod(
"getAnimationDuration", [_assetManager, asset, animationIndex]);
return duration as double;
}
Future<double> getAnimationDuration(FilamentEntity asset, int animationIndex);
///
/// Animates morph target weights/bone transforms (where each frame requires a duration of [frameLengthInMs].
/// [morphWeights] is a list of doubles in frame-major format.
/// Each frame is [numWeights] in length, and each entry is the weight to be applied to the morph target located at that index in the mesh primitive at that frame.
///
Future setMorphAnimationData(
FilamentEntity asset, MorphAnimationData animation) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setMorphAnimation", [
_assetManager,
asset,
animation.meshName,
animation.data,
animation.animatedMorphIndices,
animation.numMorphTargets,
animation.numFrames,
animation.frameLengthInMs
]);
}
void setMorphAnimationData(
FilamentEntity asset, MorphAnimationData animation);
///
/// Animates morph target weights/bone transforms (where each frame requires a duration of [frameLengthInMs].
@@ -371,272 +110,39 @@ class FilamentController {
/// Each frame is [numWeights] in length, and each entry is the weight to be applied to the morph target located at that index in the mesh primitive at that frame.
/// for now we only allow animating a single bone (though multiple skinned targets are supported)
///
Future setBoneAnimation(
FilamentEntity asset, BoneAnimationData animation) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
// var data = calloc<Float>(animation.frameData.length);
// int offset = 0;
// var numFrames = animation.frameData.length ~/ 7;
// var boneNames = calloc<Pointer<Char>>(1);
// boneNames.elementAt(0).value =
// animation.boneName.toNativeUtf8().cast<Char>();
// var meshNames = calloc<Pointer<Char>>(animation.meshNames.length);
// for (int i = 0; i < animation.meshNames.length; i++) {
// meshNames.elementAt(i).value =
// animation.meshNames[i].toNativeUtf8().cast<Char>();
// }
// for (int i = 0; i < animation.frameData.length; i++) {
// data.elementAt(offset).value = animation.frameData[i];
// offset += 1;
// }
// await _channel.invokeMethod("setBoneAnimation", [
// _assetManager,
// asset,
// data,
// numFrames,
// 1,
// boneNames,
// meshNames,
// animation.meshNames.length,
// animation.frameLengthInMs
// ]);
// calloc.free(data);
}
Future removeAsset(FilamentEntity asset) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("removeAsset", asset);
}
Future clearAssets() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("clearAssets");
}
Future zoomBegin() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("scrollBegin");
}
Future zoomUpdate(double z) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("scrollUpdate", [0.0, 0.0, z]);
}
Future zoomEnd() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("scrollEnd");
}
Future playAnimation(FilamentEntity asset, int index,
void setBoneAnimation(FilamentEntity asset, BoneAnimationData animation);
void removeAsset(FilamentEntity asset);
void clearAssets();
void zoomBegin();
void zoomUpdate(double z);
void zoomEnd();
void playAnimation(FilamentEntity asset, int index,
{bool loop = false,
bool reverse = false,
bool replaceActive = true,
double crossfade = 0.0}) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("playAnimation",
[_assetManager, asset, index, loop, reverse, replaceActive, crossfade]);
}
double crossfade = 0.0});
void setAnimationFrame(FilamentEntity asset, int index, int animationFrame);
void stopAnimation(FilamentEntity asset, int animationIndex);
void setCamera(FilamentEntity asset, String? name);
void setToneMapping(ToneMapper mapper);
void setBloom(double bloom);
void setCameraFocalLength(double focalLength);
void setCameraFocusDistance(double focusDistance);
void setCameraPosition(double x, double y, double z);
void moveCameraToAsset(FilamentEntity asset);
void setViewFrustumCulling(bool enabled);
void setCameraExposure(
double aperture, double shutterSpeed, double sensitivity);
void setCameraRotation(double rads, double x, double y, double z);
void setCameraModelMatrix(List<double> matrix);
Future setAnimationFrame(
FilamentEntity asset, int index, int animationFrame) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod(
"setAnimationFrame", [_assetManager, asset, index, animationFrame]);
}
Future stopAnimation(FilamentEntity asset, int animationIndex) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel
.invokeMethod("stopAnimation", [_assetManager, asset, animationIndex]);
}
Future setCamera(FilamentEntity asset, String? name) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
if (await _channel.invokeMethod("setCamera", [asset, name]) != true) {
throw Exception("Failed to set camera");
}
}
Future setToneMapping(ToneMapper mapper) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
if (!await _channel.invokeMethod("setToneMapping", mapper.index)) {
throw Exception("Failed to set tone mapper");
}
}
Future setBloom(double bloom) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
if (!await _channel.invokeMethod("setBloom", bloom)) {
throw Exception("Failed to set bloom");
}
}
Future setCameraFocalLength(double focalLength) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setCameraFocalLength", focalLength);
}
Future setCameraFocusDistance(double focusDistance) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setCameraFocusDistance", focusDistance);
}
Future setCameraPosition(double x, double y, double z) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setCameraPosition", [x, y, z]);
}
Future moveCameraToAsset(FilamentEntity asset) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("moveCameraToAsset", asset);
}
Future setViewFrustumCulling(bool enabled) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setViewFrustumCulling", enabled);
}
Future setCameraExposure(
double aperture, double shutterSpeed, double sensitivity) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod(
"setCameraExposure", [aperture, shutterSpeed, sensitivity]);
}
Future setCameraRotation(double rads, double x, double y, double z) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setCameraRotation", [rads, x, y, z]);
}
Future setCameraModelMatrix(List<double> matrix) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
assert(matrix.length == 16);
await _channel.invokeMethod("setCameraModelMatrix", matrix);
}
Future setTexture(FilamentEntity asset, String assetPath,
{int renderableIndex = 0}) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setTexture", [_assetManager, asset]);
}
Future setMaterialColor(FilamentEntity asset, String meshName,
int materialIndex, Color color) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var result = await _channel.invokeMethod("setMaterialColor", [
_assetManager,
asset,
meshName,
materialIndex,
[
color.red.toDouble() / 255.0,
color.green.toDouble() / 255.0,
color.blue.toDouble() / 255.0,
color.alpha.toDouble() / 255.0
]
]);
if (!result) {
throw Exception("Failed to set material color");
}
}
Future transformToUnitCube(FilamentEntity asset) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("transformToUnitCube", [_assetManager, asset]);
}
Future setPosition(FilamentEntity asset, double x, double y, double z) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setPosition", [_assetManager, asset, x, y, z]);
}
Future setScale(FilamentEntity asset, double scale) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setScale", [_assetManager, asset, scale]);
}
Future setRotation(
FilamentEntity asset, double rads, double x, double y, double z) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel
.invokeMethod("setRotation", [_assetManager, asset, rads, x, y, z]);
}
Future hide(FilamentEntity asset, String meshName) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
if (await _channel
.invokeMethod("hideMesh", [_assetManager, asset, meshName]) !=
1) {
throw Exception("Failed to hide mesh $meshName");
}
}
Future reveal(FilamentEntity asset, String meshName) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
if (await _channel
.invokeMethod("revealMesh", [_assetManager, asset, meshName]) !=
1) {
throw Exception("Failed to reveal mesh $meshName");
}
}
void setMaterialColor(
FilamentEntity asset, String meshName, int materialIndex, Color color);
void transformToUnitCube(FilamentEntity asset);
void setPosition(FilamentEntity asset, double x, double y, double z);
void setScale(FilamentEntity asset, double scale);
void setRotation(
FilamentEntity asset, double rads, double x, double y, double z);
void hide(FilamentEntity asset, String meshName);
void reveal(FilamentEntity asset, String meshName);
}

View File

@@ -0,0 +1,654 @@
import 'dart:async';
import 'dart:ffi';
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/services.dart';
import 'package:ffi/ffi.dart';
import 'package:polyvox_filament/filament_controller.dart';
import 'package:polyvox_filament/animations/bone_animation_data.dart';
import 'package:polyvox_filament/animations/morph_animation_data.dart';
import 'package:polyvox_filament/generated_bindings.dart';
class FilamentControllerFFI extends FilamentController {
late MethodChannel _channel = MethodChannel("app.polyvox.filament/event");
double _pixelRatio = 1.0;
ui.Size size = ui.Size.zero;
int? _textureId;
final _textureIdController = StreamController<int?>.broadcast();
Stream<int?> get textureId => _textureIdController.stream;
Completer _isReadyForScene = Completer();
Future get isReadyForScene => _isReadyForScene.future;
late Pointer<Void>? _assetManager;
late NativeLibrary _lib;
Pointer<Void>? _viewer;
bool _resizing = false;
///
/// This controller uses platform channels to bridge Dart with the C/C++ code for the Filament API.
/// Setting up the context/texture (since this is platform-specific) and the render ticker are platform-specific; all other methods are passed through by the platform channel to the methods specified in PolyvoxFilamentApi.h.
///
FilamentControllerFFI() {
_channel.setMethodCallHandler((call) async {
throw Exception("Unknown method channel invocation ${call.method}");
});
_lib = NativeLibrary(Platform.isIOS || Platform.isMacOS
? DynamicLibrary.process()
: DynamicLibrary.open("libpolyvox_filament.so"));
}
Future setRendering(bool render) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.set_rendering_ffi(_viewer!, render ? 1 : 0);
}
Future render() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.render_ffi(_viewer!);
}
Future setFrameRate(int framerate) async {
await _channel.invokeMethod("setFrameInterval", 1.0 / framerate);
}
void setPixelRatio(double ratio) {
_pixelRatio = ratio;
}
Future destroyViewer() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_viewer = null;
_assetManager = null;
_lib.destroy_filament_viewer_ffi(_viewer!);
_isReadyForScene = Completer();
}
Future destroyTexture() async {
await _channel.invokeMethod("destroyTexture");
_textureId = null;
_textureIdController.add(null);
}
///
/// You can insert a Filament viewport into the Flutter rendering hierarchy as follows:
/// 1) Create a FilamentController
/// 2) Insert a FilamentWidget into the rendering tree, passing this instance of FilamentController
/// 3) Initially, the FilamentWidget will only contain an empty Container (by default, with a solid red background).
/// This widget will render a single frame to get its actual size, then will itself call [createViewer]. You do not need to call [createViewer] yourself.
/// 4) The FilamentController
/// 4) The FilamentWidget will replace the empty Container with a Texture widget
/// If you need to wait
///
Future createViewer(int width, int height) async {
if (_viewer != null) {
throw Exception(
"Viewer already exists, make sure you call destroyViewer first");
}
if (_isReadyForScene.isCompleted) {
throw Exception(
"Do not call createViewer when a viewer has already been created without calling destroyViewer");
}
size = ui.Size(width * _pixelRatio, height * _pixelRatio);
_textureId =
await _channel.invokeMethod("createTexture", [size.width, size.height]);
var sharedContext = await _channel.invokeMethod("getSharedContext");
var loader = await _channel.invokeMethod("getResourceLoaderWrapper");
_viewer = _lib.create_filament_viewer_ffi(
Pointer<Void>.fromAddress(sharedContext ?? 0),
Pointer<ResourceLoaderWrapper>.fromAddress(loader));
_lib.update_viewport_and_camera_projection_ffi(
_viewer!, width, height, 1.0);
_assetManager = _lib.get_asset_manager(_viewer!);
_textureIdController.add(_textureId);
_isReadyForScene.complete(true);
}
Future resize(int width, int height,
{double contentScaleFactor = 1.0}) async {
_resizing = true;
_textureId = await _channel.invokeMethod("resize",
[width * _pixelRatio, height * _pixelRatio, contentScaleFactor]);
_textureIdController.add(_textureId);
_resizing = false;
}
Future clearBackgroundImage() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.clear_background_image_ffi(_viewer!);
}
Future setBackgroundImage(String path, {bool fillHeight = false}) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.set_background_image_ffi(
_viewer!, path.toNativeUtf8().cast<Char>(), fillHeight ? 1 : 0);
}
Future setBackgroundColor(Color color) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.set_background_color_ffi(
_viewer!,
color.red.toDouble() / 255.0,
color.green.toDouble() / 255.0,
color.blue.toDouble() / 255.0,
color.alpha.toDouble() / 255.0);
}
Future setBackgroundImagePosition(double x, double y,
{bool clamp = false}) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.set_background_image_position_ffi(_viewer!, x, y, clamp ? 1 : 0);
}
Future loadSkybox(String skyboxPath) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.load_skybox_ffi(_viewer!, skyboxPath.toNativeUtf8().cast<Char>());
}
Future loadIbl(String lightingPath, {double intensity = 30000}) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.load_ibl_ffi(
_viewer!, lightingPath.toNativeUtf8().cast<Char>(), intensity);
}
Future removeSkybox() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.remove_skybox_ffi(_viewer!);
}
Future removeIbl() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.remove_ibl_ffi(_viewer!);
}
Future<FilamentEntity> addLight(
int type,
double colour,
double intensity,
double posX,
double posY,
double posZ,
double dirX,
double dirY,
double dirZ,
bool castShadows) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var entity = _lib.add_light_ffi(_viewer!, type, colour, intensity, posX,
posY, posZ, dirX, dirY, dirZ, castShadows ? 1 : 0);
return entity;
}
Future removeLight(FilamentEntity light) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.remove_light_ffi(_viewer!, light);
}
Future clearLights() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.clear_lights_ffi(_viewer!);
}
Future<FilamentEntity> loadGlb(String path, {bool unlit = false}) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
if (unlit) {
throw Exception("Not yet implemented");
}
var asset = _lib.load_glb_ffi(
_assetManager!, path.toNativeUtf8().cast<Char>(), unlit ? 1 : 0);
if (asset == FILAMENT_ASSET_ERROR) {
throw Exception("An error occurred loading the asset at $path");
}
return asset;
}
Future<FilamentEntity> loadGltf(
String path, String relativeResourcePath) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var entity = _lib.load_gltf_ffi(
_assetManager!,
path.toNativeUtf8().cast<Char>(),
relativeResourcePath.toNativeUtf8().cast<Char>());
return entity;
}
Future panStart(double x, double y) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.grab_begin(_viewer!, x * _pixelRatio, y * _pixelRatio, 1);
}
Future panUpdate(double x, double y) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.grab_update(_viewer!, x * _pixelRatio, y * _pixelRatio);
}
Future panEnd() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.grab_end(_viewer!);
}
Future rotateStart(double x, double y) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.grab_begin(_viewer!, x * _pixelRatio, y * _pixelRatio, 0);
}
Future rotateUpdate(double x, double y) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.grab_update(_viewer!, x * _pixelRatio, y * _pixelRatio);
}
Future rotateEnd() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.grab_end(_viewer!);
}
Future setMorphTargetWeights(
FilamentEntity asset, String meshName, List<double> weights) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var weightsPtr = calloc<Float>(weights.length);
for (int i = 0; i < weights.length; i++) {
weightsPtr.elementAt(i).value = weights[i];
}
_lib.set_morph_target_weights_ffi(_assetManager!, asset,
meshName.toNativeUtf8().cast<Char>(), weightsPtr, weights.length);
calloc.free(weightsPtr);
}
Future<List<String>> getMorphTargetNames(
FilamentEntity asset, String meshName) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var names = <String>[];
var count = _lib.get_morph_target_name_count_ffi(
_assetManager!, asset, meshName.toNativeUtf8().cast<Char>());
var outPtr = calloc<Char>(255);
for (int i = 0; i < count; i++) {
_lib.get_morph_target_name(_assetManager!, asset,
meshName.toNativeUtf8().cast<Char>(), outPtr, i);
names.add(outPtr.cast<Utf8>().toDartString());
}
calloc.free(outPtr);
return names.cast<String>();
}
Future<List<String>> getAnimationNames(FilamentEntity asset) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var animationCount = _lib.get_animation_count(_assetManager!, asset);
var names = <String>[];
var outPtr = calloc<Char>(255);
for (int i = 0; i < animationCount; i++) {
_lib.get_animation_name_ffi(_assetManager!, asset, outPtr, i);
names.add(outPtr.cast<Utf8>().toDartString());
}
return names;
}
///
/// Returns the length (in seconds) of the animation at the given index.
///
Future<double> getAnimationDuration(
FilamentEntity asset, int animationIndex) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var duration = await _channel.invokeMethod(
"getAnimationDuration", [_assetManager!, asset, animationIndex]);
return duration as double;
}
///
/// Create/start a dynamic morph target animation for [asset].
///
Future setMorphAnimationData(
FilamentEntity asset, MorphAnimationData animation) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var dataPtr = calloc<Float>(animation.data.length);
for (int i = 0; i < animation.data.length; i++) {
dataPtr.elementAt(i).value = animation.data[i];
}
var morphIndicesPtr = calloc<Int>(animation.animatedMorphIndices.length);
for (int i = 0; i < animation.numMorphTargets; i++) {
// morphIndicesPtr.elementAt(i) = animation.animatedMorphIndices[i];
}
_lib.set_morph_animation(
_assetManager!,
asset,
animation.meshName.toNativeUtf8().cast<Char>(),
dataPtr,
morphIndicesPtr,
animation.numMorphTargets,
animation.numFrames,
(animation.frameLengthInMs));
calloc.free(dataPtr);
calloc.free(morphIndicesPtr);
}
///
/// Animates morph target weights/bone transforms (where each frame requires a duration of [frameLengthInMs].
/// [morphWeights] is a list of doubles in frame-major format.
/// Each frame is [numWeights] in length, and each entry is the weight to be applied to the morph target located at that index in the mesh primitive at that frame.
/// for now we only allow animating a single bone (though multiple skinned targets are supported)
///
Future setBoneAnimation(
FilamentEntity asset, BoneAnimationData animation) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
// var data = calloc<Float>(animation.frameData.length);
// int offset = 0;
// var numFrames = animation.frameData.length ~/ 7;
// var boneNames = calloc<Pointer<Char>>(1);
// boneNames.elementAt(0).value =
// animation.boneName.toNativeUtf8().cast<Char>();
// var meshNames = calloc<Pointer<Char>>(animation.meshNames.length);
// for (int i = 0; i < animation.meshNames.length; i++) {
// meshNames.elementAt(i).value =
// animation.meshNames[i].toNativeUtf8().cast<Char>();
// }
// for (int i = 0; i < animation.frameData.length; i++) {
// data.elementAt(offset).value = animation.frameData[i];
// offset += 1;
// }
// await _channel.invokeMethod("setBoneAnimation", [
// _assetManager!,
// asset,
// data,
// numFrames,
// 1,
// boneNames,
// meshNames,
// animation.meshNames.length,
// animation.frameLengthInMs
// ]);
// calloc.free(data);
}
Future removeAsset(FilamentEntity asset) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.remove_asset_ffi(_assetManager!, asset);
}
Future clearAssets() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.clear_assets_ffi(_viewer!);
}
Future zoomBegin() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("scrollBegin");
}
Future zoomUpdate(double z) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.scroll_update(_viewer!, 0.0, 0.0, z);
}
Future zoomEnd() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.scroll_end(_viewer!);
}
Future playAnimation(FilamentEntity asset, int index,
{bool loop = false,
bool reverse = false,
bool replaceActive = true,
double crossfade = 0.0}) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.play_animation_ffi(_assetManager!, asset, index, loop ? 1 : 0,
reverse ? 1 : 0, replaceActive ? 1 : 0, crossfade);
}
Future setAnimationFrame(
FilamentEntity asset, int index, int animationFrame) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod(
"setAnimationFrame", [_assetManager!, asset, index, animationFrame]);
}
Future stopAnimation(FilamentEntity asset, int animationIndex) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel
.invokeMethod("stopAnimation", [_assetManager!, asset, animationIndex]);
}
Future setCamera(FilamentEntity asset, String? name) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
if (await _channel.invokeMethod("setCamera", [asset, name]) != true) {
throw Exception("Failed to set camera");
}
}
Future setToneMapping(ToneMapper mapper) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.set_tone_mapping_ffi(_viewer!, mapper.index);
}
Future setBloom(double bloom) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.set_bloom_ffi(_viewer!, bloom);
}
Future setCameraFocalLength(double focalLength) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.set_camera_focal_length(_viewer!, focalLength);
}
Future setCameraFocusDistance(double focusDistance) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.set_camera_focus_distance(_viewer!, focusDistance);
}
Future setCameraPosition(double x, double y, double z) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.set_camera_position(_viewer!, x, y, z);
}
Future moveCameraToAsset(FilamentEntity asset) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.move_camera_to_asset(_viewer!, asset);
}
Future setViewFrustumCulling(bool enabled) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.set_view_frustum_culling(_viewer!, enabled ? 1 : 0);
}
Future setCameraExposure(
double aperture, double shutterSpeed, double sensitivity) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.set_camera_exposure(_viewer!, aperture, shutterSpeed, sensitivity);
}
Future setCameraRotation(double rads, double x, double y, double z) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.set_camera_rotation(_viewer!, rads, x, y, z);
}
Future setCameraModelMatrix(List<double> matrix) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
assert(matrix.length == 16);
var ptr = calloc<Float>(16);
for (int i = 0; i < 16; i++) {
ptr.elementAt(i).value = matrix[i];
}
_lib.set_camera_model_matrix(_viewer!, ptr);
calloc.free(ptr);
}
Future setMaterialColor(FilamentEntity asset, String meshName,
int materialIndex, Color color) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var result = _lib.set_material_color(
_assetManager!,
asset,
meshName.toNativeUtf8().cast<Char>(),
materialIndex,
color.red.toDouble() / 255.0,
color.green.toDouble() / 255.0,
color.blue.toDouble() / 255.0,
color.alpha.toDouble() / 255.0);
if (result != 1) {
throw Exception("Failed to set material color");
}
}
Future transformToUnitCube(FilamentEntity asset) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.transform_to_unit_cube(_assetManager!, asset);
}
Future setPosition(FilamentEntity asset, double x, double y, double z) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.set_position(_assetManager!, asset, x, y, z);
}
Future setScale(FilamentEntity asset, double scale) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.set_scale(_assetManager!, asset, scale);
}
Future setRotation(
FilamentEntity asset, double rads, double x, double y, double z) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_lib.set_rotation(_assetManager!, asset, rads, x, y, z);
}
Future hide(FilamentEntity asset, String meshName) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
if (_lib.hide_mesh(
_assetManager!, asset, meshName.toNativeUtf8().cast<Char>()) !=
1) {}
}
Future reveal(FilamentEntity asset, String meshName) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
if (_lib.reveal_mesh(
_assetManager!, asset, meshName.toNativeUtf8().cast<Char>()) !=
1) {
throw Exception("Failed to reveal mesh $meshName");
}
}
}

View File

@@ -0,0 +1,640 @@
import 'dart:async';
import 'dart:ui' as ui;
import 'package:flutter/services.dart';
import 'package:polyvox_filament/filament_controller.dart';
import 'package:polyvox_filament/animations/bone_animation_data.dart';
import 'package:polyvox_filament/animations/morph_animation_data.dart';
import 'package:polyvox_filament/generated_bindings_web.dart';
import 'filament_controller.dart';
typedef AssetManager = int;
class FilamentControllerMethodChannel extends FilamentController {
late MethodChannel _channel = MethodChannel("app.polyvox.filament/event");
double _pixelRatio = 1.0;
ui.Size size = ui.Size.zero;
int? _textureId;
final _textureIdController = StreamController<int?>.broadcast();
Stream<int?> get textureId => _textureIdController.stream;
Completer _isReadyForScene = Completer();
Future get isReadyForScene => _isReadyForScene.future;
late AssetManager _assetManager;
int? _viewer;
///
/// This controller uses platform channels to bridge Dart with the C/C++ code for the Filament API.
/// Setting up the context/texture (since this is platform-specific) and the render ticker are platform-specific; all other methods are passed through by the platform channel to the methods specified in PolyvoxFilamentApi.h.
///
FilamentController() {
_channel.setMethodCallHandler((call) async {
throw Exception("Unknown method channel invocation ${call.method}");
});
}
Future setRendering(bool render) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
return _channel.invokeMethod("setRendering", render);
}
Future render() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("render");
}
Future setFrameRate(int framerate) async {
await _channel.invokeMethod("setFrameInterval", 1.0 / framerate);
}
void setPixelRatio(double ratio) {
_pixelRatio = ratio;
}
Future destroyViewer() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
_viewer = null;
await _channel.invokeMethod("destroyViewer");
_isReadyForScene = Completer();
}
Future destroyTexture() async {
await _channel.invokeMethod("destroyTexture");
_textureId = null;
_assetManager = 0;
_textureIdController.add(null);
}
///
/// The process for creating/initializing the Filament layer is as follows:
/// 1) Create a FilamentController
/// 2) Insert a FilamentWidget into the rendering tree
/// 3) Initially, this widget will only contain an empty Container. After the first frame is rendered, the widget itself will automatically call [createViewer] with the width/height from its constraints
/// 4) The FilamentWidget will replace the empty Container with the Texture widget.
///
Future createViewer(int width, int height) async {
if (_viewer != null) {
throw Exception(
"Viewer already exists, make sure you call destroyViewer first");
}
if (_isReadyForScene.isCompleted) {
throw Exception(
"Do not call createViewer when a viewer has already been created without calling destroyViewer");
}
size = ui.Size(width * _pixelRatio, height * _pixelRatio);
_textureId =
await _channel.invokeMethod("createTexture", [size.width, size.height]);
_viewer = await _channel
.invokeMethod("createFilamentViewer", [size.width, size.height]);
await _channel.invokeMethod("updateViewportAndCameraProjection",
[size.width.toInt(), size.height.toInt(), 1.0]);
_assetManager = await _channel.invokeMethod("getAssetManager");
_textureIdController.add(_textureId);
_isReadyForScene.complete(true);
}
bool _resizing = false;
Future resize(int width, int height,
{double contentScaleFactor = 1.0}) async {
_resizing = true;
_textureId = await _channel.invokeMethod("resize",
[width * _pixelRatio, height * _pixelRatio, contentScaleFactor]);
_textureIdController.add(_textureId);
_resizing = false;
}
Future clearBackgroundImage() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("clearBackgroundImage");
}
Future setBackgroundImage(String path, {bool fillHeight = false}) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setBackgroundImage", [path, fillHeight]);
}
Future setBackgroundColor(Color color) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setBackgroundColor", [
color.red.toDouble() / 255.0,
color.green.toDouble() / 255.0,
color.blue.toDouble() / 255.0,
color.alpha.toDouble() / 255.0
]);
}
Future setBackgroundImagePosition(double x, double y,
{bool clamp = false}) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel
.invokeMethod("setBackgroundImagePosition", [x, y, clamp ? 1 : 0]);
}
Future loadSkybox(String skyboxPath) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("loadSkybox", skyboxPath);
}
Future loadIbl(String lightingPath, {double intensity = 30000}) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("loadIbl", [lightingPath, intensity]);
}
Future removeSkybox() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("removeSkybox");
}
Future removeIbl() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("removeIbl");
}
// copied from LightManager.h
// enum class Type : uint8_t {
// SUN, //!< Directional light that also draws a sun's disk in the sky.
// DIRECTIONAL, //!< Directional light, emits light in a given direction.
// POINT, //!< Point light, emits light from a position, in all directions.
// FOCUSED_SPOT, //!< Physically correct spot light.
// SPOT, //!< Spot light with coupling of outer cone and illumination disabled.
// };
Future<FilamentEntity> addLight(
int type,
double colour,
double intensity,
double posX,
double posY,
double posZ,
double dirX,
double dirY,
double dirZ,
bool castShadows) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var entity = await _channel.invokeMethod("addLight", [
type,
colour,
intensity,
posX,
posY,
posZ,
dirX,
dirY,
dirZ,
castShadows ? 1 : 0
]);
return entity as FilamentEntity;
}
Future removeLight(FilamentEntity light) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("removeLight", light);
}
Future clearLights() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("clearLights");
}
Future<FilamentEntity> loadGlb(String path, {bool unlit = false}) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var asset =
await _channel.invokeMethod("loadGlb", [_assetManager, path, unlit]);
if (asset == FILAMENT_ASSET_ERROR) {
throw Exception("An error occurred loading the asset at $path");
}
return asset;
}
Future<FilamentEntity> loadGltf(
String path, String relativeResourcePath) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var entity = await _channel
.invokeMethod("loadGltf", [_assetManager, path, relativeResourcePath]);
return entity as FilamentEntity;
}
Future panStart(double x, double y) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel
.invokeMethod("grabBegin", [x * _pixelRatio, y * _pixelRatio, 1]);
}
Future panUpdate(double x, double y) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel
.invokeMethod("grabUpdate", [x * _pixelRatio, y * _pixelRatio]);
}
Future panEnd() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("grabEnd");
}
Future rotateStart(double x, double y) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel
.invokeMethod("grabBegin", [x * _pixelRatio, y * _pixelRatio, 0]);
}
Future rotateUpdate(double x, double y) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel
.invokeMethod("grabUpdate", [x * _pixelRatio, y * _pixelRatio]);
}
Future rotateEnd() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("grabEnd");
}
Future setMorphTargetWeights(
FilamentEntity asset, String meshName, List<double> weights) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setMorphTargetWeights",
[_assetManager, asset, meshName, weights, weights.length]);
}
Future<List<String>> getMorphTargetNames(
FilamentEntity asset, String meshName) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var names = await _channel
.invokeMethod("getMorphTargetNames", [_assetManager, asset, meshName]);
return names.cast<String>();
}
Future<List<String>> getAnimationNames(FilamentEntity asset) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var names = await _channel
.invokeMethod("getAnimationNames", [_assetManager, asset]);
return names.cast<String>();
}
///
/// Returns the length (in seconds) of the animation at the given index.
///
Future<double> getAnimationDuration(
FilamentEntity asset, int animationIndex) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var duration = await _channel.invokeMethod(
"getAnimationDuration", [_assetManager, asset, animationIndex]);
return duration as double;
}
///
/// Animates morph target weights/bone transforms (where each frame requires a duration of [frameLengthInMs].
/// [morphWeights] is a list of doubles in frame-major format.
/// Each frame is [numWeights] in length, and each entry is the weight to be applied to the morph target located at that index in the mesh primitive at that frame.
///
Future setMorphAnimationData(
FilamentEntity asset, MorphAnimationData animation) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setMorphAnimation", [
_assetManager,
asset,
animation.meshName,
animation.data,
animation.animatedMorphIndices,
animation.numMorphTargets,
animation.numFrames,
animation.frameLengthInMs
]);
}
///
/// Animates morph target weights/bone transforms (where each frame requires a duration of [frameLengthInMs].
/// [morphWeights] is a list of doubles in frame-major format.
/// Each frame is [numWeights] in length, and each entry is the weight to be applied to the morph target located at that index in the mesh primitive at that frame.
/// for now we only allow animating a single bone (though multiple skinned targets are supported)
///
Future setBoneAnimation(
FilamentEntity asset, BoneAnimationData animation) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
// var data = calloc<Float>(animation.frameData.length);
// int offset = 0;
// var numFrames = animation.frameData.length ~/ 7;
// var boneNames = calloc<Pointer<Char>>(1);
// boneNames.elementAt(0).value =
// animation.boneName.toNativeUtf8().cast<Char>();
// var meshNames = calloc<Pointer<Char>>(animation.meshNames.length);
// for (int i = 0; i < animation.meshNames.length; i++) {
// meshNames.elementAt(i).value =
// animation.meshNames[i].toNativeUtf8().cast<Char>();
// }
// for (int i = 0; i < animation.frameData.length; i++) {
// data.elementAt(offset).value = animation.frameData[i];
// offset += 1;
// }
// await _channel.invokeMethod("setBoneAnimation", [
// _assetManager,
// asset,
// data,
// numFrames,
// 1,
// boneNames,
// meshNames,
// animation.meshNames.length,
// animation.frameLengthInMs
// ]);
// calloc.free(data);
}
Future removeAsset(FilamentEntity asset) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("removeAsset", asset);
}
Future clearAssets() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("clearAssets");
}
Future zoomBegin() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("scrollBegin");
}
Future zoomUpdate(double z) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("scrollUpdate", [0.0, 0.0, z]);
}
Future zoomEnd() async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("scrollEnd");
}
Future playAnimation(FilamentEntity asset, int index,
{bool loop = false,
bool reverse = false,
bool replaceActive = true,
double crossfade = 0.0}) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("playAnimation",
[_assetManager, asset, index, loop, reverse, replaceActive, crossfade]);
}
Future setAnimationFrame(
FilamentEntity asset, int index, int animationFrame) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod(
"setAnimationFrame", [_assetManager, asset, index, animationFrame]);
}
Future stopAnimation(FilamentEntity asset, int animationIndex) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel
.invokeMethod("stopAnimation", [_assetManager, asset, animationIndex]);
}
Future setCamera(FilamentEntity asset, String? name) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
if (await _channel.invokeMethod("setCamera", [asset, name]) != true) {
throw Exception("Failed to set camera");
}
}
Future setToneMapping(ToneMapper mapper) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
if (!await _channel.invokeMethod("setToneMapping", mapper.index)) {
throw Exception("Failed to set tone mapper");
}
}
Future setBloom(double bloom) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
if (!await _channel.invokeMethod("setBloom", bloom)) {
throw Exception("Failed to set bloom");
}
}
Future setCameraFocalLength(double focalLength) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setCameraFocalLength", focalLength);
}
Future setCameraFocusDistance(double focusDistance) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setCameraFocusDistance", focusDistance);
}
Future setCameraPosition(double x, double y, double z) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setCameraPosition", [x, y, z]);
}
Future moveCameraToAsset(FilamentEntity asset) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("moveCameraToAsset", asset);
}
Future setViewFrustumCulling(bool enabled) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setViewFrustumCulling", enabled);
}
Future setCameraExposure(
double aperture, double shutterSpeed, double sensitivity) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod(
"setCameraExposure", [aperture, shutterSpeed, sensitivity]);
}
Future setCameraRotation(double rads, double x, double y, double z) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setCameraRotation", [rads, x, y, z]);
}
Future setCameraModelMatrix(List<double> matrix) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
assert(matrix.length == 16);
await _channel.invokeMethod("setCameraModelMatrix", matrix);
}
Future setTexture(FilamentEntity asset, String assetPath,
{int renderableIndex = 0}) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setTexture", [_assetManager, asset]);
}
Future setMaterialColor(FilamentEntity asset, String meshName,
int materialIndex, Color color) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
var result = await _channel.invokeMethod("setMaterialColor", [
_assetManager,
asset,
meshName,
materialIndex,
[
color.red.toDouble() / 255.0,
color.green.toDouble() / 255.0,
color.blue.toDouble() / 255.0,
color.alpha.toDouble() / 255.0
]
]);
if (!result) {
throw Exception("Failed to set material color");
}
}
Future transformToUnitCube(FilamentEntity asset) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("transformToUnitCube", [_assetManager, asset]);
}
Future setPosition(FilamentEntity asset, double x, double y, double z) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setPosition", [_assetManager, asset, x, y, z]);
}
Future setScale(FilamentEntity asset, double scale) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel.invokeMethod("setScale", [_assetManager, asset, scale]);
}
Future setRotation(
FilamentEntity asset, double rads, double x, double y, double z) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
await _channel
.invokeMethod("setRotation", [_assetManager, asset, rads, x, y, z]);
}
Future hide(FilamentEntity asset, String meshName) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
if (await _channel
.invokeMethod("hideMesh", [_assetManager, asset, meshName]) !=
1) {
throw Exception("Failed to hide mesh $meshName");
}
}
Future reveal(FilamentEntity asset, String meshName) async {
if (_viewer == null || _resizing) {
throw Exception("No viewer available, ignoring");
}
if (await _channel
.invokeMethod("revealMesh", [_assetManager, asset, meshName]) !=
1) {
throw Exception("Failed to reveal mesh $meshName");
}
}
}

View File

@@ -5,9 +5,10 @@ import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/scheduler.dart';
import 'package:polyvox_filament/filament_controller.dart';
import 'dart:async';
import 'filament_controller.dart';
import 'filament_controller_method_channel.dart';
typedef ResizeCallback = void Function(Size oldSize, Size newSize);

2219
lib/generated_bindings.dart Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -49,13 +49,12 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
if(path != nil) {
do {
print("Loading file at path \(path!)")
let data = try Data(contentsOf: URL(fileURLWithPath:path!))
let nsData = data as NSData
let resId = UInt32(instance.resources.count)
instance.resources[resId] = nsData
let length = nsData.length
print("Got file of length \(length)")
print("Resolved asset to file of length \(length) at path \(path!)")
return ResourceBuffer(data:nsData.bytes, size:UInt32(nsData.count), id:UInt32(resId))
} catch {
print("ERROR LOADING RESOURCE")
@@ -146,6 +145,11 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
let methodName = call.method;
switch methodName {
case "getSharedContext":
result(nil)
case "getResourceLoaderWrapper":
let resourceLoaderWrapper = make_resource_loader(loadResource, freeResource, Unmanaged.passUnretained(self).toOpaque())
result(unsafeBitCast(resourceLoaderWrapper, to:Int64.self))
case "createTexture":
let args = call.arguments as! Array<Int32>
createPixelBuffer(width:Int(args[0]), height:Int(args[1]))
@@ -181,7 +185,7 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
case "destroyViewer":
if(viewer != nil) {
destroy_swap_chain(viewer)
delete_filament_viewer(viewer)
destroy_filament_viewer(viewer)
viewer = nil
}
result(true)
@@ -198,7 +202,7 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
resize(width:Int32(width), height:Int32(height))
create_swap_chain(viewer, CVPixelBufferGetBaseAddress(pixelBuffer!), width, height)
let metalTextureId = Int(bitPattern:Unmanaged.passUnretained(metalTexture!).toOpaque())
create_render_target(viewer, metalTextureId, width, height);
create_render_target(viewer, UInt32(metalTextureId), width, height);
update_viewport_and_camera_projection(viewer, width, height, Float(args[2] as! Double))
rendering = true
print("Resized to \(args[0])x\(args[1])")
@@ -206,7 +210,7 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
case "createFilamentViewer":
if(viewer != nil) {
destroy_swap_chain(viewer)
delete_filament_viewer(viewer)
destroy_filament_viewer(viewer)
viewer = nil
}
let callback = make_resource_loader(loadResource, freeResource, Unmanaged.passUnretained(self).toOpaque())
@@ -219,7 +223,7 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
let metalTextureId = Int(bitPattern:Unmanaged.passUnretained(metalTexture!).toOpaque())
create_render_target(viewer, metalTextureId, width,height);
create_render_target(viewer, UInt32(metalTextureId), width,height);
update_viewport_and_camera_projection(viewer, width, height, 1.0)
set_frame_interval(viewer, Float(frameInterval))
@@ -232,7 +236,8 @@ public class SwiftPolyvoxFilamentPlugin: NSObject, FlutterPlugin, FlutterTexture
clear_background_image(viewer)
result(true)
case "setBackgroundImage":
set_background_image(viewer, call.arguments as! String)
let args = call.arguments as! [Any]
set_background_image(viewer, args[0] as! String, args[1] as! Bool)
result(true)
case "setBackgroundImagePosition":
let args = call.arguments as! [Any]

View File

@@ -78,11 +78,13 @@ namespace polyvox {
Renderer* getRenderer();
void setBackgroundColor(const float r, const float g, const float b, const float a);
void setBackgroundImage(const char* resourcePath);
void setBackgroundImage(const char* resourcePath, bool fillHeight);
void clearBackgroundImage();
void setBackgroundImagePosition(float x, float y, bool clamp);
void moveCameraToAsset(EntityId entityId);
void setViewFrustumCulling(bool enabled);
void moveCameraToAsset(EntityId entityId);
void setCameraExposure(float aperture, float shutterSpeed, float sensitivity);
void setCameraPosition(float x, float y, float z);
void setCameraRotation(float rads, float x, float y, float z);

View File

@@ -6,12 +6,12 @@
typedef int32_t EntityId;
const void* create_filament_viewer(const void* const context, const ResourceLoaderWrapper* const loader);
ResourceLoaderWrapper* make_resource_loader(LoadResourceFromOwner loadFn, FreeResourceFromOwner freeFn, void* owner);
void delete_filament_viewer(const void* const viewer);
ResourceLoaderWrapper* make_resource_loader(LoadFilamentResourceFromOwner loadFn, FreeFilamentResourceFromOwner freeFn, void* owner);
void destroy_filament_viewer(const void* const viewer);
void* get_asset_manager(const void* const viewer);
void create_render_target(const void* const viewer, intptr_t textureId, uint32_t width, uint32_t height);
void create_render_target(const void* const viewer, uint32_t textureId, uint32_t width, uint32_t height);
void clear_background_image(const void* const viewer);
void set_background_image(const void* const viewer, const char *path);
void set_background_image(const void* const viewer, const char *path, bool fillHeight);
void set_background_image_position(const void* const viewer, float x, float y, bool clamp);
void set_background_color(const void* const viewer, const float r, const float g, const float b, const float a);
void set_tone_mapping(const void* const viewer, int toneMapping);
@@ -26,8 +26,9 @@ void clear_lights(const void* const viewer);
EntityId load_glb(void *assetManager, const char *assetPath, bool unlit);
EntityId load_gltf(void *assetManager, const char *assetPath, const char *relativePath);
bool set_camera(const void* const viewer, EntityId asset, const char *nodeName);
void set_view_frustum_culling(const void* const viewer, bool enabled);
void render(const void* const viewer, uint64_t frameTimeInNanos);
void create_swap_chain(const void* const viewer, const void* const surface, uint32_t width, uint32_t height);
void create_swap_chain(const void* const viewer, const void* const window, uint32_t width, uint32_t height);
void destroy_swap_chain(const void* const viewer);
void set_frame_interval(const void* const viewer, float interval);
void update_viewport_and_camera_projection(const void* const viewer, uint32_t width, uint32_t height, float scaleFactor);
@@ -82,15 +83,12 @@ void get_morph_target_name(void* assetManager, EntityId asset, const char *meshN
int get_morph_target_name_count(void* assetManager, EntityId asset, const char *meshName);
void remove_asset(const void* const viewer, EntityId asset);
void clear_assets(const void* const viewer);
void load_texture(void* assetManager, EntityId asset, const char *assetPath, int renderableIndex);
void set_texture(void* assetManager, EntityId asset);
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);
void transform_to_unit_cube(void* assetManager, EntityId asset);
void set_position(void* assetManager, EntityId asset, float x, float y, float z);
void set_rotation(void* assetManager, EntityId asset, float rads, float x, float y, float z);
void set_scale(void* assetManager, EntityId asset, float scale);
void move_camera_to_asset(const void* const viewer, EntityId asset);
void set_view_frustum_culling(const void* const viewer, bool enabled);
void set_camera_exposure(const void* const viewer, float aperture, float shutterSpeed, float sensitivity);
void set_camera_position(const void* const viewer, float x, float y, float z);
void set_camera_rotation(const void* const viewer, float rads, float x, float y, float z);

View File

@@ -0,0 +1,83 @@
#ifndef _POLYVOX_FILAMENT_FFI_API_H
#define _POLYVOX_FILAMENT_FFI_API_H
#include "PolyvoxFilamentApi.h"
///
/// This header replicates most of the methods in PolyvoxFilamentApi.h, and is only intended to be used to generate client FFI bindings.
/// The intention is that calling one of these methods will call its respective method in PolyvoxFilamentApi.h, but wrapped in some kind of thread runner to ensure thread safety.
///
typedef int32_t EntityId;
const void* create_filament_viewer_ffi(void* const context, const ResourceLoaderWrapper* const loader);
void destroy_filament_viewer_ffi(void* const viewer);
void render_ffi(void* const viewer);
void set_rendering_ffi(void* const viewer, bool rendering);
void set_frame_interval_ffi(float frameInterval);
void update_viewport_and_camera_projection_ffi(void* const viewer, const uint32_t width, const uint32_t height, const float scaleFactor);
void create_render_target_ffi(void* const viewer, uint32_t textureId, uint32_t width, uint32_t height);
void set_background_color_ffi(void* const viewer, const float r, const float g, const float b, const float a);
void clear_background_image_ffi(void* const viewer);
void set_background_image_ffi(void* const viewer, const char *path, bool fillHeight);
void set_background_image_position_ffi(void* const viewer, float x, float y, bool clamp);
void set_background_color_ffi(void* const viewer, const float r, const float g, const float b, const float a);
void set_tone_mapping_ffi(void* const viewer, int toneMapping);
void set_bloom_ffi(void* const viewer, float strength);
void load_skybox_ffi(void* const viewer, const char *skyboxPath);
void load_ibl_ffi(void* const viewer, const char *iblPath, float intensity);
void remove_skybox_ffi(void* const viewer);
void remove_ibl_ffi(void* const viewer);
EntityId add_light_ffi(void* const viewer, uint8_t type, float colour, float intensity, float posX, float posY, float posZ, float dirX, float dirY, float dirZ, bool shadows);
void remove_light_ffi(void* const viewer, EntityId entityId);
void clear_lights_ffi(void* const viewer);
EntityId load_glb_ffi(void* const assetManager, const char *assetPath, bool unlit);
EntityId load_gltf_ffi(void* const assetManager, const char *assetPath, const char *relativePath);
void remove_asset_ffi(const void* const viewer, EntityId asset);
void clear_assets_ffi(const void* const viewer);
bool set_camera_ffi(void* const viewer, EntityId asset, const char *nodeName);
void apply_weights_ffi(
void* assetManager,
EntityId asset,
const char *const entityName,
float *const weights,
int count
);
void set_morph_target_weights_ffi(
void* assetManager,
EntityId asset,
const char *const entityName,
const float *const morphData,
int numWeights
);
bool set_morph_animation_ffi(
void* assetManager,
EntityId asset,
const char *const entityName,
const float *const morphData,
const int* const morphIndices,
int numMorphTargets,
int numFrames,
float frameLengthInMs);
void set_bone_animation_ffi(
void* assetManager,
EntityId asset,
const float* const frameData,
int numFrames,
int numBones,
const char** const boneNames,
const char** const meshName,
int numMeshTargets,
float frameLengthInMs);
void play_animation_ffi(void* assetManager, EntityId asset, int index, bool loop, bool reverse, bool replaceActive, float crossfade);
void set_animation_frame_ffi(void* assetManager, EntityId asset, int animationIndex, int animationFrame);
void stop_animation_ffi(void* assetManager, EntityId asset, int index);
int get_animation_count_ffi(void* assetManager, EntityId asset);
void get_animation_name_ffi(void* assetManager, EntityId asset, char *const outPtr, int index);
float get_animation_duration_ffi(void* assetManager, EntityId asset, int index);
void get_morph_target_name_ffi(void* assetManager, EntityId asset, const char *meshName, char *const outPtr, int index);
int get_morph_target_name_count_ffi(void* assetManager, EntityId asset, const char *meshName);
#endif // _POLYVOX_FILAMENT_FFI_API_H

View File

@@ -0,0 +1,4 @@
#import <Flutter/Flutter.h>
@interface PolyvoxFilamentPlugin : NSObject<FlutterPlugin>
@end

View File

@@ -3,7 +3,6 @@
#include <stdint.h>
#if defined(__cplusplus)
#include "Log.hpp"
extern "C" {
#endif
//
@@ -28,6 +27,10 @@ extern "C" {
struct ResourceBuffer {
#if defined(__cplusplus)
ResourceBuffer(const void* const data, const uint32_t size, const uint32_t id) : data(data), size(size), id(id) {};
ResourceBuffer(const ResourceBuffer& rb) : data(rb.data), size(rb.size), id(rb.id) { };
ResourceBuffer(const ResourceBuffer&& rb) : data(rb.data), size(rb.size), id(rb.id) { };
ResourceBuffer& operator=(const ResourceBuffer& other) = delete;
#endif
const void * const data;
const uint32_t size;
@@ -35,41 +38,41 @@ extern "C" {
};
typedef struct ResourceBuffer ResourceBuffer;
typedef ResourceBuffer (*FlutterFilamentLoadResource)(const char* uri);
typedef ResourceBuffer (*LoadResourceFromOwner)(const char* const, void* const owner);
typedef void (*FlutterFilamentFreeResource)(ResourceBuffer);
typedef void (*FreeResourceFromOwner)(ResourceBuffer, void* const owner);
typedef ResourceBuffer (*LoadFilamentResource)(const char* uri);
typedef ResourceBuffer (*LoadFilamentResourceFromOwner)(const char* const, void* const owner);
typedef void (*FreeFilamentResource)(ResourceBuffer);
typedef void (*FreeFilamentResourceFromOwner)(ResourceBuffer, void* const owner);
// this may be compiled as either C or C++, depending on which compiler is being invoked (e.g. binding to Swift will compile as C).
// the former does not allow default initialization to be specified inline), so we need to explicitly set the unused members to nullptr
struct ResourceLoaderWrapper {
#if defined(__cplusplus)
ResourceLoaderWrapper(FlutterFilamentLoadResource loader, FlutterFilamentFreeResource freeResource) : mLoadResource(loader), mFreeResource(freeResource), mLoadResourceFromOwner(nullptr), mFreeResourceFromOwner(nullptr),
ResourceLoaderWrapper(LoadFilamentResource loader, FreeFilamentResource freeResource) : mLoadFilamentResource(loader), mFreeFilamentResource(freeResource), mLoadFilamentResourceFromOwner(nullptr), mFreeFilamentResourceFromOwner(nullptr),
mOwner(nullptr) {}
ResourceLoaderWrapper(LoadResourceFromOwner loader, FreeResourceFromOwner freeResource, void* const owner) : mLoadResource(nullptr), mFreeResource(nullptr), mLoadResourceFromOwner(loader), mFreeResourceFromOwner(freeResource), mOwner(owner) {
ResourceLoaderWrapper(LoadFilamentResourceFromOwner loader, FreeFilamentResourceFromOwner freeResource, void* const owner) : mLoadFilamentResource(nullptr), mFreeFilamentResource(nullptr), mLoadFilamentResourceFromOwner(loader), mFreeFilamentResourceFromOwner(freeResource), mOwner(owner) {
};
ResourceBuffer load(const char* uri) const {
if(mLoadResourceFromOwner) {
return mLoadResourceFromOwner(uri, mOwner);
if(mLoadFilamentResourceFromOwner) {
return mLoadFilamentResourceFromOwner(uri, mOwner);
}
return mLoadResource(uri);
return mLoadFilamentResource(uri);
}
void free(ResourceBuffer rb) const {
if(mFreeResourceFromOwner) {
mFreeResourceFromOwner(rb, mOwner);
if(mFreeFilamentResourceFromOwner) {
mFreeFilamentResourceFromOwner(rb, mOwner);
} else {
mFreeResource(rb);
mFreeFilamentResource(rb);
}
}
#endif
FlutterFilamentLoadResource mLoadResource;
FlutterFilamentFreeResource mFreeResource;
LoadResourceFromOwner mLoadResourceFromOwner;
FreeResourceFromOwner mFreeResourceFromOwner;
LoadFilamentResource mLoadFilamentResource;
FreeFilamentResource mFreeFilamentResource;
LoadFilamentResourceFromOwner mLoadFilamentResourceFromOwner;
FreeFilamentResourceFromOwner mFreeFilamentResourceFromOwner;
void* mOwner;
};
typedef struct ResourceLoaderWrapper ResourceLoaderWrapper;

View File

@@ -1,53 +0,0 @@
#ifndef UNLIT_MATERIAL_PROVIDER
#define UNLIT_MATERIAL_PROVIDER
#include "material/standard.h"
namespace polyvox {
class StandardMaterialProvider : public MaterialProvider {
const Material* _m;
const Material* _ms[1];
const Engine* _engine;
public:
StandardMaterialProvider(Engine* engine) {
_engine = engine;
_m = Material::Builder()
.package( STANDARD_STANDARD_DATA, STANDARD_STANDARD_SIZE)
.build(*engine);
_ms[0] = _m;
}
filament::MaterialInstance* createMaterialInstance(MaterialKey* config, UvMap* uvmap,
const char* label = "material", const char* extras = nullptr) {
MaterialInstance* d = (MaterialInstance*)_m->getDefaultInstance();
return d;
}
/**
* 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() {
// TODO - do we need to do anything here?
}
bool needsDummyData(filament::VertexAttribute attrib) const noexcept {
return false;
}
};
}
#endif

View File

@@ -1,12 +0,0 @@
.global STANDARD_STANDARD_OFFSET;
.global STANDARD_STANDARD_SIZE;
.global STANDARD_PACKAGE
.section .rodata
STANDARD_PACKAGE:
.incbin "standard.bin"
STANDARD_STANDARD_OFFSET:
.int 0
STANDARD_STANDARD_SIZE:
.int 1031374

View File

@@ -1,12 +0,0 @@
.global _STANDARD_STANDARD_OFFSET;
.global _STANDARD_STANDARD_SIZE;
.global _STANDARD_PACKAGE
.section __TEXT,__const
_STANDARD_PACKAGE:
.incbin "standard.bin"
_STANDARD_STANDARD_OFFSET:
.int 0
_STANDARD_STANDARD_SIZE:
.int 1031374

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@@ -1,13 +0,0 @@
#ifndef STANDARD_H_
#define STANDARD_H_
#include <stdint.h>
extern "C" {
extern const uint8_t STANDARD_PACKAGE[];
extern int STANDARD_STANDARD_OFFSET;
extern int STANDARD_STANDARD_SIZE;
}
#define STANDARD_STANDARD_DATA (STANDARD_PACKAGE + STANDARD_STANDARD_OFFSET)
#endif

View File

@@ -1,5 +1,7 @@
#include "AssetManager.hpp"
#include <string>
#include <sstream>
#include <thread>
#include <filament/Engine.h>
#include <filament/TransformManager.h>
#include <filament/Texture.h>
@@ -17,8 +19,8 @@
#include "StreamBufferAdapter.hpp"
#include "SceneAsset.hpp"
#include "Log.hpp"
#include "AssetManager.hpp"
#include "material/StandardMaterialProvider.hpp"
#include "material/UnlitMaterialProvider.hpp"
#include "material/FileMaterialProvider.hpp"
#include "gltfio/materials/uberarchive.h"
@@ -52,16 +54,13 @@ _scene(scene) {
_gltfResourceLoader = new ResourceLoader({.engine = _engine,
.normalizeSkinningWeights = true });
// auto uberdata = resourceLoaderWrapper->load("packages/polyvox_filament/assets/materials.uberz");
// auto uberdata = resourceLoaderWrapper->load("packages/polyvox_filament/assets/materials_ios_arm64.uberz");
// _ubershaderProvider = gltfio::createUbershaderProvider(
// _engine, uberdata.data, uberdata.size);
// _engine, uberdata.data, uberdata.size);
_ubershaderProvider = gltfio::createUbershaderProvider(
_engine, UBERARCHIVE_DEFAULT_DATA, UBERARCHIVE_DEFAULT_SIZE);
// _ubershaderProvider = gltfio::createJitShaderProvider(_engine, true);
// _ubershaderProvider = new StandardMaterialProvider(_engine);
EntityManager &em = EntityManager::get();
//_unlitProvider = new UnlitMaterialProvider(_engine);
@@ -78,6 +77,7 @@ _scene(scene) {
AssetManager::~AssetManager() {
_gltfResourceLoader->asyncCancelLoad();
_ubershaderProvider->destroyMaterials();
//_unlitProvider->destroyMaterials();
destroyAll();
AssetLoader::destroy(&_assetLoader);
@@ -252,7 +252,8 @@ void AssetManager::updateAnimations() {
for (auto& asset : _assets) {
vector<AnimationStatus> completed;
vector<int> completed;
int index = 0;
for(auto& anim : asset.mAnimations) {
auto elapsed = float(std::chrono::duration_cast<std::chrono::milliseconds>(now - anim.mStart).count()) / 1000.0f;
@@ -316,10 +317,14 @@ void AssetManager::updateAnimations() {
}
// animation has completed
} else {
completed.push_back(anim);
completed.push_back(index);
asset.fadeGltfAnimationIndex = -1;
}
asset.mAnimator->updateBoneMatrices();
index++;
}
for(auto& it : completed) {
asset.mAnimations.erase(asset.mAnimations.begin() + it);
}
}
}
@@ -657,6 +662,8 @@ void AssetManager::playAnimation(EntityId e, int index, bool loop, bool reverse,
animation.mDuration = asset.mAnimator->getAnimationDuration(index);
asset.mAnimations.push_back(animation);
Log("Current animation count %d ", asset.mAnimations.size());
}
void AssetManager::stopAnimation(EntityId entityId, int index) {

View File

@@ -112,10 +112,8 @@ FilamentViewer::FilamentViewer(const void* context, const ResourceLoaderWrapper*
#if TARGET_OS_IPHONE
_engine = Engine::create(Engine::Backend::METAL);
#elif TARGET_OS_MAC
_engine = Engine::create(Engine::Backend::METAL);
#else
_engine = Engine::create(Engine::Backend::OPENGL, nullptr, (void*)context, nullptr);
_engine = Engine::create(Engine::Backend::OPENGL); //L, nullptr, (void*)context, nullptr);
#endif
_renderer = _engine->createRenderer();
@@ -153,7 +151,6 @@ FilamentViewer::FilamentViewer(const void* context, const ResourceLoaderWrapper*
const float aperture = _mainCamera->getAperture();
const float shutterSpeed = _mainCamera->getShutterSpeed();
const float sens = _mainCamera->getSensitivity();
// _mainCamera->setExposure(2.0f, 1.0f, 1.0f);
Log("Camera aperture %f shutter %f sensitivity %f", aperture, shutterSpeed, sens);
@@ -173,10 +170,6 @@ FilamentViewer::FilamentViewer(const void* context, const ResourceLoaderWrapper*
_view->setAntiAliasing(AntiAliasing::NONE);
// auto materialRb = _resourceLoader->load("file:///mnt/hdd_2tb/home/hydroxide/projects/filament/unlit.filamat");
// Log("Loaded resource of size %d", materialRb.size);
// _materialProvider = new FileMaterialProvider(_engine, (void*) materialRb.data, (size_t)materialRb.size);
EntityManager &em = EntityManager::get();
_ncm = new NameComponentManager(em);
@@ -377,7 +370,6 @@ void FilamentViewer::loadPngTexture(string path, ResourceBuffer rb) {
Texture::PixelBufferDescriptor::Callback freeCallback = [](void *buf, size_t,
void *data) {
Log("Deleting LinearImage");
delete reinterpret_cast<LinearImage*>(data);
};
@@ -423,14 +415,13 @@ void FilamentViewer::setBackgroundColor(const float r, const float g, const floa
void FilamentViewer::clearBackgroundImage() {
_imageMaterial->setDefaultParameter("showImage", 0);
if (_imageTexture) {
Log("Destroying existing texture");
_engine->destroy(_imageTexture);
Log("Destroyed.");
_imageTexture = nullptr;
Log("Destroyed background image texture");
}
}
void FilamentViewer::setBackgroundImage(const char *resourcePath) {
void FilamentViewer::setBackgroundImage(const char *resourcePath, bool fillHeight) {
string resourcePathString(resourcePath);
@@ -444,7 +435,18 @@ void FilamentViewer::setBackgroundImage(const char *resourcePath) {
// TODO - implement stretch/etc
const Viewport& vp = _view->getViewport();
Log("Image width %d height %d vp width %d height %d", _imageWidth, _imageHeight, vp.width, vp.height);
_imageScale = mat4f { float(vp.width) / float(_imageWidth) , 0.0f, 0.0f, 0.0f, 0.0f, float(vp.height) / float(_imageHeight), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f };
float xScale = float(vp.width) / float(_imageWidth);
float yScale;
if(fillHeight) {
yScale = 1.0f;
} else {
yScale = float(vp.height) / float(_imageHeight);
}
_imageScale = mat4f { xScale , 0.0f, 0.0f, 0.0f, 0.0f, yScale , 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f };
_imageMaterial->setDefaultParameter("transform", _imageScale);
_imageMaterial->setDefaultParameter("image", _imageTexture, _imageSampler);
@@ -550,14 +552,14 @@ FilamentViewer::~FilamentViewer() {
Renderer *FilamentViewer::getRenderer() { return _renderer; }
void FilamentViewer::createSwapChain(const void *surface, uint32_t width, uint32_t height) {
void FilamentViewer::createSwapChain(const void *window, uint32_t width, uint32_t height) {
#if TARGET_OS_IPHONE
_swapChain = _engine->createSwapChain((void*)surface, filament::backend::SWAP_CHAIN_CONFIG_APPLE_CVPIXELBUFFER);
_swapChain = _engine->createSwapChain((void*)window, filament::backend::SWAP_CHAIN_CONFIG_APPLE_CVPIXELBUFFER);
#else
if(surface) {
_swapChain = _engine->createSwapChain(width, height, filament::backend::SWAP_CHAIN_CONFIG_TRANSPARENT | filament::backend::SWAP_CHAIN_CONFIG_READABLE);
if(window) {
_swapChain = _engine->createSwapChain((void*)window, filament::backend::SWAP_CHAIN_CONFIG_TRANSPARENT | filament::backend::SWAP_CHAIN_CONFIG_READABLE);
} else {
_swapChain = _engine->createSwapChain((void*)surface, filament::backend::SWAP_CHAIN_CONFIG_TRANSPARENT | filament::backend::SWAP_CHAIN_CONFIG_READABLE);
_swapChain = _engine->createSwapChain(width, height, filament::backend::SWAP_CHAIN_CONFIG_TRANSPARENT | filament::backend::SWAP_CHAIN_CONFIG_READABLE);
}
#endif
Log("Swapchain created.");
@@ -588,7 +590,7 @@ void FilamentViewer::createRenderTarget(intptr_t textureId, uint32_t width, uint
// Make a specific viewport just for our render target
_view->setRenderTarget(_rt);
Log("Set render target for textureId %u %u x %u", textureId, width, height);
Log("Set render target for glTextureId %u %u x %u", textureId, width, height);
}
@@ -861,8 +863,18 @@ void FilamentViewer::updateViewportAndCameraProjection(
contentScaleFactor);
}
void FilamentViewer::moveCameraToAsset(EntityId entityId) {
void FilamentViewer::setCameraPosition(float x, float y, float z) {
Camera& cam =_view->getCamera();
_cameraPosition = math::mat4f::translation(math::float3(x,y,z));
cam.setModelMatrix(_cameraPosition * _cameraRotation);
}
void FilamentViewer::setViewFrustumCulling(bool enabled) {
_view->setFrustumCullingEnabled(enabled);
}
void FilamentViewer::moveCameraToAsset(EntityId entityId) {
auto asset = _assetManager->getAssetByEntityId(entityId);
if(!asset) {
Log("Failed to find asset attached to specified entity id.");
@@ -878,20 +890,8 @@ void FilamentViewer::moveCameraToAsset(EntityId entityId) {
Log("Moved camera to %f %f %f, lookAt %f %f %f, near %f far %f", eye[0], eye[1], eye[2], lookAt[0], lookAt[1], lookAt[2], cam.getNear(), cam.getCullingFar());
}
void FilamentViewer::setViewFrustumCulling(bool enabled) {
_view->setFrustumCullingEnabled(enabled);
}
void FilamentViewer::setCameraPosition(float x, float y, float z) {
Camera& cam =_view->getCamera();
_cameraPosition = math::mat4f::translation(math::float3(x,y,z));
cam.setModelMatrix(_cameraPosition * _cameraRotation);
}
void FilamentViewer::setCameraRotation(float rads, float x, float y, float z) {
Camera& cam =_view->getCamera();
_cameraRotation = math::mat4f::rotation(rads, math::float3(x,y,z));
cam.setModelMatrix(_cameraPosition * _cameraRotation);
}
@@ -936,7 +936,7 @@ void FilamentViewer::grabUpdate(float x, float y) {
return;
}
Camera& cam =_view->getCamera();
auto eye = cam.getPosition();
auto eye = cam.getPosition();// math::float3 {0.0f, 0.5f, 50.0f } ;// ; //
auto target = eye + cam.getForwardVector();
auto upward = cam.getUpVector();
Viewport const& vp = _view->getViewport();
@@ -945,7 +945,9 @@ void FilamentViewer::grabUpdate(float x, float y) {
cam.setModelMatrix(trans);
} else {
auto trans = cam.getModelMatrix() * mat4::rotation(
0.02,
0.01,
// math::float3 { 0.0f, 1.0f, 0.0f });
math::float3 { (y - _startY) / vp.height, (x - _startX) / vp.width, 0.0f });
cam.setModelMatrix(trans);
}

View File

@@ -17,31 +17,31 @@ extern "C" {
#include "PolyvoxFilamentApi.h"
FLUTTER_PLUGIN_EXPORT const void* create_filament_viewer(const void* context, const ResourceLoaderWrapper* const loader) {
return (void*) new FilamentViewer(context, loader);
return (const void*) new FilamentViewer(context, loader);
}
FLUTTER_PLUGIN_EXPORT ResourceLoaderWrapper* make_resource_loader(LoadResourceFromOwner loadFn, FreeResourceFromOwner freeFn, void* const owner) {
FLUTTER_PLUGIN_EXPORT ResourceLoaderWrapper* make_resource_loader(LoadFilamentResourceFromOwner loadFn, FreeFilamentResourceFromOwner freeFn, void* const owner) {
return new ResourceLoaderWrapper(loadFn, freeFn, owner);
}
FLUTTER_PLUGIN_EXPORT void create_render_target(const void* const viewer, intptr_t textureId, uint32_t width, uint32_t height) {
FLUTTER_PLUGIN_EXPORT void create_render_target(const void* const viewer, uint32_t textureId, uint32_t width, uint32_t height) {
((FilamentViewer*)viewer)->createRenderTarget(textureId, width, height);
}
FLUTTER_PLUGIN_EXPORT void delete_filament_viewer(const void* const viewer) {
FLUTTER_PLUGIN_EXPORT void destroy_filament_viewer(const void* const viewer) {
delete((FilamentViewer*)viewer);
}
FLUTTER_PLUGIN_EXPORT void set_background_color(const void* const viewer, const float r, const float g, const float b, const float a) {
((FilamentViewer*)viewer)->setBackgroundColor(r, g, b, a);
((FilamentViewer*)viewer)->setBackgroundColor(r, g, b, a);
}
FLUTTER_PLUGIN_EXPORT void clear_background_image(const void* const viewer) {
((FilamentViewer*)viewer)->clearBackgroundImage();
}
FLUTTER_PLUGIN_EXPORT void set_background_image(const void* const viewer, const char* path) {
((FilamentViewer*)viewer)->setBackgroundImage(path);
FLUTTER_PLUGIN_EXPORT void set_background_image(const void* const viewer, const char* path, bool fillHeight) {
((FilamentViewer*)viewer)->setBackgroundImage(path, fillHeight);
}
FLUTTER_PLUGIN_EXPORT void set_background_image_position(const void* const viewer, float x, float y, bool clamp) {
@@ -97,14 +97,14 @@ extern "C" {
return ((FilamentViewer*)viewer)->setCamera(asset, nodeName);
}
FLUTTER_PLUGIN_EXPORT void move_camera_to_asset(const void* const viewer, EntityId asset) {
((FilamentViewer*)viewer)->moveCameraToAsset(asset);
}
FLUTTER_PLUGIN_EXPORT void set_view_frustum_culling(const void* const viewer, bool enabled) {
((FilamentViewer*)viewer)->setViewFrustumCulling(enabled);
}
FLUTTER_PLUGIN_EXPORT void move_camera_to_asset(const void* const viewer, EntityId asset) {
((FilamentViewer*)viewer)->moveCameraToAsset(asset);
}
FLUTTER_PLUGIN_EXPORT void set_camera_focus_distance(const void* const viewer, float distance) {
((FilamentViewer*)viewer)->setCameraFocusDistance(distance);
}
@@ -147,8 +147,8 @@ extern "C" {
((FilamentViewer*)viewer)->destroySwapChain();
}
FLUTTER_PLUGIN_EXPORT void create_swap_chain(const void* const viewer, const void* const surface=nullptr, uint32_t width=0, uint32_t height=0) {
((FilamentViewer*)viewer)->createSwapChain(surface, width, height);
FLUTTER_PLUGIN_EXPORT void create_swap_chain(const void* const viewer, const void* const window, uint32_t width, uint32_t height) {
((FilamentViewer*)viewer)->createSwapChain(window, width, height);
}
FLUTTER_PLUGIN_EXPORT void update_viewport_and_camera_projection(const void* const viewer, uint32_t width, uint32_t height, float scaleFactor) {
@@ -348,14 +348,6 @@ extern "C" {
((FilamentViewer*)viewer)->clearAssets();
}
FLUTTER_PLUGIN_EXPORT void load_texture(void* assetManager, EntityId asset, const char* assetPath, int renderableIndex) {
// ((AssetManager*)assetManager)->loadTexture(assetPath, renderableIndex);
}
FLUTTER_PLUGIN_EXPORT void set_texture(void* assetManager, EntityId asset) {
// ((AssetManager*)assetManager)->setTexture();
}
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) {
return ((AssetManager*)assetManager)->setMaterialColor(asset, meshName, materialIndex, r, g, b, a);
}
@@ -370,7 +362,7 @@ extern "C" {
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);
}
}
FLUTTER_PLUGIN_EXPORT void set_scale(void* assetManager, EntityId asset, float scale) {
((AssetManager*)assetManager)->setScale(asset, scale);

View File

@@ -0,0 +1,111 @@
#include "ResourceBuffer.hpp"
#include "FilamentViewer.hpp"
#include "filament/LightManager.h"
#include "Log.hpp"
#include "ThreadPool.hpp"
#include <thread>
#include <functional>
using namespace polyvox;
#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default")))
class RenderLoop {
public:
explicit RenderLoop() {
_t = new std::thread([this]() {
while(!_stop) {
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(_access);
if(_tasks.empty()) {
_cond.wait_for(lock, std::chrono::duration<int, std::milli>(5));
continue;
}
task = std::move(_tasks.front());
_tasks.pop_front();
std::this_thread::sleep_for(
std::chrono::milliseconds(_frameIntervalInMilliseconds));
}
task();
}
});
}
~RenderLoop() {
_stop = true;
_t->join();
}
void setRendering(bool rendering) {
_rendering = rendering;
}
template<class Rt>
auto add_task(std::packaged_task<Rt()>& pt) -> std::future<Rt> {
std::unique_lock<std::mutex> lock(_access);
auto ret = pt.get_future();
_tasks.push_back([pt=std::make_shared<std::packaged_task<Rt()>>(std::move(pt))]{ (*pt)();});
_cond.notify_one();
return ret;
}
private:
bool _stop = false;
bool _rendering = false;
int _frameIntervalInMilliseconds = 1000 / 60;
std::mutex _access;
FilamentViewer* _viewer = nullptr;
std::thread* _t = nullptr;
std::condition_variable _cond;
std::deque<std::function<void()>> _tasks;
};
extern "C" {
#include "PolyvoxFilamentApi.h"
static RenderLoop* _rl;
FLUTTER_PLUGIN_EXPORT const void* create_filament_viewer_ffi(const void* context, const ResourceLoaderWrapper* const loader) {
if(!_rl) {
_rl = new RenderLoop();
}
std::packaged_task<const void*()> lambda([&]() mutable {
return (const void*) new FilamentViewer(context, loader);
});
auto fut = _rl->add_task(lambda);
fut.wait();
return fut.get();
}
FLUTTER_PLUGIN_EXPORT bool set_rendering(bool rendering) {
if(!_rl) {
return false;
}
_rl->setRendering(rendering);
return true;
}
FLUTTER_PLUGIN_EXPORT void render_ffi(void* const viewer) {
std::packaged_task<void()> lambda([&]() mutable {
render(viewer, 0);
});
auto fut = _rl->add_task(lambda);
fut.wait();
}
FLUTTER_PLUGIN_EXPORT void set_background_color_ffi(const void* const viewer, const float r, const float g, const float b, const float a) {
std::packaged_task<void()> lambda([&]() mutable {
set_background_color(viewer, r, g,b, a);
});
auto fut = _rl->add_task(lambda);
fut.wait();
}
}

View File

@@ -15,19 +15,24 @@ dependencies:
vector_math: ^2.1.2
plugin_platform_interface: ^2.0.0
tuple:
ffi:
wasm_ffi:
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^1.0.0
ffigen:
ffigen: ^9.0.1
ffigen:
output: 'lib/generated_bindings.dart'
headers:
entry-points:
- 'ios/include/PolyvoxFilamentApi.h'
- 'ios/include/PolyvoxFilamentFFIApi.h'
compiler-opts-automatic:
macos:
include-c-standard-library: false
flutter:
assets:

File diff suppressed because it is too large Load Diff