expose setToneMapping method

This commit is contained in:
Nick Fisher
2023-08-24 09:29:16 +08:00
parent 7687e54a0d
commit 482666da31
6 changed files with 51 additions and 8 deletions

View File

@@ -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();

View File

@@ -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);

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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<polyvox::ToneMapping>(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) {