Change material's emission intensity

Hi, I’m using this code to change a material’s emission color and intensity:
material.SetColor(“_EmissionColor”, color * intensity);

And it works. Kinda… it changes the color correctly, however, the intensity seems to be random and not the same as the value I give, for example, if I set intensity = 3 I get intensity 5.08, and if I set intensity = 7 then I get intensity 6.3 (… in the “HDR Color” picker)… what am I missing? how can I match the intensity value I give via the code to the one seen in the UI’s HDR Color picker?

I don’t mean to state the obvious, but is it because you’re multiplying the intensity by whatever color is in your code?

Well… I’m doing what is suggested here: https://discussions.unity.com/t/728142

But it doesn’t work as expected.

I didn’t really know how it worked myself, but if you read through the rest of the thread, they talk about how the weird behavior, and how the intensity on the color picker works. The last post on the thread mentions the solution.

Yeah, saw it… the Mathf.Pow “solution” doesn’t work… more random numbers… ;/

1 Like

Just some quick digging, found this thread, it might be the solution you’re looking for. It looks like you need to subtract some arbitrary value before plugging it into the power of 2.

https://discussions.unity.com/t/739533

Hope it works for you.

Not sure if we are talking about the same thing, but I guess this picker is unable to retrive these values, because intensity slider is not part of color itself, so when you close the window you get some color like (3.1, 4.5, 2.2, 1) and it’s hard to say what was your intensity you selected. For example mid gray (0.5) with multiplier 12 is the same as white (1) multiplied by 6. Also there is issue like this: https://issuetracker.unity3d.com/issues/hdrp-weird-conversion-of-fog-hdr-color-units

Thanks but I’ve been there also. It doesn’t work.

Well, I got it to work, if someone finds this… it’s the color value basically, it is needed to multiply the intensity by a pure and unaffected color value. So material.GetColor(“_EmissionColor”) is not good (unless the intensity is set to zero so the color value will be unaffected). The “pure” color value should be saved in a local variable and be reused with each intensity change.

Example:
Set intensity to 3.5 of a reddish color:
material.SetColor(“_EmissionColor”, new Vector4(0.749f, 0.059f, 0.059f, 1.000f) * Mathf.Pow(2.0F, 3.5f))

1 Like

This does not work, Im confused…

Caching original Color, along with the UI intensity,

Then doing material.SetColor(“_EmisisonColor”, color * Mathf.Pow(2, intensity)) results in a much brighter value.

using UnityEngine;

public class EmissionControl : MonoBehaviour
{
    public Material targetMaterial; // Assign your material in the Inspector
    public Color emissionColor = Color.white; // Color of the emission
    public float intensity = 1.0f; // Intensity of the emission

    void Start()
    {
        // Set the initial emission
        SetEmission(intensity);
    }

    public void SetEmission(float newIntensity)
    {
        // Calculate the new emission color
        Color finalColor = emissionColor * newIntensity;

        // Apply the emission color to the material
        targetMaterial.SetColor("_EmissionColor", finalColor);

        // Enable the emission property
        DynamicGI.SetEmissive(targetMaterial, finalColor);
    }

    // Example method to change intensity over time
    void Update()
    {
        // Example: Increase intensity over time
        intensity += Time.deltaTime * 0.1f;
        SetEmission(intensity);
    }
}

you can use this code to change emission on a material easily , don’t forget to hit the like :smiley: peace.
also if you like to change the emission with lerping in specific range that you can provide tell me to write for you. love you guys