Search Unity

Question Shader Graph - object position node is wrong on multiple objects

Discussion in 'Shader Graph' started by cemleme, Sep 14, 2019.

  1. cemleme

    cemleme

    Joined:
    Mar 24, 2017
    Posts:
    30
    Hello everyone,

    I am experimenting on a shader and struggling as I am not very familiar with the shaders.

    Using shader graph on LWRP

    What I am trying to do is very simple. I am using the object node the get the world position of the object to displace the whole mesh upwards according to the mesh's position.

    The thing is it works when I create a single mesh with the material but when there are more than 1 object, the object node starts to give exact same position for all objects (or maybe their average position?).

    The workaround is disabling the dynamic batching on LWRP settings. then it works as intended. here is a very simple scene view of the working system




    soo finally my question:

    1) is it worth disabling the dynamic batching? do I lose too much?

    2) is it possible to disable dynamic batching on only 1 shader using shader graph? and again is it even worth for single shading?

    3) is there any other way to handle this? (I can create a shader variable and set it on awake to use as the object position node)

    I didn't put a video to show how it's not working with dynamic batching but basically all of the tiles, wherever they are have exact same displacement as they all have the same data from the object node.

    Thanks
     
  2. Pixelome

    Pixelome

    Joined:
    Sep 30, 2014
    Posts:
    11
    I had a similar issue, and solved it by assigning a new material (with the same shader) to each object. You can manually duplicate the material, or programmatically
    new Material(yourMaterial);
    .

    Although I would also like to know if there is more elegant solution.
     
  3. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    For normal shaders there is a DisableBatching tag than can say that a shader is not available for batching, but i have not seen a way to enable it in the shadergraph.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Disable dynamic batching and enable the SRP batcher instead.
     
    BlockValue likes this.
  5. Pixelome

    Pixelome

    Joined:
    Sep 30, 2014
    Posts:
    11
    @bgolus in my case dynamic batching was already disabled and SRP batching enabled. I even disabled static batching just to check. I'm still on 2019.2.8f1 so the behavior might differ in a newer version.

    In the end I went along with copying the material, I won't have more than 1-2 instances at most times so I'm not worried. But it would be nice to find a better solution.
     
  6. eoy

    eoy

    Joined:
    Jul 8, 2017
    Posts:
    1
    I have the same issue as described above. Is there any kind of workaround using a custom function node?
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Nope. Support has to be added at a much higher level. The only solution right now is to not use Shader Graph, or to hand modify the generated shader code from Shader Graph.
     
  8. coltonm

    coltonm

    Joined:
    Nov 7, 2016
    Posts:
    4
    Hey @bgolus, if I were willing to learn to do this without Shader Graph, where would you recommend I start?

    thanks
     
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Define "do this".
    If it's "fix the bug described in this thread", then AFAIK it's already fixed.
    If it's "write shaders for SRPs", please don't ask off topic questions in existing threads.
     
  10. coltonm

    coltonm

    Joined:
    Nov 7, 2016
    Posts:
    4
    Ah right, I meant to say solve this bug via shader code rather than resorting to multiple, identical materials.
    I'll consider my question out of scope then, cheers and thanks for your time.
     
  11. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    If the issue still exists when using Shader Graph, then it's something that should be reported as still being a problem, assuming you've already disabled dynamic and static batching.

    If you want to fix the issue by modifying the shader code manually, then it would depend on what the problem is that's causing it. I do not personally remember what the specific cause of the issue was to be honest, but I vaguely remember it being something surprisingly nasty, like the matrices used to calculate world space being bogus while the vertex shader section of the Shader Graph code was running, and then set correctly just afterwards. But I could be wrong, it's been almost a year and I don't use the SRPs with any regularity.
     
  12. d2clon

    d2clon

    Joined:
    Aug 19, 2017
    Posts:
    19
    The only solution I have found for my self is duplicating the Material in runtime using this script:

    Code (CSharp):
    1.  
    2. public class MaterialDuplicator : MonoBehaviour
    3. {
    4.   void Awake()
    5.   {
    6.     Renderer renderer = gameObject.GetComponent<Renderer>();
    7.     renderer.material = new Material(renderer.material);
    8.   }
    9. }
    10.  
    11.  
    Attach this script to the Object with the Renderer
     
    Last edited: Jul 5, 2021
  13. Bovine

    Bovine

    Joined:
    Oct 13, 2010
    Posts:
    195
    Interesting. It was only happening in play mode but I am guessing the objects are marked static and being batched resulting in bogus object space.

    Thanks for the thread. I guess for draw calls this could get expensive for large numbers of objects...
     
    james_co likes this.