Last active
May 21, 2025 13:29
-
-
Save partybusiness/bc01758622a75e7bc506f4a2e6ba78fd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shader_type spatial; | |
render_mode unshaded; | |
// example shader that uses the light pattern to flicker brightness on and off | |
// other possible ways to use this is give it render_mode additive have lights_on and lights_off versions of a texture and mix between them | |
uniform sampler2D light_texture:source_color; | |
uniform float fps:hint_range(1.0, 60.0, 1.0) = 10.0; | |
// temporal light pattern | |
uniform sampler2D light_pattern:repeat_enable, filter_nearest, source_color; | |
void fragment() { | |
ivec2 sample_pos = ivec2(int(floor(TIME * fps)) % textureSize(light_pattern, 0).x, 0); | |
float light_strength = texelFetch(light_pattern, sample_pos, 0).r; | |
ALBEDO = texture(light_texture, UV).rgb * light_strength; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@tool | |
extends PortableCompressedTexture2D | |
class_name LightPattern | |
# This is based off the classic light patterns from Quake | |
# I copied strings from here: | |
# https://github.com/id-Software/Quake/blob/bf4ac424ce754894ac8f1dae6a3981954bc9852d/qw-qc/world.qc#L335 | |
enum Patterns { | |
FREEFORM = -1, | |
NORMAL = 0, | |
FLICKER_1, | |
SLOW_STRONG_PULSE, | |
CANDLE_1, | |
FAST_STROBE, | |
GENTLE_PULSE_1, | |
FLICKER_2, | |
CANDLE_2, | |
CANDLE_3, | |
SLOW_STROBE, | |
FLUORESCENT_FLICKER, | |
SLOW_PULSE, | |
} | |
# used to keep the preset dropdown re-activating back to freeform because we set a | |
var setting_from_preset:bool = false | |
@export var preset_pattern:Patterns = Patterns.FREEFORM: | |
get: | |
return preset_pattern | |
set(new_value): | |
setting_from_preset = true | |
preset_pattern = new_value | |
match preset_pattern: | |
Patterns.NORMAL: | |
pattern_string = "m" | |
Patterns.FLICKER_1: | |
pattern_string = "mmnmmommommnonmmonqnmmo" | |
Patterns.SLOW_STRONG_PULSE: | |
pattern_string = "abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba" | |
Patterns.CANDLE_1: | |
pattern_string = "mmmmmaaaaammmmmaaaaaabcdefgabcdefg" | |
Patterns.FAST_STROBE: | |
pattern_string = "mamamamamama" | |
Patterns.GENTLE_PULSE_1: | |
pattern_string = "jklmnopqrstuvwxyzyxwvutsrqponmlkj" | |
Patterns.FLICKER_2: | |
pattern_string = "nmonqnmomnmomomno" | |
Patterns.CANDLE_2: | |
pattern_string = "mmmaaaabcdefgmmmmaaaammmaamm" | |
Patterns.CANDLE_3: | |
pattern_string = "mmmaaammmaaammmabcdefaaaammmmabcdefmmmaaaa" | |
Patterns.SLOW_STROBE: | |
pattern_string = "aaaaaaaazzzzzzzz" | |
Patterns.FLUORESCENT_FLICKER: | |
pattern_string = "mmamammmmammamamaaamammma" | |
Patterns.SLOW_PULSE: | |
pattern_string = "abcdefghijklmnopqrrqponmlkjihgfedcba" | |
setting_from_preset = false | |
@export var pattern_string:String = "m": | |
get: | |
return pattern_string | |
set(p_value): | |
if p_value.length() == 0: | |
return # don't allow empty string | |
p_value = p_value.to_lower() # force to lowercase | |
# TODO do we need to limit the characters? | |
pattern_string = p_value | |
if Engine.is_editor_hint(): | |
if !setting_from_preset: | |
preset_pattern = Patterns.FREEFORM | |
request_update() | |
var image:Image | |
func request_update() -> void: | |
if !Engine.is_editor_hint(): | |
return # don't update during final build | |
image = Image.create(pattern_string.length(), 1, true, Image.FORMAT_R8) | |
var buffer := pattern_string.to_ascii_buffer() | |
for i in buffer.size(): | |
var buff = buffer[i] | |
var val:float = (buff - 97) / 13.0 # 97 is ascii a, 13 is half-way to 26 letters | |
var colour:Color = Color(val, val, val) | |
image.set_pixel(i, 0, colour) | |
#fill_image() | |
#image.generate_mipmaps() | |
create_from_image(image, PortableCompressedTexture2D.COMPRESSION_MODE_LOSSLESS) | |
emit_changed() | |
func _init(): | |
request_update() | |
#// 0 normal | |
#lightstyle(0, "m"); | |
#// 1 FLICKER (first variety) | |
#lightstyle(1, "mmnmmommommnonmmonqnmmo"); | |
#// 2 SLOW STRONG PULSE | |
#lightstyle(2, "abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba"); | |
#// 3 CANDLE (first variety) | |
#lightstyle(3, "mmmmmaaaaammmmmaaaaaabcdefgabcdefg"); | |
#// 4 FAST STROBE | |
#lightstyle(4, "mamamamamama"); | |
#// 5 GENTLE PULSE 1 | |
#lightstyle(5,"jklmnopqrstuvwxyzyxwvutsrqponmlkj"); | |
#// 6 FLICKER (second variety) | |
#lightstyle(6, "nmonqnmomnmomomno"); | |
#// 7 CANDLE (second variety) | |
#lightstyle(7, "mmmaaaabcdefgmmmmaaaammmaamm"); | |
#// 8 CANDLE (third variety) | |
#lightstyle(8, "mmmaaammmaaammmabcdefaaaammmmabcdefmmmaaaa"); | |
#// 9 SLOW STROBE (fourth variety) | |
#lightstyle(9, "aaaaaaaazzzzzzzz"); | |
#// 10 FLUORESCENT FLICKER | |
#lightstyle(10, "mmamammmmammamamaaamammma"); | |
#// 11 SLOW PULSE NOT FADE TO BLACK | |
#lightstyle(11, "abcdefghijklmnopqrrqponmlkjihgfedcba"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment