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

Set Light Layers by script HDRP

Discussion in 'High Definition Render Pipeline' started by Demencja, Mar 16, 2020.

  1. Demencja

    Demencja

    Joined:
    Nov 7, 2016
    Posts:
    21
    How to set light layers via script?

    Code like this dont work.

    Code (CSharp):
    1.         private void Awake()
    2.         {
    3.             light = GetComponent<Light>();
    4.             light.renderingLayerMask = 8;
    5.         }
    6.  
    7.  
    Do you also have a problem with this?

    PS. I activated the layers in the settings.
     
    trombonaut likes this.
  2. Demencja

    Demencja

    Joined:
    Nov 7, 2016
    Posts:
    21
    Everything works. The editor simply does not refresh this field.
     

    Attached Files:

    trombonaut likes this.
  3. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    624
    Hey, I just wrote this script to set Light Layers for all childs and found it pretty useful already.
    Enjoy:

    Code (CSharp):
    1. //using Sirenix.OdinInspector;
    2. using UnityEngine;
    3. using UnityEngine.Rendering.HighDefinition;
    4.  
    5. namespace JL.Environment
    6. {
    7.     public class LightLayerForChilds : MonoBehaviour
    8.     {
    9.         [SerializeField]
    10.         //[ValidateInput("ValidateInput1",
    11.         //    "Same component exists in childs, this is not allowed")]
    12.         LightLayerEnum _targetLayer = LightLayerEnum.LightLayerDefault;
    13.         [SerializeField]
    14.         //[ValidateInput("ValidateInput2",
    15.         //    "Same component exists in parent, this is not allowed")]
    16.         bool _dummyUpdateToggle;
    17.  
    18.         private void OnValidate()
    19.         {
    20.             // guessing we never want to choose Everything or Nothing as
    21.             // valid layer so we exclude it here to avoid edge cases
    22.             if (_targetLayer == LightLayerEnum.Nothing) return;
    23.             if (_targetLayer == LightLayerEnum.Everything) return;
    24.  
    25.             System.Array enumValues = System.Enum.GetValues(typeof(LightLayerEnum));
    26.  
    27.             foreach (Light light in GetComponentsInChildren<Light>(true))
    28.             {
    29.                 int bitmask = light.renderingLayerMask;
    30.                 bitmask = CalculateBitmask(bitmask, enumValues);
    31.                 light.renderingLayerMask = bitmask;
    32.             }
    33.  
    34.             foreach (Renderer renderer in GetComponentsInChildren<Renderer>(true))
    35.             {
    36.                 int bitmask = (int)renderer.renderingLayerMask;
    37.                 bitmask = CalculateBitmask(bitmask, enumValues);
    38.                 renderer.renderingLayerMask = (uint)bitmask;
    39.             }
    40.         }
    41.  
    42.         int CalculateBitmask(int currentBitmask, System.Array enumValues)
    43.         {
    44.             //int originalBitVal = currentBitmask;
    45.  
    46.             foreach (LightLayerEnum current in enumValues)
    47.             {
    48.                 // if everything is not set, the inverse in SetBitmask
    49.                 // will set all bits to 0, as if nothing was selected
    50.                 // so we can just ignore it here
    51.                 // it would probably also mess with decal layers
    52.                 if (current == LightLayerEnum.Everything) continue;
    53.  
    54.                 int layerBitVal = (int)current;
    55.  
    56.                 bool set = current == _targetLayer;
    57.                 //if (set) Debug.Log("Set " + current);
    58.                 currentBitmask = SetBitmask(currentBitmask, layerBitVal, set);
    59.             }
    60.  
    61.             //Debug.Log("| Bitmask : " + currentBitmask +
    62.             //        "\r\n| Original: " + originalBitVal);
    63.  
    64.             return currentBitmask;
    65.         }
    66.  
    67.         int SetBitmask(int bitmask, int bitVal, bool set)
    68.         {
    69.             if (set)
    70.                 // or "|" will add the value, the 1 at the right position
    71.                 bitmask |= bitVal;
    72.             else
    73.                 // and "&" will multiply the value, but we take the inverse
    74.                 // so the bit position is 0 while all others are 1
    75.                 // everything stays as it is, except for the one value
    76.                 // which will be set to 0
    77.                 bitmask &= ~bitVal;
    78.  
    79.             return bitmask;
    80.         }
    81.  
    82.         //bool ValidateInput1(LightLayerEnum layer)
    83.         //{
    84.         //    return GetComponentsInChildren<LightLayerForChilds>(true).Length <= 1;
    85.         //}
    86.         //bool ValidateInput2(bool boolValue)
    87.         //{
    88.         //    return GetComponentsInParent<LightLayerForChilds>(true).Length <= 1;
    89.         //}
    90.     }
    91. }
    92.  
    If you are using Odin, you can uncomment the ValidateInput Lines.
     
    Last edited: May 14, 2021