Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Change material's emission intensity

Discussion in 'Scripting' started by kfireven, Jun 14, 2022.

  1. kfireven

    kfireven

    Joined:
    Oct 18, 2016
    Posts:
    38
    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?
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    I don't mean to state the obvious, but is it because you're multiplying the intensity by whatever color is in your code?
     
  3. kfireven

    kfireven

    Joined:
    Oct 18, 2016
    Posts:
    38
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
  5. kfireven

    kfireven

    Joined:
    Oct 18, 2016
    Posts:
    38
    Yeah, saw it.. the Mathf.Pow "solution" doesn't work... more random numbers... ;/
     
  6. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
  7. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,108
    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
     
  8. kfireven

    kfireven

    Joined:
    Oct 18, 2016
    Posts:
    38
  9. kfireven

    kfireven

    Joined:
    Oct 18, 2016
    Posts:
    38
    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))
     
    Last edited: Jun 15, 2022
    De_sha likes this.