Search Unity

Resolved Sprite comes out stretched. Looks fine in preview.

Discussion in 'Shader Graph' started by Josiah_Ironclad, Aug 13, 2021.

  1. Josiah_Ironclad

    Josiah_Ironclad

    Joined:
    Sep 24, 2019
    Posts:
    156
    I'm working on a sprite shader for my project's entities, and since I'd like to keep the benefits of a spritesheet (convenience among other things), I'm trying to do some UV magic to switch which entity is displayed based on a given value in the materials.

    This is my spritesheet currently; it's 32x16 pixels:
    upload_2021-8-13_22-23-47.png

    I tried two methods; One by just using the Flipbook node, and the other I found in this kind of unrelated tutorial.

    Flipbook method:
    upload_2021-8-13_22-18-34.png

    Tutorial method:
    upload_2021-8-13_22-19-19.png
    "Spritesheet Position" is a Vector2 of (0, 0) for the slime, and (1, 0) for the skeleton dude.
    "Spritesheet Size" is also a Vector2, and is constant across materials, (2, 1).

    The rest of my shader doesn't do anything other than mess with the texture's colors. And as you can see, it looks perfectly fine in the preview image. But inside the scene...
    upload_2021-8-13_22-27-45.png (just fyi, this is 3 separate entity objects)

    I have been fiddling with quite a few things, like the texture's wrap mode in import settings, the Sprite Renderer's various settings inside the scene, the shader itself (hence why I tried two methods).

    A workaround I found is setting the spritesheet size to (1, 1), and the position to (0, 0) for the slime, and (0.5, 0) for the skeleton. But this seems impractical/unscalable at best, considering the bigger the spritesheet gets, the smaller the appropriate values are gonna get, and I'm gonna have to edit them all whenever I add or remove a row or column in the spritesheet image file.
    The same workaround doesn't work with the Flipbook node, as changing the size to 1x1 just makes it impossible to switch to the skeleton.

    Additionally, I looked up tutorials specifically implementing the aforementioned tutorial's solution, and both their preview and actual game look fine, but my image gets stretched unless I do the wacky workaround.
     
    Last edited: Aug 13, 2021
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    The Sprite renderer automatically adjust the UVs on the mesh the sprite is rendered with to limit itself to the sprite you assign. There's no need to adjust the UV yourself, and if you do want to do that so you can do something like a flip book animation in the shader, you'd want to not use a Sprite Renderer, or not assign any Sprite to the Sprite Renderer so the UVs aren't modified.
     
    Josiah_Ironclad likes this.
  3. Josiah_Ironclad

    Josiah_Ironclad

    Joined:
    Sep 24, 2019
    Posts:
    156
    I just switched to a Mesh Renderer and a quad for the Filter, and it looks fine now.

    Though since I've always used Sprites for 2D stuff, I am a bit hesitant to switch to quads. Since I'm already needing to alter Z position for other stuff, needing to do so for render ordering isn't that big of a deal for me. I'm just so unfamiliar with quads and I'll probably have to do quite a bit of research later on regarding stuff like optimization and such.

    I'll give this some extra thought and research tomorrow (it's late), and if I decide to stick with quads, I'll change the thread prefix to "Resolved". :)


    Also a note. I'm not really trying to do animation. It's just that Shader Graph doesn't accept a sprite of type "Multiple", so the texture you pass in is the entire spritesheet, so I had to do some UV altering myself. And without the Sprite field set to something, the sprite renderer just doesn't render anything.
     
    Last edited: Aug 13, 2021