S
Sidartha
Guest
Sorry folks, I`m still a rookie on Game Maker and despite of watching some tutorials I couldn`t apply a distortion shader with a vignette shader on the whole game screen.
I`ve created two shaders: One named "sh_underwater" and the other named "shd_vignette_noise":
SHD_VIGNETTE_NOISE---------------------------------------------------------------------------------------------
VERTEX SHADER
attribute vec3 in_Position;
attribute vec4 in_Colour;
attribute vec2 in_TextureCoord;
varying vec2 var_texcoord;
varying vec4 v_vColour;
void main()
{
gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * vec4(in_Position, 1.0);
v_vColour = in_Colour;
var_texcoord = in_TextureCoord;
}
FRAGMENT SHADER
varying vec2 var_texcoord;
varying vec4 v_vColour;
uniform vec4 u_settings; //vignette inner circle size, vignette outter circle size, noise strength
uniform vec3 u_vignette_colour; //R,G,B
float random(vec3 _scale, float _seed)
{
return fract(sin(dot(vec3(var_texcoord, 1.0)+_seed, _scale))*43758.5453+_seed);
}
void main()
{
vec4 base = texture2D( gm_BaseTexture, var_texcoord );
float vignette = distance( vec2(0.5, 0.5), var_texcoord );
vignette = u_settings.x - vignette * u_settings.y;
vec3 vignette_colour = (u_vignette_colour/255.0) * vignette;
float noise_strength = u_settings.z;
float noise = noise_strength * ( 0.5 - random( vec3( 1.0 ), length( vec2(var_texcoord.x/var_texcoord.y, var_texcoord.y)) ) );
if (u_settings.w == 1.0) {vignette_colour += noise;}
gl_FragColor = v_vColour * vec4(base.rgb * vignette_colour, base.a);
}
SH_UNDERWATER-------------------------------------------------------------------------------------------------
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;
varying vec2 fragCoord;
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;
fragCoord = in_Position.xy;
}
FRAGMENT SHADER
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
varying vec2 fragCoord;
uniform vec2 iResolution; // viewport resolution (in pixels)
uniform float iGlobalTime; // shader playback time (in seconds)
uniform float modificaOndaX;
uniform float modificaOndaY;
uniform sampler2D tex_water;
// ---- SETTINGS ----
#define speed 3.0
// the amount of shearing (shifting of a single column or row)
// 1.0 = entire screen height offset (to both sides, meaning it's 2.0 in total)
#define xDistMag 0.01500 // 0.00125
#define yDistMag 0.00500 // 0.00125
// cycle multiplier for a given screen height
// 2*PI = you see a complete sine wave from top..bottom
#define xSineCycles 6.126 // 6.126
#define ySineCycles 6.126 // 6.126
// ---- CODE ----
void main()
{
vec2 uv = vec2(fragCoord.x,fragCoord.y);
uv = fragCoord.xy / iResolution.xy;
// the value for the sine has 2 inputs:
// 1. the time, so that it animates.
// 2. the y-row, so that ALL scanlines do not distort equally.
float time = iGlobalTime*speed;
float xAngle = time + fragCoord.y * ySineCycles;
float yAngle = time + fragCoord.x * xSineCycles;
vec2 distortOffset =
vec2(sin(xAngle), sin(yAngle)) * // amount of shearing
vec2(xDistMag * modificaOndaX,yDistMag * modificaOndaY); // magnitude adjustment
// shear the coordinates
uv += distortOffset;
gl_FragColor = texture2D(tex_water, uv);
}
And I`ve created an object "obj_control" to apply the effect:
CREATE
//distortion effect shader
u_resolution_water = shader_get_uniform(sh_underwater, "iResolution");
u_seconds_water = shader_get_uniform(sh_underwater, "iGlobalTime");
u_modifica_ondaX = shader_get_uniform(sh_underwater, "modificaOndaX");
u_modifica_ondaY = shader_get_uniform(sh_underwater, "modificaOndaY");
u_texture_water = shader_get_sampler_index(sh_underwater, "tex_water");
sec = 0;
//Vignette effect shader
uni_settings = shader_get_uniform(shd_vignette_noise, "u_settings");
uni_vignette_colour = shader_get_uniform(shd_vignette_noise, "u_vignette_colour");
//Stopping the automated draw at application surface on postdraw event
application_surface_draw_enable(false);
STEP
sec += 1/room_speed;
POST-DRAW
shader_set(sh_underwater);
shader_set_uniform_f(u_resolution_water,1080.0,810.0);
shader_set_uniform_f(u_seconds_water,sec);
shader_set_uniform_f(u_modifica_ondaX, /*irandom_range(5, 15)*0.1 */ .250);
shader_set_uniform_f(u_modifica_ondaY, /*irandom_range(5, 15)*0.1 */ .750);
texture_set_stage(u_texture_water, surface_get_texture(application_surface)); //feed application surface into shader
draw_rectangle(0,0,1080,810,false);
shader_reset();
shader_set(shd_vignette_noise);
shader_set_uniform_f(uni_settings, 1.2, 1.9, 0.06, 1); //vignette inner circle size, vignette outter circle size, noise strength, noise enable (1 or 0 only).
shader_set_uniform_f(uni_vignette_colour, 245, 223, 201); //R,G,B - 0 to 255
draw_surface(application_surface,0,0);
shader_reset();
Thanks for your patience, guys!
I`ve created two shaders: One named "sh_underwater" and the other named "shd_vignette_noise":
SHD_VIGNETTE_NOISE---------------------------------------------------------------------------------------------
VERTEX SHADER
attribute vec3 in_Position;
attribute vec4 in_Colour;
attribute vec2 in_TextureCoord;
varying vec2 var_texcoord;
varying vec4 v_vColour;
void main()
{
gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * vec4(in_Position, 1.0);
v_vColour = in_Colour;
var_texcoord = in_TextureCoord;
}
FRAGMENT SHADER
varying vec2 var_texcoord;
varying vec4 v_vColour;
uniform vec4 u_settings; //vignette inner circle size, vignette outter circle size, noise strength
uniform vec3 u_vignette_colour; //R,G,B
float random(vec3 _scale, float _seed)
{
return fract(sin(dot(vec3(var_texcoord, 1.0)+_seed, _scale))*43758.5453+_seed);
}
void main()
{
vec4 base = texture2D( gm_BaseTexture, var_texcoord );
float vignette = distance( vec2(0.5, 0.5), var_texcoord );
vignette = u_settings.x - vignette * u_settings.y;
vec3 vignette_colour = (u_vignette_colour/255.0) * vignette;
float noise_strength = u_settings.z;
float noise = noise_strength * ( 0.5 - random( vec3( 1.0 ), length( vec2(var_texcoord.x/var_texcoord.y, var_texcoord.y)) ) );
if (u_settings.w == 1.0) {vignette_colour += noise;}
gl_FragColor = v_vColour * vec4(base.rgb * vignette_colour, base.a);
}
SH_UNDERWATER-------------------------------------------------------------------------------------------------
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;
varying vec2 fragCoord;
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;
fragCoord = in_Position.xy;
}
FRAGMENT SHADER
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
varying vec2 fragCoord;
uniform vec2 iResolution; // viewport resolution (in pixels)
uniform float iGlobalTime; // shader playback time (in seconds)
uniform float modificaOndaX;
uniform float modificaOndaY;
uniform sampler2D tex_water;
// ---- SETTINGS ----
#define speed 3.0
// the amount of shearing (shifting of a single column or row)
// 1.0 = entire screen height offset (to both sides, meaning it's 2.0 in total)
#define xDistMag 0.01500 // 0.00125
#define yDistMag 0.00500 // 0.00125
// cycle multiplier for a given screen height
// 2*PI = you see a complete sine wave from top..bottom
#define xSineCycles 6.126 // 6.126
#define ySineCycles 6.126 // 6.126
// ---- CODE ----
void main()
{
vec2 uv = vec2(fragCoord.x,fragCoord.y);
uv = fragCoord.xy / iResolution.xy;
// the value for the sine has 2 inputs:
// 1. the time, so that it animates.
// 2. the y-row, so that ALL scanlines do not distort equally.
float time = iGlobalTime*speed;
float xAngle = time + fragCoord.y * ySineCycles;
float yAngle = time + fragCoord.x * xSineCycles;
vec2 distortOffset =
vec2(sin(xAngle), sin(yAngle)) * // amount of shearing
vec2(xDistMag * modificaOndaX,yDistMag * modificaOndaY); // magnitude adjustment
// shear the coordinates
uv += distortOffset;
gl_FragColor = texture2D(tex_water, uv);
}
And I`ve created an object "obj_control" to apply the effect:
CREATE
//distortion effect shader
u_resolution_water = shader_get_uniform(sh_underwater, "iResolution");
u_seconds_water = shader_get_uniform(sh_underwater, "iGlobalTime");
u_modifica_ondaX = shader_get_uniform(sh_underwater, "modificaOndaX");
u_modifica_ondaY = shader_get_uniform(sh_underwater, "modificaOndaY");
u_texture_water = shader_get_sampler_index(sh_underwater, "tex_water");
sec = 0;
//Vignette effect shader
uni_settings = shader_get_uniform(shd_vignette_noise, "u_settings");
uni_vignette_colour = shader_get_uniform(shd_vignette_noise, "u_vignette_colour");
//Stopping the automated draw at application surface on postdraw event
application_surface_draw_enable(false);
STEP
sec += 1/room_speed;
POST-DRAW
shader_set(sh_underwater);
shader_set_uniform_f(u_resolution_water,1080.0,810.0);
shader_set_uniform_f(u_seconds_water,sec);
shader_set_uniform_f(u_modifica_ondaX, /*irandom_range(5, 15)*0.1 */ .250);
shader_set_uniform_f(u_modifica_ondaY, /*irandom_range(5, 15)*0.1 */ .750);
texture_set_stage(u_texture_water, surface_get_texture(application_surface)); //feed application surface into shader
draw_rectangle(0,0,1080,810,false);
shader_reset();
shader_set(shd_vignette_noise);
shader_set_uniform_f(uni_settings, 1.2, 1.9, 0.06, 1); //vignette inner circle size, vignette outter circle size, noise strength, noise enable (1 or 0 only).
shader_set_uniform_f(uni_vignette_colour, 245, 223, 201); //R,G,B - 0 to 255
draw_surface(application_surface,0,0);
shader_reset();
Thanks for your patience, guys!
Last edited by a moderator: