Search Unity

How to control values in shader globally?

Discussion in 'Scripting' started by monkeybrother, Sep 13, 2011.

  1. monkeybrother

    monkeybrother

    Joined:
    Oct 14, 2010
    Posts:
    83
    Hi. I have made a shader in Strumpy's shader editor and use it on a large object (an arena) that has hundreds of individual pieces (meshes), each with it's own material but with the same shader. Now, if I want to change let's say the specular value on all these pieces that use the same shader, is there a simple way of doing that?
    It doesn't really matter if it's something in the editor or if you can do it with a script, I just don't want to select every piece individually and change the value by hand. Any help appreciated!
     
  2. Nemox

    Nemox

    Joined:
    Feb 16, 2010
    Posts:
    396
    I'm not sure if it's applicable to your particular setup, but you could perhaps combine all the smaller objects into one larger object with a texture atlas, so all of them can use the same material. At that point, you can just change that single material.

    Otherwise, you'd need to keep a list of all the objects in script, and use a loop to change the specular value of each one.
     
  3. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    Nith666 and Alekxss like this.
  4. monkeybrother

    monkeybrother

    Joined:
    Oct 14, 2010
    Posts:
    83
    Thanks for your answers.

    ivkoni: That looks like exactly what I need, but even though I'm pretty certain I've done it right, it just doesn't change anything.

    Here's the line in the shader:
    and this is what I'm using in Startup() in the main controller script:
     
  5. monkeybrother

    monkeybrother

    Joined:
    Oct 14, 2010
    Posts:
    83
    Nevermind. I searched the forums and found that the values you want to changed can't be exposed in the shader. Unfortunately you can't not expose values in Strumpy's (?), but at least it works if you edit by hand.

    Thanks again.
     
  6. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    If you're using global variables, you don't define it as a property in the shader. You simply define the value like you would any other builtin value.

    i.e. inside your shader where you declare your other inputs (sampler2D, float4, etc...) you write;
    Code (csharp):
    1. float _BlowOut;
    And the shader will know that it's to use the globally defined float called _BlowOut automatically.
     
    TheMemorius, Henry_Sun, jq911 and 2 others like this.