Ragster
Member
I've never worked with Shaders before this, but I managed to get one working and I really doubt it's optimized. The goal of my Shader is to use three custom colors for a sprite, replacing pixels in the sprite that are 100% red, 100% green, and 100% blue. I just used a bunch of if's to do this:
It works as intended, but if anyone knows a better way of doing this, I would greatly appreciate the help.
Code:
attribute vec3 in_Position;
attribute vec4 in_Colour;
attribute vec2 in_TextureCoord;
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
void main() {
vec4 object_space_pos = vec4(in_Position.x,in_Position.y,in_Position.z,1);
gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION]*object_space_pos;
v_vColour = in_Colour;
v_vTexcoord = in_TextureCoord;
}
//######################_==_YOYO_SHADER_MARKER_==_######################@~
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform vec3 paletteColorR;
uniform vec3 paletteColorG;
uniform vec3 paletteColorB;
void main() {
vec4 pixelColor = texture2D(gm_BaseTexture,v_vTexcoord);
float red = pixelColor.r;
float green = pixelColor.g;
float blue = pixelColor.b;
if (red > 0.0 && green == 0.0 && blue == 0.0) pixelColor = vec4(paletteColorR.r*red,paletteColorR.g*red,paletteColorR.b*red,pixelColor.a);
else if (red == 0.0 && green > 0.0 && blue == 0.0) pixelColor = vec4(paletteColorG.r*green,paletteColorG.g*green,paletteColorG.b*green,pixelColor.a);
else if (red == 0.0 && green == 0.0 && blue > 0.0) pixelColor = vec4(paletteColorB.r*blue,paletteColorB.g*blue,paletteColorB.b*blue,pixelColor.a);
gl_FragColor = v_vColour*pixelColor;
}