2
\$\begingroup\$

enter image description here

I am willing to change emission level thorough code without effecting its color.

(ie. changing the brightness number shown in the box at the right side of the inspector, while keeping the emission colour swatch unchanged)

I have tried this so far, but I don't know how to use my value variable to update the material:

public class EmissionColourChange : MonoBehaviour {

    Material thisMat;
    Color c;
    public float value; 

    void Start () {
        thisMat = GetComponent<Renderer>().material;
        c = GetComponent<Renderer>().material.color;
        thisMat.EnableKeyword("_EMISSION");
        value = c.r;
    }


    void Update () {
        thisMat.SetColor("_EmissionColor", new Color(c.r,c.g,c.b));
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ What is not working? \$\endgroup\$ – jgallant Apr 5 '17 at 9:52
  • \$\begingroup\$ Your code (copied exactly) works on my machine. Is the script enabled / applied to the game object? \$\endgroup\$ – Zebraman May 11 '17 at 17:33
2
\$\begingroup\$

You're bang on for a low-dynamic range emissive colour. To get access to brighter emission we need to move to a high-dynamic range, which is outside the range of the Color type.

So, we replace it with a Vector:

thisMat.SetColor("_EmissionColor", new Vector4(c.r,c.g,c.b,0) * value);

Now you have access to make the colour as bright as you want by increasing the multiplier value. This is what changes the number shown to the right of the emission colour swatch in the inspector.

Note that the colour & value you put in isn't exactly the parameters you'll see in the material inspector when you look at the result. Because we're mixing together the colour and brightness level, Unity has to tease them apart again to show in the inspector. It looks like it does that by setting the multiplier value equal the greatest component of the colour (R, G, or B), then divides the colour by that. So you'll still have:

myColour * myBrightnessMultiplier = inspectorColour * inspectorBrightnessMultiplier

it just might pick a different way of splitting the brightness representation between the two parts. The rendering math should all come out the same.

\$\endgroup\$
0
\$\begingroup\$

If you take the time to look at the color to give to the Emission field, you will see that it uses the HSV Color model (hue, saturation, and lightness), not the "traditional" RGB one.

Make sure the lightness value of your color is > 0 if you want the emission to be visible.

\$\endgroup\$
  • \$\begingroup\$ I doubt this is the cause of the issue. Once you've constructed a Color struct, it doesn't matter whether you chose the parameters in RGB or HSV colour space - the SetColor method sees only the resulting Color instance. (And in fact, the Color struct only has an RGB or RGBA constructor, no HSV option is presented) \$\endgroup\$ – DMGregory Apr 5 '17 at 16:59
  • \$\begingroup\$ You are right @DMGregory, but if the lightness value of the color is 0, then, the emission won't be visible. \$\endgroup\$ – Hellium Apr 6 '17 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.