Advertisement
nezvers

SpritePalettizer Cockos2D shader example

May 26th, 2020
1,967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Fragment shader
  2. #ifdef GL_ES
  3. precision mediump float;
  4. #endif
  5.  
  6. varying vec4 v_fragmentColor;
  7. varying vec2 v_texCoord;
  8. uniform sampler2D u_texture;                //I'm assuming this is objects original texture
  9.  
  10. uniform sampler2D palette;                  //Use palletes in collum with colors in rows
  11. uniform float palette_count = 1.0;          //Tells the shader how many palettes you have
  12. uniform float palette_index = 0.0;          //Telss the shader which palette to choose
  13.  
  14. void main()
  15. {
  16.     float increment = 1.0/palette_count;                    //Value for getting palette index
  17.     float y = increment * palette_index + increment * 0.5;  // + safety measure for floating point imprecision
  18.     vec4 color = texture2D(u_texture, v_texCoord.xy);       //Original graysscale color used as collumn index
  19.     vec4 new_collor = texture(palette, vec2(color.r, y));   //get color from palette texture
  20.     float a = step(0.00392, color.a);                       //check if transparent color is less than 1/255 for backgrounds
  21.     new_color.a *= a;                                       //if BG is transparent, then alpha is multiplied by 0
  22.    
  23.     gl_FragColor = new_color;                               //set new color from palette
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement