-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrustumCullingPreview.cs
52 lines (46 loc) · 1.71 KB
/
FrustumCullingPreview.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using UnityEngine.Rendering;
/// <summary>
/// Created by Kenamis of Rainy Reign Games
/// </summary>
namespace RainyReignGames.Utilities
{
/// <summary>
/// Place this script on the camera that you want its culling matrix to get applied to the scene view camera.
/// </summary>
[ExecuteInEditMode, RequireComponent(typeof(Camera))]
public class FrustumCullingPreview : MonoBehaviour
{
Camera cullingCamera;
ScriptableCullingParameters cameraCullingParameters;
void OnEnable()
{
cullingCamera = GetComponent<Camera>();
RenderPipelineManager.beginCameraRendering += OnBeginCameraRendering;
RenderPipelineManager.endCameraRendering += OnEndCameraRendering;
}
private void OnDisable()
{
RenderPipelineManager.beginCameraRendering -= OnBeginCameraRendering;
RenderPipelineManager.endCameraRendering -= OnEndCameraRendering;
}
private void OnBeginCameraRendering(ScriptableRenderContext context, Camera camera)
{
if (ReferenceEquals(camera, SceneView.lastActiveSceneView.camera))
{
camera.cullingMatrix = cullingCamera.projectionMatrix * cullingCamera.worldToCameraMatrix;
camera.cullingMask = cullingCamera.cullingMask;
}
}
private void OnEndCameraRendering(ScriptableRenderContext context, Camera camera)
{
if (ReferenceEquals(camera, SceneView.lastActiveSceneView.camera))
{
camera.ResetCullingMatrix();
}
}
}
}
#endif