Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Light.Intensity doesn't work with HDRP

Discussion in 'Scripting' started by Wolgraph, Jul 7, 2019.

  1. Wolgraph

    Wolgraph

    Joined:
    Feb 16, 2019
    Posts:
    1
    Hi.
    I'm creating procedural light sources and I have a UI to control their color temperature and intensity.
    I set up two listener withing the ui GameObject which send the value of the two slider to the script attached to the lamp. this other script update the value of light intensity and color temperature depending on the value of the slider.

    Color temperature works fine, But I really don't know how to make the intensity work.
    I tried to print the incoming value (#4) in order to check if it passes, then I assign the new intensity and print the light.intensity' value(#6).
    in both debug.log I get the correct light value, but it actually does nothing. any suggestion
    I'm working with High Definition Render Pipeline and Unity 2019.2, cause I need the to use the physical value of luminous flux.

    Code (CSharp):
    1.  
    2. public void UpdateLight(float value)
    3.     {
    4.         Debug.Log(value);
    5.         light.intensity = value;
    6.         Debug.Log(light.intensity);
    7.  
     
  2. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    257
    Hi,

    In HDRP the scripting of lights is done through the HDAdditionalLightData class and not the builtin Light. The whole scripting API is contained in the class and available here: https://docs.unity3d.com/Packages/c...ighDefinition_HDAdditionalLightData_intensity. With this API you will also be able to change the intensity unit of the light.

    Here is an Example of how to create a new Light by script (and change it's parameters)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering.HighDefinition;
    5.  
    6. public class LightCreator : MonoBehaviour
    7. {
    8.     void Start()
    9.     {
    10.         // Util function to create an HDRP light on a GameObject:
    11.         var hdLight = gameObject.AddHDLight(HDLightType.ConeSpot);
    12.  
    13.         // Set light parameters
    14.         hdLight.intensity = 5;
    15.         hdLight.range = 1.5f;
    16.         hdLight.SetSpotAngle(60);
    17.     }
    18. }
    19.  
     
    Lumus-XXX, konsic and Joe-Censored like this.
  3. newmonuments

    newmonuments

    Joined:
    Oct 21, 2014
    Posts:
    15
    @antoinel_unity I'm going nuts with 2019.2.9f1 Unity HDRP scripting, when I copy your c# script I get the following errors:

    "type of namespace name 'HighDefinition' does not exist in the namespace 'UnityEngine.Rendering'"
    and
    "'GameObject' does not contain a definition for 'Add HDLight' and no accessible extension method 'AddHDLight' accepting a first argument of type 'GameObject' could be found"

    What I'd like to do is change the intensity of an existing light in the scene. Supaworst's thread has a simple solution (below) but I get similar errors.

    Supaworst's solution (doesn't appear to work):

    Ok, no problem, i've figured out how to do it.
    Just take the light as a normal game object and get the component "HDadditionalLightData".

    I hope it could be useful for somebody.
     
    glatteinfo likes this.
  4. antoinel_unity

    antoinel_unity

    Unity Technologies

    Joined:
    Jan 7, 2019
    Posts:
    257
    Hey, sorry i should have specified it but the light API is only available in HDRP for Unity 2019.3 version and up.
    If you're using 2019.2 or lower, you need to manually get the HDAdditionalLightData and change what you want.
     
  5. newmonuments

    newmonuments

    Joined:
    Oct 21, 2014
    Posts:
    15
    I ended up being able to change light intensity in unity HDRP by changing the light color (assuming it's hd color). HDR colors have 4 values, the 4th being an intensity multiplier, which effectively increases light brightness. In other words, if the 4th light hdr value increases from 2 to 5, then it will make your light brighter. If someone else has same prob, hope this helps.

    to make sure colors are hdr, set variables like so:

    public Light lightObj;

    [ColorUsage(true, true)]
    private Color matColor;

    private void Awake()
    {
    Renderer nRend = obj.GetComponent<Renderer>();
    lightObj.color = matColor;
    }
     
    xi003 likes this.
  6. mb28

    mb28

    Joined:
    Oct 7, 2012
    Posts:
    27
    Here is some code to help anyone out that runs across this thread. This is tested and works in 2019.3 for me:

    Also, make sure you have this line at the top of your script outside of the MonoBehaviour
    using UnityEngine.Rendering.HighDefinition;

    Code (CSharp):
    1. //set light as a game object in inspector
    2. public GameObject lgt;
    3.  
    4. //variable where we will store the HD lighting data
    5. private HDAdditionalLightData lightData;
    6.  
    7. void Start(){
    8. lightData = lgt.GetComponent<HDAdditionalLightData>();
    9. }
    10.  
    11. void Update(){
    12. lightData.intensity = 40f;
    13. }
     
    Last edited: May 18, 2020
  7. Fyfer96

    Fyfer96

    Joined:
    May 5, 2020
    Posts:
    1
    Hi,
    I'm in 2019.3 and I've copied the above code exactly, added script to my light, and dragged the light I want to control into the game object but the intensity does not change.
    Any ideas?

    Thanks,
    Ally
     
  8. mb28

    mb28

    Joined:
    Oct 7, 2012
    Posts:
    27
    Hey Ally, looks like I forgot to include a line before. Apologies for that one. Make sure you have "
    using UnityEngine.Rendering.HighDefinition;" outside of the monobehaviour, towards the very top of your script file
     
  9. mb28

    mb28

    Joined:
    Oct 7, 2012
    Posts:
    27
    So for a more complete script since I missed a line earlier. Also updated my previous post:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering.HighDefinition;
    5.  
    6. public class light_intensity : MonoBehaviour {
    7.     public GameObject lgt;
    8.     private HDAdditionalLightData lightData;
    9.  
    10.     void Start () {
    11.         lightData = lgt.GetComponent<HDAdditionalLightData>();
    12.     }
    13.  
    14.     void Update () {
    15.         lightData.intensity = 40f;  
    16.     }
    17. }
     
    IGGUS likes this.
  10. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,246
    It is a bit awkward that HDRP has these 'tacked on' components to make things work. It seems like HDRP is this patch on top of the core engine, not a part of it (which it is as a package!).
    Would it not be better to extend the light class to contain additional parameters used by HDRP, maybe URP in future, simply unused by built-in?
     
    Marc-Saubion likes this.
  11. GXMark

    GXMark

    Joined:
    Oct 13, 2012
    Posts:
    514
    Yes please will unity make the Light Component work with HDRP and not tack on interim solutions for faster shipping of versions !
     
    Marc-Saubion likes this.
  12. spulkknight

    spulkknight

    Joined:
    Aug 30, 2021
    Posts:
    1
    Is there a way to get the component referenced in a proyect?
    I think I didn't set my VScode properly or my Unity can''t just find the reference.
    All I wanted to do is something simple: After 3 seconds of starting the first photograme, increase the intensity to 8 f (in float).
    How can I fix it?

    public class prender : MonoBehaviour
    {
    private Light lightInc;

    --
    void Start()
    {
    lightInc = GetComponent<Light>();
    lightInc.Intensity = (8f, 3f);


    }
    --
     
  13. LeePerry

    LeePerry

    Joined:
    Aug 27, 2013
    Posts:
    7
    Wow... this is some seriously ghetto stuff in there. Inability to simply adjust the brightness of a light in script. Come on.
     
    Marou1 and Marc-Saubion like this.
  14. GXMark

    GXMark

    Joined:
    Oct 13, 2012
    Posts:
    514
    Don't always assume that Light Component will give you what you desire !
    Its HDRP more complex component structure

    You access light intensity using this
    lightObj.GetComponent<HDAdditionalLightData>();
     
  15. glatteinfo

    glatteinfo

    Joined:
    Nov 17, 2019
    Posts:
    19
    "HDadditionalLightData". worked. thanks!