1

how to change the color of the application surface, using the texture??

https://en.wikipedia.org/wiki/List_of_8-bit_computer_hardware_palettes

https://en.wikipedia.org/wiki/List_of_8-bit_computer_hardware_palettes#/media/File:CGA_palette_color_test_chart.png

https://upload.wikimedia.org/wikipedia/commons/c/c5/CGA_palette_color_test_chart.png

my fragment shader start in this way:

varying vec2 v_vTexcoord;
varying vec4 v_vColour;

void main()
{
    gl_FragColor = v_vColour * texture2D( gm_BaseTexture, v_vTexcoord );
}

thanks

Heavybrush
  • 79
  • 1
  • 10

1 Answers1

2

this can be approached in many different ways, however, a simple method is to create uniforms for both colors in your palette currently and replacement colors. For example;

uniform vec3 rep1;
uniform vec3 rep2;
uniform vec3 rep3;

uniform vec3 new1;
uniform vec3 new2;
uniform vec3 new3;

and then you can check if the sampled pixel is one of your colours to replace and pass the new colour instead if so;

//Sample the original pixel
gl_FragColor = v_vColour * texture2D( gm_BaseTexture, v_vTexcoord );

//Make it easier to compare (out of 255 instead of 1)
vec3 test = vec3(
    gl_FragColor.r * 255.0,
    gl_FragColor.g * 255.0, 
    gl_FragColor.b * 255.0
);

//Check if it needs to be replaced
if (test == rep1) {test = new1;}
if (test == rep2) {test = new2;}
if (test == rep3) {test = new3;}

//return the result in the original format
gl_FragColor = vec4(
    test.r / 255.0,
    test.g / 255.0,
    test.b / 255.0,
    gl_FragColor.a
);

Then in your room creation code (or other initialization code) you can set the shader and define the pallete to be replaced and what to replace with it, for example:

// -- Set Pallete --
shader_set(sPalleter)

//Red
shader_set_uniform_f(shader_get_uniform(sPalleter, "rep1"), 255, 0, 0)
shader_set_uniform_f(shader_get_uniform(sPalleter, "new1"), 155, 188, 15)

//White
shader_set_uniform_f(shader_get_uniform(sPalleter, "rep2"), 255, 255, 255)
shader_set_uniform_f(shader_get_uniform(sPalleter, "new2"), 48, 98, 48)

//Black
shader_set_uniform_f(shader_get_uniform(sPalleter, "rep3"), 0, 0, 0)
shader_set_uniform_f(shader_get_uniform(sPalleter, "new3"), 15, 56, 15)

Extending this you could create a sprite and potentially sample it for the uniforms, anyway hope this helped!

Giraugh
  • 116
  • 6