Advertisement
noradninja

Light LOD

Oct 23rd, 2023 (edited)
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Light_LOD_Enumerator : MonoBehaviour {
  6.     public enum LODState
  7.     {
  8.         Full,
  9.         Reduced
  10.     }
  11.     public GameObject player;
  12.     public bool enableLightLOD = true;
  13.     public float distance;
  14.     public LODState lightLOD;
  15.     [SerializeField] private int tick;
  16.     [SerializeField] private Light thisLight;
  17.     private Vector3 playerPos;
  18.     private Vector3 thisPos;
  19.     // Use this for initialization
  20.     void Start ()
  21.     {
  22.         thisLight = this.GetComponent<Light>();
  23.         tick = 0;
  24.     }
  25.    
  26.     // Check is called (FPS/tick target) per frame
  27.     void Update () {
  28.         if (tick != 15) //change this to increase polling frequency
  29.             tick++;
  30.         else
  31.         {
  32.             thisPos = this.transform.position;
  33.             playerPos = player.transform.position;
  34.             distance = Vector3.Distance(thisPos, playerPos); //how far are we from the player
  35.             tick = 0;
  36.             TickUpdate();
  37.         }  
  38.     }
  39.  
  40.     private void TickUpdate()
  41.     {
  42.         if (!enableLightLOD) return; //check if we enabled this; if not, dump
  43.         if (thisLight.type == LightType.Spot)
  44.         {
  45.             lightLOD = distance <= thisLight.range * 1.25f ? LODState.Full : LODState.Reduced; //LOD0 if < the range of the light else LOD1, I dont like magic numbers but Unity Spotlight Range doesnt include the falloff area
  46.         }
  47.         else lightLOD = distance <= thisLight.range ? LODState.Full : LODState.Reduced; //LOD0 if < the range of the light else LOD1, this covers your point lights as the range is spherical
  48.     switch (lightLOD)//enable/disable light. To change behavior, edit the cases, for example instead of turning them on and off you can swap to not important/vertex lighting for speed, could even add an extra state if you modify the enum and the ternary operators above
  49.         {
  50.             case LODState.Full:
  51.                 thisLight.enabled = true;
  52.                 break;
  53.             case LODState.Reduced:
  54.                 thisLight.enabled = false;
  55.                 break;
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement