Changed terrain texture UVs

This commit is contained in:
Lorenzo Torres 2025-08-14 23:35:56 +02:00
parent d6baada9ce
commit 74bf3f7ac3
5 changed files with 70 additions and 25 deletions

View file

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

View file

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

View file

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

View file

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

View file

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