summaryrefslogtreecommitdiff
path: root/RTP_DeferredParams.cs
blob: 2730d4a93a3d2eed7bcf256f41a74ae4c2a23f42 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

[AddComponentMenu("Relief Terrain/Helpers/Deferred Params")]
[RequireComponent(typeof(Camera))]
[DisallowMultipleComponent]
[ExecuteInEditMode]
public class RTP_DeferredParams : MonoBehaviour
{
	private Camera mycam;

	private CommandBuffer combufPreLight;

	private CommandBuffer combufPostLight;

	public Material CopyPropsMat;

	private HashSet<Camera> sceneCamsWithBuffer = new HashSet<Camera>();

	public void OnEnable()
	{
		if (NotifyDecals())
		{
			return;
		}
		if (mycam == null)
		{
			mycam = GetComponent<Camera>();
			if (mycam == null)
			{
				return;
			}
		}
		Initialize();
		Camera.onPreRender = (Camera.CameraCallback)Delegate.Combine(Camera.onPreRender, new Camera.CameraCallback(SetupCam));
	}

	public void OnDisable()
	{
		NotifyDecals();
		Cleanup();
	}

	public void OnDestroy()
	{
		NotifyDecals();
		Cleanup();
	}

	private bool NotifyDecals()
	{
		Type type = Type.GetType("UBERDecalSystem.DecalManager");
		if (type != null)
		{
			bool flag = false;
			if (UnityEngine.Object.FindObjectOfType(type) != null && UnityEngine.Object.FindObjectOfType(type) is MonoBehaviour && (UnityEngine.Object.FindObjectOfType(type) as MonoBehaviour).enabled)
			{
				(UnityEngine.Object.FindObjectOfType(type) as MonoBehaviour).Invoke("OnDisable", 0f);
				(UnityEngine.Object.FindObjectOfType(type) as MonoBehaviour).Invoke("OnEnable", 0f);
				return true;
			}
		}
		return false;
	}

	private void Cleanup()
	{
		if (combufPreLight != null)
		{
			if ((bool)mycam)
			{
				mycam.RemoveCommandBuffer(CameraEvent.BeforeReflections, combufPreLight);
				mycam.RemoveCommandBuffer(CameraEvent.AfterLighting, combufPostLight);
			}
			foreach (Camera item in sceneCamsWithBuffer)
			{
				if ((bool)item)
				{
					item.RemoveCommandBuffer(CameraEvent.BeforeReflections, combufPreLight);
					item.RemoveCommandBuffer(CameraEvent.AfterLighting, combufPostLight);
				}
			}
		}
		sceneCamsWithBuffer.Clear();
		Camera.onPreRender = (Camera.CameraCallback)Delegate.Remove(Camera.onPreRender, new Camera.CameraCallback(SetupCam));
	}

	private void SetupCam(Camera cam)
	{
		bool flag = false;
		if (cam == mycam || flag)
		{
			RefreshComBufs(cam, flag);
		}
	}

	public void RefreshComBufs(Camera cam, bool isSceneCam)
	{
		if (!cam || combufPreLight == null || combufPostLight == null)
		{
			return;
		}
		CommandBuffer[] commandBuffers = cam.GetCommandBuffers(CameraEvent.BeforeReflections);
		bool flag = false;
		CommandBuffer[] array = commandBuffers;
		foreach (CommandBuffer commandBuffer in array)
		{
			if (commandBuffer.name == combufPreLight.name)
			{
				flag = true;
				break;
			}
		}
		if (!flag)
		{
			cam.AddCommandBuffer(CameraEvent.BeforeReflections, combufPreLight);
			cam.AddCommandBuffer(CameraEvent.AfterLighting, combufPostLight);
			if (isSceneCam)
			{
				sceneCamsWithBuffer.Add(cam);
			}
		}
	}

	public void Initialize()
	{
		if (combufPreLight != null)
		{
			return;
		}
		int num = Shader.PropertyToID("_UBERPropsBuffer");
		if (CopyPropsMat == null)
		{
			if (CopyPropsMat != null)
			{
				UnityEngine.Object.DestroyImmediate(CopyPropsMat);
			}
			CopyPropsMat = new Material(Shader.Find("Hidden/UBER_CopyPropsTexture"));
			CopyPropsMat.hideFlags = HideFlags.DontSave;
		}
		combufPreLight = new CommandBuffer();
		combufPreLight.name = "UBERPropsPrelight";
		combufPreLight.GetTemporaryRT(num, -1, -1, 0, FilterMode.Point, RenderTextureFormat.RHalf);
		combufPreLight.Blit(BuiltinRenderTextureType.CameraTarget, num, CopyPropsMat);
		combufPostLight = new CommandBuffer();
		combufPostLight.name = "UBERPropsPostlight";
		combufPostLight.ReleaseTemporaryRT(num);
	}
}