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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Creating a Light with shadows programmatically

Discussion in 'High Definition Render Pipeline' started by 3dimYannick, Mar 3, 2020.

  1. 3dimYannick

    3dimYannick

    Joined:
    Mar 26, 2013
    Posts:
    14
    So I'm trying to figure out why I cannot get shadows working when I create a Light at runtime.

    Light light = gameObject.AddComponent<Light>();
    light.type = LightType.Directional;
    light.shadows = LightShadows.Soft;

    So this is what I used, nothing fancy.

    When I run this piece of code I do get light, as I see my objects being lit. Just no shadows.
    So here's the weird part (I think), when I select the directional light in the editor the shadows appears!

    And yes lights I create in the editor do have schadows when I enable them.

    So my question is, what am I missing?
    Do I need to do a call somewhere or at the light?

    pictures
    upload_2020-3-3_9-50-38.png
    upload_2020-3-3_9-50-57.png
     
  2. chap-unity

    chap-unity

    Unity Technologies

    Joined:
    Nov 4, 2019
    Posts:
    701
    I managed to repro the issue quite easily and it looks like a bug indeed.
    I will investigate and create a report.
    Thank you for pointing that out.
     
    3dimYannick likes this.
  3. 3dimYannick

    3dimYannick

    Joined:
    Mar 26, 2013
    Posts:
    14
    Great to hear, glad I could help!
     
  4. chap-unity

    chap-unity

    Unity Technologies

    Joined:
    Nov 4, 2019
    Posts:
    701
    As a matter of fact, it's not an issue, it's just the wrong method.
    To add a light you need to use the GameobjectExtension AddHDLight (which is used the same way as AddComponent method)
    https://docs.unity3d.com/Packages/c...ering.HighDefinition.GameObjectExtension.html

    To modify the parameter of the light, you need to modify the HDAdditionalLightData component on the newly created light https://docs.unity3d.com/Packages/c...ndering.HDPipeline.HDAdditionalLightData.html

    Code (CSharp):
    1.  
    2. using UnityEngine.Rendering.HighDefinition;
    3. ...
    4. HDAdditionalLightData light = this.gameObject.AddHDLight(HDLightTypeAndShape.Directional);
    5. light.EnableShadows(true);
    When using the regular light component, the HDAdditionnalLightData is not added until the gameobject is selected in the inspector so that's why the shadows did not appear until then.
     
    Last edited: Mar 3, 2020
  5. 3dimYannick

    3dimYannick

    Joined:
    Mar 26, 2013
    Posts:
    14
    Thanks I got it working now!

    So the documentation for HDRP (and other packages) will always be separate from the basic unity API documentation?

    Also one other thing I discovered which I'm not sure is mentioned somewhere has to do with creating asset bundles.
    When you create a separate asset bundle for lights, you have to add the namespace "HighDefinition" to your asset bundle script.

    Otherwise you cannot find "HDAdditionalLightData" component on the game object at runtime.

    Because it will be bundled without that component.

    Although the editor will show it when you select the light (maybe it adds it when you select a light?)
     
  6. chap-unity

    chap-unity

    Unity Technologies

    Joined:
    Nov 4, 2019
    Posts:
    701
    yes for the documentation.

    For the light, that's most likely what is happening (the same way as the previous issue) !
     
  7. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I had to do similar dances and spins and moonwalks back when I was generating HDRP reflection probes :) I'm not sure its immediately obvious to people there's API for this as Unity classically didn't have 2 components each time to worry about before.

    Also hard to communicate nuance like that. I guess google will solve it in the end!
     
  8. JohnKP-Mindshow

    JohnKP-Mindshow

    Joined:
    Oct 25, 2017
    Posts:
    56
    this is hilariously undocumented :confused:
     
  9. 3dimYannick

    3dimYannick

    Joined:
    Mar 26, 2013
    Posts:
    14
    Well interestingly though, I do not refer to the lights directly, but a prefab game object containing one or more game objects with the light component on it. It might still make sense but it isn't really that transparent since I wasn't directly referring to the "HDAdditionalLightData" class.
     
  10. JohnKP-Mindshow

    JohnKP-Mindshow

    Joined:
    Oct 25, 2017
    Posts:
    56
    Could you give some clarity on why HDLight objects get added in a different way than every other type of Unity component?

    I'm confused about a few things:
    1) What the Light component actually does now vs what HDAdditionalLightData does.
    2) Why turning an object into a light in HDRP required using a different method than any other type of component in Unity (GameObject.AddComponent<T> and GameObject.AddHDLight), which seems super hacky and outside the paradigm for components Unity has established for years.

    If HD Lights are really that different, maybe just using a wholey different component would be clearer to the user, instead of using the Light component as some dummy terminal that is sometimes driven by a secret component with nothing in its inspector + poorly documented codepaths that are required to run to programatically generate the lights, that is completely different than the path one would use to add any other component in Unity.
     
  11. Julien_Unity

    Julien_Unity

    Unity Technologies

    Joined:
    Nov 17, 2015
    Posts:
    68
    The reason is that a big part of the logic related to lights (culling, shadows etc) is done in Unity core which is not accessible from user land where SRPs live. Since those classes (light, camera) can't be inherited properly we had to use this additional data paradigm to be able to had SRP specific parameters and behaviors.
     
    chap-unity likes this.
  12. emaduell

    emaduell

    Joined:
    Nov 14, 2018
    Posts:
    4
    I've been playing around with the HDAdditionalLightData and what I can't access is for example the "Display Emissive Mesh" from an Area Light from the C# script. I'm using the script to create several Area Lights and I would love to see their mesh on runtime by default ... I've not been able how to access this parameter though the HDAdditionalLightData nor any info on the forums ... any hint ?

    And what about the Color Temperature ? I can do find the EnableColorTemperature() method, but not how to set the actual temperature... ?¿
     
  13. emaduell

    emaduell

    Joined:
    Nov 14, 2018
    Posts:
    4
    Anyone know how to access properties as for ex. "Display Emissive Mesh" from an Area Light from the C# script ? I can't find the API interface ... Thanks for any tips ...
     
  14. chap-unity

    chap-unity

    Unity Technologies

    Joined:
    Nov 4, 2019
    Posts:
    701
    Hello @emaduell , currently, emissive mesh creation for the area light can only be done manually in the editor.

    However, there is a variable called "displayAreaLightEmissiveMesh" in HDAdditionnalLightData class that displays the area light emissive mesh when set to true. Except, this variable is internal so it can't be accessed without modifying your current HDRP package.