Search Unity

Question Why Shader Global Variables are not compatibale between Shader.SetGlobal and Material.set?

Discussion in 'Shaders' started by elfstar, Dec 20, 2022.

  1. elfstar

    elfstar

    Joined:
    Aug 24, 2021
    Posts:
    2
    Hi all! i have my shader applying in different gameplay scenario which requires some shader properties to be used as global values and at the same time as local values. The problem is that after applying Shader.SetGlobal(_propertyA, value), it will not work well when i then apply Material.Set(_propertyA, value). i am wondering if these two thethods are compatible when they modify the same global shader property value. Does anybody encounter the same problem? Shoud i use different shader properties for Shader.SetGlobal and Material.Set? THANK YOU
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    The code first checks the data in the material, then globals.
    And no, there's currently no way to "unset" the value on a material. As soon as you set the value on the material, you have to update it every time you change the global value.
     
  3. elfstar

    elfstar

    Joined:
    Aug 24, 2021
    Posts:
    2
    ok. i am not quite understand "As soon as you set the value on the material, you have to update it every time you change the global value." Do you mean if i call Material.Set(_propertyA, value), i should also call Shader.SetGlobal(_propertyA, value) afterwards to change the value of "_propertyA"? Are they sharing the same CBuffer if i define "_propertyA" out of UnityPerMaterial?
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    He means if you do material.Set(_propertyA, value1) at some point, and then later call Shader.SetGlobal(_propertyA, value2), that specific material will still have "value1". If you want it to have the same as the global, you'll need to call Set on the material.

    So it's best to decide if a material value should always be global or per-material from the beginning so you don't run into issues. If it should always be shared between materials, then do not make it a property in the shader Properties{} section and avoid calling Set for it on an individual material.
     
    aleksandrk likes this.