Implemented terrain texturing

This commit is contained in:
Lorenzo Torres 2025-08-14 22:53:53 +02:00
parent 1475fd2101
commit 9fdd47ea6e
19 changed files with 1977 additions and 87 deletions

View file

@ -12,6 +12,8 @@ struct PointLight {
layout(location = 0) out vec4 outColor;
in vec4 gl_FragCoord;
layout(location = 2) in vec3 Normal;
layout(location = 3) in vec3 FragPos;
layout(location = 4) in vec2 TexCoords;
@ -35,14 +37,19 @@ layout(push_constant) uniform pc {
int light_count;
} pushConstants;
vec3 calc_directional_light(vec3 normal, vec3 viewDir) {
layout (set = 1, binding = 1) uniform sampler2D sand;
layout (set = 1, binding = 2) uniform sampler2D grass;
layout (set = 1, binding = 3) uniform sampler2D rock;
vec3 calc_directional_light(vec3 normal, vec3 viewDir, vec3 diffuse) {
vec3 lightDir = normalize(-directional_light.direction);
float diff = max(dot(normal, lightDir), 0.0);
vec3 diffuse = directional_light.diffuse * diff;
return (directional_light.ambient + diffuse);
vec3 ambient = directional_light.ambient * diffuse;
vec3 d = directional_light.diffuse * diff * diffuse;
return (ambient + d);
}
vec3 calc_point_light(int index, vec3 normal, vec3 fragPos, vec3 viewDir) {
vec3 calc_point_light(int index, vec3 normal, vec3 fragPos, vec3 viewDir, vec3 diffuse) {
float constant = point_lights.point_lights[index].data[0];
float linear = point_lights.point_lights[index].data[1];
float quadratic = point_lights.point_lights[index].data[2];
@ -52,35 +59,43 @@ vec3 calc_point_light(int index, vec3 normal, vec3 fragPos, vec3 viewDir) {
vec3 reflectDir = reflect(-lightDir, normal);
float distance = length(point_lights.point_lights[index].position - fragPos);
float attenuation = 1.0 / (constant + linear * distance + quadratic * (distance * distance));
vec3 ambient = point_lights.point_lights[index].ambient;
vec3 diffuse = point_lights.point_lights[index].diffuse * diff;
vec3 ambient = point_lights.point_lights[index].ambient * diffuse;
vec3 d = point_lights.point_lights[index].diffuse * diff * diffuse;
ambient *= attenuation;
diffuse *= attenuation;
return (ambient + diffuse);
d *= attenuation;
return (ambient + d);
}
void main() {
vec3 norm = normalize(Normal);
vec3 viewDir = normalize(viewPos.pos - FragPos);
vec3 result = calc_directional_light(norm, viewDir);
float height = FragPos.y;
float sandWeight = 1.0 - smoothstep(0.0, 0.035, height);
float grassWeight = smoothstep(0.035, 0.15, height) - smoothstep(0.25, 0.4, height);
float rockWeight = smoothstep(0.25, 0.4, height);
float total = sandWeight + grassWeight + rockWeight;
sandWeight /= total;
grassWeight /= total;
rockWeight /= total;
vec4 sandColor = texture(sand, TexCoords);
vec4 grassColor = texture(grass, TexCoords);
vec4 rockColor = texture(rock, TexCoords);
vec4 finalColor = sandColor * sandWeight +
grassColor * grassWeight +
rockColor * rockWeight;
vec3 result = calc_directional_light(norm, viewDir, vec3(finalColor));
//vec3 result = vec3(0.0, 0.0, 0.0);
for(int i = 0; i < pushConstants.light_count; i++)
result += calc_point_light(i, norm, FragPos, viewDir);
result += calc_point_light(i, norm, FragPos, viewDir, vec3(finalColor));
vec3 tall = vec3(1.0, 0.0, 0.0);
vec3 mid = vec3(0.0, 1.0, 0.0);
vec3 water = vec3(0.0, 0.0, 1.0);
vec3 color;
if (FragPos.y < 0.05) {
color = water;
} else if (FragPos.y > 0.3) {
color = tall;
} else {
color = mid;
}
outColor = vec4(color+result, 1.0);
outColor = vec4(result, 1.0);
//outColor = vec4(finalColor);
}

View file

@ -1,5 +1,6 @@
#version 450
layout(location = 0) in vec3 vertPos;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 uv;
@ -19,17 +20,18 @@ layout(location = 4) out vec2 TexCoords;
layout (set = 1, binding = 0) uniform sampler2D diffuseSampler;
void main() {
float texelSize = 1.0 / 200;
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 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 dX = (hR - hL) * 15.0;
float dY = (hU - hD) * 15.0;
float y = texture(diffuseSampler, uv).x * 3;
vec4 out_vec = proj.proj * view.view * vec4(vec3(vertPos.x, y, vertPos.z), 1.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);
Normal = normalize(vec3(-dX, -dY, 1.0));

BIN
assets/textures/grass.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 MiB

BIN
assets/textures/rock.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 MiB

BIN
assets/textures/sand.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1 MiB