2016-09-26 43 views
0

如何更改應用程序表面的顏色,使用紋理?遊戲製造商 - 如何使用着色器和紋理交換調色板

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

這樣我的片段着色器開始:

varying vec2 v_vTexcoord; 
varying vec4 v_vColour; 

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

感謝

回答

1

這可以在許多不同的方式加以處理,但是,一種簡單的方法是爲當前調色板中的兩種顏色和替換顏色創建制服。例如;

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) 

擴展這一你可以創建一個精靈,並可能爲制服進行採樣,無論如何希望這有助於!