Advertisement
Guest User

Untitled

a guest
Apr 11th, 2015
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.24 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4.  
  5. public class CulledObject : MonoBehaviour
  6. {
  7.     /// <summary>
  8.     /// Defines the render distance between objects, if the object is within this distance then it will render.
  9.     /// </summary>
  10.     public static float RenderDistance = 20;
  11.    
  12.     private Camera playerCamera;            // the camera linked to the player
  13.     private MeshRenderer mesh;              // the mesh to enable/disable during the distance check
  14.    
  15.     private bool outOfDistanceLastFrame;    // if we were out of distance in the last Update()
  16.    
  17.     void Start ()
  18.     {
  19.         // attempt to get the camera controlled by the player
  20.         playerCamera = GameObject.FindGameObjectWithTag ("MainCamera").GetComponent<Camera> ();
  21.         if (playerCamera == null)
  22.             print ("The main camera was not found inside CulledObject script.");
  23.        
  24.         // attempt to get the meshRenderer component on the culled object
  25.         mesh = GetComponent<MeshRenderer> ();
  26.         if (mesh == null)
  27.             print ("CulledObjects require a mesh component on the same object, in order to enable and disable the object.");
  28.     }
  29.    
  30.     void Update ()
  31.     {
  32.         // calculates if object is within the camera distances range
  33.         var inRange = InDistanceOfCamera ();
  34.        
  35.         // if the object is not within the camera range, set out of distance
  36.         if (!inRange)
  37.             OnObjectOutOfDistance ();
  38.         // if the object is in range, is viewed and was out of distance last frame
  39.         else if (ObjectIsViewedByCamera () && outOfDistanceLastFrame)
  40.             OnObjectInDistance ();
  41.         // if the object is: in range, not viewed and was not out of distance last frame
  42.         else if (!ObjectIsViewedByCamera () && !outOfDistanceLastFrame)
  43.             OnObjectOutOfDistance ();
  44.     }
  45.    
  46.     /// <summary>
  47.     /// Checks if the object is currently viewed by the players camera
  48.     /// </summary>
  49.     bool ObjectIsViewedByCamera ()
  50.     {
  51.         var view = playerCamera.WorldToViewportPoint (transform.position);
  52.         return ((ValueIsClose (view.x)) && (ValueIsClose (view.y)) && (view.z >= 0));
  53.     }
  54.    
  55.     /// <summary>
  56.     /// Checks if the float is between 0 and 1
  57.     /// </summary>
  58.     /// <param name="val">The value to check.</param>
  59.     /// <returns></returns>
  60.     static bool ValueIsClose (float val)
  61.     {
  62.         return ((val > -1.5) && (val < 1.5));
  63.     }
  64.    
  65.     /// <summary>
  66.     /// Calculates the distance between the gameObject and the players camera.
  67.     /// </summary>
  68.     /// <returns>Distance between the transform and player.</returns>
  69.     float GetCullDistance ()
  70.     {
  71.         return Math.Abs (Vector3.Distance (transform.position, playerCamera.transform.position));
  72.     }
  73.    
  74.     /// <summary>
  75.     /// Calls the appropiate events for the object being in or out of distance.
  76.     /// </summary>
  77.     /// <param name="distance"></param>
  78.     bool InDistanceOfCamera ()
  79.     {
  80.         return (GetCullDistance () < RenderDistance);
  81.     }
  82.    
  83.     /// <summary>
  84.     /// Called when the object is out of the distance of the camera - and it was in distance last frame.
  85.     /// </summary>
  86.     void OnObjectOutOfDistance ()
  87.     {
  88.         outOfDistanceLastFrame = true;
  89.        
  90.         if (mesh.enabled)
  91.             mesh.enabled = false;
  92.     }
  93.    
  94.     /// <summary>
  95.     /// Called when the object is in distance of the camera - and it was not in distance last frame.
  96.     /// </summary>
  97.     void OnObjectInDistance ()
  98.     {
  99.         outOfDistanceLastFrame = false;
  100.        
  101.         if (!mesh.enabled)
  102.             mesh.enabled = true;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement