Created
April 17, 2020 00:54
-
-
Save limdingwen/726cd5728ade9a5f66f7dcc174f21203 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
using UnityEngine.TestTools; | |
/// <summary> | |
/// Starts the recursive portal rendering process, taking into account portal occlusion volumes. | |
/// </summary> | |
public class PortalRenderer : MonoBehaviour | |
{ | |
public static PortalRenderer Instance; | |
[SerializeField] private Camera portalCamera; | |
[SerializeField] private int maxRecursions = 2; | |
[SerializeField] private LayerMask noCloneMask; | |
[SerializeField] private LayerMask renderCloneMask; | |
/// <summary> | |
/// Debug read only: Indicates the total amount of recursive renders performed by portals this frame. | |
/// </summary> | |
public int debugTotalRenderCount; | |
/// <summary> | |
/// Debug read only: Indicates how many portal renders were due to being directly visible by the player. | |
/// Use this field to debug the size of portal occlusion volumes. | |
/// </summary> | |
public int debugDirectRenderCount; | |
private void Awake() | |
{ | |
Instance = this; | |
} | |
private void OnPreRender() | |
{ | |
// Find the current occlusion volume | |
var currentOcclusionVolume = | |
PortalOcclusionVolumeManager.Instance.FindOcclusionVolumeContainingPosition(GameManager.Instance.MainCamera.transform.position); | |
// Render portals in occlusion volume | |
if (currentOcclusionVolume == null) | |
return; | |
var mainCamera = GameManager.Instance.MainCamera; | |
var mainCameraTransform = mainCamera.transform; | |
var cameraPlanes = GeometryUtility.CalculateFrustumPlanes(mainCamera); | |
debugTotalRenderCount = 0; | |
debugDirectRenderCount = 0; | |
foreach (var portal in currentOcclusionVolume.Portals) | |
{ | |
if (!portal.ShouldRender(cameraPlanes)) | |
continue; | |
portal.RenderViewthroughRecursive( | |
mainCameraTransform.position, | |
mainCameraTransform.rotation, | |
out _, | |
out _, | |
out var renderCount, | |
portalCamera, | |
0, | |
maxRecursions, | |
currentOcclusionVolume, | |
noCloneMask, | |
renderCloneMask, | |
PortalableObjectManager.Instance.FpsPovPortalableObject | |
? PortalableObjectManager.Instance.FpsPovPortalableObject.CurrentTouchingPortalSender | |
: null); | |
debugTotalRenderCount += renderCount; | |
debugDirectRenderCount++; | |
} | |
} | |
private void OnPostRender() | |
{ | |
PortalRenderTexturePoolManager.Instance.ReleaseAll(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment