Search Unity

Shader Graph Unlit sprite is Node Position in Object Space still 0 to 1?

Discussion in 'Shaders' started by VenandiVatis, Jan 19, 2020.

  1. VenandiVatis

    VenandiVatis

    Joined:
    Dec 5, 2016
    Posts:
    17
    I am trying to make a gradient for sprites where the colour lerps from the centre colour to the adjacent sprite colours. However, I am still stuck on the first basic step, a simple lerp from top to bottom, but I am starting to suspect is it not me, but it is Unity that is not working correctly.

    Using the 2D Unlit Sprite master, I take the Note Position, in Object space, split for G channel, and using that as T, I lerp from pure Red (1,0,0) to pure Green (0,1,0).
    Like this:
    upload_2020-1-19_9-8-27.png

    As you can see the lerp never reaches Green. Running a test scene, the colours I see on the sprite are like that.
    HOWEVER, if I manually set T to 1, pure green is correctly displayed in the sprite.

    After banging my head on this for many hours, I am starting to suspect the position node is NOT returning values from 0 to 1 for sprites. The tech level for me to be able to debug the shader and read/print the actual values the position node reports is far above my ability.

    Can anyone tell me what I am doing wrong? and/or what are the expected values of the Position Node in object space?

    Thank you,
    V
     
  2. VenandiVatis

    VenandiVatis

    Joined:
    Dec 5, 2016
    Posts:
    17
    I think I can confirm the values Position node reports for Sprites are indeed different from 3D objects.
    Dropped a Cylinder next to the sprite and used the same shader.
    The bottom and top squares are the sprites I use just to reference the base colours (they use normal sprite shader).
    The middle square is the sprite using my shader, I wanted it to behave like the cylinder.

    upload_2020-1-19_9-21-12.png
     
  3. VenandiVatis

    VenandiVatis

    Joined:
    Dec 5, 2016
    Posts:
    17
    I should have posted this question sooner, as rereading it an idea on how to test this pop-up! :D :D:)
    I think I can confirm, for sprites, the position node is reporting -0.5 to 0.5 instead of 0 to 1. I have no idea why the scale is mode 0.5 down, but adding 0.5 to the output of the node breaks the 3D objects, but the sprite lerps correctly.

    upload_2020-1-19_9-33-14.png
    upload_2020-1-19_9-33-34.png
    As you can see, you can no longer see the border between the three sprites.

    V
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    The issue is sprites don’t have an “object space”. The rendered mesh used by sprites is always generated in world space. If you flip the position node between Object and World you won’t see it change for sprites, but it will change for regular meshes (as long as they’re not being statically or dynamically batched, in which case they too are rendering meshes already in world space). So your center sprite, because it’s at the world origin, and is presumably 1 unit tall, has a y position range of -0.5 to 0.5, since the bottom vertices are 0.5 units below the world origin.

    The cylinder is different. It’s 2 units tall, and you’re scaling it down to 50% on its y axis so it’s the same height as your 1 unit tall sprite. It’s range is -1 to 1 since it does have an object space that’s not just the same as world space. Put two cylinders in there and that might change as Unity might batch them!

    The short version is you don’t want to use position for this. You want to use UVs. UVs are generally going to be in a 0.0 to 1.0 range for a sprite renderer mesh using a non-atlased sprite. With an atlased sprite all bets are off as the UVs are where ever that sprite is in the atlas.
     
    akasabutski, zelderus and bobbaluba like this.
  5. VenandiVatis

    VenandiVatis

    Joined:
    Dec 5, 2016
    Posts:
    17
    Hi bgolus, thank you for your reply. But I do see differences between object and world space, and none of the objects is at the origin.

    If I have a single sprite on the scene, not at world origin, switching the node position in the shader graph between wold and object space, will display different results. In object space, it will lerp from top/bottom of the object and not the world, in the world, it will lerp considering the full world view. In other words, it will works as I would expect.
    It is only when a second sprite (using the same material) is introduced in the scene that it stops working in object space.

    Also, in the images I showed, all objects are away from world origin. Scene (0,0,0) is actually off-screen a bit to the left/bottom.

    The cylinder I showed, is indeed scaled down. However, I have the same result in another scene, where I have multiple cylinders and sprite (some cylinders scaled-down, some scaled-up, and one at 1 scale).

    So far, I fixed the issue just by adding 0.5 to all position nodes outputs. That produced the expected result in all sprites in my real scene (currently, just 20 sprites scattered around the scene).
    My sprites do not use an atlas, as all I care is their colour, so I will try to learn a bit more on UV topic, thanks.

    V
     
  6. VenandiVatis

    VenandiVatis

    Joined:
    Dec 5, 2016
    Posts:
    17