Search Unity

Change a material's emission color intensity property

Discussion in 'Scripting' started by heaversm, Jan 11, 2019.

  1. heaversm

    heaversm

    Joined:
    Jul 12, 2011
    Posts:
    11
    I want to programmatically change a material's emission color intensity. How do I target this via script?

     
    ow3n, cxode and arufolo like this.
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    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.
     
  3. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    You use the Material.SetColor method. (https://docs.unity3d.com/ScriptReference/Material.SetColor.html)

    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:

    upload_2019-1-11_16-16-38.png

    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().
     
    Mashimaro7, SrDidE, FloAtm and 6 others like this.
  4. heaversm

    heaversm

    Joined:
    Jul 12, 2011
    Posts:
    11
    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:

    Code (CSharp):
    1. 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).
     
    mmmshuddup likes this.
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    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?
     
    SparrowGS likes this.
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    this, also you're sending it a vector4 but only fill out the first 3.

    here's an example from one of my games:

    Code (CSharp):
    1.         IEnumerator ChangeGlowStrip (Color[] color) {
    2.  
    3.  
    4.  
    5.             for (int i = 0; i < glowStrip.Length; i++)
    6.                 glowStrip [i].SetColor ("_EmissionColor", color [0]);
    7.             int index = 0;
    8.             float v = 0.0f;
    9.  
    10.             while (fightOver == false) {
    11.  
    12.                 v += Time.deltaTime / 7;
    13.                 float s = Mathf.PingPong (v, .5f) * 2.0f;
    14.                 s = (1.0f - (1.0f - s) * (1.0f - s) * (1.0f - s) * (1.0f - s));
    15.                 for (int i = 0; i < glowStrip.Length; i++)
    16.                     glowStrip [i].SetColor ("_EmissionColor", color [index] * s * 1.75f);
    17.                 if (v >= 1.0f) {
    18.                     v = 0.0f;
    19.                     index = (index + 1) % color.Length;
    20.                 }
    21.                 yield return null;
    22.             }
     
    TungNguyen2046, photonic and Teymur12 like this.
  7. heaversm

    heaversm

    Joined:
    Jul 12, 2011
    Posts:
    11
    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!
     
  8. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    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.).
     
    mmmshuddup and albrechtjess like this.
  9. albrechtjess

    albrechtjess

    Joined:
    Oct 11, 2017
    Posts:
    11
    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.
     
  10. mowangdesign

    mowangdesign

    Joined:
    Jun 17, 2019
    Posts:
    1


    "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!
     
  11. JayJack

    JayJack

    Joined:
    Dec 29, 2020
    Posts:
    2
    You want to make sure to multiply by a positive value; not a negative one. Try this:
    Code (CSharp):
    1. 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:
    Code (CSharp):
    1.  
    2. [Serializefield] private Color _emissionColorValue;
    3. [Serializefield] private float _intensity;
    4.  
    5. haloMaterial.SetVector("_EmissionColor", _emissionColorValue * _intensity);
    6.  
     
    Last edited: Jan 3, 2021
    manbearpigman and photonic like this.
  12. misticalages

    misticalages

    Joined:
    Aug 3, 2020
    Posts:
    12
    Hey, whoever is looking for this, this is a script with decrease value


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MATERAILINTENSITY : MonoBehaviour
    6. {
    7.     public Material haloMaterial;
    8.     public Color _emissionColorValue;
    9.     public float desiredValue;
    10.     public float _intensity;
    11.     public float decrease;
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         haloMaterial.SetVector("_EmissionColor", _emissionColorValue * _intensity);
    17.         _intensity = _intensity - decrease;
    18.         if (Input.GetKey(KeyCode.S))
    19.         {
    20.             _intensity = desiredValue;
    21.         }
    22.         if(_intensity<0)
    23.         {
    24.             _intensity = 0;
    25.         }
    26.     }
    27. }
    28.  
     
  13. stefanbauwens42

    stefanbauwens42

    Joined:
    Dec 18, 2018
    Posts:
    4
    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.
    Code (CSharp):
    1. material.SetColor("_EmissionColor", startingColor * Mathf.Pow(2, intensity))
     
  14. joeross0

    joeross0

    Joined:
    Dec 31, 2017
    Posts:
    8
    Im just going to add on to this :) This will Take in a hex string and output a Color.

    Code (CSharp):
    1. private Color GetColor(string hex)
    2.     {
    3.         ColorUtility.TryParseHtmlString(hex, out Color color);
    4.         return color;
    5.     }
    This is for 32 bit colors
    Code (CSharp):
    1. private Color32 GetColor(string hex)
    2.     {
    3.         ColorUtility.TryParseHtmlString(hex, out Color color);
    4.         return color;
    5.     }
     
  15. florinel2102

    florinel2102

    Joined:
    May 21, 2019
    Posts:
    76
    For a negative value is working 'color * 1/x' instead of 'color * -x' from my test .
     
  16. sam_nau

    sam_nau

    Joined:
    Oct 26, 2013
    Posts:
    9
    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`.