Shaders Trying shaders, shader working but black screen

danibus

Member
I'm trying to make 2 shaders, following this example

Shaders are "God ray shader" and "underwater shader". Very simple and nice shaders.
Both are working ok together. But If I try to use only one single shader (i.e. "God ray shader") I get "god ray effect" in a black screen.
That means, shader is working, but I can't see my game sprites and backgr.

New in shader's world, read and tried to modify code lot of times, I can't get working only one shader.
Can you help me?

Also one question. When shaders are working, seems making click with mouse is not... accurate.
I have one object with big sprite "X" exit square to close game (like Windows), working when shaders are not working. But when shaders are working, I make click in "X" and nothing happens. Why?
 

Relic

Member
When going from “both shaders” to “one shader” have you set and reset the shader at the appropriate points in your code?

If the shaders work fine together it’s more likely how you choose to activate and reset them- can you show some code.
 

danibus

Member
I think all it's ok but... I show you the code, take note I tried to remove underwater shader here:

OBJ SHADER
///Get Shader Uniforms
//godray shader
u_resolution = shader_get_uniform(sh_godrays, "iResolution");
u_seconds = shader_get_uniform(sh_godrays, "iGlobalTime");

/**************************************************/
//distortion effect shader
//u_resolution_water = shader_get_uniform(sh_underwater, "iResolution");
//u_seconds_water = shader_get_uniform(sh_underwater, "iGlobalTime");
//u_texture_water = shader_get_sampler_index(sh_underwater, "tex_water");
/**************************************************/

sec = 0;

///Stop Automated Drawing of Application Surface in PostDraw Event
application_surface_draw_enable(false);

///Increase Time
sec += 1 / room_speed;

hh = window_get_height();
ww = window_get_width();

/**************************************************/
///Draw Shader Underwater effect
//draw wave distortion effect
//shader_set(sh_underwater);
//shader_set_uniform_f(u_resolution_water,ww,hh);
//shader_set_uniform_f(u_seconds_water,sec);
//texture_set_stage(u_texture_water, surface_get_texture(application_surface));
//draw_rectangle(0,0,ww,hh,false);
//shader_reset();
/**************************************************/

//Draw Shader Godray effect
shader_set(sh_godrays);

shader_set_uniform_f(u_resolution,800.0,600.0);
shader_set_uniform_f(u_resolution,ww,hh);
shader_set_uniform_f(u_seconds,sec);
draw_rectangle(0,0,ww,hh,false);

shader_reset();



Now both shaders

SHADER GOD RAYS
//Godray Vertex Shader
attribute vec3 in_Position; // (x,y,z)
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;
}

//Godray 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)
//Source: shadertoy.com - "Light rays" by user "ElusivePete"
float rayStrength(vec2 raySource, vec2 rayRefDirection, vec2 coord, float seedA, float seedB, float speed)
{
vec2 sourceToCoord = coord - raySource;
float cosAngle = dot(normalize(sourceToCoord), rayRefDirection);
return clamp(
(0.45 + 0.15 * sin(cosAngle * seedA + iGlobalTime * speed)) +
(0.3 + 0.2 * cos(-cosAngle * seedB + iGlobalTime * speed)),
0.0, 1.0) *
clamp((iResolution.x - length(sourceToCoord)) / iResolution.x, 0.5, 1.0);
}
void main()
{
vec2 coord = vec2(fragCoord.x, fragCoord.y);

// Set the parameters of the sun rays
vec2 rayPos1 = vec2(iResolution.x * 0.7, iResolution.y * -0.4);
vec2 rayRefDir1 = normalize(vec2(1.0, -0.116));
float raySeedA1 = 36.2214;
float raySeedB1 = 21.11349;
float raySpeed1 = 1.5;

vec2 rayPos2 = vec2(iResolution.x * 0.8, iResolution.y * -0.6);
vec2 rayRefDir2 = normalize(vec2(1.0, 0.241));
const float raySeedA2 = 22.39910;
const float raySeedB2 = 18.0234;
const float raySpeed2 = 1.1;

// Calculate the colour of the sun rays on the current fragment
vec4 rays1 =
vec4(1.0, 1.0, 1.0, 1.0) *
rayStrength(rayPos1, rayRefDir1, coord, raySeedA1, raySeedB1, raySpeed1);

vec4 rays2 =
vec4(1.0, 1.0, 1.0, 1.0) *
rayStrength(rayPos2, rayRefDir2, coord, raySeedA2, raySeedB2, raySpeed2);

gl_FragColor = rays1 * 0.5 + rays2 * 0.4;

// Attenuate brightness towards the bottom, simulating light-loss due to depth.
// Give the whole thing a blue-green tinge as well.
float brightness = 1.0 - (coord.y / iResolution.y);
gl_FragColor.x *= 0.1 + (brightness * 0.8);
gl_FragColor.y *= 0.3 + (brightness * 0.6);
gl_FragColor.z *= 0.5 + (brightness * 0.5);

}

SHADER UNDERWATER

//Underwater Vertex Shader
attribute vec3 in_Position; // (x,y,z)
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;
}

//Underwater 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 sampler2D tex_water;
// Jason Allen Doucette
// http://xona.com/jason/
//
// Quake-style Underwater Distortion
// May 15, 2016
// ---- SETTINGS ----------------------------------------------------------------
#define speed 1.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.00125
#define yDistMag 0.00125

// cycle multiplier for a given screen height
// 2*PI = you see a complete sine wave from top..bottom
#define xSineCycles 6.126
#define ySineCycles 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,yDistMag); // magnitude adjustment

// shear the coordinates
uv += distortOffset;

gl_FragColor = texture2D(tex_water, uv);
}
 
Top