2016-08-11 36 views
1

如何編寫CGA着色器,將調色板限制爲4種顏色,並將原始顏色與那些顏色(青色,品紅色,黑​​色,白色)相匹配?如何在glsl中編寫cga着色器?

我工作的遊戲製作工作室專業,實際上允許使用着色器編寫頂點和片段代碼的

我已經問過這個問題也是在yoyogames社區和OpenGL的論壇

人回答我這個:

varying vec2 v_vTexcoord; 
varying vec4 v_vColour; 

const mat3 rgb_to_wcm = mat3(1,-1, 0, 1, 0,-1, -1, 1, 1); 

void main() 
{ 
    vec4 rgba = v_vColour * texture2D(gm_BaseTexture, v_vTexcoord); 
    vec3 wcm = rgb_to_wcm * rgba.rgb; 
    vec3 rgb = dot(wcm,vec3(1,1,1)) < 0.5 
     ? vec3(0,0,0) 
     : wcm.x > wcm.y 
      ? (wcm.x > wcm.z ? vec3(1,1,1) : vec3(1,0,1)) 
      : (wcm.y > wcm.z ? vec3(0,1,1) : vec3(1,0,1)); 
    gl_FragColor = vec4(rgb, rgba.a); 
} 

它的工作原理,但不會返回CGA調色板,它返回一個黑白(未灰度)

我該怎麼辦?

回答

1

非常感謝 我以不同的方式,也許更簡單的做

https://www.shadertoy.com/view/MdlyDH

// ▄████████ ▄██████▄  ▄████████ 
// ███ ███ ███ ███ ███ ███ 
// ███ █▀ ███ █▀ ███ ███ 
// ███   ▄███   ███ ███ 
// ███  ▀▀███ ████▄ ▀███████████ 
// ███ █▄ ███ ███ ███ ███ 
// ███ ███ ███ ███ ███ ███ 
// ████████▀ ████████▀ ███ █▀ 
// 

/* 

////only for game maker studio//// 

varying vec2 v_vTexcoord; 
varying vec4 v_vColour; 

varying vec2 fragCoord; 
//you have to put varying vec2 fragCoord also in the vertex shader 
//after write in the last row of the local scope of the vertex shader: fragCoord = in_Position.xy 

uniform vec2 iResolution; 
uniform float iGlobalTime; 

//palette 0 is not cga but gameboy, I put it as bonus 
uniform int palette; 
uniform float gamma; 

*/ 

// rgb to float 
// rgb to float = rgb/255 

// 0 = 0.0 
// 85 = 0.333 
// 170 = 0.666 
// 255 = 1.0 

void mainImage(out vec4 fragColor, in vec2 fragCoord) 
{ 
    vec2 uv = fragCoord.xy/iResolution.xy; 

    vec3 c = vec3(0.0); 
    float alpha = 1.0; 

    //only for shadertoy 
    int palette = 2; //the number between 0 and 6 change palette 
    float gamma = 1.5; //the gamma change the threshold of the palette swapper 

    //c = texture2D(gm_BaseTexture,uv).rgb; 
    c = texture(iChannel0,uv).rgb; 

    c.r = pow(abs(c.r),gamma); 
    c.g = pow(abs(c.g),gamma); 
    c.b = pow(abs(c.b),gamma); 

    vec3 col1 = vec3(0.0); 
    vec3 col2 = vec3(0.0); 
    vec3 col3 = vec3(0.0); 
    vec3 col4 = vec3(0.0); 

    if(palette == 0) { 
     col1 = vec3(0.612,0.725,0.086); 
     col2 = vec3(0.549,0.667,0.078); 
     col3 = vec3(0.188,0.392,0.188); 
     col4 = vec3(0.063,0.247,0.063); 
    } 
    if(palette == 1) { 
     col1 = vec3(0.0); 
     col2 = vec3(0.0,0.666,0.666); 
     col3 = vec3(0.666,0.0,0.666); 
     col4 = vec3(0.666,0.666,0.666); 
    } 
    if(palette == 2) { 
     col1 = vec3(0.0); 
     col2 = vec3(0.333,1.0,1.0); 
     col3 = vec3(1.0,0.333,1.0); 
     col4 = vec3(1.0); 
    } 
    if(palette == 3) { 
     col1 = vec3(0.0); 
     col2 = vec3(0.0,0.666,0.0); 
     col3 = vec3(0.666,0.0,0.0); 
     col4 = vec3(0.666,0.333,0.0); 
    } 
    if(palette == 4) { 
     col1 = vec3(0.0); 
     col2 = vec3(0.333,1.0,0.333); 
     col3 = vec3(1.0,0.333,0.333); 
     col4 = vec3(1.0,1.0,0.333); 
    } 
    if(palette == 5) { 
     col1 = vec3(0.0); 
     col2 = vec3(0.0,0.666,0.666); 
     col3 = vec3(0.666,0.0,0.0); 
     col4 = vec3(0.666,0.666,0.666); 
    } 
    if(palette == 6) { 
     col1 = vec3(0.0); 
     col2 = vec3(0.333,0.666,0.666); 
     col3 = vec3(1.0,0.333,0.333); 
     col4 = vec3(1.0); 
    } 

    float dist1 = length(c - col1); 
    float dist2 = length(c - col2); 
    float dist3 = length(c - col3); 
    float dist4 = length(c - col4); 

    float d = min(dist1,dist2); 
    d = min(d,dist3); 
    d = min(d,dist4); 

    if(d == dist1) { 
     c = col1; 
    } 
    else if(d == dist2) { 
     c = col2; 
    } 
    else if(d == dist3) { 
     c = col3; 
    } 
    else { 
     c = col4; 
    } 

    //gl_FragColor = vec4(c,alpha).rgba; 
    fragColor = vec4(c,alpha).rgba; 
} 

我在這裏也包括這個shadertoy着色器轉換爲遊戲廠商工作室着色器,因爲在路上游戲製作工作室有沒有統一的時間和分辨率來進行時間編輯和vec2我還包括所有cga調色板的註釋右調色板和獎金調色板0這是一個GAMEBOY調色板

我還包括從RGB值爲255至浮子誰沒有可能能夠轉換

這裏唯一的問題是,它不能夠反轉顏色或改變位置的轉換在調色板中的顏色,它需要原始的來源和輸出4種顏色,這是唯一的差距。