Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using System.Collections;
- public class CulledObject : MonoBehaviour
- {
- /// <summary>
- /// Defines the render distance between objects, if the object is within this distance then it will render.
- /// </summary>
- public static float RenderDistance = 20;
- private Camera playerCamera; // the camera linked to the player
- private MeshRenderer mesh; // the mesh to enable/disable during the distance check
- private bool outOfDistanceLastFrame; // if we were out of distance in the last Update()
- void Start ()
- {
- // attempt to get the camera controlled by the player
- playerCamera = GameObject.FindGameObjectWithTag ("MainCamera").GetComponent<Camera> ();
- if (playerCamera == null)
- print ("The main camera was not found inside CulledObject script.");
- // attempt to get the meshRenderer component on the culled object
- mesh = GetComponent<MeshRenderer> ();
- if (mesh == null)
- print ("CulledObjects require a mesh component on the same object, in order to enable and disable the object.");
- }
- void Update ()
- {
- // calculates if object is within the camera distances range
- var inRange = InDistanceOfCamera ();
- // if the object is not within the camera range, set out of distance
- if (!inRange)
- OnObjectOutOfDistance ();
- // if the object is in range, is viewed and was out of distance last frame
- else if (ObjectIsViewedByCamera () && outOfDistanceLastFrame)
- OnObjectInDistance ();
- // if the object is: in range, not viewed and was not out of distance last frame
- else if (!ObjectIsViewedByCamera () && !outOfDistanceLastFrame)
- OnObjectOutOfDistance ();
- }
- /// <summary>
- /// Checks if the object is currently viewed by the players camera
- /// </summary>
- bool ObjectIsViewedByCamera ()
- {
- var view = playerCamera.WorldToViewportPoint (transform.position);
- return ((ValueIsClose (view.x)) && (ValueIsClose (view.y)) && (view.z >= 0));
- }
- /// <summary>
- /// Checks if the float is between 0 and 1
- /// </summary>
- /// <param name="val">The value to check.</param>
- /// <returns></returns>
- static bool ValueIsClose (float val)
- {
- return ((val > -1.5) && (val < 1.5));
- }
- /// <summary>
- /// Calculates the distance between the gameObject and the players camera.
- /// </summary>
- /// <returns>Distance between the transform and player.</returns>
- float GetCullDistance ()
- {
- return Math.Abs (Vector3.Distance (transform.position, playerCamera.transform.position));
- }
- /// <summary>
- /// Calls the appropiate events for the object being in or out of distance.
- /// </summary>
- /// <param name="distance"></param>
- bool InDistanceOfCamera ()
- {
- return (GetCullDistance () < RenderDistance);
- }
- /// <summary>
- /// Called when the object is out of the distance of the camera - and it was in distance last frame.
- /// </summary>
- void OnObjectOutOfDistance ()
- {
- outOfDistanceLastFrame = true;
- if (mesh.enabled)
- mesh.enabled = false;
- }
- /// <summary>
- /// Called when the object is in distance of the camera - and it was not in distance last frame.
- /// </summary>
- void OnObjectInDistance ()
- {
- outOfDistanceLastFrame = false;
- if (!mesh.enabled)
- mesh.enabled = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement