Adding procedural textures to the ground

This commit is contained in:
2024-09-01 15:30:51 +02:00
parent f9e1a56615
commit e0293db640
14 changed files with 259 additions and 17 deletions

View File

@@ -16,7 +16,6 @@ func _process(delta):
if Input.is_action_pressed("grab"):
var grabbing_movement = mouse_last_global_position - get_global_mouse_position()
print(grabbing_movement)
movement += grabbing_movement
var mouse_pos = get_viewport().get_mouse_position()

View File

@@ -2,16 +2,31 @@ class_name Map
extends Node2D
const OUT_OF_BOUND_DIST = 2000
@export var n_animals_to_liberate := 4
func _ready():
var map_size = GameTerrain.TERRAIN_SIZE * GameTerrain.MAP_RATIO
$Ground.set_polygon(PackedVector2Array([
Vector2(0,0),
Vector2(map_size.x, 0),
Vector2(map_size.x, map_size.y),
Vector2(0, map_size.y),
for polygon in [$Ground, $Fertility]:
polygon.set_polygon(PackedVector2Array([
Vector2(0,0),
Vector2(map_size.x, 0),
Vector2(map_size.x, map_size.y),
Vector2(0, map_size.y),
]))
polygon.material.set_shader_parameter("data_texture", GameTerrain.texture)
polygon.material.set_shader_parameter(
"data_texture_size",
GameTerrain.TERRAIN_SIZE * GameTerrain.MAP_RATIO
)
$OutOfBound.set_polygon(PackedVector2Array([
Vector2(-OUT_OF_BOUND_DIST,-OUT_OF_BOUND_DIST),
Vector2(map_size.x + OUT_OF_BOUND_DIST, -OUT_OF_BOUND_DIST),
Vector2(map_size.x + OUT_OF_BOUND_DIST, map_size.y + OUT_OF_BOUND_DIST),
Vector2(-OUT_OF_BOUND_DIST, map_size.y + OUT_OF_BOUND_DIST),
]))
func _on_gui_scanner_selected(type : Scanners.Type):

View File

@@ -0,0 +1,57 @@
shader_type canvas_item;
#define pow2(x) (x * x)
#define iResolution 1.0/SCREEN_PIXEL_SIZE
uniform sampler2D data_texture;
uniform vec2 data_texture_size;
uniform sampler2D texture_low : filter_nearest, repeat_enable;
uniform float texture_low_threshold : hint_range(0, 1) = 0.3;
uniform sampler2D texture_medium : filter_nearest, repeat_enable;
uniform sampler2D texture_high : filter_nearest, repeat_enable;
uniform float texture_high_threshold : hint_range(0, 1) = 0.7;
uniform int dimension;
uniform vec2 texture_size = vec2(100,100);
uniform float smooth_change_range = 0.15;
varying vec2 vert;
void vertex() {
vert = VERTEX;
}
void fragment() {
vec4 pixel_color = texture(data_texture, vert/data_texture_size);
float value = pixel_color.x;
if (dimension == 1) value = pixel_color.y;
if (dimension == 2) value = pixel_color.z;
vec4 color = texture(texture_medium, vert/texture_size);
if (value < texture_low_threshold)
color =
min(
(texture_low_threshold - value) / smooth_change_range,
1.0
) * texture(texture_low, vert/texture_size)
+ (1.0 - min(
(texture_low_threshold - value) / smooth_change_range,
1.0
)
) * texture(texture_medium, vert/texture_size);
if (value > texture_high_threshold)
color =
min(
(value - texture_high_threshold) / smooth_change_range,
1.0
) * texture(texture_high, vert/texture_size)
+ (1.0 - min(
(value - texture_high_threshold) / smooth_change_range,
1.0
)
) * texture(texture_medium, vert/texture_size);
COLOR = color;
}

View File

@@ -151,11 +151,29 @@ func get_stat(
func setup_texture(
levels : Vector3i
):
var water_noise := generate_noise()
var fertility_noise := generate_noise()
for x in range(0, TERRAIN_SIZE.x) :
for y in range(0, TERRAIN_SIZE.y):
set_pixel(Vector2i(x,y), levels)
set_pixel(
Vector2i(x,y),
Vector3i(
color_value_to_level(water_noise.get_noise_2d(x, y)),
color_value_to_level(fertility_noise.get_noise_2d(x, y)/2),
-5
)
)
update_texture()
func generate_noise() -> Noise:
var noise := FastNoiseLite.new()
noise.seed = randi()
noise.fractal_lacunarity = 1
noise.frequency = 0.005
return noise
func update_texture():
texture.update(image)