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?

     
  2. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,648
    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.
     
    heaversm likes this.
  3. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    1,705
    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().
     
    heaversm likes 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).
     
  5. dgoyette

    dgoyette

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

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,648
    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.             }
     
  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:
    1,705
    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.).