Search Unity

Question Use tilemap as mask for sprite

Discussion in '2D' started by reticivis, Feb 6, 2023.

  1. reticivis

    reticivis

    Joined:
    Jun 1, 2020
    Posts:
    3
    Currently, my game looks like this.
    upload_2023-2-5_20-53-59.png
    And I want it too look something like this (rough mockup via photoshop)
    upload_2023-2-5_20-59-13.png
    There's 2 effects that I want that I've mocked up with this image
    1. I want the tilemap to act as a mask to overlay an image on. I want it to act as if the tilemap was painted with this image, so the image moves with the tilemap (or rather moves when the camera moves)
    2. I want the image to get darker as it gets closer to the edge of the "air" where the player is.
    I don't care if I have to use shaders or other weird techniques as long as it produces this effect, but ideally I'd like to take the easiest route Unity provides.
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    Just do it like you did the mockup!

    - put the circuit board sprite down
    - layer the tilemap in front of it
    - parent them both to the same common object

    For the faux AO edge darkness, just put another sprite, or else make a set of tiles to go int the tilemap!

    Three (3) primary ways that Unity draws / stacks / sorts / layers / overlays stuff:

    https://forum.unity.com/threads/orthographic-camera-rendering-order.1114078/#post-7167037

    In short,

    1. The default 3D Renderers draw stuff according to Z depth - distance from camera.

    2. SpriteRenderers draw according to their Sorting Layer and Sorting Depth properties

    3. UI Canvas Renderers draw in linear transform sequence, like a stack of papers

    If you find that you need to mix and match items using these different ways of rendering, and have them appear in ways they are not initially designed for, you need to:

    - identify what you are using
    - search online for the combination of things you are doing and how to to achieve what you want.

    There may be more than one solution to try.

    Additional reading in the official docs:

    https://docs.unity3d.com/Manual/2DSorting.html

    And SortingGroups can also be extremely helpful in certain circumstances:

    https://docs.unity3d.com/Manual/class-SortingGroup.html
     
  3. reticivis

    reticivis

    Joined:
    Jun 1, 2020
    Posts:
    3
    I'm not sure if I'm doing it wrong or if I worded my original question poorly
    upload_2023-2-7_9-36-48.png
    I want the circuit board sprite to only show where the tilemap has tiles, as if someone was painting on the tiles or using the tiles as a clipping mask. The background will have a different sprite I will add later. I'm asking the question because I have no idea how to get a tilemap to act as a clipping mask. Sprites have a built-in clipping mask feature but it doesn't work for tilemaps.
     
  4. mrmattbett

    mrmattbett

    Joined:
    Jun 15, 2020
    Posts:
    2
    I am having the same issue and ChatGPT didn't help. I get the feeling I should render the tilemap into a sprite and display it, but doing that every tilemap change would be laggy in my game so a different way would be nice. again, a shader or a weird layering trick would be fine, I am already exploiting unity's camera systems to make a parallax effect, so I'm not exactly mimicking my old blockly games
     

    Attached Files:

  5. reticivis

    reticivis

    Joined:
    Jun 1, 2020
    Posts:
    3
    upload_2023-4-19_20-23-24.png
    I ended up using a tilemap shader to texture it and a 2nd tilemap on top to add the border manually
     
  6. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    unity currently does not support this

    i see you have some feathering aswell in your mockup

    mask feathering is doubly not supported

    the only way to achieve this would be with a custom shader, for masking, which I have also serched for without luck