Advertisement
nezvers

SpritePalettizer Defold shader example

May 27th, 2020
1,814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. varying mediump vec2 var_texcoord0;                 //Original texture coordinates of current pixel
  2.  
  3. uniform mediump sampler2D texture_sampler;          //I'm assuming this is objects original texture
  4. uniform mediump sampler2D palette;                  //Texture of color palettes (palette colors goes in rows)
  5. uniform mediump float palette_count = 1.0;          //Tells the shader how many palettes you have
  6. uniform mediump float palette_index = 0.0;          //Telss the shader which palette to choose
  7.  
  8. void main()
  9. {
  10.     float mediump  increment = 1.0/palette_count;                        //Value for getting palette index
  11.     float mediump  y = increment * palette_index + increment * 0.5;      // + safety measure for floating point imprecision
  12.     vec4 mediump color = texture2D(texture_sampler, var_texcoord0.xy);  //Original grayscale color used as collumn index
  13.     vec4 mediump new_collor = texture2D(palette, vec2(color.r, y));     //get color from palette texture
  14.     float mediump a = step(0.00392, color.a);                           //check if transparent color is less than 1/255 for backgrounds
  15.     new_color.a *= a;                                           //if BG is transparent, then alpha is multiplied by 0
  16.     gl_FragColor = new_color;                                   //Set output color
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement