Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Accessing Standard Shader Values?

Discussion in 'Unity 5 Pre-order Beta' started by infinitypbr, Oct 30, 2014.

  1. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Sorry for the stupid question, but how does one access the standard shader values? I'd like to adjust the emission of a material to make it pulse, but I'm unsure how to actually access the value via code.

    Thanks!
     
  2. ThreadLok

    ThreadLok

    Joined:
    Nov 8, 2012
    Posts:
    5
    I've been having the same problem. I found that it's a lot more code to get down to the various materials now:

    Renderer rend = goModel.GetComponent<Renderer>();
    Material matModel = rend.material;

    I dug around to find the shader code and found the variable used for emissive color, so now you can get and set the material's emissive colors with

    matModel.SetColor( "_EmissionColor", Color.black );

    But I can't find a way to get to the value in the GUI for emission itself. That's a lot to get a pulse to animate, which is also what I was trying for coincidentally :)
     
  3. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    I'll guess "_EmissionScaleUI" float will do the trick. That's listed in the shader.

    I'll check it out later, since I'm back in 4.6 working on the real project instead of playing around :)
    But thanks for the rest -- that looks about right!
     
  4. Silly_Rollo

    Silly_Rollo

    Joined:
    Dec 21, 2012
    Posts:
    501
    Actually ScaleUI is only the number in the inspector. It doesn't affect the actual emission if you set it directly (or at least it didn't work for me)

    This is pretty much the code ripped from the shader source I used in my script with the wonky formatting that always happens on this forum.
    Code (csharp):
    1.  
    2.             _lightMat = render.material;
    3.             _lightMatIntensity = _lightMat.GetFloat("_EmissionScaleUI");
    4.             var emissionMap =  _lightMat.GetTexture("_EmissionMap");
    5.             if (emissionMap != null) {
    6.                 _lightMatColor = _lightMat.GetColor("_EmissionColorWithMapUI");
    7.             }
    8.             else {
    9.                 _lightMatColor = _lightMat.GetColor("_EmissionColorUI");
    10.             }
    11.  
    12. //and then later
    13. private void SetIntensity(float amount) {
    14.             _lightMat.SetColor("_EmissionColor", EvalFinalEmissionColor(_lightMatIntensity * amount));
    15. }
    16.  
    17.     Color EvalFinalEmissionColor(float emissionScale) {
    18.         if (emissionScale < 0.0f) {
    19.             emissionScale = 0.0f;
    20.         }
    21.         return _lightMatColor * Mathf.LinearToGammaSpace(emissionScale);
    22.     }
    23.  
    24.  
    I'm using it so the emission matches the light flicker.
     
  5. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    If you have your material selected and click on "edit" next to "Standard Shader", you will get first to a page with all value names, you don't have to dig in the source.
     
  6. Kuba

    Kuba

    Unity Technologies

    Joined:
    Jan 13, 2009
    Posts:
    416
    Properties with the UI postfix in the name are just used to make the UI nicer when a custom material editor is used (which is the case for the Standard shader). What is actually used in the shader is e.g. "_EmissionColor".

    If a custom material editor is not used (say, you duplicated the shader and removed the line that specifies the material editor) then all the properties with the UI postfix are hidden and you get a more bare bones access to the inputs.
     
    ThreadLok likes this.
  7. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Cool, this code works for pulsing the color

    Code (csharp):
    1. GetComponent(Renderer).material.SetColor("_EmissionColor", Color(currentValue,0,currentValue,1));
    (I'm pulsing black [no emission] to pink)
     
    ThreadLok likes this.
  8. cAyouMontreal

    cAyouMontreal

    Joined:
    Jun 30, 2011
    Posts:
    315
    Hey there, I'm tweening the _EmissionColor too, but it seems when I apply the new color the shader is losing it's own emissive value (the object is not glowing anymore, but it still has the new color). But once I pause my game and go back to the shader and just barely change a value in the shader editor, like the emission value, the glow is back. What can I do to fix this? Maybe a function to call to force refreshing the emissive value? Thanks.

    Edit: Using b14 currently.
     
    Last edited: Dec 11, 2014
  9. cAyouMontreal

    cAyouMontreal

    Joined:
    Jun 30, 2011
    Posts:
    315
    I can provide screen captures if it's necessary.
     
  10. nickpettit

    nickpettit

    Joined:
    Dec 31, 2013
    Posts:
    33
    @Kuba Can you elaborate on your explanation? I'm trying to write code that will boost the emissive power of a material using the Standard Shader, but I can only seem to change the color. I'm guessing there's a way for me to change the color in such a way that boosts the power (like the slider does in the UI) but I can't seem to figure it out.
     
    Whippets likes this.