Skip to content

Instantly share code, notes, and snippets.

@spaceemotion
Created December 24, 2016 22:09
Show Gist options
  • Save spaceemotion/c3e8ac144cbf9a9e8bca3baa8f005a39 to your computer and use it in GitHub Desktop.
Save spaceemotion/c3e8ac144cbf9a9e8bca3baa8f005a39 to your computer and use it in GitHub Desktop.
Free dynamic (run time) object occlusion scripts (https://www.reddit.com/r/Unity3D/comments/5k1gqv)
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OcclusionBase : MonoBehaviour {
public bool isVisible { get; set; }
public DateTime lastSeen { get; set; }
}
using System;
using UnityEngine;
public class OcclusionCamera : MonoBehaviour {
void Start () {
if (!Configs.EnableObjectOcclusion)
{
return;
}
Camera camera = gameObject.GetComponent<Camera>();
camera.farClipPlane = Configs.ViewDistance;
InvokeRepeating("CheckView", 0, Configs.OcclusionSampleDelay);
}
void CheckView () {
int step = Math.Max(Screen.width, Screen.height) / Configs.OcclusionSamples;
for (int x = 0; x < Screen.width; x += step)
{
for (int y = 0; y < Screen.height; y += step)
{
Ray SampleRay = Camera.main.ScreenPointToRay(new Vector3(x, y, 0f));
RaycastHit hit;
if (Physics.Raycast(SampleRay, out hit, Configs.ViewDistance))
{
//Debug.DrawRay(SampleRay.origin, SampleRay.direction, Color.red, 1);
GameObject target = hit.transform.gameObject;
OcclusionBase thisTargetScript = target.GetComponent<OcclusionBase>();
while(thisTargetScript == null && target.transform.parent != null)
{
if(target.transform.parent != null)
{
target = target.transform.parent.gameObject;
}
thisTargetScript = target.GetComponent<OcclusionBase>();
};
if (thisTargetScript != null)
{
thisTargetScript.lastSeen = DateTime.Now;
thisTargetScript.isVisible = true;
}
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class OcclusionObject : OcclusionBase
{
List<Renderer> renderers { get; set; }
List<Light> lights { get; set; }
private bool isVisibleLast { get; set; }
float PlayerDistance { get; set; }
void Start () {
if (!enabled || !Configs.EnableObjectOcclusion) { return; }
renderers = GetChildRenderers(gameObject);
lights = GetChildLights(gameObject);
Renderer thisRenderer = gameObject.GetComponent<Renderer>();
if (thisRenderer != null) {
renderers.Add(thisRenderer);
}
if (renderers != null || renderers.Count == 0)
{
InvokeRepeating("CheckStatus", Configs.OcclusionSampleDelay, Configs.OcclusionSampleDelay);
InvokeRepeating("Hide", Configs.OcclusionSampleDelay, Configs.OcclusionHideDelay);
}
isVisible = true;
}
public void Hide()
{
if ((DateTime.Now - lastSeen).TotalSeconds > Configs.OcclusionHideDelay && PlayerDistance > Configs.MinOcclusionDistance)
{
isVisible = false;
}
}
List<Light> GetChildLights(GameObject toSearch)
{
List<Light> theseLights = toSearch.GetComponentsInChildren<Light>().ToList();
return theseLights;
}
List<Renderer> GetChildRenderers(GameObject toSearch)
{
List<Renderer> theseRenderers = new List<Renderer>();
int ChildCount = toSearch.transform.childCount;
for (int i = 0; i < ChildCount; i++)
{
GameObject thisChild = toSearch.transform.GetChild(i).gameObject;
if (thisChild.GetComponent<OcclusionObject>() == null)
{
Renderer thisRenderer = thisChild.GetComponent<Renderer>();
if (thisRenderer != null)
{
theseRenderers.Add(thisRenderer);
}
theseRenderers.AddRange(GetChildRenderers(thisChild));
}
}
return theseRenderers;
}
public void Toggle()
{
PlayerDistance = Vector3.Distance(GameObjects.Player.transform.position, transform.position);
bool visible = isVisible || PlayerDistance < Configs.MinOcclusionDistance;
foreach (Renderer thisRenderer in renderers)
{
if(thisRenderer.enabled == isVisible) { return; } //they're probably all the same
thisRenderer.enabled = visible;
}
foreach (Light thisLight in lights)
{
thisLight.enabled = visible;
}
}
public void CheckStatus()
{
Toggle();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OcclusionTerrain : OcclusionBase {
Terrain terrain { get; set; }
private bool isVisibleLast { get; set; }
void Start()
{
if (!Configs.EnableObjectOcclusion) { return; }
terrain = GetComponent<Terrain>();
isVisible = true;
InvokeRepeating("CheckStatus", Configs.OcclusionSampleDelay, Configs.OcclusionHideDelay);
}
public void Toggle()
{
terrain.drawHeightmap = isVisible;
terrain.drawTreesAndFoliage = isVisible;
isVisible = false;
}
public void CheckStatus()
{
Toggle();
}
}
@spaceemotion
Copy link
Author

This is an archived copy of the hastebin links by the reddit comment linked in the title.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment