diff --git a/ios/include/FilamentViewer.hpp b/ios/include/FilamentViewer.hpp index 0ad4ce9e..7728b6ee 100644 --- a/ios/include/FilamentViewer.hpp +++ b/ios/include/FilamentViewer.hpp @@ -76,9 +76,10 @@ namespace polyvox { Renderer* getRenderer(); + void setBackgroundColor(const float* color); void setBackgroundImage(const char* resourcePath); void setBackgroundImagePosition(float x, float y, bool clamp); - + 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); void setCameraModelMatrix(const float* const matrix); diff --git a/ios/include/PolyvoxFilamentApi.h b/ios/include/PolyvoxFilamentApi.h index c014881c..0915180d 100644 --- a/ios/include/PolyvoxFilamentApi.h +++ b/ios/include/PolyvoxFilamentApi.h @@ -27,6 +27,9 @@ void* filament_viewer_new(void* context, ResourceBuffer (*loadResource)(const ch void filament_viewer_delete(void* viewer); void create_render_target(void* viewer, uint32_t textureId, uint32_t width, uint32_t height); void set_background_image(void* viewer, const char* path); + +// color is rgba +void set_background_color(void* viewer, const float* color); void set_background_image_position(void* viewer, float x, float y, bool clamp); void load_skybox(void* viewer, const char* skyboxPath); void load_ibl(void* viewer, const char* iblPath); @@ -104,6 +107,7 @@ void set_position(void* asset, float x, float y, float z); void set_rotation(void* asset, float rads, float x, float y, float z); void set_scale(void* asset, float scale); +void set_camera_exposure(void* viewer, float aperture, float shutterSpeed, float sensitivity); void set_camera_position(void* viewer, float x, float y, float z); void set_camera_rotation(void* viewer, float rads, float x, float y, float z); void set_camera_model_matrix(void* viewer, const float* const matrix); diff --git a/ios/include/material/FileMaterialProvider.hpp b/ios/include/material/FileMaterialProvider.hpp new file mode 100644 index 00000000..885e1650 --- /dev/null +++ b/ios/include/material/FileMaterialProvider.hpp @@ -0,0 +1,89 @@ +#ifndef FILE_MATERIAL_PROVIDER +#define FILE_MATERIAL_PROVIDER + +#include +#include +#include + +namespace polyvox { + class FileMaterialProvider : public MaterialProvider { + + Material* _m; + const Material* _ms[1]; + Texture* mDummyTexture = nullptr; + + + public: + FileMaterialProvider(Engine* engine, void* const data, size_t size) { + _m = Material::Builder() + .package(data, size) + .build(*engine); + _ms[0] = _m; + unsigned char texels[4] = {}; + mDummyTexture = Texture::Builder() + .width(1).height(1) + .format(Texture::InternalFormat::RGBA8) + .build(*engine); + Texture::PixelBufferDescriptor pbd(texels, sizeof(texels), Texture::Format::RGBA, + Texture::Type::UBYTE); + mDummyTexture->setImage(*engine, 0, std::move(pbd)); + } + + filament::MaterialInstance* createMaterialInstance(MaterialKey* config, UvMap* uvmap, + const char* label = "material", const char* extras = nullptr) { + + auto getUvIndex = [uvmap](uint8_t srcIndex, bool hasTexture) -> int { + return hasTexture ? int(uvmap->at(srcIndex)) - 1 : -1; + }; + + Log("CREATING MATERIAL INSTANCE"); + auto instance = _m->createInstance(); + mat3f identity; + instance->setParameter("baseColorUvMatrix", identity); + instance->setParameter("normalUvMatrix", identity); + + instance->setParameter("baseColorIndex", getUvIndex(config->baseColorUV, config->hasBaseColorTexture)); + instance->setParameter("normalIndex", getUvIndex(config->normalUV, config->hasNormalTexture)); + if(config->hasNormalTexture) { + Log("HAS NORMAL TEXTURE"); + } else { + Log("NO NORMAL TEXTURE?"); + } + // TextureSampler sampler; + // instance->setParameter("normalMap", mDummyTexture, sampler); + // instance->setParameter("baseColorMap", mDummyTexture, sampler); + return instance; + } + + /** + * Creates or fetches a compiled Filament material corresponding to the given config. + */ + virtual Material* getMaterial(MaterialKey* config, UvMap* uvmap, const char* label = "material") { + return _m; + } + + /** + * 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() { + + } + + bool needsDummyData(filament::VertexAttribute attrib) const noexcept { + return true; + } + }; +} + +#endif \ No newline at end of file diff --git a/ios/include/material/UnlitMaterialProvider.hpp b/ios/include/material/UnlitMaterialProvider.hpp index 31872d5a..79a0a5f2 100644 --- a/ios/include/material/UnlitMaterialProvider.hpp +++ b/ios/include/material/UnlitMaterialProvider.hpp @@ -9,14 +9,24 @@ namespace polyvox { public: UnlitMaterialProvider(Engine* engine) { _m = Material::Builder() - .package( UNLITOPAQUE_UNLIT_OPAQUE_DATA, UNLITOPAQUE_UNLIT_OPAQUE_SIZE) + .package( UNLIT_OPAQUE_UNLIT_DATA, UNLIT_OPAQUE_UNLIT_SIZE) .build(*engine); + if(_m) { + Log("YES"); + } else { + Log("NO!"); + } _ms[0] = _m; } filament::MaterialInstance* createMaterialInstance(MaterialKey* config, UvMap* uvmap, const char* label = "material", const char* extras = nullptr) { - MaterialInstance* d = (MaterialInstance*)_m->getDefaultInstance(); + MaterialInstance* d = (MaterialInstance*)_m->getDefaultInstance(); + if(d) { + Log("YES"); + } else { + Log("NO INSTANCE!"); + } return d; } diff --git a/ios/src/FilamentViewer.cpp b/ios/src/FilamentViewer.cpp index 6e868dd5..a8e76177 100644 --- a/ios/src/FilamentViewer.cpp +++ b/ios/src/FilamentViewer.cpp @@ -74,12 +74,13 @@ extern "C" { #include "material/image_material.h" - #include "material/unlitopaque.h" + #include "material/unlit_opaque.h" } #include "FilamentViewer.hpp" #include "StreamBufferAdapter.hpp" #include "material/UnlitMaterialProvider.hpp" +#include "material/FileMaterialProvider.hpp" using namespace filament; using namespace filament::math; @@ -96,10 +97,10 @@ namespace polyvox { const double kNearPlane = 0.05; // 5 cm const double kFarPlane = 1000.0; // 1 km -// const float kScaleMultiplier = 100.0f; -const float kAperture = 16.0f; -const float kShutterSpeed = 1.0f / 125.0f; -const float kSensitivity = 100.0f; + +// const float kAperture = 1.0f; +// const float kShutterSpeed = 1.0f; +// const float kSensitivity = 50.0f; struct Vertex { filament::math::float2 position; uint32_t color; @@ -147,22 +148,29 @@ FilamentViewer::FilamentViewer(void* context, LoadResource loadResource, _view = _engine->createView(); decltype(_view->getBloomOptions()) opts; - opts.enabled = true; + opts.enabled = false; _view->setBloomOptions(opts); _view->setScene(_scene); _view->setCamera(_mainCamera); - ToneMapper *tm = new LinearToneMapper(); - colorGrading = ColorGrading::Builder().toneMapper(tm).build(*_engine); - delete tm; + // ToneMapper *tm = new LinearToneMapper(); + // colorGrading = ColorGrading::Builder().toneMapper(tm).build(*_engine); + // delete tm; - _view->setColorGrading(colorGrading); + // _view->setColorGrading(colorGrading); _cameraFocalLength = 28.0f; _mainCamera->setLensProjection(_cameraFocalLength, 1.0f, kNearPlane, kFarPlane); - _mainCamera->setExposure(kAperture, kShutterSpeed, kSensitivity); + // _mainCamera->setExposure(kAperture, kShutterSpeed, kSensitivity); + + 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); View::DynamicResolutionOptions options; options.enabled = false; @@ -170,7 +178,7 @@ FilamentViewer::FilamentViewer(void* context, LoadResource loadResource, // options.minScale = filament::math::float2{ minScale }; // options.maxScale = filament::math::float2{ maxScale }; // options.sharpness = sharpness; - options.quality = View::QualityLevel::HIGH; + options.quality = View::QualityLevel::ULTRA; _view->setDynamicResolutionOptions(options); @@ -179,12 +187,16 @@ FilamentViewer::FilamentViewer(void* context, LoadResource loadResource, _view->setMultiSampleAntiAliasingOptions(multiSampleAntiAliasingOptions); - _view->setAntiAliasing(AntiAliasing::FXAA); + _view->setAntiAliasing(AntiAliasing::NONE); - _materialProvider = - // new UnlitMaterialProvider(_engine); - gltfio::createUbershaderProvider( - _engine, UBERARCHIVE_DEFAULT_DATA, UBERARCHIVE_DEFAULT_SIZE); + // auto materialRb = _loadResource("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); + //_materialProvider = new UnlitMaterialProvider(_engine); + + _materialProvider = gltfio::createUbershaderProvider( + _engine, UBERARCHIVE_DEFAULT_DATA, UBERARCHIVE_DEFAULT_SIZE); + Log("Created material provider"); EntityManager &em = EntityManager::get(); _ncm = new NameComponentManager(em); @@ -398,6 +410,12 @@ void FilamentViewer::loadTextureFromPath(string path) { } +void FilamentViewer::setBackgroundColor(const float* color) { + _imageMaterial->setDefaultParameter("showImage", 0); + + _imageMaterial->setDefaultParameter("backgroundColor", RgbType::sRGB, float3(color[0], color[1], color[2])); +} + void FilamentViewer::setBackgroundImage(const char *resourcePath) { string resourcePathString(resourcePath); @@ -427,7 +445,7 @@ void FilamentViewer::setBackgroundImage(const char *resourcePath) { _imageMaterial->setDefaultParameter("showImage", 1); _imageMaterial->setDefaultParameter("backgroundColor", RgbType::sRGB, - float3(1.f)); + float3(0.f)); } @@ -657,6 +675,15 @@ void FilamentViewer::removeAsset(SceneAsset *asset) { mtx.unlock(); } +/// +/// Set the exposure for the current active camera. +/// +void FilamentViewer::setCameraExposure(float aperture, float shutterSpeed, float sensitivity) { + Camera& cam =_view->getCamera(); + Log("Setting aperture (%03f) shutterSpeed (%03f) and sensitivity (%03f)", aperture, shutterSpeed, sensitivity); + cam.setExposure(aperture, shutterSpeed, sensitivity); +} + /// /// Set the focal length of the active camera. /// diff --git a/ios/src/PolyvoxFilamentApi.cpp b/ios/src/PolyvoxFilamentApi.cpp index 3330e7e2..1a766dfe 100644 --- a/ios/src/PolyvoxFilamentApi.cpp +++ b/ios/src/PolyvoxFilamentApi.cpp @@ -23,6 +23,10 @@ extern "C" { delete((FilamentViewer*)viewer); } + void set_background_color(void* viewer, const float* color) { + ((FilamentViewer*)viewer)->setBackgroundColor(color); + } + void set_background_image(void* viewer, const char* path) { ((FilamentViewer*)viewer)->setBackgroundImage(path); } @@ -71,6 +75,10 @@ extern "C" { return ((FilamentViewer*)viewer)->setCamera((SceneAsset*)asset, nodeName); } + void set_camera_exposure(void* viewer, float aperture, float shutterSpeed, float sensitivity) { + ((FilamentViewer*)viewer)->setCameraExposure(aperture, shutterSpeed, sensitivity); + } + void set_camera_position(void* viewer, float x, float y, float z) { ((FilamentViewer*)viewer)->setCameraPosition(x, y, z); } diff --git a/linux/polyvox_filament_plugin.cc b/linux/polyvox_filament_plugin.cc index 479314b0..4fa7a232 100644 --- a/linux/polyvox_filament_plugin.cc +++ b/linux/polyvox_filament_plugin.cc @@ -44,15 +44,17 @@ struct _PolyvoxFilamentPlugin { double width; double height; + + bool _resizing = false; + bool _rendering = false; }; G_DEFINE_TYPE(PolyvoxFilamentPlugin, polyvox_filament_plugin, g_object_get_type()) -static bool _rendering = false; static gboolean on_frame_tick(GtkWidget* widget, GdkFrameClock* frame_clock, gpointer self) { - if(_rendering) { - PolyvoxFilamentPlugin* plugin = (PolyvoxFilamentPlugin*)self; + PolyvoxFilamentPlugin* plugin = (PolyvoxFilamentPlugin*)self; + if(plugin->_rendering) { render(plugin->_viewer, 0); fl_texture_registrar_mark_texture_frame_available(plugin->texture_registrar, plugin->texture); @@ -150,6 +152,15 @@ static FlMethodResponse* _set_background_image(PolyvoxFilamentPlugin* self, FlMe return FL_METHOD_RESPONSE(fl_method_success_response_new(result)); } +static FlMethodResponse* _set_background_color(PolyvoxFilamentPlugin* self, FlMethodCall* method_call) { + + const float* color = fl_value_get_float32_list(fl_method_call_get_args(method_call)); + set_background_color(self->_viewer, color); + + g_autoptr(FlValue) result = fl_value_new_string("OK"); + return FL_METHOD_RESPONSE(fl_method_success_response_new(result)); +} + static FlMethodResponse* _add_light(PolyvoxFilamentPlugin* self, FlMethodCall* method_call) { FlValue* args = fl_method_call_get_args(method_call); @@ -339,6 +350,16 @@ static FlMethodResponse* _set_camera_model_matrix(PolyvoxFilamentPlugin* self, F return FL_METHOD_RESPONSE(fl_method_success_response_new(result)); } +static FlMethodResponse* _set_camera_exposure(PolyvoxFilamentPlugin* self, FlMethodCall* method_call) { + FlValue* args = fl_method_call_get_args(method_call); + auto aperture = (float)fl_value_get_float(fl_value_get_list_value(args, 0)); + auto shutter_speed = (float)fl_value_get_float(fl_value_get_list_value(args, 1)); + auto sensitivity = (float)fl_value_get_float(fl_value_get_list_value(args, 2)); + set_camera_exposure(self->_viewer, aperture, shutter_speed, sensitivity); + g_autoptr(FlValue) result = fl_value_new_string("OK"); + return FL_METHOD_RESPONSE(fl_method_success_response_new(result)); +} + static FlMethodResponse* _set_camera_position(PolyvoxFilamentPlugin* self, FlMethodCall* method_call) { FlValue* args = fl_method_call_get_args(method_call); auto x = (float)fl_value_get_float(fl_value_get_list_value(args, 0)); @@ -363,7 +384,7 @@ static FlMethodResponse* _set_camera_rotation(PolyvoxFilamentPlugin* self, FlMet static FlMethodResponse* _set_rendering(PolyvoxFilamentPlugin* self, FlMethodCall* method_call) { FlValue* args = fl_method_call_get_args(method_call); - _rendering = (bool)fl_value_get_bool(args); + self->_rendering = (bool)fl_value_get_bool(args); g_autoptr(FlValue) result = fl_value_new_string("OK"); return FL_METHOD_RESPONSE(fl_method_success_response_new(result)); } @@ -536,7 +557,9 @@ static FlMethodResponse* _resize(PolyvoxFilamentPlugin* self, FlMethodCall* meth const double width = fl_value_get_float(fl_value_get_list_value(args, 0)); const double height = fl_value_get_float(fl_value_get_list_value(args, 1)); - if(width != self->width || height != self->height) { + if(!self->_resizing && (width != self->width || height != self->height)) { + self->_rendering = false; + self->_resizing = true; destroy_swap_chain(self->_viewer); @@ -550,6 +573,9 @@ static FlMethodResponse* _resize(PolyvoxFilamentPlugin* self, FlMethodCall* meth update_viewport_and_camera_projection(self->_viewer, width, height, 1.0f); std::cout << "Created new texture " << self->texture << std::endl; + + self->_resizing = false; + self->_rendering = true; } g_autoptr(FlValue) result = @@ -584,6 +610,8 @@ static void polyvox_filament_plugin_handle_method_call( render(self->_viewer, 0); g_autoptr(FlValue) result = fl_value_new_string("OK"); response = FL_METHOD_RESPONSE(fl_method_success_response_new(result)); + } else if(strcmp(method, "setBackgroundColor") == 0) { + response = _set_background_color(self, method_call); } else if(strcmp(method, "setBackgroundImage") == 0) { response = _set_background_image(self, method_call); } else if(strcmp(method, "addLight") == 0) { @@ -622,6 +650,8 @@ static void polyvox_filament_plugin_handle_method_call( response = _set_camera(self, method_call); } else if(strcmp(method, "setCameraModelMatrix") == 0) { response = _set_camera_model_matrix(self, method_call); + } else if(strcmp(method, "setCameraExposure") == 0) { + response = _set_camera_exposure(self, method_call); } else if(strcmp(method, "setCameraPosition") == 0) { response = _set_camera_position(self, method_call); } else if(strcmp(method, "setCameraRotation") == 0) {