Change material's emission scale at runtime

Is it possible to change the emission scale on unity 5’s standard shader at runtime ?
_EmissionScale can only be changed through the UI.

What i tried to do is to set the emission to a value higher than 0 and change the emissionColor through script, from black (no emission) to some arbitrary color.

The result is that the emission changes, but the glow and global illumitions remain absent.

Is there a way to change the emission at runtime ?

Changing the emission property using Material.SetColor() doesn’t update the GI.

The way to go is using DynamicGI.SetEmissive (which also seems to be faster), also, the trick is to multiply the color by the desired intensity (ie emission scale) :

DynamicGI.SetEmissive(renderer, new Color(1f, 0.1f, 0.5f, 1.0f) * intensity)

Thanks a lot mat ! : )

I suggest you to take a look at the DynamicGI class. If I remember well, if you change the Albedo or the Emission of a material or whatever property that should affect GI, you need to perform an update on some materials.

Try to apply DynamicGI.UpdateMaterials and/or DynamicGI.UpdateEnvironment on your receiving and/or your emissive object.

I know it might be too late for Bolbo now BUT for future generation like myself i found the answer in this link

Basically just set the initial emission to something so small like 0.01 so the emissive shader can compile.

Hope this helps :wink:

I couldn’t get the code above working… at all.

But after digging around the Unity documentation. For anyone that’s interested on a simple solution to help get them started on understanding emission changing

	[SerializeField] private Renderer _renderer;

    if(Input.GetKeyDown(KeyCode.K))  //for easy testing in Update
		{
			_renderer.material.SetColor ("_EmissionColor", Color.red);
			DynamicGI.UpdateMaterials (_renderer);
			DynamicGI.UpdateEnvironment ();
		}

Hope this helps others.
Sharing is caring

Find the answer in unity shader file StandardShaderGUI.cs

Following is my test code.

private void Test(GameObject obj)
{
    MeshRenderer mr = obj.GetComponent<MeshRenderer>();
    mr.material.SetColor("_EmissionColor", Color.yellow);
    bool shouldEmissionBeEnabled = ShouldEmissionBeEnabled(mr.material.GetColor("_EmissionColor"));
    if (shouldEmissionBeEnabled)
    {
        mr.material.EnableKeyword("_EMISSION");
    }
    else
    {
        mr.material.DisableKeyword("_EMISSION");
    }
}

static bool ShouldEmissionBeEnabled(Color color)
{
    return color.maxColorComponent > (0.1f / 255.0f);
}