如何从 Gamemaker Studio 2 着色器中获取房间(或屏幕)坐标?

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

我基本上是编写着色器的新手,这可能不是一个很好的起点,但我正在尝试制作一个着色器来制作一个对象"sparkle."我不会深入关于它应该看起来如何的细节,但要做到这一点,我需要一个随着物体在房间中(或在屏幕上,因为相机是固定的)位置而变化的值。我尝试了 v_vTexcoord、in_Position、gl_Position 和其他方法,但没有达到预期的结果。如果我用错了它们或遗漏了什么,我不会感到惊讶,但任何建议都会有所帮助。

我认为它们不会有帮助,但这是我的顶点着色器:

//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*/;
}

和我的片段着色器:

//
//
//
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);}
}

如果这个问题没有意义,我会尝试在评论中澄清任何其他问题。

如果你想在世界space中获得一个位置,那么你必须将顶点坐标从模型space转换到世界space。
这可以通过模型(世界)矩阵(gm_Matrices[MATRIX_WORLD])来完成。参见 game-maker-studio-2 - Matrices

例如:

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

不是,Cartesian世界space位置可以通过:
(另见 Swizzling

vec3 pos = world_space_pos.xyz;