Skip to content

Commit 15ec83b

Browse files
Wave Bar Shader (#204)
* Add new wave shader It doesnt work yet * Added Correct Gradient resource to fill --------- Co-authored-by: Thomas Wessel <turpleturtle12@gmail.com>
1 parent 34c6d88 commit 15ec83b

4 files changed

Lines changed: 91 additions & 1 deletion

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
shader_type canvas_item;
2+
3+
uniform float fillLevel : hint_range(0.0, 1.0) = 0.5;
4+
uniform float waveIntensity : hint_range(0.0, 3.0) = 1.0;
5+
uniform sampler2D waveGradient;
6+
uniform vec4 backWaveTint : source_color = vec4(0.7, 0.7, 0.8, 1.0);
7+
8+
void fragment() {
9+
vec2 centeredUV = UV * 2.0 - 1.0;
10+
11+
vec4 outputColor = vec4(0.0, 0.0, 0.0, 1.0);
12+
13+
//Only do stuff if the bar is filled
14+
if (fillLevel > 0.0) {
15+
16+
////**** Gradient Texture Sampling ****////
17+
18+
//Gets the centered pos of the fillLevel
19+
float centeredFillLevel = mix(1.0, -1.0, fillLevel);
20+
21+
//Calculate where we should sample the gradient for this section of the water
22+
float gradientPosition = fillLevel - (centeredUV.y - centeredFillLevel) / 2.0;
23+
24+
//Finally get the color from the gradient
25+
vec4 sampledColorFromGradient = texture(waveGradient, vec2(gradientPosition, 0.5));
26+
27+
////**** Wave Sim ****////
28+
29+
float waveBase = smoothstep(0.1, 0.9, sin(centeredUV.x * 2.0 + PI * 0.5) - 0.3);
30+
31+
float centerWaveAmp = 0.05 * waveIntensity; //How much larger the center wave should be
32+
float mainWaveAmp = 0.04 * waveIntensity; //Overall sin amplitude
33+
float surfaceRippleAmp = 0.03 * waveIntensity; //How much the two waves should vary
34+
float verticalMovement = 0.03 * waveIntensity; //How much it moves vertically
35+
36+
//Flatten stuff out if the bar is full
37+
if (fillLevel > 0.9999) {
38+
centerWaveAmp = 0.0;
39+
mainWaveAmp = 0.0;
40+
surfaceRippleAmp = 0.0;
41+
verticalMovement = 0.0;
42+
}
43+
44+
//How far from the the bottom the waves should appear
45+
float waveHeight = mix(1.0, -1.0, fillLevel) + sin(TIME * PI * 0.5) * verticalMovement;
46+
47+
//Offset wave position and change direction
48+
float frontWaveYOffset = sin((TIME * 2.0 + centeredUV.x * 3.0) * 2.0) * mainWaveAmp;
49+
float backWaveYOffset = sin((TIME * -2.0 + centeredUV.x * 3.0 + PI) * 2.0) * mainWaveAmp;
50+
51+
//Calc surface ripples
52+
float surfaceAmplitudes = sin(TIME * 3.0) * surfaceRippleAmp * waveBase;
53+
54+
//Final Y positions of the waves
55+
float frontWaveY = waveHeight - (surfaceAmplitudes + frontWaveYOffset);
56+
float backWaveY = waveHeight - (-surfaceAmplitudes + backWaveYOffset);
57+
58+
//Determine which wave is in front for this fragment
59+
float isBelowFrontWave = step(frontWaveY, centeredUV.y);
60+
float isBelowBackWave = step(backWaveY, centeredUV.y);
61+
62+
float backOnlyVisibility = clamp(isBelowBackWave - isBelowFrontWave, 0.0, 1.0);
63+
64+
if (isBelowFrontWave > 0.5) {
65+
outputColor = sampledColorFromGradient; //Just assign the color
66+
} else if (backOnlyVisibility > 0.5) {
67+
outputColor = vec4(sampledColorFromGradient.rgb * backWaveTint.rgb,sampledColorFromGradient.a * backWaveTint.a);
68+
}
69+
}
70+
71+
COLOR = outputColor;
72+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://difiiwiddwutp

Scenes/BattleDirector/NotePlacementBar.tscn

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
[gd_scene load_steps=12 format=3 uid="uid://duhiilcv4tat3"]
1+
[gd_scene load_steps=15 format=3 uid="uid://duhiilcv4tat3"]
22

33
[ext_resource type="Script" uid="uid://gj666xe815py" path="res://Scenes/BattleDirector/Scripts/NotePlacementBar.cs" id="1_456es"]
44
[ext_resource type="Texture2D" uid="uid://cnyr5usjdv0ni" path="res://Scenes/BattleDirector/Assets/NoteQueue_Frame.png" id="2_3tw16"]
55
[ext_resource type="Texture2D" uid="uid://gcst7q2acsqm" path="res://Scenes/BattleDirector/Assets/PlacementBar_Under.png" id="2_5a8x5"]
6+
[ext_resource type="Shader" uid="uid://difiiwiddwutp" path="res://Scenes/BattleDirector/Assets/wave.gdshader" id="2_kb2co"]
67
[ext_resource type="Texture2D" uid="uid://c3chrsxrulapd" path="res://Classes/Notes/Assets/Note_PlayerBasic.png" id="3_6ylx6"]
78
[ext_resource type="Texture2D" uid="uid://bi4tbiovlm2g1" path="res://Scenes/BattleDirector/Assets/PlacementBar_Over.png" id="3_kb2co"]
89
[ext_resource type="Texture2D" uid="uid://caw70lr5e1yiq" path="res://Classes/Notes/Assets/Note_PlayerDouble.png" id="4_6w8ha"]
@@ -11,6 +12,16 @@
1112
offsets = PackedFloat32Array(0)
1213
colors = PackedColorArray(0, 0, 0, 1)
1314

15+
[sub_resource type="GradientTexture2D" id="GradientTexture2D_kb2co"]
16+
gradient = SubResource("Gradient_xvck1")
17+
18+
[sub_resource type="ShaderMaterial" id="ShaderMaterial_e2fap"]
19+
shader = ExtResource("2_kb2co")
20+
shader_parameter/fillLevel = 0.0
21+
shader_parameter/waveIntensity = 1.0
22+
shader_parameter/waveGradient = SubResource("GradientTexture2D_kb2co")
23+
shader_parameter/backWaveTint = Color(0.7, 0.7, 0.8, 1)
24+
1425
[sub_resource type="GradientTexture2D" id="GradientTexture2D_0bqho"]
1526
gradient = SubResource("Gradient_xvck1")
1627
width = 32
@@ -52,6 +63,7 @@ anchor_bottom = 1.0
5263
grow_horizontal = 2
5364
grow_vertical = 2
5465
script = ExtResource("1_456es")
66+
_waveMaterial = SubResource("ShaderMaterial_e2fap")
5567
_notePlacementBar = NodePath("PlacementBar")
5668
_particles = NodePath("PlacementBar/Rock")
5769
_fullBarParticles = NodePath("PlacementBar/FullBarParticles")
@@ -77,6 +89,7 @@ virtual_keyboard_enabled = false
7789
middle_mouse_paste_enabled = false
7890

7991
[node name="PlacementBar" type="TextureProgressBar" parent="."]
92+
material = SubResource("ShaderMaterial_e2fap")
8093
layout_mode = 0
8194
offset_left = 41.0
8295
offset_top = 33.0

Scenes/BattleDirector/Scripts/NotePlacementBar.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ private double CurrentBarValue
2323
{
2424
_notePlacementBar.Value = value;
2525
_particles.Emitting = CurrentBarValue >= MaxValue; //This is so goated
26+
_waveMaterial.SetShaderParameter("fillLevel", _notePlacementBar.Value / MaxValue);
2627
}
2728
}
2829

30+
[Export]
31+
private ShaderMaterial _waveMaterial; //Sort of breaks the pixel art style, but its cool
32+
2933
[Export]
3034
private TextureProgressBar _notePlacementBar;
3135
private Gradient _gradTex;

0 commit comments

Comments
 (0)