Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Light_LOD_Enumerator : MonoBehaviour {
- public enum LODState
- {
- Full,
- Reduced
- }
- public GameObject player;
- public bool enableLightLOD = true;
- public float distance;
- public LODState lightLOD;
- [SerializeField] private int tick;
- [SerializeField] private Light thisLight;
- private Vector3 playerPos;
- private Vector3 thisPos;
- // Use this for initialization
- void Start ()
- {
- thisLight = this.GetComponent<Light>();
- tick = 0;
- }
- // Check is called (FPS/tick target) per frame
- void Update () {
- if (tick != 15) //change this to increase polling frequency
- tick++;
- else
- {
- thisPos = this.transform.position;
- playerPos = player.transform.position;
- distance = Vector3.Distance(thisPos, playerPos); //how far are we from the player
- tick = 0;
- TickUpdate();
- }
- }
- private void TickUpdate()
- {
- if (!enableLightLOD) return; //check if we enabled this; if not, dump
- if (thisLight.type == LightType.Spot)
- {
- 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
- }
- 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
- 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
- {
- case LODState.Full:
- thisLight.enabled = true;
- break;
- case LODState.Reduced:
- thisLight.enabled = false;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement