0
\$\begingroup\$

Some 2D top-down games use tile maps and use transparent tiles for representing static objects. For example Stoneshard. When the character walks behind a tree, the tree becomes partially transparent. ( More recognizable example may be Stardew Valley, but I don't remember exactly )

It does not seem like a difficulty in terms of rendering, however I wonder what is efficient way to detect such condition, when the character "intersects" particular potentially transparent object.

I can determine the indices and values of tiles which are intersected by the character, but what if the character is partially covered by some object? Then the whole object should become transparent. Does it mean that I have to implement some kind of data object to represent the potentially transparent object on the map and its tiles?

It may seem like I have already come up with solution myself, but I suppose there can be some other, more efficient ways to do this

The character is going to intersect the tree crown The tree crown became partially transparent

\$\endgroup\$
4
  • \$\begingroup\$ I would consider this question too complex to answer as... It will depend on how you render your scene as there are many ways to render a tile map. And yes you will need to track all objects that can become transparent and set that transparency depending on the relative position of the player (and whatnot) you want visible behind said object (it may be as simple as applying transparency to screen pos if character is always center (fixed pos) screen). Also note that draw order is important when using transparency, you can not just rely on just the Z buffer to deduce visibility. \$\endgroup\$
    – Blindman67
    Jan 25 at 18:43
  • \$\begingroup\$ @Blindman67 I would like to know more about different ways to render a tile map and how they affect solution of transparency problem. If you can provide some links to articles or etc. I would really appreciate this \$\endgroup\$
    – Steyrix
    Jan 26 at 9:27
  • \$\begingroup\$ Whats wrong with something like OnTriggerEnter2D? \$\endgroup\$
    – Pow
    Feb 1 at 13:34
  • \$\begingroup\$ @Pow I am not familiar with OnTriggerEnter2D and Unity \$\endgroup\$
    – Steyrix
    Feb 1 at 14:11

2 Answers 2

0
\$\begingroup\$

Implement alpha blending at the pixel level during rendering for partial transparency in 2D top-down games without requiring complex data structures for individually transparent objects.

\$\endgroup\$
0
\$\begingroup\$

I managed to implement this kind of behavior. First step was to parse the certain layer of the map, containing potentially transparent objects, to a collection of data structures. Each of them holds the mesh for respective tiles. For a case, when an object is presented by multiple adjacent tiles, I used simple algorithm to check if tiles are present in the layer at positions adjacent to the target tile.

Second step was to create the same shader instance for each object and set uniform value, representing the transparency/opacity on intersecting the object. To determine, which object on the layer is intersected, I added property to the object data structure hold, which holds the set of tiles' indices related to the object.

The shader itself is simple.

in vec2 fragmentUV;
out vec4 fColor;

uniform sampler2D textureSample;
uniform float transparency;

void main(void)
{
    fColor = vec4(1.0, 1.0, 1.0, transparency) * texture(textureSample, fragmentUV);

    if (fColor.a <= 0) {
        discard;
    }
} 
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .