GMS 2 Share my findings from solving a weird layer issue. Hint: your object actually has a z value/depth value.


I made a post yesterday that detailed the problem I run into
In short, I have an instance that draws a surface with a shader that simulates a limited circle view for the player. The player view is full black outside the circle like this.

This control instance is placed in layer A. And It works fine when Layer A is the top layer in the layer panel. The depth of this layer is 0. I have the player object placed in a layer below with depth 100. My fragment shader code looks like this:

// Simple pass through fragment shader
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
varying vec3 v_Position;
uniform float player_x;
uniform float player_y;

void main()
{   // playerPosition is the position of player object
    // v_Position equals in_Position which is passed through from vertext shader
    vec3 playerPosition = vec3(player_x,player_y,0);
    float distanceToPlayer = distance(playerPosition,v_Position);
    // If the rendered fragment position is within 100 pixels from the player object make it transparent
    if (distanceToPlayer < 100.0) {
     gl_FragColor = vec4(1.0,1.0,1.0,0) * texture2D( gm_BaseTexture, v_vTexcoord );

    // Otherwise make it black
    else {
     gl_FragColor = vec4(1.0,1.0,1.0,1.0) * texture2D( gm_BaseTexture, v_vTexcoord );
// code here
Now I didn't put much thought on the z position for the playerPosition, so I just give it a zero, which causes a huge issue afterward. This shader works fine since the control instance has depth 0 and despite the player object having a depth of 100, I give it a z of zero. So the distance calculation comes out right since they are considered to have the same depth and therefore on the same plane. However, the shader failed when I add a new layer on the very top, which makes the control instance sitting on a layer with depth 100. The player is still of depth 0. This messed up the distance calculation and the shader failed to create a circle view because the distance now is always larger than 100.

I spent a lot of time trying to figure out why and this detail finally came upon me. After changing the shader code to make sure the player and the control instance are calculated with the same depth, it works perfectly now.

I want to share this experience as a reminder that always pay attention to the depth of your objects when calculating distance in the shader.