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

Question UV offset problem

Discussion in 'Shader Graph' started by Gorynych_1, Feb 7, 2021.

  1. Gorynych_1

    Gorynych_1

    Joined:
    Dec 12, 2016
    Posts:
    2
    Hello!
    I have two question.
    1) When I use the "tilling and offset" node, strange artifacts appear on the texture. Here they are:

    Screenshot_43.png

    Here is the original texture:
    Sprite-11.png

    Can someone explain to me what I'm doing wrong?

    2) I want to scale the original texture of the sprite by the position of the vertex and apply an additional texture of a smaller size to it. Something like this:
    Screenshot_45.png

    Is there a way to achieve this?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    When textures are being drawn outside of the starting 0.0 to 1.0 UV range there are a handful of behaviors that are can be chosen from using the Wrap Mode setting on the texture asset. The default option for non-sprite textures is Repeat, which just means the texture is repeated over and over again. The default option for sprite textures is Clamp, which uses whatever color value is on the texture's edge. There's also Mirror, which is like Repeat but the texture is flipped on each repeat, and Mirror Once, which mirrors for negative UVs, then Clamps outside of +/-1.0 UV.

    Your texture is drawn right up to the edges of the texture, and as it is a sprite is defaulting to Clamp. Thus you're going to get that stretched appearance. This (well hidden) page from the Unity documentation shows the same artifacts when using clamp, as well as what the other options look like.
    https://docs.unity3d.com/Manual/SL-SamplerStates.html

    (clamp, repeat, mirror, mirror once, and using clamp & repeat on separate axis)

    Note there are no options for having areas outside of the 0.0 to 1.0 range be a solid color or transparent, which is probably what you were expecting to happen. When rendering a sprite normally w/o any UV scaling or offsets the mesh it's being drawn by is itself preventing the texture from being seen outside of that UV range, so it's usually not an issue.

    There are two solutions to this.

    One is to make your texture at least one pixel wider in all directions and make sure it's completely transparent for those pixels. Also make sure you've checked "Border Mip Maps" in that texture asset's import settings to ensure the mip maps keep that transparent edge. One fallout of this is if you really do need mip maps (like if you plan on drawing this object really small on screen), in the smaller mip maps the forced transparent border might cause the texture to visually shrink and even disappear.

    The second option is to use a mask of some kind so only the area inside the 0.0 to 1.0 UV range is visible. Unity's built in procedural Rectangle node is perfect for this. Pass the Tiling and Offset node's UV output to a Rectangle node with the width and height set to 1.0. Then you'll need to use a Split node to get just the alpha from the texture and multiply it by the Rectangle node output. And a Vector4 node to combine it all back together.

    Sure ... by calculating the appropriate scale and offset you'd need to get the effect you want in C# and setting that on the material that sprite uses. You probably wanted a pure shader based solution, of which there isn't one. At least not a guaranteed way that doesn't already basically force you to need unique materials per sprite anyway. If you have a single sprite by itself you can plausibly extract its pivot position and scale from the Model matrix. As soon as you have two of the same sprite, Unity will batch them together into a single mesh so you loose any information about the individual sprites' original transforms. You can force Unity not to batch sprites together by assigning unique materials to each sprite ... but at that point you could also just assign unique material properties which will likely be easier than trying to extract the info you need from the matrices.
     
  3. Gorynych_1

    Gorynych_1

    Joined:
    Dec 12, 2016
    Posts:
    2
    Thank you very much, bgolus!
    This is very useful information! I could not find information on this either by pictures or by requests.
    Thanks for your time.