diff --git a/include/vierkant/Material.hpp b/include/vierkant/Material.hpp index 7ef5c25b..a19b61af 100644 --- a/include/vierkant/Material.hpp +++ b/include/vierkant/Material.hpp @@ -4,6 +4,8 @@ #pragma once +#include + #include "vierkant/Geometry.hpp" #include "vierkant/Image.hpp" #include "vierkant/Pipeline.hpp" @@ -11,74 +13,69 @@ namespace vierkant { -DEFINE_CLASS_PTR(Material) +//! define resource-identifiers +DEFINE_NAMED_UUID(MaterialId) +DEFINE_NAMED_UUID(TextureSourceId) +DEFINE_NAMED_UUID(SamplerId) -class Material +enum class BlendMode : uint8_t { -public: - enum class BlendMode : uint8_t - { - Opaque = 0, - Blend = 1, - Mask = 2 - }; - - enum class CullMode : uint8_t - { - None = 0, - Front, - Back, - FrontAndBack - }; - - enum TextureType : uint32_t - { - Color = 0x001, - Normal = 0x002, - Ao_rough_metal = 0x004, - Emission = 0x008, - Displacement = 0x010, - VolumeThickness = 0x020, - Transmission = 0x040, - Clearcoat = 0x080, - SheenColor = 0x100, - SheenRoughness = 0x200, - Iridescence = 0x400, - IridescenceThickness = 0x800, - Specular = 0x1000, - SpecularColor = 0x2000, - Environment = 0x4000 - }; + Opaque = 0, + Blend = 1, + Mask = 2 +}; - static MaterialPtr create() { return MaterialPtr(new Material()); }; +enum class CullMode : uint8_t +{ + None = 0, + Front, + Back, + FrontAndBack +}; - [[nodiscard]] std::size_t hash() const; +enum class TextureType : uint32_t +{ + Color = 0x001, + Normal = 0x002, + Ao_rough_metal = 0x004, + Emission = 0x008, + Displacement = 0x010, + VolumeThickness = 0x020, + Transmission = 0x040, + Clearcoat = 0x080, + SheenColor = 0x100, + SheenRoughness = 0x200, + Iridescence = 0x400, + IridescenceThickness = 0x800, + Specular = 0x1000, + SpecularColor = 0x2000, + Environment = 0x4000 +}; +struct material_t +{ + vierkant::MaterialId id; std::string name; - glm::vec4 color = glm::vec4(1); - - glm::vec4 emission = glm::vec4(0, 0, 0, 1); - - float metalness = 0.f; + glm::vec4 base_color = glm::vec4(1.f); + glm::vec3 emission = glm::vec3(0.f); + float emissive_strength = 1.f; float roughness = 1.f; - + float metalness = 0.f; float occlusion = 1.f; - bool two_sided = false; - //! null-surface (skip surface interaction) bool null_surface = false; - BlendMode blend_mode = BlendMode::Opaque; - - float alpha_cutoff = 0.5f; - - float transmission = 0.f; + bool twosided = false; + // transmission + float ior = 1.5f; glm::vec3 attenuation_color = glm::vec3(1.f); + // volumes + float transmission = 0.f; float attenuation_distance = std::numeric_limits::infinity(); // phase-function asymmetry parameter (forward- vs. back-scattering) [-1, 1] @@ -87,14 +84,22 @@ class Material // ratio of scattering vs. absorption (sigma_s / sigma_t) float scattering_ratio = 0.f; - float ior = 1.5f; + // idk rasterizer only thingy + float thickness = 1.f; - float clearcoat_factor = 0.f; + vierkant::BlendMode blend_mode = vierkant::BlendMode::Opaque; + float alpha_cutoff = 0.5f; + // specular + float specular_factor = 1.f; + glm::vec3 specular_color = glm::vec3(1.f); + + // clearcoat + float clearcoat_factor = 0.f; float clearcoat_roughness_factor = 0.f; + // sheen glm::vec3 sheen_color = glm::vec3(0.f); - float sheen_roughness = 0.f; // iridescence @@ -104,19 +109,65 @@ class Material // iridescence thin-film layer given in nanometers (nm) glm::vec2 iridescence_thickness_range = {100.f, 400.f}; - bool depth_test = true; + // optional texture-transform (todo: per image) + glm::mat4 texture_transform = glm::mat4(1); - bool depth_write = true; + // maps TextureType to a TextureId/SamplerId. sorted in enum order, which is important in other places. + std::map textures; + std::map samplers; +}; - VkCullModeFlagBits cull_mode = VK_CULL_MODE_BACK_BIT; +bool operator==(const vierkant::material_t &lhs, const vierkant::material_t &rhs); +inline bool operator!=(const vierkant::material_t &lhs, const vierkant::material_t &rhs) { return !(lhs == rhs); } - std::map textures; +struct texture_sampler_t +{ + enum class Filter + { + NEAREST = 0, + LINEAR, + CUBIC + }; - // optional texture-transform (todo: per image) - glm::mat4 texture_transform = glm::mat4(1); + enum class AddressMode + { + REPEAT = 0, + MIRRORED_REPEAT, + CLAMP_TO_EDGE, + CLAMP_TO_BORDER, + MIRROR_CLAMP_TO_EDGE, + }; + + AddressMode address_mode_u = AddressMode::REPEAT; + AddressMode address_mode_v = AddressMode::REPEAT; + + Filter min_filter = Filter::LINEAR; + Filter mag_filter = Filter::LINEAR; + glm::mat4 transform = glm::mat4(1); +}; + +DEFINE_CLASS_PTR(Material) + +class Material +{ +public: + static MaterialPtr create() { return MaterialPtr(new Material()); }; + + material_t m; + std::map textures; private: Material() = default; }; -}// namespace vierkant \ No newline at end of file +}// namespace vierkant + +// template specializations for hashing +namespace std +{ +template<> +struct hash +{ + size_t operator()(vierkant::material_t const &m) const; +}; +}// namespace std \ No newline at end of file diff --git a/include/vierkant/Object3D.hpp b/include/vierkant/Object3D.hpp index 6f8cf18e..799e53f0 100644 --- a/include/vierkant/Object3D.hpp +++ b/include/vierkant/Object3D.hpp @@ -78,7 +78,7 @@ class Object3D : public std::enable_shared_from_this template inline T &add_component(const T &component = {}) { - if(auto reg = m_registry.lock()) { return reg->template emplace(m_entity, component); } + if(auto reg = m_registry.lock()) { return reg->template emplace_or_replace(m_entity, component); } throw std::runtime_error("error adding component: no registry defined"); } diff --git a/include/vierkant/RayBuilder.hpp b/include/vierkant/RayBuilder.hpp index 2ff758dc..9b963d22 100644 --- a/include/vierkant/RayBuilder.hpp +++ b/include/vierkant/RayBuilder.hpp @@ -85,7 +85,7 @@ class RayBuilder uint32_t texture_type_flags = 0; - uint32_t blend_mode = static_cast(Material::BlendMode::Opaque); + uint32_t blend_mode = static_cast(vierkant::BlendMode::Opaque); float alpha_cutoff = 0.5f; diff --git a/include/vierkant/drawable.hpp b/include/vierkant/drawable.hpp index c6725638..1281f1f6 100644 --- a/include/vierkant/drawable.hpp +++ b/include/vierkant/drawable.hpp @@ -32,7 +32,7 @@ struct alignas(16) material_struct_t float ambient = 1.f; - uint32_t blend_mode = static_cast(Material::BlendMode::Opaque); + uint32_t blend_mode = static_cast(vierkant::BlendMode::Opaque); float alpha_cutoff = 0.5f; diff --git a/include/vierkant/model/model_loading.hpp b/include/vierkant/model/model_loading.hpp index a7aa8f62..6a30e137 100644 --- a/include/vierkant/model/model_loading.hpp +++ b/include/vierkant/model/model_loading.hpp @@ -8,7 +8,6 @@ #include #include -#include #include #include @@ -19,8 +18,9 @@ namespace vierkant { -DEFINE_NAMED_UUID(TextureSourceId) -DEFINE_NAMED_UUID(SamplerId) + +//! define resource-identifiers +DEFINE_NAMED_UUID(MeshId) //! contains uncompressed or BC7-compressed images using texture_variant_t = std::variant; @@ -35,86 +35,6 @@ using geometry_variant_t = std::variant::infinity(); - - // idk rasterizer only thingy - float thickness = 1.f; - - vierkant::Material::BlendMode blend_mode = vierkant::Material::BlendMode::Opaque; - float alpha_cutoff = 0.5f; - - bool twosided = false; - - // specular - float specular_factor = 1.f; - glm::vec3 specular_color = glm::vec3(1.f); - - // clearcoat - float clearcoat_factor = 0.f; - float clearcoat_roughness_factor = 0.f; - - // sheen - glm::vec3 sheen_color = glm::vec3(0.f); - float sheen_roughness = 0.f; - - // iridescence - float iridescence_factor = 0.f; - float iridescence_ior = 1.3f; - - // iridescence thin-film layer given in nanometers (nm) - glm::vec2 iridescence_thickness_range = {100.f, 400.f}; - - // optional texture-transform (todo: per image) - glm::mat4 texture_transform = glm::mat4(1); - - // maps TextureType to a TextureId/SamplerId. sorted in enum order, which is important in other places. - std::map textures; - std::map samplers; -}; - -struct texture_sampler_t -{ - enum class Filter - { - NEAREST = 0, - LINEAR, - CUBIC - }; - - enum class AddressMode - { - REPEAT = 0, - MIRRORED_REPEAT, - CLAMP_TO_EDGE, - CLAMP_TO_BORDER, - MIRROR_CLAMP_TO_EDGE, - }; - - AddressMode address_mode_u = AddressMode::REPEAT; - AddressMode address_mode_v = AddressMode::REPEAT; - - Filter min_filter = Filter::LINEAR; - Filter mag_filter = Filter::LINEAR; - glm::mat4 transform = glm::mat4(1); -}; - enum class LightType : uint32_t { Omni = 0, @@ -151,7 +71,7 @@ struct mesh_assets_t geometry_variant_t geometry_data; //! common materials for all submeshes - std::vector materials; + std::vector materials; //! common textures for all materials std::unordered_map textures; diff --git a/src/DrawContext.cpp b/src/DrawContext.cpp index 302b8216..03f8f4aa 100644 --- a/src/DrawContext.cpp +++ b/src/DrawContext.cpp @@ -124,12 +124,10 @@ DrawContext::DrawContext(vierkant::DevicePtr device) : m_device(std::move(device box->tangents.clear(); box->normals.clear(); vierkant::mesh_component_t mesh_component = {vierkant::Mesh::create_from_geometry(m_device, box, {})}; - auto &mat = mesh_component.mesh->materials.front(); - mat->depth_write = false; - mat->depth_test = true; - mat->cull_mode = VK_CULL_MODE_FRONT_BIT; - mat->textures[vierkant::Material::TextureType::Environment] = {}; m_drawable_skybox = vierkant::create_drawables(mesh_component, drawable_params).front(); + m_drawable_skybox.pipeline_format.depth_write = false; + m_drawable_skybox.pipeline_format.depth_test = true; + m_drawable_skybox.pipeline_format.cull_mode = VK_CULL_MODE_FRONT_BIT; } } diff --git a/src/Material.cpp b/src/Material.cpp index 2fd160a0..49bf02b3 100644 --- a/src/Material.cpp +++ b/src/Material.cpp @@ -8,41 +8,85 @@ namespace vierkant { -std::size_t Material::hash() const +bool operator==(const vierkant::material_t &lhs, const vierkant::material_t &rhs) +{ + if(lhs.id != rhs.id) { return false; } + if(lhs.name != rhs.name) { return false; } + if(lhs.base_color != rhs.base_color) { return false; } + if(lhs.emission != rhs.emission) { return false; } + if(lhs.emissive_strength != rhs.emissive_strength) { return false; } + if(lhs.roughness != rhs.roughness) { return false; } + if(lhs.metalness != rhs.metalness) { return false; } + if(lhs.occlusion != rhs.occlusion) { return false; } + if(lhs.null_surface != rhs.null_surface) { return false; } + if(lhs.twosided != rhs.twosided) { return false; } + if(lhs.ior != rhs.ior) { return false; } + if(lhs.attenuation_color != rhs.attenuation_color) { return false; } + if(lhs.transmission != rhs.transmission) { return false; } + if(lhs.attenuation_distance != rhs.attenuation_distance) { return false; } + if(lhs.phase_asymmetry_g != rhs.phase_asymmetry_g) { return false; } + if(lhs.scattering_ratio != rhs.scattering_ratio) { return false; } + if(lhs.thickness != rhs.thickness) { return false; } + if(lhs.blend_mode != rhs.blend_mode) { return false; } + if(lhs.alpha_cutoff != rhs.alpha_cutoff) { return false; } + if(lhs.specular_factor != rhs.specular_factor) { return false; } + if(lhs.specular_color != rhs.specular_color) { return false; } + if(lhs.clearcoat_factor != rhs.clearcoat_factor) { return false; } + if(lhs.clearcoat_roughness_factor != rhs.clearcoat_roughness_factor) { return false; } + if(lhs.sheen_color != rhs.sheen_color) { return false; } + if(lhs.sheen_roughness != rhs.sheen_roughness) { return false; } + if(lhs.iridescence_factor != rhs.iridescence_factor) { return false; } + if(lhs.iridescence_ior != rhs.iridescence_ior) { return false; } + if(lhs.iridescence_thickness_range != rhs.iridescence_thickness_range) { return false; } + if(lhs.texture_transform != rhs.texture_transform) { return false; } + if(lhs.textures != rhs.textures) { return false; } + if(lhs.samplers != rhs.samplers) { return false; } + return true; +} + +}// namespace vierkant + +using vierkant::hash_combine; + +size_t std::hash::operator()(vierkant::material_t const &m) const { size_t h = 0; - hash_combine(h, name); - hash_combine(h, color); - hash_combine(h, emission); - hash_combine(h, metalness); - hash_combine(h, roughness); - hash_combine(h, occlusion); - hash_combine(h, two_sided); - hash_combine(h, blend_mode); - hash_combine(h, alpha_cutoff); - hash_combine(h, transmission); - hash_combine(h, attenuation_color); - hash_combine(h, attenuation_distance); - hash_combine(h, ior); - hash_combine(h, clearcoat_factor); - hash_combine(h, clearcoat_roughness_factor); - hash_combine(h, sheen_color); - hash_combine(h, sheen_roughness); - hash_combine(h, iridescence_factor); - hash_combine(h, iridescence_ior); - hash_combine(h, iridescence_thickness_range); - hash_combine(h, depth_test); - hash_combine(h, depth_write); - hash_combine(h, cull_mode); + hash_combine(h, m.id); + hash_combine(h, m.name); + hash_combine(h, m.base_color); + hash_combine(h, m.emission); + hash_combine(h, m.emissive_strength); + hash_combine(h, m.roughness); + hash_combine(h, m.metalness); + hash_combine(h, m.occlusion); + hash_combine(h, m.null_surface); + hash_combine(h, m.twosided); + hash_combine(h, m.blend_mode); + hash_combine(h, m.alpha_cutoff); + hash_combine(h, m.transmission); + hash_combine(h, m.phase_asymmetry_g); + hash_combine(h, m.scattering_ratio); + hash_combine(h, m.attenuation_color); + hash_combine(h, m.attenuation_distance); + hash_combine(h, m.ior); + hash_combine(h, m.clearcoat_factor); + hash_combine(h, m.clearcoat_roughness_factor); + hash_combine(h, m.sheen_color); + hash_combine(h, m.sheen_roughness); + hash_combine(h, m.iridescence_factor); + hash_combine(h, m.iridescence_ior); + hash_combine(h, m.iridescence_thickness_range); + hash_combine(h, m.texture_transform); - for(const auto &[type, tex] : textures) + for(const auto &[type, tex_id]: m.textures) { hash_combine(h, type); - hash_combine(h, tex); + hash_combine(h, tex_id); + } + for(const auto &[type, sampler_id]: m.samplers) + { + hash_combine(h, type); + hash_combine(h, sampler_id); } - - hash_combine(h, texture_transform); return h; } - -}// namespace vierkant diff --git a/src/PBRDeferred.cpp b/src/PBRDeferred.cpp index 3f7f49ca..ec655946 100644 --- a/src/PBRDeferred.cpp +++ b/src/PBRDeferred.cpp @@ -386,7 +386,7 @@ void PBRDeferred::update_recycling(const SceneConstPtr &scene, const CameraPtr & { for(const auto &mat: mesh->materials) { - auto h = mat->hash(); + auto h = std::hash()(mat->m); if(frame_asset.material_hashes[mat] != h) { materials_unchanged = false; } frame_asset.material_hashes[mat] = h; } diff --git a/src/RayBuilder.cpp b/src/RayBuilder.cpp index 93b9895e..bb24b691 100644 --- a/src/RayBuilder.cpp +++ b/src/RayBuilder.cpp @@ -134,7 +134,8 @@ RayBuilder::build_result_t RayBuilder::create_mesh_structures(const create_mesh_ const auto &entry = params.mesh->entries[i]; const auto &lod_0 = entry.lods.front(); - const auto &material = params.mesh->materials[entry.material_index]; + const auto &mesh_material = params.mesh->materials[entry.material_index]; + const auto &material = mesh_material->m; // throw on non-triangle entries if(entry.primitive_type != VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST) @@ -156,7 +157,7 @@ RayBuilder::build_result_t RayBuilder::create_mesh_structures(const create_mesh_ VkMicromapUsageEXT micromap_usage = {}; // attach an existing opacity-micromap for this geometry - if(params.micromap_assets.size() > i && material->blend_mode == vierkant::Material::BlendMode::Mask) + if(params.micromap_assets.size() > i && material.blend_mode == vierkant::BlendMode::Mask) { const auto &optional_micromap_asset = params.micromap_assets[i]; @@ -182,7 +183,7 @@ RayBuilder::build_result_t RayBuilder::create_mesh_structures(const create_mesh_ auto &geometry = geometries[i]; geometry.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR; - geometry.flags = material->blend_mode == vierkant::Material::BlendMode::Opaque ? VK_GEOMETRY_OPAQUE_BIT_KHR : 0; + geometry.flags = material.blend_mode == vierkant::BlendMode::Opaque ? VK_GEOMETRY_OPAQUE_BIT_KHR : 0; geometry.geometryType = VK_GEOMETRY_TYPE_TRIANGLES_KHR; geometry.geometry.triangles = triangles; @@ -437,6 +438,7 @@ RayBuilder::scene_acceleration_data_t RayBuilder::create_toplevel(const scene_ac const auto &mesh_entry = mesh->entries[i]; const auto &lod = mesh_entry.lods.front(); const auto &mesh_material = mesh->materials[mesh_entry.material_index]; + const auto &m = mesh->materials[mesh_entry.material_index]->m; const auto &asset = acceleration_assets[i]; @@ -450,7 +452,7 @@ RayBuilder::scene_acceleration_data_t RayBuilder::create_toplevel(const scene_ac // instance flags VkGeometryInstanceFlagsKHR instance_flags = - mesh_material->two_sided ? VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR : 0; + m.twosided ? VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR : 0; // store next entry-index size_t entry_idx = entries.size(); @@ -466,49 +468,49 @@ RayBuilder::scene_acceleration_data_t RayBuilder::create_toplevel(const scene_ac { material_indices[mesh_material] = materials.size(); RayBuilder::material_struct_t material = {}; - material.color = mesh_material->color; - material.emission = mesh_material->emission; - material.roughness = mesh_material->roughness; - material.metalness = mesh_material->metalness; - material.transmission = mesh_material->transmission; - material.ior = mesh_material->ior; - material.attenuation_distance = mesh_material->attenuation_distance; - material.attenuation_color = mesh_material->attenuation_color; - material.clearcoat_factor = mesh_material->clearcoat_factor; - material.clearcoat_roughness_factor = mesh_material->clearcoat_roughness_factor; - material.sheen_color = {mesh_material->sheen_color, 0.f}; - material.sheen_roughness = mesh_material->sheen_roughness; - - material.blend_mode = static_cast(mesh_material->blend_mode); - material.alpha_cutoff = mesh_material->alpha_cutoff; - material.two_sided = mesh_material->two_sided; - material.null_surface = mesh_material->null_surface; - material.phase_asymmetry_g = mesh_material->phase_asymmetry_g; - material.scattering_ratio = mesh_material->scattering_ratio; - - material.iridescence_strength = mesh_material->iridescence_factor; - material.iridescence_ior = mesh_material->iridescence_ior; - material.iridescence_thickness_range = mesh_material->iridescence_thickness_range; + material.color = m.base_color; + material.emission = {m.emission, m.emissive_strength}; + material.roughness = m.roughness; + material.metalness = m.metalness; + material.transmission = m.transmission; + material.ior = m.ior; + material.attenuation_distance = m.attenuation_distance; + material.attenuation_color = m.attenuation_color; + material.clearcoat_factor = m.clearcoat_factor; + material.clearcoat_roughness_factor = m.clearcoat_roughness_factor; + material.sheen_color = {m.sheen_color, 0.f}; + material.sheen_roughness = m.sheen_roughness; + + material.blend_mode = static_cast(m.blend_mode); + material.alpha_cutoff = m.alpha_cutoff; + material.two_sided = m.twosided; + material.null_surface = m.null_surface; + material.phase_asymmetry_g = m.phase_asymmetry_g; + material.scattering_ratio = m.scattering_ratio; + + material.iridescence_strength = m.iridescence_factor; + material.iridescence_ior = m.iridescence_ior; + material.iridescence_thickness_range = m.iridescence_thickness_range; for(auto &[type_flag, tex]: mesh_material->textures) { - material.texture_type_flags |= type_flag; + material.texture_type_flags |= static_cast(type_flag); uint32_t texture_index = textures.size(); textures.push_back(tex); switch(type_flag) { - case vierkant::Material::TextureType::Color: material.albedo_index = texture_index; break; + case vierkant::TextureType::Color: material.albedo_index = texture_index; break; - case vierkant::Material::TextureType::Normal: material.normalmap_index = texture_index; break; + case vierkant::TextureType::Normal: material.normalmap_index = texture_index; break; - case vierkant::Material::TextureType::Emission: material.emission_index = texture_index; break; + case vierkant::TextureType::Emission: material.emission_index = texture_index; break; - case vierkant::Material::TextureType::Ao_rough_metal: + case vierkant::TextureType::Ao_rough_metal: material.ao_rough_metal_index = texture_index; break; - case vierkant::Material::TextureType::Transmission: + case vierkant::TextureType::Transmission: material.transmission_index = texture_index; break; default: break; @@ -519,7 +521,7 @@ RayBuilder::scene_acceleration_data_t RayBuilder::create_toplevel(const scene_ac RayBuilder::entry_t top_level_entry = {}; top_level_entry.transform = transform; - top_level_entry.texture_matrix = mesh_material->texture_transform; + top_level_entry.texture_matrix = m.texture_transform; top_level_entry.buffer_index = mesh_buffer_indices[vertex_buffer_address]; top_level_entry.material_index = material_indices[mesh_material]; top_level_entry.vertex_offset = mesh_entry.vertex_offset; diff --git a/src/drawable.cpp b/src/drawable.cpp index f184ce21..65b00cb5 100644 --- a/src/drawable.cpp +++ b/src/drawable.cpp @@ -45,7 +45,8 @@ std::vector create_drawables(const vierkant::mesh_componen // sanity check material-index if(entry.material_index >= mesh->materials.size()) { continue; } - const auto &material = mesh->materials[entry.material_index]; + const auto &mesh_material = mesh->materials[entry.material_index]; + const auto &material = mesh_material->m; // acquire ref for mesh-drawable vierkant::drawable_t drawable = {}; @@ -55,17 +56,17 @@ std::vector create_drawables(const vierkant::mesh_componen // combine mesh- with entry-transform drawable.matrices.transform = params.transform * (node_transforms.empty() ? entry.transform : node_transforms[entry.node_index]); - drawable.matrices.texture = material->texture_transform; + drawable.matrices.texture = material.texture_transform; // material params - drawable.material.color = material->color; - drawable.material.emission = material->emission; - drawable.material.ambient = material->occlusion; - drawable.material.roughness = material->roughness; - drawable.material.metalness = material->metalness; - drawable.material.blend_mode = static_cast(material->blend_mode); - drawable.material.alpha_cutoff = material->alpha_cutoff; - drawable.material.two_sided = material->two_sided; + drawable.material.color = material.base_color; + drawable.material.emission.xyz() = material.emission; + drawable.material.ambient = material.occlusion; + drawable.material.roughness = material.roughness; + drawable.material.metalness = material.metalness; + drawable.material.blend_mode = static_cast(material.blend_mode); + drawable.material.alpha_cutoff = material.alpha_cutoff; + drawable.material.two_sided = material.twosided; drawable.base_index = lod_0.base_index; drawable.num_indices = lod_0.num_indices; @@ -78,10 +79,8 @@ std::vector create_drawables(const vierkant::mesh_componen drawable.num_meshlets = lod_0.num_meshlets; drawable.pipeline_format.primitive_topology = entry.primitive_type; - drawable.pipeline_format.blend_state.blendEnable = material->blend_mode == vierkant::Material::BlendMode::Blend; - drawable.pipeline_format.depth_test = material->depth_test; - drawable.pipeline_format.depth_write = material->depth_write; - drawable.pipeline_format.cull_mode = material->two_sided ? VK_CULL_MODE_NONE : material->cull_mode; + drawable.pipeline_format.blend_state.blendEnable = material.blend_mode == vierkant::BlendMode::Blend; + drawable.pipeline_format.cull_mode = material.twosided ? VK_CULL_MODE_NONE : VK_CULL_MODE_BACK_BIT; if(!drawable.use_own_buffers) { @@ -126,17 +125,17 @@ std::vector create_drawables(const vierkant::mesh_componen drawable.pipeline_format.attribute_descriptions = attribute_descriptions; // textures - if(!material->textures.empty()) + if(!mesh_material->textures.empty()) { vierkant::descriptor_t &desc_texture = drawable.descriptors[Rasterizer::BINDING_TEXTURES]; desc_texture.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; desc_texture.stage_flags = VK_SHADER_STAGE_FRAGMENT_BIT; - for(auto &[type_flag, tex]: material->textures) + for(auto &[type_flag, tex]: mesh_material->textures) { if(tex) { - drawable.material.texture_type_flags |= type_flag; + drawable.material.texture_type_flags |= static_cast(type_flag); desc_texture.images.push_back(tex); } } diff --git a/src/imgui/imgui_util.cpp b/src/imgui/imgui_util.cpp index aa1bc9f6..cdfc6d08 100644 --- a/src/imgui/imgui_util.cpp +++ b/src/imgui/imgui_util.cpp @@ -41,7 +41,7 @@ struct scoped_child_window_t } }; -void draw_material_ui(const MaterialPtr &material); +void draw_material_ui(const MaterialPtr &mesh_material); void draw_light_ui(vierkant::model::lightsource_t &light); @@ -558,7 +558,7 @@ void draw_scene_ui(const ScenePtr &scene, CameraPtr &cam, std::setname.empty() ? std::to_string(i) : mat->name; + auto mat_name = mat->m.name.empty() ? std::to_string(i) : mat->m.name; if(mat && ImGui::TreeNode((void *) (mat.get()), "%s", mat_name.c_str())) { @@ -623,14 +623,14 @@ void draw_scene_ui(const ScenePtr &scene, CameraPtr &cam, std::settextures.find(type); + auto draw_texture = [&mesh_material, w](vierkant::TextureType type, const std::string &text) { + auto it = mesh_material->textures.find(type); - if(it != material->textures.end()) + if(it != mesh_material->textures.end()) { const auto &img = it->second; @@ -646,34 +646,35 @@ void draw_material_ui(const MaterialPtr &material) } }; - ImGui::BulletText("name: %s", material->name.c_str()); + auto &material = mesh_material->m; + ImGui::BulletText("name: %s", material.name.c_str()); ImGui::Separator(); // base color - ImGui::ColorEdit4("base color", glm::value_ptr(material->color)); - draw_texture(vierkant::Material::TextureType::Color, "base color"); + ImGui::ColorEdit4("base color", glm::value_ptr(material.base_color)); + draw_texture(vierkant::TextureType::Color, "base color"); // emissive color - ImGui::ColorEdit3("emission color", glm::value_ptr(material->emission)); - ImGui::InputFloat("emissive strength", &material->emission.w); - draw_texture(vierkant::Material::TextureType::Emission, "emission"); + ImGui::ColorEdit3("emission color", glm::value_ptr(material.emission)); + ImGui::InputFloat("emissive strength", &material.emissive_strength); + draw_texture(vierkant::TextureType::Emission, "emission"); // normalmap - draw_texture(vierkant::Material::TextureType::Normal, "normals"); + draw_texture(vierkant::TextureType::Normal, "normals"); ImGui::Separator(); // roughness - ImGui::SliderFloat("roughness", &material->roughness, 0.f, 1.f); + ImGui::SliderFloat("roughness", &material.roughness, 0.f, 1.f); // metalness - ImGui::SliderFloat("metalness", &material->metalness, 0.f, 1.f); + ImGui::SliderFloat("metalness", &material.metalness, 0.f, 1.f); // occlusion - ImGui::SliderFloat("occlusion", &material->occlusion, 0.f, 1.f); + ImGui::SliderFloat("occlusion", &material.occlusion, 0.f, 1.f); // ambient-occlusion / roughness / metalness - draw_texture(vierkant::Material::TextureType::Ao_rough_metal, "occlusion / roughness / metalness"); + draw_texture(vierkant::TextureType::Ao_rough_metal, "occlusion / roughness / metalness"); ImGui::Separator(); @@ -681,70 +682,69 @@ void draw_material_ui(const MaterialPtr &material) // blend-mode const char *blend_items[] = {"Opaque", "Blend", "Mask"}; - constexpr Material::BlendMode blend_modes[] = {Material::BlendMode::Opaque, Material::BlendMode::Blend, - Material::BlendMode::Mask}; + constexpr BlendMode blend_modes[] = {BlendMode::Opaque, BlendMode::Blend, BlendMode::Mask}; int blend_mode_index = 0; for(auto blend_mode: blend_modes) { - if(material->blend_mode == blend_mode) { break; } + if(material.blend_mode == blend_mode) { break; } blend_mode_index++; } if(ImGui::Combo("blend-mode", &blend_mode_index, blend_items, IM_ARRAYSIZE(blend_items))) { - material->blend_mode = blend_modes[blend_mode_index]; + material.blend_mode = blend_modes[blend_mode_index]; } - if(material->blend_mode == Material::BlendMode::Mask) + if(material.blend_mode == BlendMode::Mask) { // alpha-cutoff - ImGui::SliderFloat("alpha-cutoff", &material->alpha_cutoff, 0.f, 1.f); + ImGui::SliderFloat("alpha-cutoff", &material.alpha_cutoff, 0.f, 1.f); } // two-sided - ImGui::Checkbox("two-sided", &material->two_sided); + ImGui::Checkbox("two-sided", &material.twosided); // null-surface - ImGui::Checkbox("null-surface", &material->null_surface); + ImGui::Checkbox("null-surface", &material.null_surface); // transmission - ImGui::SliderFloat("transmission", &material->transmission, 0.f, 1.f); - draw_texture(vierkant::Material::TextureType::Transmission, "transmission"); + ImGui::SliderFloat("transmission", &material.transmission, 0.f, 1.f); + draw_texture(vierkant::TextureType::Transmission, "transmission"); // attenuation distance - ImGui::InputFloat("attenuation distance", &material->attenuation_distance); + ImGui::InputFloat("attenuation distance", &material.attenuation_distance); // attenuation color - ImGui::ColorEdit3("attenuation color", glm::value_ptr(material->attenuation_color)); + ImGui::ColorEdit3("attenuation color", glm::value_ptr(material.attenuation_color)); // phase_asymmetry_g - ImGui::SliderFloat("phase_asymmetry_g", &material->phase_asymmetry_g, -1.f, 1.f); + ImGui::SliderFloat("phase_asymmetry_g", &material.phase_asymmetry_g, -1.f, 1.f); // scattering_ratio - ImGui::SliderFloat("scattering_ratio", &material->scattering_ratio, 0.f, 1.f); + ImGui::SliderFloat("scattering_ratio", &material.scattering_ratio, 0.f, 1.f); // index of refraction - ior - ImGui::InputFloat("ior", &material->ior); + ImGui::InputFloat("ior", &material.ior); // sheen ImGui::Separator(); ImGui::Text("sheen"); - ImGui::ColorEdit3("sheen color", glm::value_ptr(material->sheen_color)); - ImGui::SliderFloat("sheen roughness", &material->sheen_roughness, 0.f, 1.f); + ImGui::ColorEdit3("sheen color", glm::value_ptr(material.sheen_color)); + ImGui::SliderFloat("sheen roughness", &material.sheen_roughness, 0.f, 1.f); // iridescence ImGui::Separator(); ImGui::Text("iridescence"); - ImGui::SliderFloat("iridescence", &material->iridescence_factor, 0.f, 1.f); - ImGui::InputFloat("iridescence-ior", &material->iridescence_ior); - ImGui::InputFloat2("iridescence thickness", glm::value_ptr(material->iridescence_thickness_range)); + ImGui::SliderFloat("iridescence", &material.iridescence_factor, 0.f, 1.f); + ImGui::InputFloat("iridescence-ior", &material.iridescence_ior); + ImGui::InputFloat2("iridescence thickness", glm::value_ptr(material.iridescence_thickness_range)); // clearcoat ImGui::Separator(); ImGui::Text("clearcoat"); - ImGui::SliderFloat("clearcoat factor", &material->clearcoat_factor, 0.f, 1.f); - ImGui::SliderFloat("clearcoat roughness", &material->clearcoat_roughness_factor, 0.f, 1.f); + ImGui::SliderFloat("clearcoat factor", &material.clearcoat_factor, 0.f, 1.f); + ImGui::SliderFloat("clearcoat roughness", &material.clearcoat_roughness_factor, 0.f, 1.f); } void draw_light_ui(vierkant::model::lightsource_t &light) @@ -852,12 +852,13 @@ void draw_mesh_ui(const vierkant::Object3DPtr &object, vierkant::mesh_component_ { for(uint32_t i = 0; i < mesh->materials.size(); ++i) { - const auto &mat = mesh->materials[i]; - auto mat_name = mat->name.empty() ? std::to_string(i) : mat->name; + const auto &mesh_material = mesh->materials[i]; + const auto &mat = mesh_material->m; + auto mat_name = mat.name.empty() ? std::to_string(i) : mat.name; - if(mat && ImGui::TreeNode((void *) (mat.get()), "%s", mat_name.c_str())) + if(mesh_material && ImGui::TreeNode((void *) (mesh_material.get()), "%s", mat_name.c_str())) { - draw_material_ui(mat); + draw_material_ui(mesh_material); ImGui::Separator(); ImGui::TreePop(); } diff --git a/src/micromap_compute.cpp b/src/micromap_compute.cpp index 4b034998..2cbcdcd7 100644 --- a/src/micromap_compute.cpp +++ b/src/micromap_compute.cpp @@ -259,7 +259,8 @@ micromap_compute_result_t micromap_compute(const micromap_compute_context_handle { const auto &entry = mesh->entries[i]; const auto &lod_0 = entry.lods.front(); - const auto &material = mesh->materials[entry.material_index]; + const auto &mesh_material = mesh->materials[entry.material_index]; + const auto &material = mesh_material->m; const auto &buffer_size = buffer_sizes[i]; const auto &buffer_offset = buffer_offsets[i]; @@ -291,7 +292,7 @@ micromap_compute_result_t micromap_compute(const micromap_compute_context_handle param_ubo.num_triangles = lod_0.num_indices / 3; param_ubo.num_subdivisions = params.num_subdivisions; param_ubo.format = params.micromap_format; - param_ubo.alpha_cutoff = material->blend_mode == Material::BlendMode::Mask ? material->alpha_cutoff : 0.f; + param_ubo.alpha_cutoff = material.blend_mode == vierkant::BlendMode::Mask ? material.alpha_cutoff : 0.f; param_ubo.vertex_in = mesh->vertex_buffer->device_address() + vertex_stride * entry.vertex_offset; param_ubo.index_in = mesh->index_buffer->device_address() + vierkant::num_bytes(mesh->index_type) * lod_0.base_index; @@ -311,7 +312,7 @@ micromap_compute_result_t micromap_compute(const micromap_compute_context_handle auto &descriptor_img = computable.descriptors[1]; descriptor_img.stage_flags = VK_SHADER_STAGE_COMPUTE_BIT; descriptor_img.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - descriptor_img.images = {material->textures[vierkant::Material::TextureType::Color]}; + descriptor_img.images = {mesh_material->textures[vierkant::TextureType::Color]}; // store params and computable, dispatch later param_ubos.push_back(param_ubo); diff --git a/src/model/gltf.cpp b/src/model/gltf.cpp index d2fe43f4..96d8f8f8 100644 --- a/src/model/gltf.cpp +++ b/src/model/gltf.cpp @@ -292,26 +292,26 @@ vierkant::GeometryPtr create_geometry(const tinygltf::Primitive &primitive, cons return geometry; } -model::material_t convert_material(const tinygltf::Material &tiny_mat, const tinygltf::Model &model, - const std::map &image_cache, - const std::unordered_map &tex_id_cache, - const std::unordered_map &sampler_id_cache) +vierkant::material_t convert_material(const tinygltf::Material &tiny_mat, const tinygltf::Model &model, + const std::map &image_cache, + const std::unordered_map &tex_id_cache, + const std::unordered_map &sampler_id_cache) { - model::material_t ret; + vierkant::material_t ret; ret.name = tiny_mat.name; ret.base_color = *reinterpret_cast(tiny_mat.pbrMetallicRoughness.baseColorFactor.data()); ret.emission = *reinterpret_cast(tiny_mat.emissiveFactor.data()); // blend_mode defaults to opaque - if(tiny_mat.alphaMode == blend_mode_blend) { ret.blend_mode = vierkant::Material::BlendMode::Blend; } - else if(tiny_mat.alphaMode == blend_mode_mask) { ret.blend_mode = vierkant::Material::BlendMode::Mask; } + if(tiny_mat.alphaMode == blend_mode_blend) { ret.blend_mode = vierkant::BlendMode::Blend; } + else if(tiny_mat.alphaMode == blend_mode_mask) { ret.blend_mode = vierkant::BlendMode::Mask; } ret.alpha_cutoff = static_cast(tiny_mat.alphaCutoff); ret.metalness = static_cast(tiny_mat.pbrMetallicRoughness.metallicFactor); ret.roughness = static_cast(tiny_mat.pbrMetallicRoughness.roughnessFactor); ret.twosided = tiny_mat.doubleSided; - auto insert_texture = [&](int tex_index, vierkant::Material::TextureType tex_type) -> bool { + auto insert_texture = [&](int tex_index, vierkant::TextureType tex_type) -> bool { if(tex_index >= 0) { int img_index = model.textures[tex_index].source; @@ -331,25 +331,22 @@ model::material_t convert_material(const tinygltf::Material &tiny_mat, const tin }; // albedo - if(insert_texture(tiny_mat.pbrMetallicRoughness.baseColorTexture.index, Material::TextureType::Color)) + if(insert_texture(tiny_mat.pbrMetallicRoughness.baseColorTexture.index, TextureType::Color)) { ret.texture_transform = texture_transform(tiny_mat.pbrMetallicRoughness.baseColorTexture); } // ao / rough / metal - insert_texture(tiny_mat.pbrMetallicRoughness.metallicRoughnessTexture.index, Material::TextureType::Ao_rough_metal); + insert_texture(tiny_mat.pbrMetallicRoughness.metallicRoughnessTexture.index, TextureType::Ao_rough_metal); // normals - insert_texture(tiny_mat.normalTexture.index, Material::TextureType::Normal); + insert_texture(tiny_mat.normalTexture.index, TextureType::Normal); // emission - if(insert_texture(tiny_mat.emissiveTexture.index, Material::TextureType::Emission)) - { - ret.emission = glm::vec3(0.f); - } + if(insert_texture(tiny_mat.emissiveTexture.index, TextureType::Emission)) { ret.emission = glm::vec3(0.f); } // occlusion only supported alongside rough/metal - if(ret.textures.contains(Material::TextureType::Ao_rough_metal) && tiny_mat.occlusionTexture.index >= 0) + if(ret.textures.contains(TextureType::Ao_rough_metal) && tiny_mat.occlusionTexture.index >= 0) { if(tiny_mat.occlusionTexture.index != tiny_mat.pbrMetallicRoughness.metallicRoughnessTexture.index) { @@ -381,7 +378,7 @@ model::material_t convert_material(const tinygltf::Material &tiny_mat, const tin } } } - else if(ret.textures.contains(Material::TextureType::Ao_rough_metal)) + else if(ret.textures.contains(TextureType::Ao_rough_metal)) { auto ao_roughness_metal_image = image_cache.at(model.textures[tiny_mat.pbrMetallicRoughness.metallicRoughnessTexture.index].source); @@ -420,14 +417,13 @@ model::material_t convert_material(const tinygltf::Material &tiny_mat, const tin if(value.Has(ext_specular_texture)) { const auto &specular_texture_value = value.Get(ext_specular_texture); - insert_texture(specular_texture_value.Get("index").GetNumberAsInt(), Material::TextureType::Specular); + insert_texture(specular_texture_value.Get("index").GetNumberAsInt(), TextureType::Specular); } if(value.Has(ext_specular_color_texture)) { const auto &specular_color_texture_value = value.Get(ext_specular_color_texture); - insert_texture(specular_color_texture_value.Get("index").GetNumberAsInt(), - Material::TextureType::SpecularColor); + insert_texture(specular_color_texture_value.Get("index").GetNumberAsInt(), TextureType::SpecularColor); } } else if(ext == KHR_materials_transmission) @@ -440,8 +436,7 @@ model::material_t convert_material(const tinygltf::Material &tiny_mat, const tin if(value.Has(ext_transmission_texture)) { const auto &transmission_texture_value = value.Get(ext_transmission_texture); - insert_texture(transmission_texture_value.Get("index").GetNumberAsInt(), - Material::TextureType::Transmission); + insert_texture(transmission_texture_value.Get("index").GetNumberAsInt(), TextureType::Transmission); } } else if(ext == KHR_materials_volume) @@ -468,8 +463,7 @@ model::material_t convert_material(const tinygltf::Material &tiny_mat, const tin if(value.Has(ext_volume_thickness_texture)) { const auto &thickness_texture_value = value.Get(ext_volume_thickness_texture); - insert_texture(thickness_texture_value.Get("index").GetNumberAsInt(), - Material::TextureType::VolumeThickness); + insert_texture(thickness_texture_value.Get("index").GetNumberAsInt(), TextureType::VolumeThickness); } } else if(ext == KHR_materials_ior) @@ -504,14 +498,13 @@ model::material_t convert_material(const tinygltf::Material &tiny_mat, const tin if(value.Has(ext_sheen_color_texture)) { const auto &sheen_color_texture_value = value.Get(ext_sheen_color_texture); - insert_texture(sheen_color_texture_value.Get("index").GetNumberAsInt(), - Material::TextureType::SheenColor); + insert_texture(sheen_color_texture_value.Get("index").GetNumberAsInt(), TextureType::SheenColor); } if(value.Has(ext_sheen_roughness_texture)) { const auto &sheen_roughness_texture_value = value.Get(ext_sheen_roughness_texture); insert_texture(sheen_roughness_texture_value.Get("index").GetNumberAsInt(), - Material::TextureType::SheenRoughness); + TextureType::SheenRoughness); } } else if(ext == KHR_materials_iridescence) @@ -525,7 +518,7 @@ model::material_t convert_material(const tinygltf::Material &tiny_mat, const tin const auto &iridescence_texture_value = value.Get(ext_iridescence_texture); auto img_index = model.textures[iridescence_texture_value.Get("index").GetNumberAsInt()].source; assert(tex_id_cache.contains(img_index)); - ret.textures[Material::TextureType::Iridescence] = tex_id_cache.at(img_index); + ret.textures[TextureType::Iridescence] = tex_id_cache.at(img_index); } if(value.Has(ext_iridescence_ior)) { @@ -547,13 +540,13 @@ model::material_t convert_material(const tinygltf::Material &tiny_mat, const tin auto img_index = model.textures[iridescence_thickness_texture_value.Get("index").GetNumberAsInt()].source; assert(tex_id_cache.contains(img_index)); - ret.textures[Material::TextureType::IridescenceThickness] = tex_id_cache.at(img_index); + ret.textures[TextureType::IridescenceThickness] = tex_id_cache.at(img_index); assert(image_cache.contains(img_index)); auto img_iridescence_thickness = image_cache.at(img_index); - if(!ret.textures.contains(Material::TextureType::Iridescence)) + if(!ret.textures.contains(TextureType::Iridescence)) { - ret.textures[Material::TextureType::Iridescence] = tex_id_cache.at(img_index); + ret.textures[TextureType::Iridescence] = tex_id_cache.at(img_index); if(auto img = std::dynamic_pointer_cast>(img_iridescence_thickness)) { @@ -571,11 +564,11 @@ model::material_t convert_material(const tinygltf::Material &tiny_mat, const tin return ret; } -vierkant::model::texture_sampler_t convert_sampler(const tinygltf::Sampler &tiny_sampler) +vierkant::texture_sampler_t convert_sampler(const tinygltf::Sampler &tiny_sampler) { - vierkant::model::texture_sampler_t ret = {}; + vierkant::texture_sampler_t ret = {}; - using Filter = model::texture_sampler_t::Filter; + using Filter = texture_sampler_t::Filter; auto convert_tiny_filter = [](int tf) -> Filter { switch(tf) { @@ -587,7 +580,7 @@ vierkant::model::texture_sampler_t convert_sampler(const tinygltf::Sampler &tiny ret.min_filter = convert_tiny_filter(tiny_sampler.minFilter); ret.mag_filter = convert_tiny_filter(tiny_sampler.magFilter); - using AddressMode = model::texture_sampler_t::AddressMode; + using AddressMode = texture_sampler_t::AddressMode; auto convert_tiny_wrap = [](int wrap) -> AddressMode { switch(wrap) { @@ -876,7 +869,7 @@ std::optional gltf(const std::filesystem::path &path, crocore::Th // map tiny-indices to created assets std::map image_cache; - std::map sampler_cache; + std::map sampler_cache; if(pool) { diff --git a/src/model/model_loading.cpp b/src/model/model_loading.cpp index ead8a588..81663b2e 100644 --- a/src/model/model_loading.cpp +++ b/src/model/model_loading.cpp @@ -28,7 +28,7 @@ VkFormat vk_format(const crocore::ImagePtr &img) return ret; } -VkSamplerAddressMode vk_sampler_address_mode(const vierkant::model::texture_sampler_t::AddressMode &address_mode) +VkSamplerAddressMode vk_sampler_address_mode(const vierkant::texture_sampler_t::AddressMode &address_mode) { switch(address_mode) { @@ -41,7 +41,7 @@ VkSamplerAddressMode vk_sampler_address_mode(const vierkant::model::texture_samp } } -VkFilter vk_filter(const vierkant::model::texture_sampler_t::Filter &filter) +VkFilter vk_filter(const vierkant::texture_sampler_t::Filter &filter) { switch(filter) { @@ -54,7 +54,7 @@ VkFilter vk_filter(const vierkant::model::texture_sampler_t::Filter &filter) return VK_FILTER_NEAREST; } -vierkant::VkSamplerPtr create_sampler(const vierkant::DevicePtr &device, const vierkant::model::texture_sampler_t &ts, +vierkant::VkSamplerPtr create_sampler(const vierkant::DevicePtr &device, const vierkant::texture_sampler_t &ts, uint32_t num_mips) { VkSamplerCreateInfo sampler_create_info = {}; @@ -218,45 +218,23 @@ vierkant::MeshPtr load_mesh(const load_mesh_params_t ¶ms, const vierkant::mo }, tex_variant); } - const auto &materials = mesh_assets.materials; - mesh->materials.resize(std::max(1, materials.size())); + mesh->materials.resize(std::max(1, mesh_assets.materials.size())); - for(uint32_t i = 0; i < materials.size(); ++i) + for(uint32_t i = 0; i < mesh_assets.materials.size(); ++i) { + const auto &asset_mat = mesh_assets.materials[i]; + auto &material = mesh->materials[i]; material = vierkant::Material::create(); + material->m = asset_mat; - material->name = materials[i].name; - material->color = materials[i].base_color; - material->emission = glm::vec4(materials[i].emission, materials[i].emissive_strength); - material->roughness = materials[i].roughness; - material->metalness = materials[i].metalness; - material->blend_mode = materials[i].blend_mode; - material->alpha_cutoff = materials[i].alpha_cutoff; - material->two_sided = materials[i].twosided; - - material->transmission = materials[i].transmission; - material->attenuation_color = materials[i].attenuation_color; - material->attenuation_distance = materials[i].attenuation_distance; - material->ior = materials[i].ior; - - material->sheen_color = materials[i].sheen_color; - material->sheen_roughness = materials[i].sheen_roughness; - - material->sheen_color = materials[i].sheen_color; - material->sheen_roughness = materials[i].sheen_roughness; - - material->iridescence_factor = materials[i].iridescence_factor; - material->iridescence_ior = materials[i].iridescence_ior; - material->iridescence_thickness_range = materials[i].iridescence_thickness_range; - - for(const auto &[tex_type, tex_id]: materials[i].textures) + for(const auto &[tex_type, tex_id]: asset_mat.textures) { auto vk_img = texture_cache[tex_id]; // optional sampler-override - auto sampler_id_it = materials[i].samplers.find(tex_type); - if(sampler_id_it != materials[i].samplers.end()) + auto sampler_id_it = asset_mat.samplers.find(tex_type); + if(sampler_id_it != asset_mat.samplers.end()) { // clone img vk_img = texture_cache[tex_id]->clone(); @@ -270,14 +248,12 @@ vierkant::MeshPtr load_mesh(const load_mesh_params_t ¶ms, const vierkant::mo } else { - spdlog::warn("material '{}' references sampler '{}', but could not find in bundle", - materials[i].name, sampler_id.str()); + spdlog::warn("material '{}' references sampler '{}', but could not find in bundle", asset_mat.name, + sampler_id.str()); } } material->textures[tex_type] = vk_img; } - - material->texture_transform = materials[i].texture_transform; } // submit transfer and sync diff --git a/src/model/wavefront_obj.cpp b/src/model/wavefront_obj.cpp index 7c44ef60..f8796a1e 100644 --- a/src/model/wavefront_obj.cpp +++ b/src/model/wavefront_obj.cpp @@ -85,7 +85,7 @@ std::optional wavefront_obj(const std::filesystem::path &path, cr for(const auto &mat: materials) { - vierkant::model::material_t m = {}; + vierkant::material_t m = {}; m.name = mat.name; m.base_color = {mat.diffuse[0], mat.diffuse[1], mat.diffuse[2], std::clamp(1.f - mat.dissolve, 0.f, 1.f)}; m.emission = {mat.emission[0], mat.emission[1], mat.emission[2]}; @@ -102,19 +102,19 @@ std::optional wavefront_obj(const std::filesystem::path &path, cr if(!mat.diffuse_texname.empty()) { auto [tex_id, img] = get_image((base_dir / mat.diffuse_texname).string()); - m.textures[Material::TextureType::Color] = tex_id; + m.textures[vierkant::TextureType::Color] = tex_id; mesh_assets.textures[tex_id] = img; } if(!mat.normal_texname.empty()) { auto [tex_id, img] = get_image((base_dir / mat.normal_texname).string()); - m.textures[Material::TextureType::Normal] = tex_id; + m.textures[vierkant::TextureType::Normal] = tex_id; mesh_assets.textures[tex_id] = img; } mesh_assets.materials.push_back(m); } // fallback material - if(mesh_assets.materials.empty()) { mesh_assets.materials.resize(1); } + if(mesh_assets.materials.empty()) { mesh_assets.materials.push_back({}); } std::vector entry_create_infos; diff --git a/submodules/crocore b/submodules/crocore index 73e5c5e0..43c91c92 160000 --- a/submodules/crocore +++ b/submodules/crocore @@ -1 +1 @@ -Subproject commit 73e5c5e0d559e93afc9422447df1e2fda4b65ad2 +Subproject commit 43c91c9282154d7cd12876a31996e3fe36febc1b