Shaders GMS2 shader not working. Please help.

Discussion in 'Programming' started by cammodore64, Sep 5, 2019.

  1. cammodore64

    cammodore64 Member

    Joined:
    Sep 5, 2019
    Posts:
    8
    I just started playing with shaders and even though my code is exactly like the simple tutorials I'm following, nothing seems to change when I run the program. I have no idea what I'm doing wrong. Any advice would be a great help.

    Here's the fragment shader's code:

    varying vec2 v_vTexcoord;
    varying vec4 v_vColour;

    void main() {

    vec4 base_col = v_vColor * texture2D(gm_BaseTexture, v_vTexcoord);

    gl_FragColor = vec4(base_col.rgb, base_col.a * 0.5);
    }

    And here's the test object's draw event code:

    shader_set(shader0);
    draw_self();
    shader_reset();

    Again, very simple stuff and yet nothing seems to change when ran. The object sprite's "separate texture page" box is checked and color interpolation between pixels has been disabled.
     
  2. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,959
    That should be drawing the sprite at 0.5 alpha, so something is off. Have you perhaps turned off alpha blending?
     
  3. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,168
    It's not necessary to use seperate texture page in this case. Although this is not the cause of your problem.

    There's not enough information here to explain why you wouldn't see any difference between using the shader and not using the shader. I would suggest that, first, you double check that there really isn't any visible difference. Then, check if there is anything else going on in your project that might be interfering with the expected function of this shader. Possible culprits might be certain blending modes, or perhaps drawing multiple instances overlapping in the same spot.
     
  4. cammodore64

    cammodore64 Member

    Joined:
    Sep 5, 2019
    Posts:
    8
    Where would I go to set/change that?
     
  5. cammodore64

    cammodore64 Member

    Joined:
    Sep 5, 2019
    Posts:
    8
    What I posted is the entirety of the project’s code (I threw this together in a new program purely to test/get an understanding of shaders). I’ve also checked the screen output several times and the results really don’t seem to show any visible difference. Is there anything that maybe I’m not including in the code? I seriously feel like such a dope. I’ve never hit a wall like this with GMS.
     
  6. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,168
    If you don't compare side-by-side the difference between using and not using the shader, it might be difficult to visually detect any difference. Either set up a toggle button, or take a screenshot of each and compare in an image editor.
     
  7. Posh Indie

    Posh Indie Member

    Joined:
    Dec 5, 2016
    Posts:
    200
    Try changing something more obvious, such as the color (eg. making it red). See if that even works.
     
  8. cammodore64

    cammodore64 Member

    Joined:
    Sep 5, 2019
    Posts:
    8
    I changed the shader code to the following:

    varying vec2 v_vTexcoord;
    varying vec4 v_vColour;

    void main() {
    // vec4 base_col = v_vColor * texture2D(gm_BaseTexture, v_vTexcoord);

    gl_FragColor = vec4.rgba(1.0, 0.0, 0.0, 0.0);
    }

    I figure the result should have been a solid red box, but again, the sprite just appears unchanged.
     
  9. cammodore64

    cammodore64 Member

    Joined:
    Sep 5, 2019
    Posts:
    8
    I changed the draw event to draw the sprite both with and without the shader and changed the shader to draw every pixel pure red (the original sprite is a grayscale npc figure so the difference should be quite clear) there’s sadly just no change...I really appreciate your help. I’m trying everything I can over here.
     
  10. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,168
    Are you sure you don't actually have two shaders in your project, and that you aren't accidentally using the wrong shader?
     
  11. Posh Indie

    Posh Indie Member

    Joined:
    Dec 5, 2016
    Posts:
    200
    Actually, that would be invisible entirely. But regardless, that is not how it should have behaved, haha.
     
  12. Bart

    Bart Member

    Joined:
    Jun 21, 2016
    Posts:
    176
    v_vColour and v_vColor are two different variables ;)

    Code:
    gl_FragColor = vec4.rgba(1.0, 0.0, 0.0, 0.0);
    This one is a syntax issue. First construct the vec4, then access its components, like this:
    Code:
    gl_FragColor = vec4(1.0, 0.0, 0.0, 0.0).rgba;
    Although you can just assign the newly created vec4 directly since gl_FragColor is also a vec4:
    Code:
    gl_FragColor = vec4(1.0, 0.0, 0.0, 0.0);
     
    Ido-f likes this.
  13. Ido-f

    Ido-f Member

    Joined:
    Feb 19, 2018
    Posts:
    125
    Bart's reply seems to be it.
    In the future, I recommend this tool for error-catching in glsl.
     
    Bart likes this.
  14. cammodore64

    cammodore64 Member

    Joined:
    Sep 5, 2019
    Posts:
    8
    Okay, so I made all the little corrections and now I have this:
    Code:
    varying vec2 v_vTexcoord;
    varying vec4 v_vColor;
    
    void main() {
        vec4 color = v_vColor * texture2D(gm_BaseTexture, v_vTexcoord);
    
        color.r = 1.0;
        color.a = 0.5;
       
        gl_FragColor = color;
    }
    Still nothing. I feel like I'm going crazy at this point. The vector section of the shader is empty. Am I supposed to include something there? The sprite being tested has it's own texture page. Are there any other checkboxes floating around that I need to check off? Does it matter that I'm using a Mac? I'm completely out of ideas.
     
  15. Bart

    Bart Member

    Joined:
    Jun 21, 2016
    Posts:
    176
    That code looks okay, assuming that you use v_vColor in the vertex shader as well (instead of v_vColour).
    What code do you have in the vertex shader? (is this what you mean by the vector section of the shader)
     
  16. cammodore64

    cammodore64 Member

    Joined:
    Sep 5, 2019
    Posts:
    8
    Yes, I meant vertex shader. Sorry about that. Like I said, there's no code in there since I figured it would only need to use the fragment shader stuff for 2D purposes.
     
  17. Posh Indie

    Posh Indie Member

    Joined:
    Dec 5, 2016
    Posts:
    200
    It would still need pass-through code.
     
  18. cammodore64

    cammodore64 Member

    Joined:
    Sep 5, 2019
    Posts:
    8
    It worked!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice