Search Unity

How to get Transform.position in Shader Graph?

Discussion in 'Shaders' started by BasicallyGames, Apr 25, 2020.

  1. BasicallyGames

    BasicallyGames

    Joined:
    Aug 31, 2018
    Posts:
    91
    So I'm working on a shader that gets different light values based on position. For 3D objects the position node works fine as I want the all portions of the object to reflect the light value of whatever position they are at. However, I also have 2D objects which I want to be lit based solely on the transform position. If I use my current shader on one of these sprites and half of it is within a lit position and half within an unlit position, half will be lit and half will be unlit. However, I just want to determine the brightness of the entire sprite based on one point. I tried using the object node's position output, but that gave the same result as the position node. I just need some way to get the transform, sprite anchor, or center position of the object, but for some reason I can't seem to find any way to do this or any answers online, despite how simple it should be. Anyone know how to do this?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,350
    Nope, not possible.

    A single sprite by itself will work just like a normal 3D mesh.

    Multiple sprites using the same texture (either single sprite or atlas depending on your setup) are rendered as batched meshes, meaning all sprites using the same sprite & material are merged into a single large mesh with its pivot at the world origin.

    If you need a pivot per mesh, you can't use sprite renderers, and you need to make sure you've disabled dynamic batching as that'll also merge multiple simple meshes using the same material into one.
     
  3. BasicallyGames

    BasicallyGames

    Joined:
    Aug 31, 2018
    Posts:
    91
    Ugh, I'm so stupid... Started working this morning, and realized I hadn't actually assigned my sprite shader graph to my sprite material. Switched it over and turns out my shader actually works perfectly... Which is weird because you're implying it shouldn't but, well, I'm not going to complain, haha.

    Thanks for the answer and sorry for wasting your time...