Skip to content

Instantly share code, notes, and snippets.

@JustusPan
Forked from harrisyu/Uni2D.shader
Created August 15, 2020 07:04
Show Gist options
  • Save JustusPan/325c7cc2b91e09dab1f470b3c4fb1eb2 to your computer and use it in GitHub Desktop.
Save JustusPan/325c7cc2b91e09dab1f470b3c4fb1eb2 to your computer and use it in GitHub Desktop.
shader_type canvas_item;
uniform bool use_pallete;
uniform sampler2D pallete_texture;
uniform vec4 tint_color : hint_color = vec4(1.0);
uniform float tint_factor : hint_range(0, 1);
const float from_u = 0.25;
const float to_u = 0.75;
const float pal_size = 16f;
const float threshold = 0.001;
void fragment() {
vec4 source_color = texture(TEXTURE , UV.xy);
vec3 final_color = source_color.rgb;
if (use_pallete){
for (float i = 0f; i < pal_size; i++) {
vec4 from_color = texture(pallete_texture , vec2(from_u, i/pal_size));
vec4 to_color = texture(pallete_texture , vec2(to_u, i/pal_size));
if(abs(length(source_color.rgb - from_color.rgb)) < threshold) final_color = to_color.rgb;
}
}
COLOR.rgb = mix(final_color, tint_color.rgb, tint_factor);
COLOR.a = source_color.a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment