nezvers

SpritePalettizer GameMaker

May 28th, 2020
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Shader port by @XorDev.
  3.    
  4.     This is a simple palette swap shader.
  5.     Each pixel is mapped to the palette based off it's red channel (or gray) value.
  6.     The palette textures are organized so that you can have up to 255 colors per palette.
  7.     Each row represents one palette and there are no implicit limits to the number of palettes.
  8.    
  9.     Quick overview of the uniforms:
  10.     "palette" is the palette texture that colors get swapped to.
  11.     "palette_uvs" should be set to the top-left coordinates of the palette texture (on a texture page)
  12.     and the difference to the bottom-right corner (width and height).
  13.     "palette_count" is the number of palettes you have on the palette texture (number of rows).
  14.     "palette_index" is the current palette to use (ranging from 0 to the palette_count - 1).
  15. */
  16.  
  17. varying vec4 v_vColour;
  18. varying vec2 v_vTexcoord;
  19.  
  20. uniform sampler2D palette;      //Use palettes in column with colors in rows
  21. uniform vec4 palette_uvs;       //Palette texture coordinate offset and scale
  22. uniform float palette_count;    //Tells the shader how many palettes you have
  23. uniform float palette_index;    //Tells the shader which palette to choose
  24.  
  25. void main()
  26. {
  27.     float increment = 1.0/palette_count;                            //Value for getting palette index
  28.     float y = increment * palette_index + increment * 0.5;          // + safety measure for floating point imprecision
  29.     vec4 color = texture2D(gm_BaseTexture, v_vTexcoord);            //Original grayscale color used as column index
  30.     vec2 coord = vec2(color.r, y) * palette_uvs.zw + palette_uvs.xy;//Get the palette texture coordinates
  31.     vec4 new_color = texture2D(palette, coord);                     //Get color from palette texture
  32.    
  33.     gl_FragColor = new_color*color.a;                               //Set new color from palette with original alpha
  34. }
Add Comment
Please, Sign In to add comment