Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Shader Graph and Multiple Sprite Mode

Discussion in 'Shader Graph' started by Jargs, Jul 7, 2019.

  1. Jargs

    Jargs

    Joined:
    Dec 8, 2018
    Posts:
    5
    I have created a wind sway shader for 2D trees, based on Brackeys grass sway video. I'm using LWRP.

    It doesn't appear to be playing well with sprites from textures that are set to multiple sprite mode. The sway is suposed to be nothing ath the bottom and gradually increase to full strength at the top. From what I can tell, instead of seeing the bottom of the selected sprite as the bottom, it looks like the shader is treating the bottom of the entire multi-sprite texture as the "bottom" of the sprite.

    In the example below, the tree on the left is using a sprite from the top left corner of a much larger texture in multiple sprite mode. The tree on the right is using a sprite from a texture in single sprite mode. The sway intensity has been increased to really show the problem...



    Notice how the trunk of the tree on the left is swaying back and forth, but the trunk of the tree on the right is practically stationary.

    Is this normal behavior for shader graphs? Is there some way I can tell it to treat the individual sprite separately from the entire texture, or do I have to split every sprite I want to add a shader to into it's own texture?

    upload_2019-7-7_14-16-28.png
     
    Vivien_Lynn likes this.
  2. ununion

    ununion

    Joined:
    Dec 2, 2018
    Posts:
    275
    my friend ,we have same problem here. waiting for replying or have you been solved it?
     
  3. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,816
    I'll send this over to the team for them to take a look for you guys.
     
  4. Hookkshot

    Hookkshot

    Joined:
    Jan 11, 2013
    Posts:
    27
    Has there been a resolution for hits. Also running into the same issue for tree swaying as well.
     
  5. DevilCult

    DevilCult

    Joined:
    Jun 24, 2017
    Posts:
    62
    Same problem here! That was a long 2 days before finding this thread... :\
    Is it working on a single sprite? Im gonna cut everything down! lol
     
  6. DevilCult

    DevilCult

    Joined:
    Jun 24, 2017
    Posts:
    62
    Ok seem the problem is with making sprite atlas, once in the atlas shader become weirds (those using vertex displacement at least). I separated all images and placed them outside atlas and everything is fine now.

    Enjoy ! ;P
     
  7. TobyM

    TobyM

    Joined:
    Apr 28, 2017
    Posts:
    7
    Is there any update on if there will be a way to use a sprite from a sprite atlas with shader graph? I would really like to avoid having to save every single sprite as a sperate file, as it would be an inconvenience for our workflow ...
     
  8. RatouPitouMaker

    RatouPitouMaker

    Joined:
    Apr 13, 2020
    Posts:
    1
  9. _watcher_

    _watcher_

    Joined:
    Nov 7, 2014
    Posts:
    261
    Sliced, Tiled of type Multiple/Atlased .. Shader Graph has problems with all of those. The only type i was able to make work was Simple/Single Sprite/Texture combination. Defeats the purpose of having 9sliced or Atlased sprites completely.
     
    Vivien_Lynn, Storm4_ and Carrotpie like this.
  10. TTSGabriel

    TTSGabriel

    Joined:
    Mar 20, 2018
    Posts:
    2
    Hey All,

    I have found a reasonable workaround for this situation that allows you to use a Sprite sheet and still get the desired result.

    Firstly the issue is that the shader is taking into account the entire Textures UV coordinates and not the specific sprite.

    Option 1.
    Like above mentioned you can remove the sprites you want to apply this shader to from the sheet into it's own texture and lose the perfomance benefits of a sprite sheet.

    Option 2.
    • Create a second sprite sheet for the sprites you want this shader applied to.
    • Align each sprite in this sheet Horizontally, where there is only one line of sprites.
    • Ensure each sprite is aligned to the bottom of the sheet.
    • Add some padding to each sprite (Optional).

    This works since the shader uses the the Y UVs of Sprite sheet, which in this new sprite sheets case is the same for all sprites, this gives way to another slight issue see below notes.

    Notes
    • If you have Sprites that vary in height by a lot (e.g Bushes/Grass vs Trees) you will need to seperate these into their own material and adjust the Shader properties
     
    _watcher_ likes this.
  11. InvincibleCat

    InvincibleCat

    Joined:
    Dec 23, 2014
    Posts:
    83
    Is there any update on this?
    I have also submitted a bug 1244783_g924r2g1ockfm7r2

    Thanks
     
  12. mirostraka

    mirostraka

    Joined:
    Feb 17, 2014
    Posts:
    41
    Was there ever a resolution to this?
    We have encountered this issue as well and have already rather large atlases of textures done and in game, so redoing all the textures would be quite a task. is there a way to get sprite uv in shader?
     
    Storm4_ likes this.
  13. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,503
    I just tested today: this is still an issue at least for my test setup.

    SpriteAtlas. Tilemap. The ShaderGraph is very similar to what's in the Lost Crypt demo's tree sway. It seems to treat it like the tree spans the whole y-direction. So when the Atlas is in a shape like this:
    Capture.PNG
    It sways very, very slightly relative to just the tree alone because the sprites are small and there's extra unused atlas space above. It seems to use the Atlas's UVs, not the sprite's.

    edit: if I quadruple the Wind Strength in Play mode only then it seems to behave like before (with no Atlas), but that's hacky and likely will need to change every time that atlas grows.
     
    Last edited: Sep 10, 2020
  14. yuvalko

    yuvalko

    Joined:
    Dec 4, 2018
    Posts:
    2
    Hey, I solved it!

    Context:
    My solution is good only if you have a sprite sheet that is divided into equal parts.
    My character is in a 10 row, 10 column sprite sheet, each 36x36 pixels.
    and I wanted to superimpose another texture on top of it.

    Before the solution, some terminology:
    UV is actually two coordinates, both between 0 and 1 so 0,0 represent one corner and 1,1 represents the other.
    Since shader graph uv is for the whole sprite sheet than 0..1 => 0..360pixel (in my case)

    The Solution:
    The trick is to pass the UV node into a modulo node with x = 1/10 and y = 1/10 (in my case because I had a 10x10 sprites) and then to put it into a sampler UV node.

    Please note:
    it does not look good inside the shader graph as you will see many duplications of the texture.
    However, it works in the game itself just great.

    Hope it helped someone.
     
    Toomblercazz likes this.
  15. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,503
    Toomblercazz and mirostraka like this.
  16. T_Lavell

    T_Lavell

    Joined:
    May 29, 2013
    Posts:
    1
    Ah, sorry if this isn't effective but in another Brackeys video
    @ 9:30 he feeds world/object space to one of the nodes (instead of UV) and it seems to correct the issue of an effect being "stretched" over multiple sprites - even though they are on a single horizontally aligned sprite sheet.
    It's the Get Started with Shader Graph - Dissolve Tutorial video.
     
  17. andiB

    andiB

    Joined:
    Jan 13, 2013
    Posts:
    26
    I tried this but it didn't work for me: https://forum.unity.com/threads/obj...o-work-for-sprite-lit-shader-anymore.1087970/

    It would be nice to get access to the "cropped" uv coordinates the sprite renderer is using when rendering a sprite located in an atlas in order to use it with other textures...
     
    mGMM likes this.
  18. Lepisto3D

    Lepisto3D

    Joined:
    Oct 6, 2019
    Posts:
    27
    Anything new on this? Still annoying to this date. It takes up alot of unecessary space to have to separate the sprites into many different sheets.
     
  19. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,503
    Unity considers this "By Design" so don't expect a change.
     
  20. Crokett

    Crokett

    Joined:
    May 14, 2021
    Posts:
    46
    I am also interested in finding a solution to this problem!!
    I join the thread.
     
  21. antiNT

    antiNT

    Joined:
    Apr 9, 2015
    Posts:
    5
    There is no way you can reasonably expect this to be a choice made 'by design'. We need a fix !
     
  22. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,503
    It makes sense that it's 'by design' because its a consequence of by wiggling based on the UVs of the texture. If you have a texture where 0 to .2 v is occupied by a bush and .5 to 1 v is occupied by a tree then if you're wiggling it based on the V you should expect that this will mess it up for two reasons: (1) bush/tree are in different places in the texture, so 0..1 v no longer represents the bottom and top of each plant, and (2) bush/tree are different sizes, so wiggle intensity needs to vary.

    However, there is a workaround if you're willing to write a more complex shader.

    You can adjust your wind responsiveness by passing the shader values that adjust the v-position and v-responsiveness. In other words, you want to tell the shader "this tree is in the top half of its spritesheet and is tall, whereas this bush is in the bottom quarter and is small." That way, for different-sized plants with different positions within the texture you adjust how the wiggle works.

    The easiest way to do this is by exposing material properties for those values. If you can't do that, then you could adjust the shader to pass along values in some unused parameter. For example, are you using the vertex color? If not, then those values could pass along any arbitrary information. That kind of change is easier w/ a normal shader, not Shader Graph.
     
    Last edited: Sep 5, 2021
    djarcas and lilacsky824 like this.
  23. meebou

    meebou

    Joined:
    Mar 6, 2017
    Posts:
    46
    2021 last post. is there any solution out yet? :-O
     
  24. meebou

    meebou

    Joined:
    Mar 6, 2017
    Posts:
    46
    I tried it but it didnt work. Finally i got it.

    First i have a UV node, then a MODULO node and tell them how many times the sprite fits into sprite sheet, like 10x10 times you write 1/10 X and 1/10 Y. but THEN i added additional TILING AND OFFSET and there put into tiling X,Y 10,10 and then UV into the Sample Texture 2D and now it fits and is correctly sized.
     
    Last edited: Nov 26, 2023