Skip to content

Commit

Permalink
Fixed meta file problem
Browse files Browse the repository at this point in the history
  • Loading branch information
bas-boop committed Apr 22, 2024
1 parent 4c8dd26 commit 4bfd186
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
8 changes: 0 additions & 8 deletions Assets/Scenes/Testing/FrustumCulling.meta

This file was deleted.

61 changes: 61 additions & 0 deletions Assets/Scripts/Environment/FrustumCulling.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using UnityEngine;

namespace Environment
{
[RequireComponent(typeof(Camera))]
public sealed class FrustumCulling : MonoBehaviour
{
private const string CULL_ABLE_TAG = "Cullable";

private GameObject[] _cullAbleObjects;
private Renderer[] _cachedRenderers;
private Camera _this;

private void Awake() => _this = GetComponent<Camera>();

private void Start() => InitCullingObjects();

private void Update() => UpdateCullAbles();

private void InitCullingObjects()
{
_cullAbleObjects = GameObject.FindGameObjectsWithTag(CULL_ABLE_TAG);
_cachedRenderers = new Renderer[_cullAbleObjects.Length];
int length = _cullAbleObjects.Length;

for (int i = 0; i < length; i++)
{
_cachedRenderers[i] = _cullAbleObjects[i].GetComponent<Renderer>();
}
}

private void UpdateCullAbles()
{
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(_this);
int length = _cullAbleObjects.Length;

for (int i = 0; i < length; i++)
{
bool isVisible = GeometryUtility.TestPlanesAABB(planes, _cachedRenderers[i].bounds);

OcclusionCulling(ref isVisible, i);

_cullAbleObjects[i].SetActive(isVisible);
}
}

private void OcclusionCulling(ref bool isVisible, int i)
{
if (!isVisible)
return;

Vector3 direction = _cullAbleObjects[i].transform.position - _this.transform.position;
float distance = direction.magnitude;
RaycastHit hit;

if (Physics.Raycast(_this.transform.position, direction, out hit, distance)
&& hit.transform != _cullAbleObjects[i].transform)
isVisible = false;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Environment/FrustumCulling.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4bfd186

Please sign in to comment.