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

NastyMonk

Member
I made a post yesterday that detailed the problem I run into https://forum.yoyogames.com/index.php?threads/one-instance-layer-makes-the-below-layers-appear-totally-black-cant-figure-out-why.73318/#post-432014
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.
1585235472184.png

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:

GML:
//
// 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.
 
Top