- Home /
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 ?
Answer by Bolbo13 · Mar 12, 2015 at 01:43 AM
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 ! : )
Your're welcome ! Glad to know that you found a solution. :)
Answer by MaT227 · Mar 10, 2015 at 07:28 AM
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.
At runtime, i get this message under the emission property in the material :
"emissive value is animated but the material has not been configured to support emissive. Please make sure the material itself has some amount of emissive."
I get that with the emission set to 4 and a bright color. Also, I'm now using DynamicGI.Update$$anonymous$$aterials but it doesnt seem to have any effect (no glow, no GI).
Did you try with a simple scene and did you apply DynamicGI.Update$$anonymous$$aterials on all materials ? Take also a look at $$anonymous$$aterialGlobalIllu$$anonymous$$ationFlags and especially at $$anonymous$$aterialGlobalIllu$$anonymous$$ationFlags.RealtimeEmissive
$$anonymous$$aterialGlobalIllu$$anonymous$$ationFlags can be set in the inspector under the emission field (enabled only when emission is above 0).
I set up a new scene; no change. Still no global illu$$anonymous$$ation and the message (displayed on the material under the emission field) :
"emissive value is animated but the material has not been configured to support emissive. Please make sure the material itself has some amount of emissive."
Here is the relevant code :
Renderer renderer;
$$anonymous$$aterial material;
public Color color;
void Start()
{
renderer = GetComponent<Renderer>();
material = renderer.shared$$anonymous$$aterial;
//can be set in the inspector
material.globalIllu$$anonymous$$ationFlags = $$anonymous$$aterialGlobalIllu$$anonymous$$ationFlags.RealtimeEmissive;
}
void Update()
{
material.SetColor("_EmissionColor", color);
DynamicGI.Update$$anonymous$$aterials(renderer);
DynamicGI.UpdateEnvironment();
}
Answer by DarkSinBad · Jul 02, 2016 at 02:44 PM
I know it might be too late for Bolbo now BUT for future generation like myself i found the answer in this link
http://answers.unity3d.com/questions/970290/emission-at-run-time-works-in-editor-but-not-in-bu.html
Basically just set the initial emission to something so small like 0.01 so the emissive shader can compile.
Hope this helps ;)
Answer by KarlKarl2000 · Oct 08, 2016 at 09:43 AM
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
This works for me. For
DynamicGI.Update$$anonymous$$aterials (_renderer);
it's been changed to
_renderer.UpdateGI$$anonymous$$aterials();
Answer by raincarrot · Jun 17, 2017 at 04:34 PM
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);
}
Your answer