From 74bf3f7ac311ef4612ff32cb7ff28a8197fbcb63 Mon Sep 17 00:00:00 2001 From: Lorenzo Torres Date: Thu, 14 Aug 2025 23:35:56 +0200 Subject: [PATCH] Changed terrain texture UVs --- assets/shaders/terrain.frag | 6 ++-- assets/shaders/terrain.vert | 23 ++++++------ src/rendering/Mesh.zig | 58 +++++++++++++++++++++++++++---- src/rendering/TerrainPipeline.zig | 4 +-- src/sideros.zig | 4 +-- 5 files changed, 70 insertions(+), 25 deletions(-) diff --git a/assets/shaders/terrain.frag b/assets/shaders/terrain.frag index e210900..c8cdcaf 100644 --- a/assets/shaders/terrain.frag +++ b/assets/shaders/terrain.frag @@ -83,9 +83,9 @@ void main() { grassWeight /= total; rockWeight /= total; - vec4 sandColor = texture(sand, TexCoords); - vec4 grassColor = texture(grass, TexCoords); - vec4 rockColor = texture(rock, TexCoords); + vec4 sandColor = texture(sand, vec2(FragPos.x, FragPos.z)); + vec4 grassColor = texture(grass, vec2(FragPos.x, FragPos.z)); + vec4 rockColor = texture(rock, vec2(FragPos.x, FragPos.z)); vec4 finalColor = sandColor * sandWeight + grassColor * grassWeight + diff --git a/assets/shaders/terrain.vert b/assets/shaders/terrain.vert index 6c09862..a0ff53c 100644 --- a/assets/shaders/terrain.vert +++ b/assets/shaders/terrain.vert @@ -1,9 +1,8 @@ #version 450 - -layout(location = 0) in vec3 vertPos; -layout(location = 1) in vec3 normal; -layout(location = 2) in vec2 uv; +layout(location = 0) in vec2 vertPos; +layout(location = 1) in vec2 uv; +layout(location = 2) in vec2 frag; layout (binding = 0) uniform ProjUniform { mat4 proj; @@ -20,19 +19,21 @@ layout(location = 4) out vec2 TexCoords; layout (set = 1, binding = 0) uniform sampler2D diffuseSampler; void main() { + float y = texture(diffuseSampler, uv).x; + float mul = pow(2, y * 5); + float texelSize = 1.0 / (50*4); - float hL = texture(diffuseSampler, uv - vec2(texelSize, 0.0)).r * 10; - float hR = texture(diffuseSampler, uv + vec2(texelSize, 0.0)).r * 10; - float hD = texture(diffuseSampler, uv - vec2(0.0, texelSize)).r * 10; - float hU = texture(diffuseSampler, uv + vec2(0.0, texelSize)).r * 10; + float hL = texture(diffuseSampler, uv - vec2(texelSize, 0.0)).r; + float hR = texture(diffuseSampler, uv + vec2(texelSize, 0.0)).r; + float hD = texture(diffuseSampler, uv - vec2(0.0, texelSize)).r; + float hU = texture(diffuseSampler, uv + vec2(0.0, texelSize)).r; float dX = (hR - hL) * 15.0; float dY = (hU - hD) * 15.0; - float y = texture(diffuseSampler, uv).x; - vec4 out_vec = proj.proj * view.view * vec4(vec3(vertPos.x, y * 10, vertPos.z), 1.0); - FragPos = vec3(vertPos.x, y, vertPos.z); + vec4 out_vec = proj.proj * view.view * vec4(vec3(vertPos.x, y * mul, vertPos.y), 1.0); + FragPos = vec3(vertPos.x, y, vertPos.y); Normal = normalize(vec3(-dX, -dY, 1.0)); TexCoords = uv; diff --git a/src/rendering/Mesh.zig b/src/rendering/Mesh.zig index b1f123b..be83347 100644 --- a/src/rendering/Mesh.zig +++ b/src/rendering/Mesh.zig @@ -60,10 +60,52 @@ pub const Vertex = struct { } }; -fn createVertexBuffer(device: vk.Device, vertices: std.ArrayList([8]f32)) !vk.Buffer { +pub const TerrainVertex = struct { + position: [2]f32, + uv: [2]f32, + texture: [2]f32, + + pub fn bindingDescription() vk.c.VkVertexInputBindingDescription { + const binding_description: vk.c.VkVertexInputBindingDescription = .{ + .binding = 0, + .stride = @sizeOf(TerrainVertex), + .inputRate = vk.c.VK_VERTEX_INPUT_RATE_VERTEX, + }; + + return binding_description; + } + + pub fn attributeDescriptions() []const c.VkVertexInputAttributeDescription { + const attributes: []const c.VkVertexInputAttributeDescription = &.{ + .{ + .location = 0, + .binding = 0, + .format = c.VK_FORMAT_R32G32_SFLOAT, + .offset = 0, + }, + .{ + .location = 1, + .binding = 0, + .format = c.VK_FORMAT_R32G32_SFLOAT, + .offset = 8, + }, + .{ + .location = 2, + .binding = 0, + .format = c.VK_FORMAT_R32G32_SFLOAT, + .offset = 16, + }, + }; + + return attributes; + } +}; + + +fn createVertexBuffer(device: vk.Device, vertices: std.ArrayList([6]f32)) !vk.Buffer { var data: [*c]?*anyopaque = null; - const buffer = try device.initBuffer(vk.BufferUsage{ .transfer_src = true }, vk.BufferFlags{ .host_visible = true, .host_coherent = true }, @sizeOf([8]f32) * vertices.items.len); + const buffer = try device.initBuffer(vk.BufferUsage{ .transfer_src = true }, vk.BufferFlags{ .host_visible = true, .host_coherent = true }, @sizeOf([6]f32) * vertices.items.len); try vk.mapError(vk.c.vkMapMemory( device.handle, @@ -75,14 +117,14 @@ fn createVertexBuffer(device: vk.Device, vertices: std.ArrayList([8]f32)) !vk.Bu )); if (data) |ptr| { - const gpu_vertices: [*]Mesh.Vertex = @ptrCast(@alignCast(ptr)); + const gpu_vertices: [*]Mesh.TerrainVertex = @ptrCast(@alignCast(ptr)); - @memcpy(gpu_vertices, @as([]Mesh.Vertex, @ptrCast(vertices.items[0..]))); + @memcpy(gpu_vertices, @as([]Mesh.TerrainVertex, @ptrCast(vertices.items[0..]))); } vk.c.vkUnmapMemory(device.handle, buffer.memory); - const vertex_buffer = try device.initBuffer(vk.BufferUsage{ .vertex_buffer = true, .transfer_dst = true, .transfer_src = true }, vk.BufferFlags{ .device_local = true }, @sizeOf(Mesh.Vertex) * vertices.items.len); + const vertex_buffer = try device.initBuffer(vk.BufferUsage{ .vertex_buffer = true, .transfer_dst = true, .transfer_src = true }, vk.BufferFlags{ .device_local = true }, @sizeOf(Mesh.TerrainVertex) * vertices.items.len); try buffer.copyTo(device, vertex_buffer, 0); buffer.deinit(device.handle); @@ -121,7 +163,7 @@ fn createIndexBuffer(device: vk.Device, indices: std.ArrayList(u32)) !vk.Buffer } pub fn terrain(allocator: std.mem.Allocator, device: vk.Device, width: usize, height: usize, resolution: usize) !struct { vk.Buffer, vk.Buffer } { - var vertices = std.ArrayList([8]f32).init(allocator); + var vertices = std.ArrayList([6]f32).init(allocator); defer vertices.deinit(); var indices = std.ArrayList(u32).init(allocator); defer indices.deinit(); @@ -130,8 +172,10 @@ pub fn terrain(allocator: std.mem.Allocator, device: vk.Device, width: usize, he for (0..height*resolution) |z| { const offset_x = @as(f32, @floatFromInt(x)) / @as(f32, @floatFromInt(width*resolution - 1)) * @as(f32, @floatFromInt(width)); const offset_z = @as(f32, @floatFromInt(z)) / @as(f32, @floatFromInt(height*resolution - 1)) * @as(f32, @floatFromInt(height)); + const u = @as(f32, @floatFromInt(x)) / @as(f32, @floatFromInt(width*resolution - 1)); + const v = @as(f32, @floatFromInt(z)) / @as(f32, @floatFromInt(width*resolution - 1)); - const vertex: [8]f32 = .{offset_x, 0.0, offset_z, 0.0, 0.0, 0.0, @as(f32, @floatFromInt(x)) / @as(f32, @floatFromInt(width*resolution - 1)), @as(f32, @floatFromInt(z)) / @as(f32, @floatFromInt(height*resolution - 1))}; + const vertex: [6]f32 = .{offset_x, offset_z, u, v, offset_x, offset_z }; try vertices.append(vertex); } } diff --git a/src/rendering/TerrainPipeline.zig b/src/rendering/TerrainPipeline.zig index 1ec06d6..d9b6e92 100644 --- a/src/rendering/TerrainPipeline.zig +++ b/src/rendering/TerrainPipeline.zig @@ -54,8 +54,8 @@ pub fn init( // TODO: shouldn't this be closer to usage? const shader_stage_infos: []const c.VkPipelineShaderStageCreateInfo = &.{ vertex_shader_stage_info, fragment_shader_stage_info }; - const vertex_attributes: []const c.VkVertexInputAttributeDescription = Mesh.Vertex.attributeDescriptions(); - const vertex_bindings: []const c.VkVertexInputBindingDescription = &.{Mesh.Vertex.bindingDescription()}; + const vertex_attributes: []const c.VkVertexInputAttributeDescription = Mesh.TerrainVertex.attributeDescriptions(); + const vertex_bindings: []const c.VkVertexInputBindingDescription = &.{Mesh.TerrainVertex.bindingDescription()}; const vertex_input_info: c.VkPipelineVertexInputStateCreateInfo = .{ .sType = c.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, diff --git a/src/sideros.zig b/src/sideros.zig index a25512e..4d8ef29 100644 --- a/src/sideros.zig +++ b/src/sideros.zig @@ -153,8 +153,8 @@ export fn sideros_init(init: api.GameInit) callconv(.c) void { .multiplier = 1.0, .exponent = 1.0, - .width = 100, - .height = 100, + .width = 200, + .height = 200, .seed = 2497852058242342, .resolution = 1, }) catch @panic("TODO: handle this");