Search Unity

Enabling _EMISSION using a MaterialPropertyBlock

Discussion in 'General Graphics' started by LootlabGames, Apr 6, 2017.

  1. LootlabGames

    LootlabGames

    Joined:
    Nov 21, 2014
    Posts:
    343
    I need to be able to enable emission for a material without creating an instance or affecting the other renderers using the material.

    Right now if emission is already enabled this works without a problem:
    Code (CSharp):
    1. renderer.GetPropertyBlock(matPropBlock);
    2. matPropBlock.SetColor("_EmissionColor", trappedColor);
    3. renderer.SetPropertyBlock(matPropBlock);
    But I don't always want emission enabled.
    Only when I change the color using the above code.

    I'm using version 5.6.0
     
    Last edited: Apr 6, 2017
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,341
    There are a ton of posts on this topic already if you search for _EmissionColor in the forums, but the short version is if a material using the Standard shader has emission set to black you cannot turn on emission using only a material property block because the shader keyword is not enabled. You can either enable the keyword on the material at runtime (and in doing so you may need to to setup shader variant collection for your project to ensure that shader variant is loaded), or you can just set the emission color to something very dim so that emission is already enabled on the material but not visible. You can also set the emission to black in script at runtime either on the material itself or via a material property block and it'll keep working, it's just if it's set black in the editor via the inspector.

    https://forum.unity3d.com/threads/standard-shader-emission-value.280153/

    Note the test Unity does to enable and disable the emission is if the max color component is > (0.1f / 255f), or ~0.00039, so if you set your emission color in the inspector to 1/255,1/255,1/255 and the multiplier to 0.11 the emission color should be basically invisible, but still enabled.
     
    Last edited: Apr 8, 2017