From 482666da3131f2f63973358c49cec02e69c0ef98 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Thu, 24 Aug 2023 09:29:16 +0800 Subject: [PATCH] expose setToneMapping method --- ios/include/FilamentViewer.hpp | 6 ++++++ ios/include/PolyvoxFilamentApi.h | 1 + ios/src/FilamentViewer.cpp | 31 ++++++++++++++++++++++++------- ios/src/PolyvoxFilamentApi.cpp | 4 ++++ lib/filament_controller.dart | 8 ++++++++ linux/polyvox_filament_plugin.cc | 9 ++++++++- 6 files changed, 51 insertions(+), 8 deletions(-) diff --git a/ios/include/FilamentViewer.hpp b/ios/include/FilamentViewer.hpp index 6ae31988..70fd6eee 100644 --- a/ios/include/FilamentViewer.hpp +++ b/ios/include/FilamentViewer.hpp @@ -42,11 +42,17 @@ using namespace camutils; typedef int32_t EntityId; namespace polyvox { + + enum ToneMapping { + ACES, FILMIC, LINEAR + }; + class FilamentViewer { public: FilamentViewer(const void* context, const ResourceLoaderWrapper* const resourceLoaderWrapper); ~FilamentViewer(); + void setToneMapping(ToneMapping toneMapping); void loadSkybox(const char* const skyboxUri); void removeSkybox(); diff --git a/ios/include/PolyvoxFilamentApi.h b/ios/include/PolyvoxFilamentApi.h index 93363ef4..7f0be856 100644 --- a/ios/include/PolyvoxFilamentApi.h +++ b/ios/include/PolyvoxFilamentApi.h @@ -14,6 +14,7 @@ void clear_background_image(const void* const viewer); void set_background_image(const void* const viewer, const char *path); 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); void load_skybox(const void* const viewer, const char *skyboxPath); void load_ibl(const void* const viewer, const char *iblPath, float intensity); void remove_skybox(const void* const viewer); diff --git a/ios/src/FilamentViewer.cpp b/ios/src/FilamentViewer.cpp index a4420ea5..9a73b7b0 100644 --- a/ios/src/FilamentViewer.cpp +++ b/ios/src/FilamentViewer.cpp @@ -136,6 +136,8 @@ FilamentViewer::FilamentViewer(const void* context, const ResourceLoaderWrapper* Log("Main camera created"); _view = _engine->createView(); + setToneMapping(ToneMapping::ACES); + decltype(_view->getBloomOptions()) opts; opts.enabled = true; opts.strength = 0.6f; @@ -144,13 +146,6 @@ FilamentViewer::FilamentViewer(const void* context, const ResourceLoaderWrapper* _view->setScene(_scene); _view->setCamera(_mainCamera); -// ToneMapper *tm = new ACESToneMapper(); - ToneMapper *tm = new LinearToneMapper(); - colorGrading = ColorGrading::Builder().toneMapper(tm).build(*_engine); - delete tm; - - _view->setColorGrading(colorGrading); - _cameraFocalLength = 28.0f; _mainCamera->setLensProjection(_cameraFocalLength, 1.0f, kNearPlane, kFarPlane); @@ -243,6 +238,28 @@ FilamentViewer::FilamentViewer(const void* context, const ResourceLoaderWrapper* _scene->addEntity(imageEntity); } +void FilamentViewer::setToneMapping(ToneMapping toneMapping) { + + ToneMapper* tm; + switch(toneMapping) { + case ToneMapping::ACES: + tm = new ACESToneMapper(); + break; + case ToneMapping::LINEAR: + tm = new LinearToneMapper(); + break; + case ToneMapping::FILMIC: + tm = new FilmicToneMapper(); + break; + } + + + auto newColorGrading = ColorGrading::Builder().toneMapper(tm).build(*_engine); + _view->setColorGrading(newColorGrading); + _engine->destroy(colorGrading); + delete tm; +} + void FilamentViewer::setFrameInterval(float frameInterval) { Renderer::FrameRateOptions fro; fro.interval = frameInterval; diff --git a/ios/src/PolyvoxFilamentApi.cpp b/ios/src/PolyvoxFilamentApi.cpp index 7113408a..808bdb36 100644 --- a/ios/src/PolyvoxFilamentApi.cpp +++ b/ios/src/PolyvoxFilamentApi.cpp @@ -48,6 +48,10 @@ extern "C" { ((FilamentViewer*)viewer)->setBackgroundImagePosition(x, y, clamp); } + FLUTTER_PLUGIN_EXPORT void set_tone_mapping(const void* const viewer, int toneMapping) { + ((FilamentViewer*)viewer)->setToneMapping((ToneMapping)toneMapping); + } + FLUTTER_PLUGIN_EXPORT void load_skybox(const void* const viewer, const char* skyboxPath) { ((FilamentViewer*)viewer)->loadSkybox(skyboxPath); } diff --git a/lib/filament_controller.dart b/lib/filament_controller.dart index f5eac296..5c5e4b60 100644 --- a/lib/filament_controller.dart +++ b/lib/filament_controller.dart @@ -13,6 +13,8 @@ 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"); @@ -358,6 +360,12 @@ class FilamentController { } } + void setToneMapping(ToneMapper mapper) async { + if (!await _channel.invokeMethod("setToneMapping", mapper.index)) { + throw Exception("Failed to set tone mapper"); + } + } + void setCameraFocalLength(double focalLength) async { await _channel.invokeMethod("setCameraFocalLength", focalLength); } diff --git a/linux/polyvox_filament_plugin.cc b/linux/polyvox_filament_plugin.cc index 94eafe09..b36f7745 100644 --- a/linux/polyvox_filament_plugin.cc +++ b/linux/polyvox_filament_plugin.cc @@ -696,7 +696,12 @@ static FlMethodResponse* _get_morph_target_names(PolyvoxFilamentPlugin* self, Fl return FL_METHOD_RESPONSE(fl_method_success_response_new(result)); } - +static FlMethodResponse* _set_tone_mapping(PolyvoxFilamentPlugin* self, FlMethodCall* method_call) { + FlValue* args = fl_method_call_get_args(method_call); + polyvox::ToneMapping toneMapping = static_cast(fl_value_get_int(args)); + set_tone_mapping(self->viewer, toneMapping); + return FL_METHOD_RESPONSE(fl_method_success_response_new(fl_value_new_bool(true))); +} // Called when a method call is received from Flutter. static void polyvox_filament_plugin_handle_method_call( @@ -715,6 +720,8 @@ static void polyvox_filament_plugin_handle_method_call( response = _update_viewport_and_camera_projection(self, method_call); } else if(strcmp(method, "getAssetManager") ==0){ response = _get_asset_manager(self, method_call); + } else if(strcmp(method, "setToneMapping") == 0) { + response = _set_tone_mapping(self, method_call); } else if(strcmp(method, "resize") == 0) { response = _resize(self, method_call); } else if(strcmp(method, "getContext") == 0) {