Hello, I’m sorry to reopen the thread but I need to change this shader
maybe you can help me
I have to change it to swap the palette also if the application surface is not close to it
secondary, I have to render it dynamic (maybe using an uniform variable) because it have to change the palette
and if it possible, can you better comment this code, to let me understand better every step and give an help to this question?
please help me
thank you so much
here the code:
///CGA Shader
varying vec2 v_vTexcoord; //xy
varying vec4 v_vColour; //rgba
#define _ 0.0
#define o (1./3.)
#define b (2./3.)
#define B 1.0
#define check(r,g,b) color=vec4(r,g,b,0.); dist = distance(sample,color); if (dist < bestDistance) {bestDistance = dist; bestColor = color;}
void main(out vec4 v_vColour, in vec2 v_vTexcoord)
{
float dist;
float bestDistance = 1000.;
vec4 color;
vec4 bestColor;
vec2 pixel = vec2(320,200)-floor( v_vTexcoord.xy / iResolution.xy * vec2(320,200));
vec4 sample = texture2D(iChannel0, pixel / vec2(320,200));
sample += (texture2D(iChannel1, pixel / iChannelResolution[1].xy)-0.5)*0.5;
// pallete 0
check(_,_,_);
check(o,B,o);
check(B,o,o);
check(B,B,o);
vec4 color1 = bestColor;
bestDistance = 1000.;
// pallete 1
check(_,_,_);
check(o,B,B);
check(B,o,B);
check(B,B,B);
vec4 color2 = bestColor;
float t = (clamp(sin(iGlobalTime)*4.,-1.,1.)+1.)/2.;
v_vColour = mix(color2, color1, t);
gl_FragColor = v_vColour * texture2D( gm_BaseTexture, v_vTexcoord );
}
an exemple to how to render dynamic a shader in game maker is this:
here is the shader and how to manipulate it
the shader is a pixel shader and can be manipulated to change the size of the pixels
I have to change the cga colors of the palette
//-----------------------------------------------------------------
//------- PIXELATED SHADER V2--------------------------------------
//-----------------------------------------------------------------
//fragment
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform vec2 size; //the actual size in pixels
uniform float pixelSize;
void main()
{
vec2 realTexPos = v_vTexcoord * size;
vec2 floorTexPos = floor(realTexPos / pixelSize) * pixelSize;
vec2 texPos = floorTexPos / size; //la nuova posizione dei pixel
gl_FragColor = v_vColour * texture2D( gm_BaseTexture, texPos );
}
this is the code of the object to disable the surface and set the shader:
//object
//create event
///disable application surface
application_surface_draw_enable(false);
pixelSize = 10; //set pixelSize to 10
//draw gui end event
///draw application surface
if(shader_is_compiled(shader)) {
shader_set(shader);
var s = shader_get_uniform(shader,"size");
shader_set_uniform_f(s,surface_get_width(application_surface),surface_get_height(application_surface));
var ps = shader_get_uniform(shader,"pixelSize");
shader_set_uniform_f(ps,pixelSize);
draw_surface(application_surface,0,0);
shader_reset();
}
else {
draw_surface(application_surface,0,0);
}
this is the code to manipulate it:
//step event
///position in base of the mouse
x = mouse_x;
y = mouse_y;
if(mouse_wheel_up()) {
pixelSize++;
}
if(mouse_wheel_down()) {
pixelSize = max(1, pixelSize - 1);
}