quantum-space-buddies/QSB/QuantumSync/Patches/QuantumVisibilityPatches.cs

55 lines
2.2 KiB
C#
Raw Normal View History

2020-12-22 09:13:08 +00:00
using QSB.Patches;
using QSB.Player;
using System.Linq;
using System.Reflection;
using UnityEngine;
namespace QSB.QuantumSync.Patches
2020-12-22 09:13:08 +00:00
{
public class QuantumVisibilityPatches : QSBPatch
{
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
2020-12-22 21:39:53 +00:00
public override void DoPatches()
2020-12-22 09:13:08 +00:00
{
QSBCore.Helper.HarmonyHelper.AddPrefix<ShapeVisibilityTracker>("IsVisibleUsingCameraFrustum", typeof(QuantumVisibilityPatches), nameof(ShapeIsVisibleUsingCameraFrustum));
QSBCore.Helper.HarmonyHelper.AddPrefix<ShapeVisibilityTracker>("IsVisible", typeof(QuantumVisibilityPatches), nameof(ShapeIsVisible));
QSBCore.Helper.HarmonyHelper.AddPrefix<RendererVisibilityTracker>("IsVisibleUsingCameraFrustum", typeof(QuantumVisibilityPatches), nameof(RenderIsVisibleUsingCameraFrustum));
}
// ShapeVisibilityTracker patches
public static bool ShapeIsVisibleUsingCameraFrustum(ShapeVisibilityTracker __instance, ref bool __result)
{
2020-12-22 21:39:53 +00:00
__result = __instance.gameObject.activeInHierarchy
2020-12-22 09:13:08 +00:00
&& QSBPlayerManager.GetPlayerCameras()
.Any(x => (bool)__instance.GetType()
.GetMethod("IsInFrustum", BindingFlags.NonPublic | BindingFlags.Instance)
.Invoke(__instance, new object[] { x.GetFrustumPlanes() }));
return false;
}
public static bool ShapeIsVisible(ShapeVisibilityTracker __instance, ref bool __result)
{
2020-12-22 21:39:53 +00:00
__result = __instance.gameObject.activeInHierarchy
2020-12-22 09:13:08 +00:00
&& __instance.IsVisibleUsingCameraFrustum()
&& QSBPlayerManager.GetPlayerCameras()
.Any(x => VisibilityOccluder.CanYouSee(__instance, x.mainCamera.transform.position));
return false;
}
// RendererVisibilityTracker patches
2020-12-22 21:39:53 +00:00
public static bool RenderIsVisibleUsingCameraFrustum(RendererVisibilityTracker __instance, ref bool __result, Renderer ____renderer, bool ____checkFrustumOcclusion)
2020-12-22 09:13:08 +00:00
{
__result = QSBPlayerManager.GetPlayerCameras()
2020-12-22 21:39:53 +00:00
.Any(x => GeometryUtility.TestPlanesAABB(x.GetFrustumPlanes(), ____renderer.bounds))
2020-12-22 09:13:08 +00:00
&& (!____checkFrustumOcclusion || QSBPlayerManager.GetPlayerCameras()
.Any(x => !(bool)__instance.GetType()
.GetMethod("IsOccludedFromPosition", BindingFlags.NonPublic | BindingFlags.Instance)
.Invoke(__instance, new object[] { x.transform.position })));
return false;
}
}
}