Standard Shader Emission control via Script

Hello,
I wanted to try out Unity5 Standard Shader feature to re-create one of my custom shaders.

It is basically an emitting shader with an alpha parameter to control the amount of emission at runtime.

Is it possible to control the emission of the standard shader at runtime?

Hi Luca,

I was finally able to get this to work after about an hour and a half of research.

Here is my code:

void Update () {
		Renderer renderer = GetComponent<Renderer> ();
		Material mat = renderer.material;

		float emission = Mathf.PingPong (Time.time, 1.0f);
		Color baseColor = Color.yellow; //Replace this with whatever you want for your base color at emission level '1'

		Color finalColor = baseColor * Mathf.LinearToGammaSpace (emission);

		mat.SetColor ("_EmissionColor", finalColor);
	}

This should be all you need.

If you want to oscillate between something other than 0 and your ceiling (1.0 in this case) just declare emission as such:

float floor = 0.3f;
float ceiling = 1.0f;
float emission = floor + Mathf.PingPong (Time.time, ceiling - floor);

If you want to slow down or speed up the oscillation, just multiply Time.time by a constant.
Multiplying Time.time by a constant greater than 1 will speed up the oscillation.
Multiplying Time.time by a constant less than 1 will slow down the oscillation.

Best of luck in your development! Make good things!

Standard Shader Properties Here for anyone interested

_Color
_MainTex
_Cutoff
_Glossiness
_Metallic
_MetallicGlossMap
_BumpScale
_BumpMap
_Parallax
_ParallaxMap
_OcclusionStrength
_OcclusionMap
_EmissionColor
_EmissionMap
_DetailMask
_DetailAlbedoMap
_DetailNormalMapScale
_DetailNormalMap
_UVSec
_EmissionScaleUI
_EmissionColorUI
_Mode
_SrcBlend
_DstBlend
_ZWrite

Or, you can select a material, right click, “select shader” and they will all be listed in your inspector. This DOES NOT WORK if you have Shaderforge installed, however. So St0Ff’s little script will come in handy for those who use it.

Incidentally, I am attempting to turn on emmissive on my materials, the following doesn’t work.

	private const string EmissiveValue = "_EmissionScaleUI";
	private const string EmissiveColour = "_EmissionColor";
if (NightMaterials != null) {
    			foreach (Material nightMaterial in NightMaterials) {
    				//Color colour = nightMaterial.GetColor(EmissiveColour);
    				nightMaterial.SetFloat(EmissiveValue, 1);
    				//nightMaterial.SetColor(EmissiveColour, colour);
    				nightMaterial.globalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
    				Debug.Log(nightMaterial.name + " Emmisive Value = " + nightMaterial.GetFloat(EmissiveValue));
    			}
    		}


@xCyborg The keywords are found in the Edit Shader panel. Click on the three-dot button to expand the list.

// turn on emission shader flag
material.EnableKeyword(“_EMISSION”);
// turn on realtime emissive GI
material.globalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
material.SetColor(“_EmissionColor”, new Color(1.0f,1.0f,1.0f));
material.SetTexture(“_EmissionMap”), TryLoadTexture(texturePath));
,
// turn on emission shader flag
material.EnableKeyword(“_EMISSION”);
// turn on realtime emissive GI
material.globalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
material.SetColor(“_EmissionColor”, new Color(1.0f,1.0f,1.0f));
material.SetTexture(“_EmissionMap”), TryLoadTexture(texturePath));