-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlaneRenderer.cs
438 lines (379 loc) · 10 KB
/
PlaneRenderer.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#pragma warning disable 0649
using ollyisonit.UnityEditorAttributes;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
namespace ollyisonit.UnityPlaneRenderer
{
/// <summary>
/// Renders a two-dimensional image to a plane using given material as base.
/// </summary>
[ExecuteInEditMode, DisallowMultipleComponent]
public class PlaneRenderer : MonoBehaviour
{
[SerializeField, Tooltip("Texture to display on front")]
private Texture2D frontImage;
[SerializeField, Tooltip("Texture to display on back")]
private Texture2D backImage;
[SerializeField, Tooltip("Should back image be mirrored?")]
private bool mirrorBackImage;
/// <summary>
/// How many pixels in the image should be taken as one meter in world space?
/// </summary>
[SerializeField, Tooltip("How many pixels in the image should be taken as one meter in world space?")]
private float pixelsPerMeter;
/// <summary>
/// Where transform anchor will be. (0, 0) is bottom left, (1, 1) is top right.
/// </summary>
[SerializeField, Tooltip("Where transform anchor will be. (0, 0) is bottom left, (1, 1) is top right.")]
private Vector2 anchorPoint;
/// <summary>
/// Material to use for rendering image. Material will be copied on use, image will be applied to shader's main texture.
/// </summary>
[SerializeField, Tooltip("Material to use for rendering image. " +
"Material will be copied on use, image will be applied to shader's main texture.")]
private Material material;
[SerializeField, Tooltip("Distance between each plane and the center. Setting this number too small can create lighting errors.")]
private float Offset;
private HashSet<Object> deletionQueue = new HashSet<Object>();
/// <summary>
/// Image to display on front.
/// </summary>
public Texture2D FrontImage
{
get => frontImage;
set
{
frontImage = value;
Recalculate();
}
}
/// <summary>
/// Image to display on back.
/// </summary>
public Texture2D BackImage
{
get => backImage;
set
{
backImage = value;
Recalculate();
}
}
/// <summary>
/// How many pixels in the image should be taken as one meter in world space?
/// </summary>
public float PixelsPerMeter
{
get => PixelsPerMeter;
set
{
pixelsPerMeter = value;
Recalculate();
}
}
/// <summary>
/// Where transform gimbal will be. (0, 0) is bottom left, (1, 1) is top right.
/// </summary>
public Vector2 AnchorPoint
{
get => anchorPoint;
set
{
anchorPoint = value;
Recalculate();
}
}
/// <summary>
/// Material to use for rendering image. Material will be copied on use, image will be applied to shader's main texture.
/// </summary>
public Material Material
{
get => material;
set
{
material = value;
Recalculate();
}
}
/// <summary>
/// Should the plane cast shadows?
/// </summary>
[SerializeField, Tooltip("How should shadows be handled?")]
private ShadowCastingMode castShadows;
// These classes must be referenced to prevent build errors with CreatePrimitive.
private MeshFilter insuranceFilter;
private MeshRenderer insuranceRenderer;
private BoxCollider insuranceBox;
private SphereCollider insuranceSphere;
[SerializeField, ReadOnly, Tooltip("GameObject used for the front plane")]
private GameObject front;
[SerializeField, ReadOnly, Tooltip("GameObject used for the back plane")]
private GameObject back;
#region UnityMethods
#if UNITY_EDITOR
/// <summary>
/// Recalculates dimensions when editor values change.
/// </summary>
void OnValidate()
{
if (pixelsPerMeter <= 0)
{
pixelsPerMeter = float.Epsilon;
}
if (material != null)
{
EditorApplication.delayCall += RecalculateExistingFrontAndBack;
}
}
void Reset()
{
pixelsPerMeter = 100;
Offset = float.Epsilon;
CleanChildren(false);
Recalculate();
}
void Update()
{
if (!EditorApplication.isPlaying)
{
CreateFrontAndBackIfNull();
SetLocalPosition(frontImage, front, Offset);
SetLocalPosition(backImage, back, -Offset);
CleanChildren();
}
}
#endif
void OnDestroy()
{
SafeDestroy(front);
SafeDestroy(back);
}
void Start()
{
#if UNITY_EDITOR
if (EditorApplication.isPlaying)
{
#endif
front = null;
back = null;
CleanChildren(false);
Recalculate();
#if UNITY_EDITOR
}
#endif
}
#endregion
/// <summary>
/// Ensures that front and back aren't null and have been reset back to a default state with all their necessary components attached.
/// </summary>
private void ResetFrontAndBack()
{
if (front != null)
{
SafeDestroy(front);
}
front = CreateQuad("front", 180);
if (back != null)
{
SafeDestroy(back);
}
back = CreateQuad("back", 0);
}
private void CreateFrontAndBackIfNull()
{
bool shouldRecalculate = false;
if (front == null || back == null)
{
shouldRecalculate = true;
}
if (front == null)
{
front = CreateQuad("front", 180);
Debug.LogWarning("GameObject 'front' cannot be deleted because it is part of a PlaneRenderer!");
}
if (back == null)
{
back = CreateQuad("back", 0);
Debug.LogWarning("GameObject 'back' cannot be deleted because it is part of a PlaneRenderer!");
}
if (shouldRecalculate)
{
RecalculateExistingFrontAndBack();
}
}
/// <summary>
/// Creates a colliderless Quad with the given name as a child of this GameObject.
/// </summary>
private GameObject CreateQuad(string name, float rotation)
{
GameObject o = GameObject.CreatePrimitive(PrimitiveType.Quad);
o.name = name;
o.transform.SetParent(transform);
SafeDestroy(o.GetComponent<MeshCollider>());
o.GetComponent<MeshRenderer>().shadowCastingMode = castShadows;
o.transform.localRotation = Quaternion.identity;
o.transform.Rotate(new Vector3(0, rotation, 0));
o.transform.localPosition = new Vector3(0, 0, 0);
return o;
}
/// <summary>
/// Destroys a GameObject's material and resets its rotation.
/// </summary>
private void Clean(GameObject o)
{
if (o != null)
{
SafeDestroy(o.GetComponent<Renderer>().sharedMaterial);
o.transform.localRotation = Quaternion.Euler(new Vector3(0, 0, 0));
}
}
/// <summary>
/// Destroys all children of this GameObject that aren't the front or back quads.
/// </summary>
private void CleanChildren(bool showMessage = true)
{
foreach (Transform child in transform)
{
if (child != null &&
(front == null || child != front?.transform) &&
(back == null || child != back?.transform))
{
if (child != null && child.gameObject != null && !deletionQueue.Contains(child.gameObject))
{
if (showMessage)
{
Debug.LogWarning("GameObject '" + child.gameObject.name + "' was destroyed because it was added as a child of " +
"a PlaneRenderer.\nIf an object has a PlaneRenderer component, its only children can be the 'front' and 'back' objects.");
#if UNITY_EDITOR
Undo.DestroyObjectImmediate(child.gameObject);
#endif
}
if (child != null && child.gameObject != null)
{
SafeDestroy(child.gameObject);
}
}
}
}
}
/// <summary>
/// Sets the GameObject's texture to be the given image.
/// </summary>
/// <param name="o"></param>
private void SetTexture(GameObject o, Texture2D tex)
{
Renderer rend = o.GetComponent<Renderer>();
rend.material = Instantiate(material);
rend.sharedMaterial.mainTexture = tex;
}
/// <summary>
/// Recalculates the front and back quads.
/// </summary>
[ContextMenu("Recalculate")]
public void Recalculate()
{
ResetFrontAndBack();
RecalculateExistingFrontAndBack();
CleanChildren();
}
private void RecalculateExistingFrontAndBack()
{
if (front != null)
{
if (frontImage != null)
{
front.SetActive(true);
RecalculateDimensions(frontImage, front, false, Offset);
}
else
{
front.SetActive(false);
}
}
if (back != null)
{
if (backImage != null && back != null)
{
back.SetActive(true);
RecalculateDimensions(backImage, back, mirrorBackImage, -Offset);
}
else
{
back.SetActive(false);
}
}
}
/// <summary>
/// Recalculates the dimensions of the front and back quads, assuming they already exist.
/// </summary>
private void RecalculateDimensions(Texture2D image, GameObject plane, bool flip, float offset)
{
float width = image.width / pixelsPerMeter;
float height = image.height / pixelsPerMeter;
plane.transform.localScale = new Vector3(width * (flip ? -1 : 1), height, 1);
SetTexture(plane, image);
SetLocalPosition(image, plane, offset);
plane.GetComponent<MeshRenderer>().shadowCastingMode = castShadows;
#if UNITY_EDITOR
EditorUtility.SetDirty(plane);
EditorUtility.SetDirty(this.gameObject);
#endif
}
private void SetLocalPosition(Texture2D image, GameObject plane, float offset)
{
if (image != null && plane != null)
{
float width = image.width / pixelsPerMeter;
float height = image.height / pixelsPerMeter;
plane.transform.localPosition = new Vector3(-width / 2 + anchorPoint.x * width, height / 2 - anchorPoint.y * height, offset);
}
}
/// <summary>
/// Calls appropriate destroy method depending on whether game is running or not.
/// </summary>
/// <param name="go"></param>
private void SafeDestroy(UnityEngine.Object go)
{
RemoveNullObjects(deletionQueue);
if (go == null)
{
return;
}
#if UNITY_EDITOR
if (!EditorApplication.isPlaying)
{
deletionQueue.Add(go);
UnityEditor.EditorApplication.delayCall += () =>
{
deletionQueue.Remove(go);
DestroyImmediate(go, false);
};
}
else
{
#endif
deletionQueue.Add(go);
Destroy(go);
#if UNITY_EDITOR
}
#endif
}
private void RemoveNullObjects(HashSet<Object> set)
{
List<Object> toremove = new List<Object>();
foreach (Object o in set)
{
if (!o)
{
toremove.Add(o);
}
}
foreach (Object o in toremove)
{
set.Remove(o);
}
}
}
}