forked from eldskald/planar-reflections-unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlanarReflectionsProbe.cs
220 lines (194 loc) · 7.48 KB
/
PlanarReflectionsProbe.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
///////////////////////////////////////////////////////////////////////////////
// //
// Planar Reflections Probe for Unity's Built-in Render Pipeline //
// //
// Author: Rafael Bordoni //
// Date: January 25, 2022 //
// Last Update: January 23, 2023 //
// Email: [email protected] //
// Repository: https://github.com/eldskald/planar-reflections-unity //
// //
///////////////////////////////////////////////////////////////////////////////
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteAlways, AddComponentMenu("Rendering/Planar Reflections Probe")]
public class PlanarReflectionsProbe : MonoBehaviour {
[Range(1, 4)] public int targetTextureID = 1;
[Space(10)]
public bool useCustomNormal = false;
public Vector3 customNormal;
[Space(10)]
[Range(0.01f, 1.0f)] public float reflectionsQuality = 1f;
public float farClipPlane = 1000;
public bool renderBackground = true;
[Space(10)]
public bool renderInEditor = false;
private GameObject _probeGO;
private Camera _probe;
private Skybox _probeSkybox;
private ArrayList _ignoredCameras = new ArrayList();
private void OnEnable () {
Camera.onPreRender += PreRenderRoutine;
Camera.onPostRender += PostRenderRoutine;
}
private void OnDisable () {
FinalizeProbe();
Camera.onPreRender -= PreRenderRoutine;
Camera.onPostRender -= PostRenderRoutine;
}
private void OnDestroy () {
FinalizeProbe();
Camera.onPreRender -= PreRenderRoutine;
Camera.onPostRender -= PostRenderRoutine;
}
private void InitializeProbe () {
_probeGO = new GameObject("", typeof(Camera), typeof(Skybox));
_probeGO.name = "PRCamera" + _probeGO.GetInstanceID().ToString();
_probeGO.hideFlags = HideFlags.HideAndDontSave;
_probe = _probeGO.GetComponent<Camera>();
_probeSkybox = _probeGO.GetComponent<Skybox>();
_probeSkybox.enabled = false;
_probeSkybox.material = null;
}
private void FinalizeProbe () {
if (_probe == null) {
return;
}
if (Application.isEditor) {
DestroyImmediate(_probeGO);
}
else {
Destroy(_probeGO);
}
}
private bool CheckCamera (Camera cam) {
if (cam.cameraType == CameraType.Reflection) {
return true;
}
else if (!renderInEditor && cam.cameraType == CameraType.SceneView) {
return true;
}
else if (_ignoredCameras.Contains(cam)) {
return true;
}
return false;
}
private void PreRenderRoutine (Camera cam) {
if (CheckCamera(cam)) {
return;
}
else if (_probe == null) {
InitializeProbe();
}
Vector3 normal = GetNormal();
UpdateProbeSettings(cam);
CreateRenderTexture(cam);
UpdateProbeTransform(cam, normal);
CalculateObliqueProjection(normal);
_probe.Render();
string texName = "_PlanarReflectionsTex" + targetTextureID.ToString();
_probe.targetTexture.SetGlobalShaderProperty(texName);
}
private void PostRenderRoutine (Camera cam) {
if (CheckCamera(cam) || _probe == null) {
return;
}
_probe.targetTexture.Release();
_probe.targetTexture = null;
}
private void UpdateProbeSettings (Camera cam) {
_probe.CopyFrom(cam);
_probe.enabled = false;
_probe.cameraType = CameraType.Reflection;
_probe.usePhysicalProperties = false;
_probe.farClipPlane = farClipPlane;
_probeSkybox.material = null;
_probeSkybox.enabled = false;
if (renderBackground) {
_probe.clearFlags = cam.clearFlags;
if (cam.GetComponent<Skybox>()) {
Skybox camSkybox = cam.GetComponent<Skybox>();
_probeSkybox.material = camSkybox.material;
_probeSkybox.enabled = camSkybox.enabled;
}
}
else {
_probe.clearFlags = CameraClearFlags.Nothing;
}
}
private void CreateRenderTexture (Camera cam) {
int width = (int)((float)cam.pixelWidth * reflectionsQuality);
int height = (int)((float)cam.pixelHeight * reflectionsQuality);
_probe.targetTexture = new RenderTexture(width, height, 24);
_probe.targetTexture.Create();
}
private Vector3 GetNormal () {
if (!useCustomNormal) {
return transform.forward;
}
else if (customNormal.Equals(Vector3.zero)) {
return Vector3.up;
}
else {
return customNormal.normalized;
}
}
// The probe's camera position should be the the current camera's position
// mirrored by the reflecting plane. Its rotation mirrored too.
private void UpdateProbeTransform (Camera cam, Vector3 normal) {
Vector3 proj = normal * Vector3.Dot(
normal, cam.transform.position - transform.position);
_probe.transform.position = cam.transform.position - 2 * proj;
Vector3 probeForward = Vector3.Reflect(cam.transform.forward, normal);
Vector3 probeUp = Vector3.Reflect(cam.transform.up, normal);
_probe.transform.LookAt(
_probe.transform.position + probeForward, probeUp);
}
// The clip plane should coincide with the plane with reflections.
private void CalculateObliqueProjection (Vector3 normal) {
Matrix4x4 viewMatrix = _probe.worldToCameraMatrix;
Vector3 viewPosition = viewMatrix.MultiplyPoint(transform.position);
Vector3 viewNormal = viewMatrix.MultiplyVector(normal);
Vector4 plane = new Vector4(
viewNormal.x, viewNormal.y, viewNormal.z,
-Vector3.Dot(viewPosition, viewNormal));
_probe.projectionMatrix = _probe.CalculateObliqueMatrix(plane);
}
public void IgnoreCamera (Camera cam) {
if (!_ignoredCameras.Contains(cam)) {
_ignoredCameras.Add(cam);
}
}
public void UnignoreCamera (Camera cam) {
if (_ignoredCameras.Contains(cam)) {
_ignoredCameras.Remove(cam);
}
}
public void ClearIgnoredList () {
_ignoredCameras.Clear();
}
public bool IsIgnoring (Camera cam) {
return _ignoredCameras.Contains(cam);
}
public static PlanarReflectionsProbe[] FindProbesRenderingTo (int id) {
var probes = FindObjectsOfType<PlanarReflectionsProbe>();
ArrayList list = new ArrayList();
foreach (PlanarReflectionsProbe probe in probes) {
if (probe.targetTextureID == id) {
list.Add(probe);
}
}
return (PlanarReflectionsProbe[])list.ToArray(
typeof(PlanarReflectionsProbe));
}
public static PlanarReflectionsProbe FindProbeRenderingTo (int id) {
var probes = FindObjectsOfType<PlanarReflectionsProbe>();
foreach (PlanarReflectionsProbe probe in probes) {
if (probe.targetTextureID == id) {
return probe;
}
}
return null;
}
}