From 752f60c4dcb63b98a23a0d38657f92157c7b14cc Mon Sep 17 00:00:00 2001 From: ninetailsrabbit Date: Mon, 28 Oct 2024 11:30:08 +0000 Subject: [PATCH] new method to remove childen with free() instead of queue_free() and new shader to mix albedo textures on terrain --- icon.svg.import | 13 +++---- shaders/terrain/albedo_terrain_mix.gdshader | 38 +++++++++++++++++++++ utilities/nodes/node_remover.gd | 14 ++++++++ 3 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 shaders/terrain/albedo_terrain_mix.gdshader diff --git a/icon.svg.import b/icon.svg.import index 8de5b3e..f80bbf5 100644 --- a/icon.svg.import +++ b/icon.svg.import @@ -3,25 +3,26 @@ importer="texture" type="CompressedTexture2D" uid="uid://g1k1oqsoq1tp" -path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex" +path.s3tc="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.s3tc.ctex" metadata={ -"vram_texture": false +"imported_formats": ["s3tc_bptc"], +"vram_texture": true } [deps] source_file="res://icon.svg" -dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"] +dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.s3tc.ctex"] [params] -compress/mode=0 +compress/mode=2 compress/high_quality=false compress/lossy_quality=0.7 compress/hdr_compression=1 compress/normal_map=0 compress/channel_pack=0 -mipmaps/generate=false +mipmaps/generate=true mipmaps/limit=-1 roughness/mode=0 roughness/src_normal="" @@ -31,7 +32,7 @@ process/normal_map_invert_y=false process/hdr_as_srgb=false process/hdr_clamp_exposure=false process/size_limit=0 -detect_3d/compress_to=1 +detect_3d/compress_to=0 svg/scale=1.0 editor/scale_with_editor_scale=false editor/convert_colors_with_editor_theme=false diff --git a/shaders/terrain/albedo_terrain_mix.gdshader b/shaders/terrain/albedo_terrain_mix.gdshader new file mode 100644 index 0000000..62c68e2 --- /dev/null +++ b/shaders/terrain/albedo_terrain_mix.gdshader @@ -0,0 +1,38 @@ +// https://godotshaders.com/shader/albedo-terrain-mix-shader/ +shader_type spatial; + +uniform sampler2D source_texture_mask : source_color; +uniform sampler2D source_texture_black : source_color; +uniform sampler2D source_texture_red : source_color; +uniform sampler2D source_texture_green : source_color; +uniform sampler2D source_texture_blue : source_color; + +uniform float uv_size : hint_range(0.01, 100.0, 0.01) = 1.0; + +void fragment() { + +vec2 UV_Scaled = UV * uv_size; + +// texture_rgbmask UV is not scaled. +vec3 texture_rgbmask= texture(source_texture_mask, UV).rgb; +vec3 texture_black = texture(source_texture_black, UV_Scaled).rgb; +vec3 texture_red = texture(source_texture_red, UV_Scaled).rgb; +vec3 texture_green = texture(source_texture_green, UV_Scaled).rgb; +vec3 texture_blue = texture(source_texture_blue, UV_Scaled).rgb; + +float summed_texture_channels = ( + texture_rgbmask.r + + texture_rgbmask.g + + texture_rgbmask.b); + +vec3 mixed_terrain = clamp( + ( texture_rgbmask.r * texture_red + + texture_rgbmask.g * texture_green + + texture_rgbmask.b * texture_blue) / + summed_texture_channels, + vec3(0.0), vec3(1.0) // Clamp min, max values. + ); + +ALBEDO = mix(mixed_terrain,texture_black,1.0 - summed_texture_channels); + +} // Fragment end \ No newline at end of file diff --git a/utilities/nodes/node_remover.gd b/utilities/nodes/node_remover.gd index f9c6816..739b3f3 100644 --- a/utilities/nodes/node_remover.gd +++ b/utilities/nodes/node_remover.gd @@ -36,3 +36,17 @@ static func queue_free_children(node: Node, except: Array = []) -> void: remove(child) except.clear() + + +## Exceptions are passed as [Area3D.new().get_class) +static func free_children(node: Node, except: Array = []) -> void: + if node.get_child_count() == 0: + return + + var childrens = node.get_children().filter(func(child): return not child.get_class() in except) + childrens.reverse() + + for child in childrens.filter(func(_node: Node): return is_node_valid(_node)): + child.free() + + except.clear()