Accessing Standard Shader Values?

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!

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();
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 :slight_smile:

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 :slight_smile:
But thanks for the rest – that looks about right!

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.

            _lightMat = render.material;
            _lightMatIntensity = _lightMat.GetFloat("_EmissionScaleUI");
            var emissionMap =  _lightMat.GetTexture("_EmissionMap");
            if (emissionMap != null) {
                _lightMatColor = _lightMat.GetColor("_EmissionColorWithMapUI");
            }
            else {
                _lightMatColor = _lightMat.GetColor("_EmissionColorUI");
            }

//and then later 
private void SetIntensity(float amount) {
            _lightMat.SetColor("_EmissionColor", EvalFinalEmissionColor(_lightMatIntensity * amount));
}

    Color EvalFinalEmissionColor(float emissionScale) {
        if (emissionScale < 0.0f) {
            emissionScale = 0.0f;
        }
        return _lightMatColor * Mathf.LinearToGammaSpace(emissionScale);
    }

I’m using it so the emission matches the light flicker.

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.

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.

1 Like

Cool, this code works for pulsing the color

GetComponent(Renderer).material.SetColor("_EmissionColor", Color(currentValue,0,currentValue,1));

(I’m pulsing black [no emission] to pink)

1 Like

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.

I can provide screen captures if it’s necessary.

@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.

1 Like