• 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!

Shaders Shader uniforms not working

B

BlueCat6123

Guest
Forgive me if I'm making a rookie mistake, but I can't seem to spot it right off the bat.
I have an alpha shader:
Code:
varying vec2 v_vTexcoord;
varying vec4 v_vColour;

uniform float alpha;

void main()
{
    gl_FragColor = v_vColour * texture2D( gm_BaseTexture, v_vTexcoord );
    gl_FragColor.a = gl_FragColor.a * alpha;
}
and a controller object (it changes a tile layer's opacity):
GML:
//STEP EVENT
//'shader' var is set to above shader
var alphaShader = shader_get_uniform(shader,"alpha");
shader_set_uniform_f(alphaShader,alpha);

layer_shader(tileLayer,shader);
the uniform "alpha" doesn't seem to get set, and just leaves the layer always being invisible. Is there something I'm missing? I'm relatively new to shaders so it most likely is just a small mistake.
 

NightFrost

Member
I'm unsure if it is possible to send uniforms to shaders that are used via layer_shader... uniforms affect a shader only after shader_set is called, and I assume the manner in which layer shaders work is they shader_set only when the layer's turn to render its contents comes. You may have to create layer scripts instead and assign the shader to work through that. The documentation entry for layer_script_begin in fact has an extended example how begin script and end script are used to enable a shader with uniforms. That, at least, is how I've always done it too.

(Calling shader_set AFAIK resets the shader, so what you've done with the shader has no effect because GMS automatically calls it again, negating whatever you've done.)
 
Top