adding custom pipeline for water (what a journey to do so...)

This commit is contained in:
2025-06-02 00:00:08 +02:00
parent ce9335ba6a
commit f6decee5fa
22 changed files with 355 additions and 122 deletions

View File

@@ -2,6 +2,8 @@
#include <Block.h>
#include <World.h>
#include <ScopCore.h>
#define POS_TO_INDEX(posx, posz) (posx * CHUNK_SIZE.x + posz)
constexpr Scop::Vec2ui SPRITE_SIZE = { 16, 16 };
@@ -29,6 +31,11 @@ enum class Side : std::uint8_t
Side
};
struct WaterData
{
float time;
};
Scop::Vec2f GetAtlasOffset(BlockType type, Side side)
{
Scop::Vec2ui pos = BLOCKS_TO_ATLAS[static_cast<std::uint32_t>(type)][static_cast<std::uint8_t>(side)];
@@ -80,7 +87,7 @@ void Chunk::GenerateMesh()
std::vector<std::uint32_t>& index_data = (is_water ? m_water_mesh_index_data : m_mesh_index_data);
std::uint32_t& offset = (is_water ? water_offset : mesh_offset);
Scop::Vec4f base_color = is_water ? Scop::Vec4f{ 0.3f, 0.5f, 0.5f, 0.8f } : Scop::Vec4f{ 1.0f };
Scop::Vec4f base_color = is_water ? Scop::Vec4f{ 0.3f, 0.1f, 0.05f, 0.98f } : Scop::Vec4f{ 1.0f };
std::uint32_t invalid_limit = is_water ? static_cast<std::uint32_t>(BlockType::Air) : static_cast<std::uint32_t>(BlockType::Water);
if(GetBlock(Scop::Vec3i(x, y, z + 1)) <= invalid_limit)
@@ -333,6 +340,19 @@ void Chunk::UploadMesh()
actor.SetScale(Scop::Vec3f{ 2.0f });
actor.SetPosition(Scop::Vec3f(m_position.x, 0.0f, m_position.y));
actor.SetIsOpaque(false);
actor.SetCustomPipeline({
.pipeline = m_world.GetWaterPipeline(),
.data{ sizeof(WaterData) }
});
std::memset(actor.GetCustomPipeline()->data.GetData(), 0, actor.GetCustomPipeline()->data.GetSize());
auto object_update = [](Scop::NonOwningPtr<Scop::Scene> scene, Scop::NonOwningPtr<Scop::Actor> actor, Scop::Inputs& input, float delta)
{
WaterData* data = actor->GetCustomPipeline()->data.GetDataAs<WaterData>();
data->time += delta;
};
using actor_hook = std::function<void(Scop::NonOwningPtr<Scop::Actor>)>;
actor.AttachScript(std::make_shared<Scop::NativeActorScript>(actor_hook{}, object_update, actor_hook{}));
p_water_actor = &actor;
}
}

View File

@@ -6,8 +6,18 @@
#include <World.h>
#include <Utils.h>
World::World(Scop::Scene& scene) : m_noisecollection(42), m_fps_counter(), m_scene(scene), m_previous_chunk_position(-1000, 10000)
World::World(Scop::Scene& scene) : m_noisecollection(42), p_water_pipeline(std::make_shared<Scop::GraphicPipeline>()), m_fps_counter(), m_scene(scene), m_previous_chunk_position(-1000, 10000)
{
p_water_vertex_shader = Scop::LoadShaderFromFile(GetExecutablePath().parent_path().parent_path() / "Resources/Shaders/Build/WaterVertex.spv", Scop::ShaderType::Vertex, Scop::DefaultForwardVertexShaderLayout);
p_water_fragment_shader = Scop::LoadShaderFromFile(GetExecutablePath().parent_path().parent_path() / "Resources/Shaders/Build/WaterFragment.spv", Scop::ShaderType::Fragment, Scop::DefaultShaderLayout);
Scop::GraphicPipelineDescriptor pipeline_descriptor;
pipeline_descriptor.vertex_shader = p_water_vertex_shader;
pipeline_descriptor.fragment_shader = p_water_fragment_shader;
pipeline_descriptor.clear_color_attachments = false;
pipeline_descriptor.name = "water_forward_pass_pipeline";
p_water_pipeline->Init(pipeline_descriptor);
Scop::Vec2ui32 map_size;
Scop::MaterialTextures material_params;
material_params.albedo = std::make_shared<Scop::Texture>(Scop::LoadBMPFile(GetResourcesPath() / "atlas.bmp", map_size), map_size.x, map_size.y);

View File

@@ -36,6 +36,8 @@ class World
[[nodiscard]] inline Scop::Scene& GetScene() noexcept { return m_scene; }
[[nodiscard]] inline std::shared_ptr<Scop::Material> GetBlockMaterial() const { return p_block_material; }
[[nodiscard]] inline std::shared_ptr<Scop::GraphicPipeline> GetWaterPipeline() const { return p_water_pipeline; }
[[nodiscard]] Scop::NonOwningPtr<Chunk> GetChunk(Scop::Vec2i position);
[[nodiscard]] NoiseCollection& GetNoiseGenerator() noexcept { return m_noisecollection; }
@@ -52,6 +54,9 @@ class World
std::unordered_map<Scop::Vec2i, Chunk> m_chunks;
ThreadSafeQueue<std::reference_wrapper<Chunk>> m_chunks_to_upload;
std::shared_ptr<Scop::Material> p_block_material;
std::shared_ptr<Scop::GraphicPipeline> p_water_pipeline;
std::shared_ptr<Scop::Shader> p_water_vertex_shader;
std::shared_ptr<Scop::Shader> p_water_fragment_shader;
Scop::Scene& m_scene;
Scop::Vec2i m_previous_chunk_position;
Scop::Vec2i m_current_chunk_position;

View File

@@ -9,8 +9,8 @@ int main(int ac, char** av)
{
Scop::ScopEngine engine(ac, av, "Vox", 0, 0, GetExecutablePath().parent_path().parent_path() / "ScopEngine/Assets");
Scop::Scene& splash_scene = SplashScreen();
std::shared_ptr<Scop::Shader> shader = Scop::LoadShaderFromFile(GetExecutablePath().parent_path().parent_path() / "Resources/Fragment.spv", Scop::ShaderType::Fragment, Scop::DefaultShaderLayout);
std::shared_ptr<Scop::Shader> post_process_shader = Scop::LoadShaderFromFile(GetExecutablePath().parent_path().parent_path() / "Resources/PostProcess.spv", Scop::ShaderType::Fragment, Scop::PostProcessShaderLayout);
std::shared_ptr<Scop::Shader> shader = Scop::LoadShaderFromFile(GetExecutablePath().parent_path().parent_path() / "Resources/Shaders/Build/Fragment.spv", Scop::ShaderType::Fragment, Scop::DefaultShaderLayout);
std::shared_ptr<Scop::Shader> post_process_shader = Scop::LoadShaderFromFile(GetExecutablePath().parent_path().parent_path() / "Resources/Shaders/Build/PostProcess.spv", Scop::ShaderType::Fragment, Scop::PostProcessShaderLayout);
Scop::SceneDescriptor main_scene_desc;
main_scene_desc.fragment_shader = shader;