Skip to content

Instantly share code, notes, and snippets.

@limdingwen
Created April 17, 2020 00:54
Show Gist options
  • Save limdingwen/726cd5728ade9a5f66f7dcc174f21203 to your computer and use it in GitHub Desktop.
Save limdingwen/726cd5728ade9a5f66f7dcc174f21203 to your computer and use it in GitHub Desktop.
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