From fd16f3b9fcba1f75030545c3d167311c82507b1f Mon Sep 17 00:00:00 2001 From: Fabio Iotti Date: Sat, 24 Aug 2024 18:00:26 +0200 Subject: [PATCH] Improve background --- main.tscn | 9 ++- .../background_nebula.gdshader | 14 ---- scenes/backgrounds/background_nebula.gdshader | 11 +++ scenes/backgrounds/background_stars.gdshader | 76 +++++++++++++++++++ .../nebula.tscn} | 3 +- scenes/backgrounds/stars.tscn | 15 ++++ 6 files changed, 110 insertions(+), 18 deletions(-) delete mode 100644 scenes/background_nebula/background_nebula.gdshader create mode 100644 scenes/backgrounds/background_nebula.gdshader create mode 100644 scenes/backgrounds/background_stars.gdshader rename scenes/{background_nebula/background_nebula.tscn => backgrounds/nebula.tscn} (84%) create mode 100644 scenes/backgrounds/stars.tscn diff --git a/main.tscn b/main.tscn index d56db54..d78bc03 100644 --- a/main.tscn +++ b/main.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=18 format=3 uid="uid://cdaf4bh0jaa45"] +[gd_scene load_steps=19 format=3 uid="uid://cdaf4bh0jaa45"] [ext_resource type="Script" path="res://scripts/game_logic.gd" id="1_uj8ti"] [ext_resource type="Script" path="res://scripts/player.gd" id="2_plgh6"] @@ -7,9 +7,10 @@ [ext_resource type="Script" path="res://scripts/player_local.gd" id="4_7rlmh"] [ext_resource type="Texture2D" uid="uid://dx1wjviioa8u5" path="res://icons/chip.svg" id="4_pgo63"] [ext_resource type="Script" path="res://scripts/ui/planets_ui.gd" id="7_khbdl"] -[ext_resource type="PackedScene" uid="uid://dtlatmtuggp6x" path="res://scenes/background_nebula/background_nebula.tscn" id="7_sv4lv"] +[ext_resource type="PackedScene" uid="uid://dtlatmtuggp6x" path="res://scenes/backgrounds/nebula.tscn" id="7_sv4lv"] [ext_resource type="Script" path="res://scripts/player_ai.gd" id="7_v73fw"] [ext_resource type="Script" path="res://scripts/planet.gd" id="8_ve3b1"] +[ext_resource type="PackedScene" uid="uid://b0ec8jbenscpp" path="res://scenes/backgrounds/stars.tscn" id="9_lum1l"] [sub_resource type="Resource" id="Resource_mxp7y"] script = ExtResource("2_plgh6") @@ -57,7 +58,9 @@ planets_ui = NodePath("../PlanetsUI") players = Array[ExtResource("2_plgh6")]([SubResource("Resource_mxp7y"), SubResource("Resource_1dtpf"), SubResource("Resource_7wtc7")]) planets = Array[ExtResource("8_ve3b1")]([SubResource("Resource_50eg2"), SubResource("Resource_2525o"), SubResource("Resource_irtww"), SubResource("Resource_wdycy")]) -[node name="BackgroundNebula" parent="." instance=ExtResource("7_sv4lv")] +[node name="Stars" parent="." instance=ExtResource("9_lum1l")] + +[node name="Nebula" parent="." instance=ExtResource("7_sv4lv")] [node name="Planets" type="Node2D" parent="."] diff --git a/scenes/background_nebula/background_nebula.gdshader b/scenes/background_nebula/background_nebula.gdshader deleted file mode 100644 index 94c1be8..0000000 --- a/scenes/background_nebula/background_nebula.gdshader +++ /dev/null @@ -1,14 +0,0 @@ -shader_type canvas_item; - -uniform sampler2D noise: repeat_enable; - -float rand(vec2 co){ - return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453); -} - -void fragment() { - vec3 nebula = vec3(texture(noise, FRAGCOORD.xy * .0005).r, texture(noise, FRAGCOORD.xy * .0005 + 0.1).r, texture(noise, FRAGCOORD.xy * .0005 + 0.2).r) * 0.05; - vec3 star = (rand(FRAGCOORD.xy * .01) < 0.1 && rand(FRAGCOORD.xy * .02) < .01) ? vec3(rand(FRAGCOORD.xy * .03)) : vec3(0.); - - COLOR = vec4(nebula + star, 1.); -} diff --git a/scenes/backgrounds/background_nebula.gdshader b/scenes/backgrounds/background_nebula.gdshader new file mode 100644 index 0000000..81307b5 --- /dev/null +++ b/scenes/backgrounds/background_nebula.gdshader @@ -0,0 +1,11 @@ +shader_type canvas_item; + +uniform sampler2D noise: repeat_enable; + +void fragment() { + float r = texture(noise, FRAGCOORD.xy * .0005 + .0).r; + float g = texture(noise, FRAGCOORD.xy * .0005 + .1).r; + float b = texture(noise, FRAGCOORD.xy * .0005 + .2).r; + float a = texture(noise, FRAGCOORD.xy * .0002 + .3).r * .35; + COLOR = vec4(r, g, b, a); +} diff --git a/scenes/backgrounds/background_stars.gdshader b/scenes/backgrounds/background_stars.gdshader new file mode 100644 index 0000000..0d54d87 --- /dev/null +++ b/scenes/backgrounds/background_stars.gdshader @@ -0,0 +1,76 @@ +shader_type canvas_item; + +// Max 1 star for each cell, star size cannot be bigger than cell size. +const float CELL_SIZE = 8.; +const float MIN_STAR_SIZE = 1.; +const float MAX_STAR_SIZE = 6.; +const float START_DENSITY = .15; // Between 0 and 1. + +const float MIN_STAR_BRIGHTNESS = .10; +const float MAX_STAR_BRIGHTNESS = 1.; + +const bool ALIGN_TO_PIXEL = true; + +const float INV_CELL_SIZE = 1. / CELL_SIZE; +const float INV_MIN_STAR_SIZE = 1. / MIN_STAR_SIZE; +const float INV_MAX_STAR_SIZE = 1. / MAX_STAR_SIZE; + +vec3 srgb_to_linear(vec3 col) { + return pow(col, vec3(2.2)); +} + +vec3 linear_to_srgb(vec3 col) { + return pow(col, vec3(1. / 2.2)); +} + +vec4 blend_additive(vec4 bg, vec4 fg) { + vec4 dst = vec4(srgb_to_linear(bg.rgb), bg.a); + vec4 src = vec4(srgb_to_linear(fg.rgb), fg.a); + // glBlendEquation(GL_FUNC_ADD) + // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) + vec4 res = src * src.a + dst * (1. - src.a); + return vec4(linear_to_srgb(res.rgb), res.a); +} + +float rand(vec2 co){ + return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453); +} + +void fragment() { + vec2 center_cell; + vec2 pos_inside_center_cell = modf(FRAGCOORD.xy * INV_CELL_SIZE, center_cell); + + vec4 color = vec4(0., 0., 0., 0.); + + for (float y = -1.; y < 1.5; y++) { + for (float x = -1.; x < 1.5; x++) { + vec2 cell = center_cell + vec2(x, y); + vec2 pos_inside_cell = pos_inside_center_cell - vec2(x, y); + + bool cell_has_star = rand(cell * .0101) < START_DENSITY; + + if (!cell_has_star) + continue; + + vec2 star_pos_inside_cell = vec2(rand(cell * .0112), rand(cell * .0123)); + if (ALIGN_TO_PIXEL) + star_pos_inside_cell = floor(star_pos_inside_cell * CELL_SIZE) * INV_CELL_SIZE + INV_CELL_SIZE/2.; // Align to pixel. + + vec2 relative_pos = pos_inside_cell - star_pos_inside_cell; + float dist_to_star = (cell_has_star ? 0. : 999.) + length(relative_pos); + float inv_star_size = mix(INV_MIN_STAR_SIZE, INV_MAX_STAR_SIZE, rand(cell * .0134)); + + float adjusted_shape = dist_to_star + min(abs(relative_pos.x), abs(relative_pos.y)); + float pos_brightness = max(0., 1. - adjusted_shape * CELL_SIZE * inv_star_size); + float star_brightness = mix(MIN_STAR_BRIGHTNESS, MAX_STAR_BRIGHTNESS, rand(cell * 0.145)); + + vec2 rg = vec2(rand(cell * .0156), rand(cell * .0167)); + vec3 star_color = vec3(rg, max(0., 1. - rg.r - rg.g)); + star_color = mix(vec3(1.), star_color, .3); + + color = blend_additive(color, vec4(star_color, pos_brightness * star_brightness)); + } + } + + COLOR = vec4(color.rgb, min(1., color.a)); +} diff --git a/scenes/background_nebula/background_nebula.tscn b/scenes/backgrounds/nebula.tscn similarity index 84% rename from scenes/background_nebula/background_nebula.tscn rename to scenes/backgrounds/nebula.tscn index 1448855..5e0c8fe 100644 --- a/scenes/background_nebula/background_nebula.tscn +++ b/scenes/backgrounds/nebula.tscn @@ -1,8 +1,9 @@ [gd_scene load_steps=5 format=3 uid="uid://dtlatmtuggp6x"] -[ext_resource type="Shader" path="res://scenes/background_nebula/background_nebula.gdshader" id="1_nqp0c"] +[ext_resource type="Shader" path="res://scenes/backgrounds/background_nebula.gdshader" id="1_nqp0c"] [sub_resource type="FastNoiseLite" id="FastNoiseLite_8c2xm"] +fractal_octaves = 4 [sub_resource type="NoiseTexture2D" id="NoiseTexture2D_cukca"] width = 256 diff --git a/scenes/backgrounds/stars.tscn b/scenes/backgrounds/stars.tscn new file mode 100644 index 0000000..b8615b6 --- /dev/null +++ b/scenes/backgrounds/stars.tscn @@ -0,0 +1,15 @@ +[gd_scene load_steps=3 format=3 uid="uid://b0ec8jbenscpp"] + +[ext_resource type="Shader" path="res://scenes/backgrounds/background_stars.gdshader" id="1_u4u73"] + +[sub_resource type="ShaderMaterial" id="ShaderMaterial_y72ex"] +shader = ExtResource("1_u4u73") + +[node name="Stars" type="ColorRect"] +material = SubResource("ShaderMaterial_y72ex") +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +mouse_filter = 2