Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

[PerRendererData] & Shader Graph

Discussion in 'Graphics Experimental Previews' started by Kellyrayj, Feb 7, 2019.

  1. Kellyrayj

    Kellyrayj

    Joined:
    Aug 29, 2011
    Posts:
    936
    hi there,

    I was curious if shader graph shaders have a way to use the set material blocks stuff?

    I’m a bit unsure of what even to google. This is the closest thing I think that describes what I’m looking for.

    https://thomasmountainborn.com/2016/05/25/materialpropertyblocks/

    I’ve got an shader dissolve that happens on the death of an enemy. But if two enemies are on screen and I adjust the transition property, both dissolve. What should I be looking for to solve this?

    Thanks!
     
  2. Kellyrayj

    Kellyrayj

    Joined:
    Aug 29, 2011
    Posts:
    936
    Sort of solved my own problem. I accessed the set float through script and it worked on an individual basis. Though I'd still be curious if the material property blocks are in the game for this behavior.
     
  3. TheHeftyCoder

    TheHeftyCoder

    Joined:
    Oct 29, 2016
    Posts:
    91
    A little late to the part, but by accessing the set float through script, you probably mean you accessed the material itself. When you do that, you essentially create a new copy of the material, which will be bad for your performance, if there are a lot of enemies using that original material.
     
    JotaRata likes this.
  4. An3Apps

    An3Apps

    Joined:
    Nov 22, 2017
    Posts:
    8
    I know it's late, but do you know how I could achieve the same thing without accessing the material through script. Maybe through animation?
     
  5. TheHeftyCoder

    TheHeftyCoder

    Joined:
    Oct 29, 2016
    Posts:
    91
    Accessing the material through an animation clip / animator will instantiate the material. It's the same thing as doing renderer.material. You'll essentially get the same material.

    There is no way to achieve this without a script. An example of a script that does not instantiate a material is this:

    Code (CSharp):
    1.     [RequireComponent(typeof(Renderer))]
    2.     public class MaterialPropertyChanger : MonoBehaviour
    3.     {
    4.         [SerializeField] private Color color;
    5.         [SerializeField] private string nameOfMaterialProperty;
    6.         private new Renderer renderer;
    7.         private MaterialPropertyBlock mBlock;
    8.         private void Awake()
    9.         {
    10.             renderer = GetComponent<Renderer>();
    11.             mBlock = new MaterialPropertyBlock();
    12.             SetColor(color);
    13.         }
    14.  
    15.         public Color Color
    16.         {
    17.             get => color;
    18.             set
    19.             {
    20.                 SetColor(value);
    21.                 color = value;
    22.             }
    23.         }
    24.         private void SetColor(Color color)
    25.         {
    26.             renderer.GetPropertyBlock(mBlock);
    27.             mBlock.SetColor(nameOfMaterialProperty,color);
    28.             renderer.SetPropertyBlock(mBlock);
    29.         }
    30.     }
    Beware that animating the color field won't make the animation happen (if I remember correctly). To do that without instantiating the material, you need to make your own way of setting the color through the Color property over a period of time. That can be done either completely from scratch (through Update, IEnumerator or Playbles API) or by using the Timeline (extension of Playbles API). Frankly, it's too much to explain here, but I hope I helped you a bit.