如何在glsl中编写CGA着色器? [英] how to write a cga shader in glsl?

查看:113
本文介绍了如何在glsl中编写CGA着色器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写CGA着色器,以将调色板限制为4种颜色并使原始颜色与这些颜色(青色,品红色,黑​​色,白色)匹配?

How to write a CGA shader that limit the palette to 4 colors and match the original colors with those (cyan, magenta, black, white)??

我正在使用Game Maker Studio Professional,它实际上允许使用着色器来编写顶点和片段代码

I'm working on Game Maker Studio Professional, that actually allows the using of shaders writing vertex and fragment code

我已经在yoyogames社区和openGl论坛中问过这个问题

I already asked this question also in the yoyogames community and in the openGl forum

有人用这个回答了我

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调色板,它返回黑白(不是灰度)

it works, but not return a cga palette, it return a black and white (not grayscale)

我该怎么办?

推荐答案

非常感谢 我以不同的方式做过,也许更简单

Thanks a lot I did in a different way, maybe more simple

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着色器转换为游戏制造商工作室着色器的方法,因为在Game Maker Studio中,没有统一的时间和分辨率来进行时间编辑和vec2 uv

here I also included the way to convert this shadertoy shader to a game maker studio shader, because in game maker studio there isn't the uniform time and resolution to make a time editing and the vec2 uv

我还为所有cga调色板添加了评论正确的调色板,并指出了这是一个Gameboy调色板的奖励调色板0

I also included the commented right palettes for all the cga color palettes and a bonus palette 0 that it is a gameboy palette

我还包括了从rgb 255值到无法转换的浮点数的转换

I also included the conversion from rgb 255 value to the float for who could not be able to convert

这里唯一的问题是它不能反转颜色或更改调色板中颜色的位置,它取原始来源并输出4种颜色,这是唯一的差距.

the only problem here is that it is not able to invert colors or change position of the colors in the palette, it takes the original source and output the 4 colors, this is the only gap.

这篇关于如何在glsl中编写CGA着色器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆