Search Unity

Bug Shader UV Node: values outside of [(0,0), (1,1)]??

Discussion in 'Shader Graph' started by FOXAcemond, Nov 23, 2020.

?

Does the Shader Graph UV node return anything outside of [(0, 0), (1, 1)]?

  1. Yes

  2. No, looks like it's a bug.

Results are only viewable after voting.
  1. FOXAcemond

    FOXAcemond

    Joined:
    Jan 23, 2015
    Posts:
    99
    I've spent hours trying to pinpoint what goddamn pixel was creating a NaN value f*cking up my rendering.

    I noticed I came from one of my (absurdly simple) shader graph which was manipulating UV of a line renderer. Like doing a Pow (if given a negative value) on unmodified UV coordinates, so bounded between 0 and 1, right?

    I just added a clamp01 node at the exit of the UV X coordinate that I was modifying and poof. No more NaN value.

    What the hell? Does the UV node return anything outside of [(0,0), (1,1)]?
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Of course you want to have UV values in other than 0-1 range? If you couldn't scale UVs like you want, it would prevent making all sorts of interesting effects which utilize UVs. Many other value types go outside 0-1 range, too. And in many cases that's useful too.
     
  3. FOXAcemond

    FOXAcemond

    Joined:
    Jan 23, 2015
    Posts:
    99
    I don't understand. Where is supposed to be the (-0.5,-0.5) on the UV map returned by the UV node?

    Ain't UV maps looping? Meaning that the (1.5, 1.5) point, for example, is in fact the (0.5, 0.5) point.

    In my understanding, if you want to stretch your texture for instance, you define the (1, 1) point further on the mesh.

    Reading the Wikipedia page on UV coordinates : https://en.wikipedia.org/wiki/UV_mapping
    I see "UV coordinates in the range [0, 1] can then be calculated as follows". Not a general definition, but that hints to me that the range is still [0, 1].

    Last word, to clarify my interrogation, of course you can map your mesh to any UV value. But what does a negative UV value mean from the Shader Graph UV Node?
     
    Last edited: Nov 23, 2020
    ENDAS_ORIGINAL likes this.
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Well. You can scale UVs up and extract values for specific use-cases ...etc. I didn't mean that you would use negative or over 1 values as such.
     
  5. FOXAcemond

    FOXAcemond

    Joined:
    Jan 23, 2015
    Posts:
    99
    Yeah you can do whatever you want with them. But my question is specifically about the Shader Graph UV Node. What does it mean when it gives a value outside of the [0, 1] bounds.
     
  6. Oxeren

    Oxeren

    Joined:
    Aug 14, 2013
    Posts:
    121
    UV coordinate is just a vector that is associated with a vertex, it does not even necessarily has to do anything with texturing (though it usually does). When unwrapping a model in a 3d editor you can definitely assign vertices UV coordinates outside of [0, 1], and it can have it's uses. And UVs can also store other information that has nothing to do with sampling textures, thus you may need access to unclamped values.
     
    bgolus and Olmi like this.
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Here's some simple examples of a common use cases where the UVs would be outside of the 0.0 to 1.0 range.

    Lets say you have a repeating texture and you have a wall mesh that's really long. If you were to keep the UVs between 0.0 and 1.0, then you couldn't have the texture repeat multiple times across the length of the object unless you cut up the geometry exactly on the texture edges. So instead when setting up the UVs for a mesh, the artist will just scale the UVs outside of the 0.0 to 1.0 range so that it appears at the scale & repetition they want. There's no reason to keep that even > 0.0, so the artist might center it so the horizontal goes from -3.0 to +5.0, and might shift the vertical offset to better match the geometry to the texture. Say there's a line of stones that they want to have be at the top of the wall, but appears mid texture. Then the top of the UV might be somewhere mid-way up the texture, and the bottom edge slightly negative.

    There are also cases where having a mesh's UVs outside of the 0.0 to 1.0 range allows for more efficient texture packing, even when not using a traditionally repeating texture.

    And as @Oxeren noted, UVs aren't even always used for textures. It's arbitrary floating point values. So for some vfx or foliage shaders the UVs might be used to store information about mesh pivots, or a second vertex color, or per vertex masks, or a pre-assigned random number, etc. Unity's Particle System can pass to the shader all sorts of data, almost all of which just gets dumped into arbitrary UV components.

    Either way the UV node should always return the data that's in the mesh. You do not want it to clamp between 0.0 and 1.0, because so often you do actually need UVs outside that range. If you need the data to be clamped, then make sure the UVs your mesh has don't go outside that range, and enforce it with a Clamp01 node.
     
    a52 and Olmi like this.
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Also, it should be noted, with a mesh that has UV's perfectly in the 0.0 to 1.0 range, it's still possible for the UV node to return values outside of that when using MSAA! The reason for this is more complicated than I want to go into here, but suffice to say it's intended behavior that is the correct one most of the time. Again, if you want to enforce a limited range, use a Clamp01 node.
     
  9. FOXAcemond

    FOXAcemond

    Joined:
    Jan 23, 2015
    Posts:
    99
    Yes, that's what I was looking for. I know you can map any UV value on a mesh. What I was wondering was about Unity UV's node. I didn't understand why I should have clamped it. Thanks for the detailed answer, even without the gory details ;)