Added platform shader

This commit is contained in:
2026-06-10 22:48:44 +03:00
parent fc3129cd02
commit 1cd95eea6b
8 changed files with 114 additions and 61 deletions
+2 -2
View File
@@ -28,8 +28,8 @@
script = ExtResource("2_a2sw7")
[node name="AbstractPlatform" parent="Platforms" parent_id_path=PackedInt32Array(1140671629) index="0" unique_id=1226827206 instance=ExtResource("3_pmn5w")]
position = Vector2(144, -58)
platform_width = 256
position = Vector2(182, -42)
platform_width = 104
[node name="LeverDoor" parent="Doors" parent_id_path=PackedInt32Array(1468144392) index="0" unique_id=1575363350 instance=ExtResource("2_r01v2")]
position = Vector2(416, -56)
+6 -3
View File
@@ -17,12 +17,13 @@ const MAX_WIDTH = TILE_SIZE * 32
@onready var collision: CollisionShape2D = $AnimatableBody2D/CollisionShape2D
@onready var platform_sprite: Sprite2D = $PlatformSprite
@onready var platform_rect: ColorRect = $PlatformRect
@onready var icon_sprite: AnimatedSprite2D = $IconSprite
func _ready() -> void:
platform_sprite.material.set_shader_parameter("tile_size", TILE_SIZE)
platform_rect.material.set_shader_parameter("tile_size", TILE_SIZE)
platform_rect.material.set_shader_parameter("seed", randf())
rebuild_platform()
@@ -33,7 +34,9 @@ func rebuild_platform() -> void:
collision.shape = shape
shape.size = Vector2(platform_width, TILE_SIZE)
platform_sprite.material.set_shader_parameter("platform_width", platform_width)
platform_rect.size.x = platform_width
platform_rect.position.x = -platform_width/2
platform_rect.material.set_shader_parameter("platform_width", platform_width)
func _physics_process(_delta: float) -> void:
+23 -4
View File
@@ -5,19 +5,38 @@
[ext_resource type="Shader" uid="uid://d4hm3vjx1rhsf" path="res://game/shaders/platform.gdshader" id="2_jsq8n"]
[sub_resource type="ShaderMaterial" id="ShaderMaterial_2ivtb"]
resource_local_to_scene = true
shader = ExtResource("2_jsq8n")
shader_parameter/tile_size = 16.0
shader_parameter/platform_width = 16.0
shader_parameter/tile_size = 8.0
shader_parameter/platform_width = 24.0
shader_parameter/atlas = ExtResource("2_31xwx")
shader_parameter/seed = 0.038039055
[sub_resource type="RectangleShape2D" id="RectangleShape2D_jsq8n"]
resource_local_to_scene = true
size = Vector2(16, 8)
[node name="AbstractPlatform" type="Node2D" unique_id=1226827206]
script = ExtResource("1_426vp")
[node name="PlatformSprite" type="Sprite2D" parent="." unique_id=1901062470]
[node name="PlatformRect" type="ColorRect" parent="." unique_id=646284480]
material = SubResource("ShaderMaterial_2ivtb")
texture = ExtResource("2_31xwx")
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -8.0
offset_top = -4.0
offset_right = 8.0
offset_bottom = 4.0
grow_horizontal = 2
grow_vertical = 2
pivot_offset_ratio = Vector2(0.5, 0.5)
[node name="IconSprite" type="AnimatedSprite2D" parent="." unique_id=287774650]
[node name="AnimatableBody2D" type="AnimatableBody2D" parent="." unique_id=908936954]
[node name="CollisionShape2D" type="CollisionShape2D" parent="AnimatableBody2D" unique_id=175449124]
shape = SubResource("RectangleShape2D_jsq8n")
+2 -47
View File
@@ -1,52 +1,7 @@
shader_type canvas_item;
uniform vec4 black_color : source_color = vec4(0.0, 0.0, 0.0, 1.0);
uniform vec4 white_color : source_color = vec4(1.0, 1.0, 1.0, 1.0);
uniform bool switch_colors = false;
uniform float threshold : hint_range(0.0, 1.0) = 0.5;
uniform float speed : hint_range(0.0, 10.0) = 0.5;
uniform float intensity : hint_range(0.0, 1.0) = 1.0;
uniform float scale : hint_range(0.0, 100.0) = 20.0;
vec2 random(vec2 uv){
uv = vec2( dot(uv, vec2(127.1,311.7) ),
dot(uv, vec2(269.5,183.3) ) );
return -1.0 + 2.0 * fract(sin(uv) * 43758.5453123);
}
float noise(vec2 uv) {
vec2 uv_index = floor(uv);
vec2 uv_fract = fract(uv);
vec2 blur = smoothstep(0.0, 1.0, uv_fract);
return mix( mix( dot( random(uv_index + vec2(0.0,0.0) ), uv_fract - vec2(0.0,0.0) ),
dot( random(uv_index + vec2(1.0,0.0) ), uv_fract - vec2(1.0,0.0) ), blur.x),
mix( dot( random(uv_index + vec2(0.0,1.0) ), uv_fract - vec2(0.0,1.0) ),
dot( random(uv_index + vec2(1.0,1.0) ), uv_fract - vec2(1.0,1.0) ), blur.x), blur.y) + 0.5;
}
#include "get_black_n_white.gdshaderinc"
void fragment() {
if (COLOR.a > 0.01) {
float l = dot(COLOR.rgb, vec3(0.2126, 0.7152, 0.0722));
bool is_black = l < threshold;
if (switch_colors) {
is_black = !is_black;
}
if (intensity < 1.0) {
vec2 coord = UV * scale + TIME * speed;
if (noise(coord) > intensity) {
is_black = !is_black;
}
}
if (is_black) {
COLOR = black_color;
}
else {
COLOR = white_color;
}
}
COLOR = get_black_and_white(COLOR, UV, TIME);
}
@@ -0,0 +1,47 @@
uniform vec4 black_color : source_color = vec4(0.0, 0.0, 0.0, 1.0);
uniform vec4 white_color : source_color = vec4(1.0, 1.0, 1.0, 1.0);
uniform bool switch_colors = false;
uniform float threshold : hint_range(0.0, 1.0) = 0.5;
uniform float speed : hint_range(0.0, 10.0) = 0.5;
uniform float intensity : hint_range(0.0, 1.0) = 1.0;
uniform float scale : hint_range(0.0, 100.0) = 20.0;
vec2 random(vec2 uv){
uv = vec2( dot(uv, vec2(127.1,311.7) ),
dot(uv, vec2(269.5,183.3) ) );
return -1.0 + 2.0 * fract(sin(uv) * 43758.5453123);
}
float noise(vec2 uv) {
vec2 uv_index = floor(uv);
vec2 uv_fract = fract(uv);
vec2 blur = smoothstep(0.0, 1.0, uv_fract);
return mix( mix( dot( random(uv_index + vec2(0.0,0.0) ), uv_fract - vec2(0.0,0.0) ),
dot( random(uv_index + vec2(1.0,0.0) ), uv_fract - vec2(1.0,0.0) ), blur.x),
mix( dot( random(uv_index + vec2(0.0,1.0) ), uv_fract - vec2(0.0,1.0) ),
dot( random(uv_index + vec2(1.0,1.0) ), uv_fract - vec2(1.0,1.0) ), blur.x), blur.y) + 0.5;
}
vec4 get_black_and_white(vec4 color, vec2 uv, float time) {
if (color.a < 0.01) return color;
float l = dot(color.rgb, vec3(0.2126, 0.7152, 0.0722));
bool is_black = l < threshold;
if (switch_colors) {
is_black = !is_black;
}
if (intensity < 1.0) {
vec2 coord = uv * scale + time * speed;
if (noise(coord) > intensity) {
is_black = !is_black;
}
}
if (is_black) {
return black_color;
}
return white_color;
}
@@ -0,0 +1 @@
uid://bvklme5ww0g2f
+31 -3
View File
@@ -1,10 +1,38 @@
shader_type canvas_item;
uniform float tile_size = 8.0;
uniform float platform_width = 16.0;
uniform float platform_width = 24.0;
uniform sampler2D atlas : filter_nearest;
uniform float seed : hint_range(0.0, 1.0) = 0.0;
const float left_cap_count = 2.0;
const float gap_count = 8.0;
const float right_cap_count = 2.0;
float random(float v, float s) {
return fract(sin(v * 127.1 + s * 100.0) * 43758.5453);
}
void fragment() {
vec2 platform_pixel = UV * vec2(platform_width, tile_size);
vec2 atlas_pixel = platform_pixel;
float column = floor(platform_pixel.x / tile_size);
float rand_val = random(column, seed);
float tile_offset_x = mod(platform_pixel.x, tile_size);
if (platform_pixel.x < tile_size) {
float cap_index = floor(rand_val * left_cap_count);
atlas_pixel.x = cap_index * tile_size + tile_offset_x;
} else if (platform_pixel.x < platform_width - tile_size) {
float gap_index = floor(rand_val * gap_count);
atlas_pixel.x = left_cap_count * tile_size + gap_index * tile_size + tile_offset_x;
} else {
float cap_index = floor(rand_val * right_cap_count);
atlas_pixel.x = (left_cap_count + gap_count) * tile_size + cap_index * tile_size + tile_offset_x;
}
vec2 atlas_size = vec2(textureSize(atlas, 0));
vec2 atlas_uv = atlas_pixel / atlas_size;
COLOR = texture(atlas, atlas_uv);
}
Binary file not shown.