From 585495d9842a3e05088704df3d598b96c473c885 Mon Sep 17 00:00:00 2001 From: Gabriella Date: Wed, 22 Oct 2025 11:59:53 -0600 Subject: [PATCH] Create multicolored starfield Update gitignore --- .gitignore | 1 + .../mc_starfield.gdshader | 38 ++++++--- .../mc_starfield_material.tres | 7 +- .../multi_color_starfield.gdshader | 77 ++++++++++++++----- .../multi_color_starfield.tscn | 34 ++++---- 5 files changed, 109 insertions(+), 48 deletions(-) diff --git a/.gitignore b/.gitignore index 6831751..a96af4e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .editorconfig +.ds_store .godot/ diff --git a/src/multi-color-starfield/mc_starfield.gdshader b/src/multi-color-starfield/mc_starfield.gdshader index 68742c6..ff26089 100644 --- a/src/multi-color-starfield/mc_starfield.gdshader +++ b/src/multi-color-starfield/mc_starfield.gdshader @@ -3,28 +3,44 @@ shader_type canvas_item; uniform vec2 resolution = vec2(1270, 720); uniform sampler2D noise_texture: filter_nearest, repeat_enable; uniform float density: hint_range(1.0, 100.0, 0.1) = 20.0; -uniform float speed_x: hint_range(-100.0, 100, 0.1) = 0.005; -uniform float speed_y: hint_range(-100.0, 100, 0.1) = 0.0; +uniform float speed_x: hint_range(-0.01, 0.01, 0.005) = 0.005; +uniform float speed_y: hint_range(-0.01, 0.01, 0.005) = 0.0; uniform float layers: hint_range(1, 10, 1) = 5; -uniform vec4 color_1: source_color; -uniform vec4 color_2: source_color; -uniform vec4 color_3: source_color; -uniform vec4 color_4: source_color; float rand(vec2 uv) { return fract(sin(dot(uv.xy, vec2(12.9898,78.233))) * 43758.5453123); } +float rand_y(vec2 uv){ + return fract(sin(uv.y) * 43759.234231); +} + void fragment() { + // for each pixel, grab the UV value (coordinates vec2 uv = UV; + // reset aspect ratio uv.x *= resolution.x / resolution.y; + // this is the speed value, TIME is analogous to u_time vec2 speed = TIME * vec2(speed_x, speed_y); + // 'empty' float float stars = 0.0; - for(float i = 0.0; i < layers; i++){ - float shift = i * 0.3; - float brightness = 1.0 - i * 0.2; - stars += step(0.2, pow(texture(noise_texture, uv + shift + speed * (1.0 - i * 0.1)).r, density)) * brightness; + vec3 new_stars; + //for loop for each layer + for(int i = 0; i < int(layers); i++){ + // this is the parallax value + // TODO can be parameterized + float shift = float(i) * 0.3; + // brightness dependent on the layer a star is in + // simulating loss of brightness over distance + float brightness = 1.0 - float(i) * 0.2; + // we're looking for the "loudest" (whitest) noise on the noise texture + // this is inversely proportional to the denisty value + // 1 = more stars + // we add shift and speed to the uv value, and apply a simple parallax equation + // in order to get movement (as defined by the vec2 `speed` + stars += pow(texture(noise_texture, uv + shift + speed * (1.0 - float(i) * 0.1)).r, density); + new_stars = vec3(stars); + COLOR = vec4(new_stars, 1.0); } - COLOR = vec4(vec3(stars), 1.0); } diff --git a/src/multi-color-starfield/mc_starfield_material.tres b/src/multi-color-starfield/mc_starfield_material.tres index 160afdd..f44bee0 100644 --- a/src/multi-color-starfield/mc_starfield_material.tres +++ b/src/multi-color-starfield/mc_starfield_material.tres @@ -11,7 +11,6 @@ shader_parameter/density = 20.0 shader_parameter/speed_x = 0.005 shader_parameter/speed_y = 0.0 shader_parameter/layers = 7.0 -shader_parameter/color_1 = Color(0.3403393, 0.34033933, 0.3403393, 1) -shader_parameter/color_2 = Color(0, 0, 0, 1) -shader_parameter/color_3 = Color(0, 0, 0, 1) -shader_parameter/color_4 = Color(0, 0, 0, 1) +shader_parameter/upper_tolerance_for_star_color = 0.5000000074506 +shader_parameter/lower_tolerance_for_star_color = 0.25 +shader_parameter/green_tolerance_for_star_color = 0.1 diff --git a/src/multi-color-starfield/multi_color_starfield.gdshader b/src/multi-color-starfield/multi_color_starfield.gdshader index 2aec39d..9d8c72e 100644 --- a/src/multi-color-starfield/multi_color_starfield.gdshader +++ b/src/multi-color-starfield/multi_color_starfield.gdshader @@ -1,24 +1,61 @@ shader_type canvas_item; -uniform sampler2D noise_img; -uniform sampler2D gradient_tex; -uniform float speed = 1.0; -uniform vec4 smoke_color : source_color; +uniform vec2 resolution = vec2(1270, 720); +uniform sampler2D noise_texture: filter_nearest, repeat_enable; +uniform float density: hint_range(1.0, 100.0, 0.1) = 20.0; +uniform float speed_x: hint_range(-0.01, 0.01, 0.005) = 0.005; +uniform float speed_y: hint_range(-0.01, 0.01, 0.005) = 0.0; +uniform float layers: hint_range(1, 10, 1) = 5; +uniform float upper_tolerance_for_star_color: hint_range(0.0, 1.0, 0.1) = 0.75; +uniform float lower_tolerance_for_star_color: hint_range(0.0, 1.0, 0.1) = 0.25; +uniform float green_tolerance_for_star_color: hint_range(0.0, 1.0, 0.1) = 0.1; -void fragment(){ - vec2 uv1 = vec2(UV.x * 0.5 + TIME*speed, UV.y * 0.5 + TIME*speed*2.0); - vec2 uv2 = vec2(UV.x - TIME*speed, UV.y + TIME*speed*2.0); - vec2 uv3 = vec2(UV.x * 2.0, UV.y * 2.0 + TIME * speed*6.0); +float rand(vec2 uv) { + return fract(sin(dot(uv.xy, vec2(12.9898,78.233))) * 43758.5453123); +} + +float rand_y(vec2 uv){ + return fract(sin(uv.y) * 43759.234231); +} + +void fragment() { + // for each pixel, grab the UV value (coordinates + vec2 uv = UV; + // reset aspect ratio + uv.x *= resolution.x / resolution.y; + // this is the speed value, TIME is analogous to u_time + vec2 speed = TIME * vec2(speed_x, speed_y); + // 'empty' float + float stars = 0.0; + vec3 new_stars; + //for loop for each layer + for(int i = 0; i < int(layers); i++){ + // this is the parallax value + // TODO can be parameterized + float shift = float(i) * 0.3; + // brightness dependent on the layer a star is in + // simulating loss of brightness over distance + float brightness = 1.0 - float(i) * 0.2; + // we're looking for the "loudest" (whitest) noise on the noise texture + // this is inversely proportional to the denisty value + // 1 = more stars + // we add shift and speed to the uv value, and apply a simple parallax equation + // in order to get movement (as defined by the vec2 `speed` + stars += pow(texture(noise_texture, uv + shift + speed * (1.0 - float(i) * 0.1)).r, density); + new_stars = vec3(stars); + if (rand_y(uv) >= upper_tolerance_for_star_color){ + new_stars.g -= 0.2; + new_stars.r -= 1.0; + } else if (rand_y(uv) <= lower_tolerance_for_star_color){ + new_stars.g -= 0.75; + new_stars.r -= 0.25; + } else if (rand_y(uv) <= green_tolerance_for_star_color){ + new_stars.r -= 1.0; + new_stars.b -= 1.0; + } else{ + new_stars = new_stars; + } + COLOR = vec4(new_stars, 1.0); + } - float gradient = texture( gradient_tex, vec2(UV.y, UV.x) ).r; - float noise_r = texture( noise_img, uv1 ).r; - float noise_g = texture( noise_img, uv2 ).g; - float noise_b = texture( noise_img, uv3 ).b; - - vec3 new_color = vec3(noise_r, noise_g, noise_b); - - float new_alpha = noise_r * noise_g * noise_b * noise_r; - - COLOR.rgb = smoke_color.rgb; - COLOR.a = gradient; -} \ No newline at end of file +} diff --git a/src/multi-color-starfield/multi_color_starfield.tscn b/src/multi-color-starfield/multi_color_starfield.tscn index 70ac9d7..0eb90e0 100644 --- a/src/multi-color-starfield/multi_color_starfield.tscn +++ b/src/multi-color-starfield/multi_color_starfield.tscn @@ -1,22 +1,29 @@ -[gd_scene load_steps=7 format=3 uid="uid://c6f75hdvqpfaf"] +[gd_scene load_steps=5 format=3 uid="uid://c6f75hdvqpfaf"] [ext_resource type="Material" uid="uid://bgb22q7b2j1st" path="res://src/multi-color-starfield/mc_starfield_material.tres" id="1_yileu"] [ext_resource type="Shader" uid="uid://nfhuhvwogwqv" path="res://src/multi-color-starfield/multi_color_starfield.gdshader" id="2_mfsqn"] -[ext_resource type="Texture2D" uid="uid://cq1atp8etcqe8" path="res://src/multi-color-starfield/spotlight_3.png" id="3_2bx6i"] -[sub_resource type="Gradient" id="Gradient_2bx6i"] -colors = PackedColorArray(0, 0, 0, 1, 0.31431612, 0.31431612, 0.3143161, 1) - -[sub_resource type="GradientTexture2D" id="GradientTexture2D_vs6nd"] -gradient = SubResource("Gradient_2bx6i") -fill_from = Vector2(0, 0.032967035) +[sub_resource type="ShaderMaterial" id="ShaderMaterial_vs6nd"] +shader = ExtResource("2_mfsqn") +shader_parameter/resolution = Vector2(1270, 720) +shader_parameter/density = 20.0 +shader_parameter/speed_x = 0.005 +shader_parameter/speed_y = 0.0 +shader_parameter/layers = 5.0 +shader_parameter/upper_tolerance_for_star_color = 0.75 +shader_parameter/lower_tolerance_for_star_color = 0.25 +shader_parameter/green_tolerance_for_star_color = 0.1 [sub_resource type="ShaderMaterial" id="ShaderMaterial_2bx6i"] shader = ExtResource("2_mfsqn") -shader_parameter/noise_img = ExtResource("3_2bx6i") -shader_parameter/gradient_tex = SubResource("GradientTexture2D_vs6nd") -shader_parameter/speed = 1.0 -shader_parameter/smoke_color = Color(0.09638075, 0.25795576, 0.4848613, 1) +shader_parameter/resolution = Vector2(1270, 720) +shader_parameter/density = 20.0 +shader_parameter/speed_x = 0.005 +shader_parameter/speed_y = 0.0 +shader_parameter/layers = 5.0 +shader_parameter/upper_tolerance_for_star_color = 0.75 +shader_parameter/lower_tolerance_for_star_color = 0.25 +shader_parameter/green_tolerance_for_star_color = 0.1 [node name="MultiColorStarfield" type="Control"] layout_mode = 3 @@ -27,7 +34,7 @@ grow_horizontal = 2 grow_vertical = 2 [node name="ColorRect2" type="ColorRect" parent="."] -visible = false +material = SubResource("ShaderMaterial_vs6nd") layout_mode = 0 offset_right = 1270.0 offset_bottom = 720.0 @@ -40,6 +47,7 @@ offset_right = 1270.0 offset_bottom = 720.0 [node name="ColorRect" type="ColorRect" parent="."] +visible = false material = SubResource("ShaderMaterial_2bx6i") layout_mode = 0 offset_top = 2.0