Search Unity

Adding Spritemask to a tilemap.

Discussion in '2D' started by alimrafeek, Oct 17, 2020.

  1. alimrafeek

    alimrafeek

    Joined:
    May 22, 2018
    Posts:
    14
    Hi,
    I have two tilemap grids, one is used as ground and other is used as background.
    That is ground tiles are painted in ground grid and background tiles are painted in background grid.
    I want to add a sprite mask to ground tilemap only, So that I can have a 'splash graphics' and only the squares painted in ground grid show that effects.

    Is this the right way to do this or is there any other method?

    Thanks!
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,511
    On the TilemapRenderer, you can change its Mask Interaction field. This will allow it to interact with a SpriteMask.

    https://docs.unity3d.com/Manual/class-SpriteMask.html

    Hard to say without a better idea of what exactly you want to accomplish.
     
  3. alimrafeek

    alimrafeek

    Joined:
    May 22, 2018
    Posts:
    14
    No. I dont want the tilemap to interact with sprite mask.
    I want a spritemask in the place of the painted tilemap squares.

    As I said before I have two tilemaps, one for ground and one for background.
    When the enemy dies I want to make a blood stain effect only on the ground. So I thought of adding a sprite mask to the position of the ground tilemap and make the mask interaction of the blood stain to 'visible outside the mask'.
    Currently the sprite mask is only applicable to the small area I added the sprite mask.

    Is there anyway that the squares painted in ground tilemap to act as a spritemask.
    Or is there another easy way to implement this.

    Thanks!
     
  4. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,511
    Careful Placement
    If ground sprites are the same size as a tilemap cell, it should be possible to just draw the blood above the ground (using the Renderer's Order) and control where you draw it. Whether this is appropriate depends on the source of the blood splatter: if it's a particle system emitting wildly, then that may not work, but it you're placing sprites then it should be possible to confine the blood splatter within the confines of a cell (or nearby cells) by comparing the size of the sprite splatter to the cell's worldspace coordinates: see WorldToCell and CellToWorld. Then it's a geometry question.

    Robodash-like Texture Generation
    See the Robodash demo project. They create a texture to mark certain tiles as dark/shadowed. You could create a texture based on where ground exists, then use that texture as a source for your mask. If your ground tiles are square (1 pixel per ground cell position) that should work otherwise you need to approximate the ground shape at a reasonable resolution.

    Tilemap Shader/Material
    Create a shader/material for a dedicated blood TilemapRenderer. What's key is you have the same tile drawn to the ground also drawn to the blood tilemap, when there's blood. So, when you want blood on a particular ground cell, you draw the ground tile onto the blood tilemap. From the shader, you then have access to the _MainTex of the ground sprite, which means you can use its alpha so that you can apply blood splatter only within the ground cell. The blood splatter would be some repeatable texture, but it may be difficult to get that texture just right so it doesn't produce hard edges between two ground tiles, one with blood and the other without blood. Artistically, this might not be suited games that want to obscure the grid, if it's unacceptable for hard edges on the blood splatter between ground cells.