Last active
July 7, 2025 13:52
-
-
Save partybusiness/62b245d033e789541107678b2ecc5984 to your computer and use it in GitHub Desktop.
Godot shader that will let you replace colours (usually full-saturation RGB, Yellow, Cyan, Magenta) with new colours based on a texture.
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 canvas_item; | |
// expects this as 4x2 | |
// black, red, blue, magenta | |
// green, yellow, cyan, white | |
// set alpha to 0 for sections you don't want replaced | |
// usually black pixel is alpha 0 because most colours are below threshold | |
uniform sampler2D colours:source_color; | |
// threshold for what counts as full saturation | |
// usually keep it pretty high so most colours can be treated as black | |
uniform float threshold = 0.99; | |
void fragment() { | |
vec3 o = COLOR.rgb; | |
ivec2 coord = ivec2(vec2(step(threshold, o.r) + step(threshold, o.b) * 2.0, step(threshold, o.g))); | |
vec4 swap = texelFetch(colours, coord, 0); | |
COLOR.rgb = mix(COLOR.rgb, swap.rgb, swap.a); | |
} | |
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; | |
uniform sampler2D main_texture:source_color, filter_nearest; | |
// expects this as 4x2 | |
// black, red, blue, magenta | |
// green, yellow, cyan, white | |
// set alpha to 0 for sections you don't want replaced | |
// usually black pixel is alpha 0 because most colours are below threshold | |
uniform sampler2D colours:source_color; | |
// threshold for what counts as full saturation | |
// usually keep it pretty high so most colours can be treated as black | |
uniform float threshold = 0.99; | |
void fragment() { | |
vec3 o = texture(main_texture, UV).rgb; | |
ivec2 coord = ivec2(vec2(step(threshold, o.r) + step(threshold, o.b) * 2.0, step(threshold, o.g))); | |
vec4 swap = texelFetch(colours, coord, 0); | |
ALBEDO.rgb = mix(o.rgb, swap.rgb, swap.a); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment