Advertisement
KarnakGames

Godot Shader: Replace Sprite Palette With Another Palette

Mar 25th, 2019
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. shader_type canvas_item;
  2. render_mode blend_mix;
  3.  
  4. uniform sampler2D base_palette;
  5. uniform sampler2D new_palette;
  6.  
  7. float find_color_in_base_palette(in vec4 color, float pal_size, float pixel_size) {
  8.     for(float x = 0.0; x <= pal_size; x += pixel_size)   {      
  9.         vec4 pal_col = texture(base_palette, vec2(x, 0.0));
  10.  
  11.         if(pal_col.rgba == color.rgba) {
  12.             return x;
  13.         }
  14.     }
  15.  
  16.     return -1.0;
  17. }
  18.  
  19. void fragment() {
  20.     vec4 color = texture(TEXTURE, UV);
  21.     ivec2 size = textureSize(base_palette, 0);
  22.     float pos = find_color_in_base_palette(color, float(size.x), TEXTURE_PIXEL_SIZE.x);
  23.  
  24.     // We found the position of the color in the base palette, so fetch a new color from the new palette
  25.     if(pos != -1.0) {
  26.         COLOR = texture(new_palette, vec2(pos, 0.0));
  27.     }
  28.     // The color is not in the base palette, so we don't know its position. Keep the base color.
  29.     else {
  30.         COLOR = color;
  31.     }  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement