Search code examples
openglglslshadergame-maker-studio-2

How to get the room (or screen) coordinates from inside a Gamemaker Studio 2 shader?


I'm mostly new to writing shaders, and this might not be a great place to start, but I'm trying to make a shader to make an object "sparkle." I won't get into the specifics on how it's supposed to look, but to make it I need a value that changes with the object's position in the room (or on the screen, as the camera is fixed). I've tried v_vTexcoord, in_Position, gl_Position, and others without the intended result. If I've used them wrong or missed something, I wouldn't be surprised, but any advice is helpful.

I don't think they'll be helpful but here's my vertex shader:

//it's mostly the same as the default
// Simple passthrough 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 vec3 v_inpos;

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;
    v_inpos = /*this is the variable that i'd like to set to the x,y*/;
}

and my fragment shader:

//
//
//
varying vec2 v_vTexcoord; //x is <1, >.5
varying vec4 v_vColour;
varying vec3 v_inpos;
uniform float pixelH; //unused, but left in for accuracy
uniform float pixelW; //see above

void main() //WHEN AN ERROR HAPPENS, THE SHADER JUST WON'T DO ANYTHING AT ALL.
{
    vec2 offset = vec2 (pixelW, pixelH);
    gl_FragColor = v_vColour * texture2D( gm_BaseTexture, v_vTexcoord );

    /* i am planning on just testing different math until something works, but i can't
    vec3 test = vec3 (v_inpos.x, v_inpos.x, v_inpos.x); //find the values i need to test it
    test.x = mod(test.x,.08);
    test.x = test.x - 4;
    test.x = abs(test.x);


    while (test.x > 1.0){
        test.x = test.x/10;
    }
    test = vec3 (test.x, test.x, test.x);
    gl_FragColor.a = test.x;
    */
    //everything above doesn't cause an error when uncommented, i think

    //if (v_inpos.x == 0.0){gl_FragColor.rgb = vec3 (1,0,0);}
    //if (v_inpos.x > 1)  {gl_FragColor.rgb = vec3 (0,1,0);}
    //if (v_inpos.x < 1)  {gl_FragColor.rgb = vec3 (0,0,1);}
}

if this question doesn't make sense, i'll try to clarify any other questions in the comments.


Solution

  • If you want to get a position in world space, then you have to transform the vertex coordinate from model space to world space.
    This can be done by the model (world) matrix (gm_Matrices[MATRIX_WORLD]). See game-maker-studio-2 - Matrices.

    e.g.:

    vec4 object_space_pos = vec4(in_Position.xyz, 1.0);
    vec4 world_space_pos  = gm_Matrices[MATRIX_WORLD] * object_space_pos;
    

    Not, the Cartesian world space position can be get by:
    (See also Swizzling)

    vec3 pos = world_space_pos.xyz;