游戏制造商 - 如何使用着色器和纹理交换调色板
问题描述:
如何更改应用程序表面的颜色,使用纹理?游戏制造商 - 如何使用着色器和纹理交换调色板
https://en.wikipedia.org/wiki/List_of_8-bit_computer_hardware_palettes
https://upload.wikimedia.org/wikipedia/commons/c/c5/CGA_palette_color_test_chart.png
这样我的片段着色器开始:
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
void main()
{
gl_FragColor = v_vColour * texture2D(gm_BaseTexture, v_vTexcoord);
}
感谢
答
这可以在许多不同的方式加以处理,但是,一种简单的方法是为当前调色板中的两种颜色和替换颜色创建制服。例如;
uniform vec3 rep1;
uniform vec3 rep2;
uniform vec3 rep3;
uniform vec3 new1;
uniform vec3 new2;
uniform vec3 new3;
然后你可以检查采样的像素是否是你的颜色之一来替换并传递新的颜色,如果是这样的话;
//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
);
然后在你的房间生成的代码(或其他初始化代码)可以设置着色器,并确定要替换的调色板和如何用它代替,例如:
// -- 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)
扩展这一你可以创建一个精灵,并可能为制服进行采样,无论如何希望这有助于!