adding waves to water, improving water color, fixing underwater view

This commit is contained in:
2025-06-02 17:30:15 +02:00
parent f6decee5fa
commit f26ad5855d
10 changed files with 86 additions and 51 deletions

View File

@@ -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<std::array<Scop::Vec2ui, 3>, BlocksCount> BLOCKS_TO_ATLAS = {
// TOP BOTTOM SIDE
std::array<Scop::Vec2ui, 3>{ Scop::Vec2ui{ 0, 0 }, Scop::Vec2ui{ 0, 0 }, Scop::Vec2ui{ 0, 0 } }, // Air
std::array<Scop::Vec2ui, 3>{ Scop::Vec2ui{ 1, 1 }, Scop::Vec2ui{ 1, 1 }, Scop::Vec2ui{ 1, 1 } }, // Water
std::array<Scop::Vec2ui, 3>{ 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<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.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<std::uint32_t>(BlockType::Air) : static_cast<std::uint32_t>(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<std::uint32_t>(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<std::uint32_t>(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<std::uint32_t>(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<float>(y) - 0.2f : static_cast<float>(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<std::uint32_t>(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<std::uint32_t>(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<Scop::Scene> scene, Scop::NonOwningPtr<Scop::Actor> actor, Scop::Inputs& input, float delta)
{
WaterData* data = actor->GetCustomPipeline()->data.GetDataAs<WaterData>();
data->time += delta;
data->time = static_cast<double>(SDL_GetTicks64());
};
using actor_hook = std::function<void(Scop::NonOwningPtr<Scop::Actor>)>;
actor.AttachScript(std::make_shared<Scop::NativeActorScript>(actor_hook{}, object_update, actor_hook{}));

View File

@@ -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<Scop::FirstPerson3D*>(m_scene.GetCamera().get());
std::int32_t x_chunk = static_cast<std::int32_t>(camera->GetPosition().x) / static_cast<std::int32_t>(CHUNK_SIZE.x);
std::int32_t z_chunk = static_cast<std::int32_t>(camera->GetPosition().z) / static_cast<std::int32_t>(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<std::int32_t>(camera_position.x < 0.0f ? camera_position.x - 1.0f : camera_position.x),
static_cast<std::int32_t>(camera_position.z < 0.0f ? camera_position.z - 1.0f : camera_position.z),
};
m_current_chunk_position = Scop::Vec2i{
static_cast<std::int32_t>(global_block_position.x < 0 ? std::floorf(global_block_position.x / static_cast<float>(CHUNK_SIZE.x)) : global_block_position.x / static_cast<std::int32_t>(CHUNK_SIZE.x)),
static_cast<std::int32_t>(global_block_position.y < 0 ? std::floorf(global_block_position.y / static_cast<float>(CHUNK_SIZE.z)) : global_block_position.y / static_cast<std::int32_t>(CHUNK_SIZE.z)),
};
if(Scop::NonOwningPtr<Chunk> 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<std::uint32_t>(BlockType::Water);
Scop::Vec3i block_position = Scop::Vec3i{
global_block_position.x - (m_current_chunk_position.x * static_cast<std::int32_t>(CHUNK_SIZE.x)),
static_cast<std::int32_t>(camera_position.y),
global_block_position.y - (m_current_chunk_position.y * static_cast<std::int32_t>(CHUNK_SIZE.z)),
};
post_process_data.underwater = current_chunk->GetBlock(block_position) == static_cast<std::uint32_t>(BlockType::Water);
}
post_process_data.inv_res = Scop::Vec2f{ 1.0f / Scop::ScopEngine::Get().GetWindow().GetWidth(), 1.0f / Scop::ScopEngine::Get().GetWindow().GetHeight() };

View File

@@ -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));

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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<float, 4> 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<NonOwningPtr<Texture>>& render_targets, bool clear_attachments);
void TransitionAttachments(VkCommandBuffer cmd = VK_NULL_HANDLE);

View File

@@ -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);

View File

@@ -43,7 +43,6 @@ namespace Scop
if(pipeline->GetDescription().depth != NonOwningPtr<DepthImage>{ &scene.GetDepth() })
{
GraphicPipelineDescriptor descriptor = pipeline->GetDescription();
pipeline->Destroy();
descriptor.color_attachments = { &render_target };
descriptor.depth = &scene.GetDepth();
descriptor.renderer = nullptr;