• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Need to understand Shader: Fade color on a sprite

heirey

Member
Hello,

I have found a shader "sh_fade" to change the color of a sprite with a fade. Currently, this shader make a fade with white color but i dont find where i can change the color for an other color (white to blue for example)

Can you help me?

Here
Code:
Vertex:
//
// Simple passthrough vertex shader
//
attribute vec3 in_Position;                  // (x,y,z)
//attribute vec3 in_Normal;                  // (x,y,z)     unused in this shader.
attribute vec4 in_Colour;                    // (r,g,b,a)
attribute vec2 in_TextureCoord;              // (u,v)

varying vec2 v_vTexcoord;
varying vec4 v_vColour;

void main()
{
    vec4 object_space_pos = vec4( in_Position.x, in_Position.y, in_Position.z, 1.0);
    gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;
   
    v_vColour = in_Colour;
    v_vTexcoord = in_TextureCoord;
}

Fragment:
uniform vec4 f_colour;
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

void main()
{
    vec4 col = texture2D( gm_BaseTexture, v_vTexcoord );
    col.rgb = mix(col.rgb, f_colour.rgb, f_colour.a);
    gl_FragColor = v_vColour * col;
}
Code:
Create Event:
fade = 1;
fade_colour = shader_get_uniform(sh_fade, "f_colour");

Step event:
if fade > 0 { fade -= 0.07; }

Draw Event:
shader_set(sh_fade);
shader_set_uniform_f(fade_colour, 1, 1, 1, fade); // 1,1,1 for white
draw_self();
shader_reset();
Thanks!
 

jo-thijs

Member
Hi and welcome to the GMC!

You can change the color right here:
Code:
shader_set_uniform_f(fade_colour, 1, 1, 1, fade); // 1,1,1 for white
The second argument is the red component of the color to blend to, the third argument is the green component and the fourth argument is the blue component.
Those components are values between 0 and 1 where 0 is the absence of the component in the color and 1 is the complete presence of the component.

Some examples:
Using 1, 0, 0 would be pure red.
Using 0, 1, 0 would be pure green.
Using 0, 0, 1 would be pure blue.
Using 1, 1, 0 would be pure yellow.
Using 1, 0, 1 would be pure magenta.
Using 0, 1, 1 would be pure cyan.
Using 1, 0.5, 0 would be orange.
...

In a lot of places, you'll get colors where components are values between 0 and 255.
To convert to those values, you just simply divide by 255.
 
Top