Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.23 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class DistanceLOD : MonoBehaviour
  4. {
  5.     [SerializeField] private float updateDelay = 0.1f;
  6.     [SerializeField] private float cullDistance = 10f;
  7.     [SerializeField] private Renderer[] renderers;
  8.     [SerializeField] private Vector3 center = Vector3.zero;
  9.     [SerializeField] private Vector3 size = Vector3.one;
  10.     private float distance;
  11.     private Bounds bounds;
  12.     private Camera mainCamera;
  13.  
  14.     private void Awake()
  15.     {
  16.         mainCamera = Camera.main;
  17.     }
  18.  
  19.     private void OnEnable()
  20.     {
  21.         InvokeRepeating("OnUpdate", 0f, updateDelay);
  22.     }
  23.  
  24.     private void OnDisable()
  25.     {
  26.         CancelInvoke("OnUpdate");
  27.     }
  28.  
  29.     private void OnUpdate()
  30.     {
  31.         UpdateDistance();
  32.         EnableRenderers(cullDistance > distance);
  33.     }
  34.  
  35.     private void EnableRenderers(bool enable)
  36.     {
  37.         if (renderers == null)
  38.         {
  39.             return;
  40.         }
  41.  
  42.         for (var i = 0; i < renderers.Length; ++i)
  43.         {
  44.             if (renderers[i] == null)
  45.             {
  46.                 continue;
  47.             }
  48.  
  49.             renderers[i].enabled = enable;
  50.         }
  51.     }
  52.  
  53.     private void UpdateDistance()
  54.     {
  55.         if (mainCamera == null)
  56.         {
  57.             return;
  58.         }
  59.  
  60.         var camPosition = mainCamera.transform.position;
  61.  
  62.         if (bounds.Contains(camPosition))
  63.         {
  64.             distance = 0f;
  65.         }
  66.         else
  67.         {
  68.             var closestPoint = bounds.ClosestPoint(camPosition);
  69.             distance = (closestPoint - camPosition).magnitude;
  70.         }
  71.     }
  72.  
  73.     private void UpdateBounds()
  74.     {
  75.         bounds = new Bounds(transform.TransformPoint(center), size);
  76.     }
  77.  
  78.     private void OnDrawGizmos()
  79.     {
  80.         if (mainCamera == null)
  81.         {
  82.             mainCamera = Camera.main;
  83.         }
  84.  
  85.         UpdateBounds();
  86.         UpdateDistance();
  87.  
  88.         Gizmos.color = Color.cyan;
  89.         Gizmos.DrawWireCube(bounds.center, bounds.size);
  90.  
  91.         if (mainCamera != null)
  92.         {
  93.             var camPosition = mainCamera.transform.position;
  94.             var closestPoint = bounds.ClosestPoint(camPosition);
  95.             Gizmos.DrawLine(camPosition, closestPoint);
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement