Skip to content

Commit

Permalink
feat(renderer): add ActiveVoxelPalette resource to rendererPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
roby2014 committed Oct 16, 2023
1 parent 3a2a9b8 commit 174a787
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
12 changes: 12 additions & 0 deletions engine/include/cubos/engine/assets/assets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ namespace cubos::engine
/// @return Whether the version was updated.
bool update(AnyAsset& handle) const;

/// @brief Updates the given handle to the latest version of the asset.
///
/// Can be used to implement hot-reloading.
///
/// @param handle Handle to update.
/// @return Whether the version was updated.
template <typename T>
bool update(Asset<T>& handle) const
{
return update(static_cast<AnyAsset&>(handle));
}

/// @brief Unloads the given asset. Can be used to force assets to be reloaded.
/// @param handle Handle to unload.
void invalidate(const AnyAsset& handle);
Expand Down
13 changes: 13 additions & 0 deletions engine/include/cubos/engine/renderer/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace cubos::engine
/// - @ref RendererFrame - holds the current frame information.
/// - @ref RendererEnvironment - holds the environment information (ambient light, sky gradient).
/// - @ref ActiveCameras - holds the entities which represents the active cameras.
/// - @ref ActiveVoxelPalette - holds an asset handle to the currently active palette.
///
/// ## Components
/// - @ref RenderableGrid - a grid to be rendered.
Expand Down Expand Up @@ -86,6 +87,18 @@ namespace cubos::engine
core::ecs::Entity entities[4];
};

/// @brief Resource which holds an asset handle to the currently active palette.
/// @ingroup renderer-plugin
struct ActiveVoxelPalette
{
/// @brief Asset handle to the currently active palette.
Asset<VoxelPalette> asset;

/// @brief Previous asset handle save in order to check if asset changed later.
/// @todo ECS should have a .changed function to make this process easier (#273).
Asset<VoxelPalette> prev;
};

/// @brief Plugin entry function.
/// @param cubos @b CUBOS. main class
/// @ingroup renderer-plugin
Expand Down
18 changes: 18 additions & 0 deletions engine/src/cubos/engine/renderer/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ static void frameEnvironment(Write<RendererFrame> frame, Read<RendererEnvironmen
frame->skyGradient(env->skyGradient[0], env->skyGradient[1]);
}

static void checkPaletteUpdateSystem(Write<Assets> assets, Write<Renderer> renderer,
Write<ActiveVoxelPalette> activePalette)
{
if (activePalette->asset.isNull())
{
return;
}

if (assets->update(activePalette->asset) || activePalette->prev != activePalette->asset)
{
auto paletteRead = assets->read(activePalette->asset);
(*renderer)->setPalette(*paletteRead);
activePalette->prev = activePalette->asset;
}
}

/// @brief Splits the viewport recursively for the given cameras.
/// @param position Viewport position.
/// @param size Viewport size.
Expand Down Expand Up @@ -187,6 +203,7 @@ void cubos::engine::rendererPlugin(Cubos& cubos)
cubos.addResource<Renderer>();
cubos.addResource<ActiveCameras>();
cubos.addResource<RendererEnvironment>();
cubos.addResource<ActiveVoxelPalette>();

cubos.addComponent<RenderableGrid>();
cubos.addComponent<Camera>();
Expand All @@ -204,6 +221,7 @@ void cubos::engine::rendererPlugin(Cubos& cubos)
cubos.system(frameDirectionalLights).tagged("cubos.renderer.frame");
cubos.system(framePointLights).tagged("cubos.renderer.frame");
cubos.system(frameEnvironment).tagged("cubos.renderer.frame");
cubos.system(checkPaletteUpdateSystem).tagged("cubos.renderer.frame");
cubos.system(draw).tagged("cubos.renderer.draw");
cubos.system(resize).after("cubos.window.poll").before("cubos.renderer.draw");
}

0 comments on commit 174a787

Please sign in to comment.