Search Unity

Vary properties passed to tilemap shader based on tile

Discussion in '2D' started by theGreatSlutze, Feb 20, 2021.

  1. theGreatSlutze

    theGreatSlutze

    Joined:
    Jan 7, 2013
    Posts:
    25
    I understand that a limitation of the current tilemap system is that every tile in the tilemap shares the same material and shader. I’m wondering, though, if it would be possible to define a shader that behaved differently based on a property of the tile. For example, one that applied a different color operation if a flag were passed in, with that flag being set as a property of the tile.

    what baffles me perpetually is that the texture of the tiles can be varied, which must mean they don’t REALLY share the same material (or different materials are faked somehow?), but that system isnt exposed in a way programmers can take advantage of. Seems like either it should be possible or I’m really misunderstanding something!
     
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    As far as I know, the tilemap is pre-processed such that it is one giant material merged together from all the subordinate materials of each tile. Now, could you have the shader do different things based on tile? Sorta, but I don't think it can do it the way you're proposing. I believe the shader only has 1 set of properties so each tile can't send it a separate set of properties. However, the shader itself can see the combine material so you could potential detect something about the particular tile's portion of the material and change behavior based on that. I don't think it is very worth while to explore as an option though as either the shader complexity would get insane, or you'd be better off just splitting your tilemap up so you can have different shaders. Only in pretty trivial circumstances would it probably be worth it.
     
  3. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,514
    What really should exist is something akin to how you can give custom data to a Particle System.

    For the tilemap, this would be something like this:

    TileData
    {
    // other properties
    float4 customFloats;
    }


    By script, you'd change these floats like you do the other TileData properties:

    tilemap.SetCustomFloat1(cell, blah);


    These floats would then automatically get passed to the shader, be accessable in Shader Graph, etc. The closest thing to that now are hacks on the other TD properties.

    That would bridge the divide between "rolling your own custom tilemap solution" to get per-tile properties, and "staying in the nice friendly non-custom Unity API" by making the nice, friendly Unity area a bit larger.
     
  4. theGreatSlutze

    theGreatSlutze

    Joined:
    Jan 7, 2013
    Posts:
    25
    I definitely agree, @Lo-renzo ! For now I’m not even looking for that level of granularity - all I really want is to be able to have a single variant per tile type without having an entirely different tilemap for every kind of tile I want to include. That’s my kind of workaround for sure, but due to some other things about my project it’s not really practical.

    I’ll probably end up seeing what kind of visual mileage I can get from other systems - maybe spawned gameobjects pooled effectively, or particle systems or something. Thanks!