diff --git a/Application/Chunk.cpp b/Application/Chunk.cpp index ca348d7..69f2f27 100644 --- a/Application/Chunk.cpp +++ b/Application/Chunk.cpp @@ -11,6 +11,7 @@ constexpr Scop::Vec2ui ATLAS_SIZE = { 64, 64 }; constexpr Scop::Vec2f SPRITE_UNIT = Scop::Vec2f(SPRITE_SIZE) / Scop::Vec2f(ATLAS_SIZE); constexpr std::array, BlocksCount> BLOCKS_TO_ATLAS = { + // TOP BOTTOM SIDE std::array{ Scop::Vec2ui{ 0, 0 }, Scop::Vec2ui{ 0, 0 }, Scop::Vec2ui{ 0, 0 } }, // Air std::array{ Scop::Vec2ui{ 1, 1 }, Scop::Vec2ui{ 1, 1 }, Scop::Vec2ui{ 1, 1 } }, // Water std::array{ Scop::Vec2ui{ 0, 0 }, Scop::Vec2ui{ 0, 0 }, Scop::Vec2ui{ 0, 0 } }, // Dirt @@ -33,7 +34,7 @@ enum class Side : std::uint8_t struct WaterData { - float time; + double time; }; Scop::Vec2f GetAtlasOffset(BlockType type, Side side) @@ -87,7 +88,7 @@ void Chunk::GenerateMesh() std::vector& 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.1f, 0.05f, 0.98f } : Scop::Vec4f{ 1.0f }; + Scop::Vec4f base_color = is_water ? Scop::Vec4f{ 0.5f, 0.2f, 0.1f, 0.95f } : Scop::Vec4f{ 1.0f }; std::uint32_t invalid_limit = is_water ? static_cast(BlockType::Air) : static_cast(BlockType::Water); if(GetBlock(Scop::Vec3i(x, y, z + 1)) <= invalid_limit) @@ -118,6 +119,8 @@ void Chunk::GenerateMesh() else vertex_color = base_color; Scop::Vec4f vertex_pos = BLOCK_MESH[i].position + Scop::Vec3f(x, y, z); + if(is_water && (i == 1 || i == 3) && GetBlock(Scop::Vec3i(x, y + 1, z)) != static_cast(BlockType::Water)) + vertex_pos.w = 0.0; Scop::Vec2f uv = GetAtlasOffset(type, Side::Side) + (Scop::Vec2f(SPRITE_UNIT) * BLOCK_MESH[i].uv); mesh_data.push_back(Scop::Vertex(vertex_pos, vertex_color, BLOCK_MESH[i].normal, uv)); } @@ -155,6 +158,8 @@ void Chunk::GenerateMesh() else vertex_color = base_color; Scop::Vec4f vertex_pos = BLOCK_MESH[i].position + Scop::Vec3f(x, y, z); + if(is_water && (i == 5 || i == 7) && GetBlock(Scop::Vec3i(x, y + 1, z)) != static_cast(BlockType::Water)) + vertex_pos.w = 0.0; Scop::Vec2f uv = GetAtlasOffset(type, Side::Side) + (Scop::Vec2f(SPRITE_UNIT) * BLOCK_MESH[i].uv); mesh_data.push_back(Scop::Vertex(vertex_pos, vertex_color, BLOCK_MESH[i].normal, uv)); } @@ -162,7 +167,7 @@ void Chunk::GenerateMesh() offset += 4; } - if(GetBlock(Scop::Vec3i(x, y + 1, z)) <= invalid_limit) + if(std::uint32_t value = GetBlock(Scop::Vec3i(x, y + 1, z)); !is_water ? value <= invalid_limit : value != static_cast(BlockType::Water)) // We want top face water even under blocks { index_data.push_back(offset + 1); index_data.push_back(offset + 0); @@ -173,8 +178,6 @@ void Chunk::GenerateMesh() for(std::uint32_t i = 8; i < 12; i++) { - float block_top_y = is_water ? static_cast(y) - 0.2f : static_cast(y); - Scop::Vec4f vertex_color; if(!is_water) { @@ -193,7 +196,9 @@ void Chunk::GenerateMesh() } else vertex_color = base_color; - Scop::Vec4f vertex_pos = BLOCK_MESH[i].position + Scop::Vec3f(x, block_top_y, z); + Scop::Vec4f vertex_pos = BLOCK_MESH[i].position + Scop::Vec3f(x, y, z); + if(is_water) + vertex_pos.w = 0.0; Scop::Vec2f uv = GetAtlasOffset(type, Side::Top) + (Scop::Vec2f(SPRITE_UNIT) * BLOCK_MESH[i].uv); mesh_data.push_back(Scop::Vertex(vertex_pos, vertex_color, BLOCK_MESH[i].normal, uv)); } @@ -268,6 +273,8 @@ void Chunk::GenerateMesh() else vertex_color = base_color; Scop::Vec4f vertex_pos = BLOCK_MESH[i].position + Scop::Vec3f(x, y, z); + if(is_water && (i == 17 || i == 19) && GetBlock(Scop::Vec3i(x, y + 1, z)) != static_cast(BlockType::Water)) + vertex_pos.w = 0.0; Scop::Vec2f uv = GetAtlasOffset(type, Side::Side) + (Scop::Vec2f(SPRITE_UNIT) * BLOCK_MESH[i].uv); mesh_data.push_back(Scop::Vertex(vertex_pos, vertex_color, BLOCK_MESH[i].normal, uv)); } @@ -305,6 +312,8 @@ void Chunk::GenerateMesh() else vertex_color = base_color; Scop::Vec4f vertex_pos = BLOCK_MESH[i].position + Scop::Vec3f(x, y, z); + if(is_water && (i == 21 || i == 23) && GetBlock(Scop::Vec3i(x, y + 1, z)) != static_cast(BlockType::Water)) + vertex_pos.w = 0.0; Scop::Vec2f uv = GetAtlasOffset(type, Side::Side) + (Scop::Vec2f(SPRITE_UNIT) * BLOCK_MESH[i].uv); mesh_data.push_back(Scop::Vertex(vertex_pos, vertex_color, BLOCK_MESH[i].normal, uv)); } @@ -325,7 +334,6 @@ void Chunk::UploadMesh() Scop::Actor& actor = m_world.GetScene().CreateActor(mesh); actor.GetModelRef().SetMaterial(m_world.GetBlockMaterial(), 0); - actor.SetScale(Scop::Vec3f{ 2.0f }); actor.SetPosition(Scop::Vec3f(m_position.x, 0.0f, m_position.y)); p_actor = &actor; } @@ -337,7 +345,6 @@ void Chunk::UploadMesh() Scop::Actor& actor = m_world.GetScene().CreateActor(mesh); actor.GetModelRef().SetMaterial(m_world.GetBlockMaterial(), 0); - actor.SetScale(Scop::Vec3f{ 2.0f }); actor.SetPosition(Scop::Vec3f(m_position.x, 0.0f, m_position.y)); actor.SetIsOpaque(false); actor.SetCustomPipeline({ @@ -349,7 +356,7 @@ void Chunk::UploadMesh() auto object_update = [](Scop::NonOwningPtr scene, Scop::NonOwningPtr actor, Scop::Inputs& input, float delta) { WaterData* data = actor->GetCustomPipeline()->data.GetDataAs(); - data->time += delta; + data->time = static_cast(SDL_GetTicks64()); }; using actor_hook = std::function)>; actor.AttachScript(std::make_shared(actor_hook{}, object_update, actor_hook{})); diff --git a/Application/World.cpp b/Application/World.cpp index babf747..f89f7d8 100644 --- a/Application/World.cpp +++ b/Application/World.cpp @@ -16,7 +16,7 @@ World::World(Scop::Scene& scene) : m_noisecollection(42), p_water_pipeline(std:: 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); + p_water_pipeline->Setup(pipeline_descriptor); Scop::Vec2ui32 map_size; Scop::MaterialTextures material_params; @@ -47,18 +47,25 @@ World::World(Scop::Scene& scene) : m_noisecollection(42), p_water_pipeline(std:: } Scop::FirstPerson3D* camera = reinterpret_cast(m_scene.GetCamera().get()); - std::int32_t x_chunk = static_cast(camera->GetPosition().x) / static_cast(CHUNK_SIZE.x); - std::int32_t z_chunk = static_cast(camera->GetPosition().z) / static_cast(CHUNK_SIZE.z); - m_current_chunk_position = Scop::Vec2i{ x_chunk, z_chunk }; + Scop::Vec3f camera_position = camera->GetPosition(); + Scop::Vec2i global_block_position = Scop::Vec2i{ + static_cast(camera_position.x < 0.0f ? camera_position.x - 1.0f : camera_position.x), + static_cast(camera_position.z < 0.0f ? camera_position.z - 1.0f : camera_position.z), + }; + + m_current_chunk_position = Scop::Vec2i{ + static_cast(global_block_position.x < 0 ? std::floorf(global_block_position.x / static_cast(CHUNK_SIZE.x)) : global_block_position.x / static_cast(CHUNK_SIZE.x)), + static_cast(global_block_position.y < 0 ? std::floorf(global_block_position.y / static_cast(CHUNK_SIZE.z)) : global_block_position.y / static_cast(CHUNK_SIZE.z)), + }; if(Scop::NonOwningPtr current_chunk = GetChunk(m_current_chunk_position); current_chunk) { - Scop::Vec3i camera_pos = Scop::Vec3i(std::floor(scene->GetCamera()->GetPosition().x), scene->GetCamera()->GetPosition().y, std::floor(scene->GetCamera()->GetPosition().z)); - camera_pos.x %= CHUNK_SIZE.x; - camera_pos.z %= CHUNK_SIZE.z; - camera_pos.x += CHUNK_SIZE.x; - camera_pos.z += CHUNK_SIZE.z; - post_process_data.underwater = current_chunk->GetBlock(Scop::Vec3i(camera_pos.x % CHUNK_SIZE.x, camera_pos.y, camera_pos.z % CHUNK_SIZE.z)) == static_cast(BlockType::Water); + Scop::Vec3i block_position = Scop::Vec3i{ + global_block_position.x - (m_current_chunk_position.x * static_cast(CHUNK_SIZE.x)), + static_cast(camera_position.y), + global_block_position.y - (m_current_chunk_position.y * static_cast(CHUNK_SIZE.z)), + }; + post_process_data.underwater = current_chunk->GetBlock(block_position) == static_cast(BlockType::Water); } post_process_data.inv_res = Scop::Vec2f{ 1.0f / Scop::ScopEngine::Get().GetWindow().GetWidth(), 1.0f / Scop::ScopEngine::Get().GetWindow().GetHeight() }; diff --git a/Resources/Shaders/Fragment.nzsl b/Resources/Shaders/Fragment.nzsl index a43ae31..5702e32 100644 --- a/Resources/Shaders/Fragment.nzsl +++ b/Resources/Shaders/Fragment.nzsl @@ -37,7 +37,7 @@ fn main(input: VertOut) -> FragOut discard; const ambient = vec3[f32](0.1, 0.1, 0.1); - const directional_color = vec3[f32](5.0, 5.0, 5.0); + const directional_color = vec3[f32](2.5, 2.5, 2.5); const specular_strength = 0.5; let directional_vector = normalize(vec3[f32](1.0, 0.8, 0.75)); diff --git a/Resources/Shaders/PostProcess.nzsl b/Resources/Shaders/PostProcess.nzsl index 213ccb7..14f4b5a 100644 --- a/Resources/Shaders/PostProcess.nzsl +++ b/Resources/Shaders/PostProcess.nzsl @@ -65,9 +65,9 @@ option EnableRadialBlur: bool = true; fn SampleRadialBlur(uv: vec2[f32]) -> vec4[f32] { const center = vec2[f32](0.5, 0.5); - const blur_strength: f32 = 0.01; + const blur_strength: f32 = 0.02; const samples: i32 = 16; - const fade_start: f32 = -0.1; + const fade_start: f32 = 0.0; let dir: vec2[f32] = uv - center; let dist: f32 = length(dir); @@ -83,7 +83,7 @@ fn SampleRadialBlur(uv: vec2[f32]) -> vec4[f32] sample_uv -= step; } let color = accum_color / f32(samples); - let fade: f32 = clamp((dist - fade_start) / (1.0 - fade_start), 0.0, 1.0); + let fade: f32 = clamp((dist - fade_start) / (1.0 - fade_start) * 1.3, 0.0, 1.0); return color * (1.0 - fade); } @@ -105,7 +105,7 @@ fn main(input: VertOut) -> FragOut { const fog_near: f32 = 0.9; const fog_far: f32 = 1.0; - const fog_color: vec4[f32] = vec4[f32](0.0, 0.0, 0.25, 1.0); + const fog_color: vec4[f32] = vec4[f32](0.001, 0.003, 0.012, 1.0); let fog_factor: f32 = (fog_far - depth) / (fog_far - fog_near); fog_factor = clamp(fog_factor, 0.0, 1.0); output.color = MixVec4f32(fog_color, output.color, fog_factor); diff --git a/Resources/Shaders/WaterFragment.nzsl b/Resources/Shaders/WaterFragment.nzsl index 0b8470b..6d97d84 100644 --- a/Resources/Shaders/WaterFragment.nzsl +++ b/Resources/Shaders/WaterFragment.nzsl @@ -5,7 +5,8 @@ struct VertOut { [location(0)] color: vec4[f32], [location(1)] uv: vec2[f32], - [location(2)] time: f32, + [location(2)] normal: vec4[f32], + [location(3)] time: f32, [builtin(position)] pos: vec4[f32] } @@ -33,7 +34,7 @@ fn main(input: VertOut) -> FragOut if(input.color.a == 0.0) discard; let output: FragOut; - output.color = input.color * u_albedo.Sample(input.uv) * vec4[f32](sin(input.time), 1.0, cos(input.time), 1.0); + output.color = input.color * vec4[f32](u_albedo.Sample(input.uv).rgb * 0.25, 1.0); return output; } diff --git a/Resources/Shaders/WaterVertex.nzsl b/Resources/Shaders/WaterVertex.nzsl index 824f2d4..fe22fd8 100644 --- a/Resources/Shaders/WaterVertex.nzsl +++ b/Resources/Shaders/WaterVertex.nzsl @@ -1,22 +1,24 @@ [nzsl_version("1.0")] +[feature(float64)] module; import ViewerData from ScopEngine.ViewerData; struct VertIn { - [location(0)] pos: vec4[f32], - [location(1)] color: vec4[f32], - [location(2)] normal: vec4[f32], - [location(3)] uv: vec2[f32] + [location(0)] pos: vec4[f32], + [location(1)] color: vec4[f32], + [location(2)] normal: vec4[f32], + [location(3)] uv: vec2[f32] } struct VertOut { - [location(0)] color: vec4[f32], - [location(1)] uv: vec2[f32], - [location(2)] time: f32, - [builtin(position)] pos: vec4[f32] + [location(0)] color: vec4[f32], + [location(1)] uv: vec2[f32], + [location(2)] normal: vec4[f32], + [location(3)] time: f32, + [builtin(position)] pos: vec4[f32] } struct ModelData @@ -27,23 +29,32 @@ struct ModelData struct CustomData { - time: f32, + time: f64, } external { - [set(0), binding(0)] viewer_data: uniform[ViewerData], - [set(0), binding(1)] data: uniform[CustomData], - model: push_constant[ModelData] + [set(0), binding(0)] viewer_data: uniform[ViewerData], + [set(0), binding(1)] data: uniform[CustomData], + model: push_constant[ModelData] } [entry(vert)] fn main(input: VertIn) -> VertOut { - let output: VertOut; - output.color = input.color; - output.uv = input.uv; - output.time = data.time; - output.pos = viewer_data.view_proj_matrix * model.matrix * input.pos; - return output; + let position = model.matrix * vec4[f32](input.pos.xyz, 1.0); + if(input.pos.w == 0.0) + { + const speed: f32 = 0.003; + position.y -= 0.2; + position.y += (sin(position.x + f32(data.time) * speed) + sin(position.z + f32(data.time) * speed)) * 0.05; + } + + let output: VertOut; + output.color = input.color; + output.uv = input.uv; + output.normal = input.normal; + output.time = f32(data.time); + output.pos = viewer_data.view_proj_matrix * position; + return output; } diff --git a/ScopEngine/Assets/Shaders/ForwardVertex.nzsl b/ScopEngine/Assets/Shaders/ForwardVertex.nzsl index 1814167..3c8fbc3 100644 --- a/ScopEngine/Assets/Shaders/ForwardVertex.nzsl +++ b/ScopEngine/Assets/Shaders/ForwardVertex.nzsl @@ -42,7 +42,7 @@ fn main(input: VertIn) -> VertOut output.uv = input.uv; output.norm = normalize(input.normal); output.transformed_norm = mat3[f32](model.normal) * output.norm.xyz; - output.frag_position = model.matrix * input.pos; + output.frag_position = model.matrix * vec4[f32](input.pos.xyz, 1.0); output.camera_position = viewer_data.camera_position; output.pos = viewer_data.view_proj_matrix * output.frag_position; return output; diff --git a/ScopEngine/Runtime/Includes/Renderer/Pipelines/Graphics.h b/ScopEngine/Runtime/Includes/Renderer/Pipelines/Graphics.h index c3195d5..be1c0ce 100644 --- a/ScopEngine/Runtime/Includes/Renderer/Pipelines/Graphics.h +++ b/ScopEngine/Runtime/Includes/Renderer/Pipelines/Graphics.h @@ -31,10 +31,22 @@ namespace Scop class GraphicPipeline : public Pipeline { + friend class Render2DPass; + friend class FinalPass; + friend class ForwardPass; + friend class PostProcessPass; + friend class SkyboxPass; + public: GraphicPipeline() = default; - void Init(GraphicPipelineDescriptor descriptor); + inline void Setup(GraphicPipelineDescriptor descriptor) + { + if(!descriptor.vertex_shader || !descriptor.fragment_shader) + FatalError("Vulkan: invalid shaders"); + m_description = std::move(descriptor); + } + bool BindPipeline(VkCommandBuffer command_buffer, std::size_t framebuffer_index, std::array clear) noexcept; void EndPipeline(VkCommandBuffer command_buffer) noexcept override; void Destroy() noexcept; @@ -48,6 +60,7 @@ namespace Scop inline ~GraphicPipeline() noexcept { Destroy(); } private: + void Init(GraphicPipelineDescriptor descriptor); void CreateFramebuffers(const std::vector>& render_targets, bool clear_attachments); void TransitionAttachments(VkCommandBuffer cmd = VK_NULL_HANDLE); diff --git a/ScopEngine/Runtime/Sources/Renderer/Pipelines/Graphics.cpp b/ScopEngine/Runtime/Sources/Renderer/Pipelines/Graphics.cpp index a394374..07bb5f7 100644 --- a/ScopEngine/Runtime/Sources/Renderer/Pipelines/Graphics.cpp +++ b/ScopEngine/Runtime/Sources/Renderer/Pipelines/Graphics.cpp @@ -10,10 +10,7 @@ namespace Scop { void GraphicPipeline::Init(GraphicPipelineDescriptor descriptor) { - if(!descriptor.vertex_shader || !descriptor.fragment_shader) - FatalError("Vulkan: invalid shaders"); - - m_description = std::move(descriptor); + Setup(std::move(descriptor)); m_description.vertex_shader->SetPipelineInUse(this); m_description.fragment_shader->SetPipelineInUse(this); diff --git a/ScopEngine/Runtime/Sources/Renderer/RenderPasses/ForwardPass.cpp b/ScopEngine/Runtime/Sources/Renderer/RenderPasses/ForwardPass.cpp index 764a915..f8659fe 100644 --- a/ScopEngine/Runtime/Sources/Renderer/RenderPasses/ForwardPass.cpp +++ b/ScopEngine/Runtime/Sources/Renderer/RenderPasses/ForwardPass.cpp @@ -43,7 +43,6 @@ namespace Scop if(pipeline->GetDescription().depth != NonOwningPtr{ &scene.GetDepth() }) { GraphicPipelineDescriptor descriptor = pipeline->GetDescription(); - pipeline->Destroy(); descriptor.color_attachments = { &render_target }; descriptor.depth = &scene.GetDepth(); descriptor.renderer = nullptr;