Search Unity

Question Change values of shader at runtime?

Discussion in 'Shader Graph' started by MrVSM, Jul 16, 2019.

  1. MrVSM

    MrVSM

    Joined:
    Mar 24, 2019
    Posts:
    12
    Hi, I am looking for a way to during runtime change values of a shader graph shader, specifically enabling emission (or same effect could be achieved by changing value of a mix or color node?) My idea is to

    void OnTriggerEnter(Collider col)
    {
    // enable/disable emission here?
    }

    so that in-game, when a character is within proximity to an object, that object glows.

    With materials I could do something like

    gameObject.GetComponent<Renderer>().material.color = hoverColor;

    but I'd like to do more than just change a specific material, it'd be nice to get a method that could be used with multiple objects in game without making specific materials for each.
     
    AntonioModer likes this.
  2. MrVSM

    MrVSM

    Joined:
    Mar 24, 2019
    Posts:
    12
    For some clarification - here's my current code that works

    public class Currency : MonoBehaviour
    {
    public Material hoverColor;
    public GameObject campfire;
    public Material startColor;


    void OnTriggerEnter(Collider col)
    {
    if(col.gameObject.tag == "Campfire")
    {
    campfire.GetComponent<Renderer>().material = hoverColor;
    }
    }

    void OnTriggerExit(Collider col)
    {
    campfire.GetComponent<Renderer>().material = startColor;
    }

    }


    this code swaps the material between startColor and hoverColor depending on how close the player is to the object. What I am looking for is a way to have just one shader, and enable/disable the emission node section somehow.

    Thanks for the help, it is greatly appreciated.
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Check out the documentation for the ".color" property.
    https://docs.unity3d.com/ScriptReference/Material-color.html

    Specifically this bit here:
    The property name used by SetColor when using Shader Graph is a property's reference string, which you can set by expanding a property in the blackboard. By default Shader Graph generates a random string of gobbledygook for the reference, which you can use, or you can rename it to something sane. Then it's just a matter of using material.SetColor("_MyPropertyReferenceName", myColor); to override the material's color.

    Note: when using renderer.material for anything you are actually generating a new material at runtime.
    https://docs.unity3d.com/ScriptReference/Renderer-material.html
    A better way of modifying material properties per-renderer is to use a MaterialPropertyBlock. Using that with Renderer.SetPropertyBlock() will let you temporarily override a material's properties without generating a new material. You can set it back to the original settings by clearing the block and calling SetPropertyBlock() again with the cleared block, or just call rend.SetPropertyBlock(null).
     
    AntonioModer likes this.
  4. zo-digital

    zo-digital

    Joined:
    Dec 20, 2021
    Posts:
    4
    Maybe a bit late, but I changed values of a shader at runtime through
    a) exposed property in shader graph
    b) through a script and changed the value (with or without instantiation of the material)
     
  5. Shapely

    Shapely

    Joined:
    Jun 6, 2020
    Posts:
    7
    Any way to add properties to a shader graph at runtime? I want to add vec3s for positions for the shader to react to and need to have the number of them extensible.
     
  6. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,314
    You are necroing thread with rather offtopic questions.
    No, you can use array or buffer instead, but you need to write custom function as this is not supported out of the box in SG.