Change a material's emission color intensity property

I want to programmatically change a material’s emission color intensity. How do I target this via script?

3 Likes

You set it by (color * intensity)

Where you change color, just multiply the color with the desired intensity.

Turns out (yellow * 3) is actually a thing.

14 Likes

You use the Material.SetColor method. (Unity - Scripting API: Material.SetColor)

The only “tricky” thing is making sure you’re setting the correct property. You’ll have to look at the shader on your material to see which properties is has. You should see something like this:

Look for the exactly name of the emission color (in this shader, for example, it’s “_EmissionColor” and use that as the first parameter to Material.SetColor().

9 Likes

I think I must be setting the color wrong - using the multiplier does change the color, but rather than getting a more or less intense yellow, I get a purple. Here’s what I’m doing:

haloMaterial.SetVector("_EmissionColor", new Vector4(0.8196f,0.783f,0) * -4.0f);

where the Vector4 values are coming from the RGB picker (on an RGB 0-1 scale).

1 Like

I haven’t personally tried to use SetVector to set a color. I’d assume it would work, but I only use SetColor.

I also have no idea what a negative color value would imply… Is there a reason your color isn’t positive?

1 Like

this, also you’re sending it a vector4 but only fill out the first 3.

here’s an example from one of my games:

        IEnumerator ChangeGlowStrip (Color[] color) {



            for (int i = 0; i < glowStrip.Length; i++)
                glowStrip [i].SetColor ("_EmissionColor", color [0]);
            int index = 0;
            float v = 0.0f;

            while (fightOver == false) {

                v += Time.deltaTime / 7;
                float s = Mathf.PingPong (v, .5f) * 2.0f;
                s = (1.0f - (1.0f - s) * (1.0f - s) * (1.0f - s) * (1.0f - s));
                for (int i = 0; i < glowStrip.Length; i++)
                    glowStrip [i].SetColor ("_EmissionColor", color [index] * s * 1.75f);
                if (v >= 1.0f) {
                    v = 0.0f;
                    index = (index + 1) % color.Length;
                }
                yield return null;
            }
3 Likes

Ah - well, the desired intensity is negative when I change it on the color slider, thus the negative multiplier. I didn’t know you could use SetColor - I just came across setVector in a thread. At any rate - I can play with the multiplier value. Thank you all for your help!

Ah, I see, regarding Intensity. The slider is basically converting the scale for you to make it seem more manageable. From the docs:

So, setting intensity to negative values doesn’t result in multiplying the color by a negative. It results in multiplying it by 0.5, 0.25, 0.125, etc, for each negative step. The darkest it can go is multiplying your color by 0.

So, sounds like you’re on the right track. Use SetColor, and multiply by a value >= 0 to make the color brighter and brighter (with “1” being the baseline.).

2 Likes

Thank you @dgoyette for breaking that down. Everyone kept saying multiply and it was doing exactly what @heaversm said for me but sounds like I actually need to multiply it in those steps.

“Look for the exactly name of the emission color (in this shader, for example, it’s “_EmissionColor” and use that as the first parameter to Material.SetColor().”

This is really saved me. Thank you!

You want to make sure to multiply by a positive value; not a negative one. Try this:

haloMaterial.SetVector("_EmissionColor", new Vector4(0.8196f,0.783f,0f, 1f) * 2f);

Also, I would highly recommend serializing the color and intensity in the inspector so you can tweak it if you need to. It will be so much easier than jumping back and forth to the code:

[Serializefield] private Color _emissionColorValue;
[Serializefield] private float _intensity;

haloMaterial.SetVector("_EmissionColor", _emissionColorValue * _intensity);
3 Likes

Hey, whoever is looking for this, this is a script with decrease value

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MATERAILINTENSITY : MonoBehaviour
{
    public Material haloMaterial;
    public Color _emissionColorValue;
    public float desiredValue;
    public float _intensity;
    public float decrease;

    // Update is called once per frame
    void Update()
    {
        haloMaterial.SetVector("_EmissionColor", _emissionColorValue * _intensity);
        _intensity = _intensity - decrease;
        if (Input.GetKey(KeyCode.S))
        {
            _intensity = desiredValue;
        }
        if(_intensity<0)
        {
            _intensity = 0;
        }
    }
}
5 Likes

I know this is an old thread but to clarify if you want a similar intensity as achieved by the HDR color picker you actually need to multiply with 2 raised to the power of your intensity.

material.SetColor("_EmissionColor", startingColor * Mathf.Pow(2, intensity))
6 Likes

Im just going to add on to this :slight_smile: This will Take in a hex string and output a Color.

private Color GetColor(string hex)
    {
        ColorUtility.TryParseHtmlString(hex, out Color color);
        return color;
    }

This is for 32 bit colors

private Color32 GetColor(string hex)
    {
        ColorUtility.TryParseHtmlString(hex, out Color color);
        return color;
    }

For a negative value is working ‘color * 1/x’ instead of ‘color * -x’ from my test .

This general idea worked for me (modifying the color value with a Math.Pow multiplier), but for my shader it only worked when I targeted _Color not _EmissionColor.

1 Like
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 want to change the emission with lerping in a specific range with steps you provide, you can tell me to write for you. love you guys