How to use shaders to make parts of an object transparent based upon contact with another object?

I am trying to make a game in GameMaker 2 where objects become transparent depending on the object behind them. I currently have it so I am just checking each step (within the object to become transparent) whether in contact with the background object and then setting alpha to 0.5 if true.

Here is what I mean, the object changes from:

Opaque To Half Alpha

The problem is that I want the object to transition to being transparent, not just jump between the two states when it touches the edge of the yellow section (e.g. Any part of the ball in the yellow is transparent and any part in the blue is opaque).

I tried to achieve this using shaders but I don't have a lot of experience with them, I tried achieving the affect by creating a shader where I passed in the center of the yellow box, its width and its height to check whether any pixel was inside the rectangle and then set it to transparent but this method used a lot of nested if statements which I understand to be bad form and also will not work if there are more than two different coloured sections in a room. Is there any way to achieve the effect I want (where only the parts of the ball inside the yellow section become transparent) of do I have to deal with it just jumping from completely opaque to completely transparent.
 

NightFrost

Member
Ther problem with the shader you wrote is how it cannot distinguish between pixels that belong to objects that should lower alpha, to other objects, or to simple background graphics. You need to construct a mask from appropriate objects. I'll call the object that needs to become transparent as "player"to make this less confusing.

First, check if player is in collision with objects that should make it partially transparent. Use the collision list commands to fetch a complete set of them. If their amount is larger than zero, create a surface that is the size of the player sprite. Set it as target and draw all the colliding objects to it relative to player position. Meaning, when you imagine placing the surface on top of the player, the shapes of the colliding objects match both in the room and on the surface. (Yes, you can set draw positions that are outside surface; only the pixels that will actually hit the surface get drawn.) Next, when you draw the player, send this surface as sampler to the transparency shader. In the shader, you read the sampler from the exact same position as player sprite and subtract the alpha value of that sampler pixel - multiplied by some factor - from player sprite's alpha. For example if you want 50% transparency on overlapping pixels, that is player pixel alpha minus sampler alpha times half.

(Remember to destroy the surface after shader is done.)
 
Top