• 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 Weird shader behavior [SOLVED]

aereddia

Member
So I have this shader where I want to draw white diagonal lines across an image. Pretty simple stuff really, I'll even list the code here so you can see it.

Code:
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform float degree;
uniform vec4 uvs;
uniform float u_time;

//Basically just rotates the image by 90*
void rotate2d(inout vec2 st)
{
    //90* converted to radians
    float a = 3.14159/2.0;
    
    st -= 0.5;
    st *= mat2(cos(a),-sin(a),sin(a),cos(a));
    st += 0.5;
}

//Get x and y values between 0 and 1 for % through sprite
vec2 getPositionThroughSprite()
{
    vec2 topRight = v_vTexcoord - uvs.rg;
    vec2 width = uvs.ba - uvs.rg;
    
    return topRight/width;
}

//Get diagonal lines, values 0 or 1 for on or off
float getLines()
{
    vec2 st = getPositionThroughSprite();
    rotate2d(st);
    
    float line = 1.0 - (st.x-st.y - 2.0);

    //While testing, u_time is always set to .8, but if I manually replace u_time with 0.8, the entire image comes out as 0. Very weird.
    line -= mod(u_time*4.0,4.0);
    
    float n = 0.0;
    float diff = 0.06;
    n += step(-diff,line)-step(diff,line);
    n += step(-.2,line)-step(-.15,line);
    
    return n;
}

//Turns the image greyscale
vec4 getGreyScaleImage()
{
    vec4 c = texture2D( gm_BaseTexture, v_vTexcoord );
    vec3 lum = vec3(0.299, 0.587, 0.114);
    
    //degree is a uniform from 0 to 1 where 1 is completely greyscale, 0 is completely normal
    c.rgb = mix(c.rgb, vec3(dot( c.rgb, lum)), degree);
    
    return c;
}

void main()
{
    vec4 c = getGreyScaleImage();
    gl_FragColor = c;
    gl_FragColor.rgb += vec3(getLines());
}
So the getLines() functions returns this image when I apply it to gl_FragColor.rgb as an assignment. We can see the values are either 0 or 1 and the silhouette of the image is still there.
Code:
 gl_FragColor.rgb = vec3(getLines());
Leech lines.PNG

But as soon as I change it to a += assignment, the lines disappear. Why? Both of these pieces of code work fine individually.
Code:
 gl_FragColor.rgb += vec3(getLines());
Leech +.PNG

Now if I remove the getGreyScaleImage() call and use the base values from the texture page, it works fine.
Code:
gl_FragColor = texture2D( gm_BaseTexture, v_vTexcoord );
gl_FragColor.rgb += vec3(getLines());
Leech normal.PNG

Does anyone know why this might be happening? I don't see anything in my getGreyScaleImage() method that should be causing issues, but it is for some reason.
 

aereddia

Member
Problem solved. I had this code in my create event and it was causing issues:

uvs = shader_get_uniform(shader, "uvs");
uvs2 = shader_get_uniform(sh_mirror_dimension, "uvs");
u_time = shader_get_uniform(sh_mirror_dimension, "u_time");


"shader" stores the variable of the shader I'm currently building which was different from sh_mirror_dimension. I changed the u_time pickup, restarted gamemaker, and now everything is working fine.
 
Top