Misc renderer improvements:

- Reuse renderer and gltf loader between scenes
- Use puish descriptors to simplify the management of descriptor sets in the scene renderer
- Don't keep the imgui swapchain in the scene classes
- Fix alpha blending, add alpha masking
- Fix mipmaps not being used in renderer
- Add cache when loading images and models
- Only premultiply the colours if required
- Add support for gltTF animations

Allow removing an object and its descendants from the scene, allow loading from a memory buffer
This commit is contained in:
Guillaume Meunier
2025-10-23 21:39:12 +02:00
parent 1f6ea0d4ca
commit aad1bb6a1b
21 changed files with 918 additions and 375 deletions
+63 -13
View File
@@ -20,6 +20,9 @@
#include <array>
#include <memory>
#include <type_traits>
#include <utility>
#include <variant>
#include <vector>
#include <entt/entity/entity.hpp>
@@ -59,6 +62,7 @@ struct material
float roughness_factor = 1;
float occlusion_strength = 0;
float normal_scale = 0;
float alpha_cutoff = 0.5;
uint32_t base_color_texcoord = 0;
uint32_t metallic_roughness_texcoord = 0;
@@ -69,6 +73,13 @@ struct material
// TODO: add fastgltf::TextureTransform?
};
enum class alpha_mode_t
{
opaque,
mask,
blend
};
std::shared_ptr<texture> base_color_texture;
std::shared_ptr<texture> metallic_roughness_texture;
std::shared_ptr<texture> occlusion_texture;
@@ -78,9 +89,7 @@ struct material
// Disable back face culling with this material
bool double_sided = true;
// TODO transparency
// fastgltf::AlphaMode alphaMode;
// float alpha_cutoff;
alpha_mode_t alpha_mode;
gpu_data staging;
@@ -88,17 +97,8 @@ struct material
std::shared_ptr<buffer_allocation> buffer;
size_t offset;
// The descriptor set is managed by the scene renderer, it is updated whenever ds_dirty is true
// Bindings 0-4: textures
// Binding 5: uniform buffer
std::shared_ptr<vk::raii::DescriptorSet> ds;
// Set to true to update the descriptor set at the next frame
bool ds_dirty;
std::string name;
std::string shader_name = "lit";
bool blend_enable = true;
};
struct primitive
@@ -118,6 +118,7 @@ struct primitive
std::shared_ptr<material> material_;
};
// TODO make sure the mesh cannot be changed while it is used
struct mesh
{
std::vector<primitive> primitives;
@@ -129,7 +130,7 @@ struct vertex
{
glm::vec3 position;
glm::vec3 normal;
glm::vec3 tangent;
glm::vec4 tangent;
std::array<glm::vec2, 2> texcoord;
glm::vec4 color;
std::array<glm::vec4, 1> joints;
@@ -180,6 +181,55 @@ struct node
bool reverse_side;
uint32_t global_layer_mask;
};
struct animation_track_base
{
enum class interpolation_t
{
step,
linear,
cubic_spline,
};
entt::entity target;
interpolation_t interpolation;
};
template <auto node::*Field>
struct animation_track_impl : animation_track_base
{
static const constexpr auto field = Field;
using type = std::remove_reference_t<decltype(std::declval<node>().*field)>;
std::vector<float> timestamp;
std::vector<type> value;
};
using animation_track_position = animation_track_impl<&node::position>;
using animation_track_orientation = animation_track_impl<&node::orientation>;
using animation_track_scale = animation_track_impl<&node::scale>;
static_assert(std::is_same_v<animation_track_position::type, glm::vec3>);
static_assert(std::is_same_v<animation_track_orientation::type, glm::quat>);
static_assert(std::is_same_v<animation_track_scale::type, glm::vec3>);
struct animation
{
std::string name;
std::vector<
std::variant<
animation_track_position,
animation_track_orientation,
animation_track_scale>>
tracks;
float duration = 0;
float current_time = 0;
bool playing = true;
bool looping = true;
};
} // namespace components
entt::entity find_node_by_name(entt::registry & scene, std::string_view name);