-
Notifications
You must be signed in to change notification settings - Fork 2
/
PreviewScene.cs
412 lines (352 loc) · 11.7 KB
/
PreviewScene.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
// Author: Chris Yarbrough
namespace Nementic
{
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;
/// <summary>
/// A hidden scene used to render custom previews.
/// </summary>
/// <remarks>
/// Create a reference to the preview scene via its constructor
/// and store it as a serialized member (SerializeField).
/// Call <see cref="Load()"/> before using any other parts
/// of the interface and dispose of the reference with <see cref="Destroy"/>
/// to clear up resources and avoid memory leaks in the editor.
///
/// Populate the scene via <see cref="Instantiate"/> or <see cref="Add"/>.
/// By default, the entire scene is rendered in the way it was set up,
/// but it is also possible to <see cref="Focus"/> a single object.
/// Refer to the target object by its index in the order it was added.
/// A focused object will be positioned at scene origin.
///
/// Use the <see cref="Camera"/> property to setup the camera and call
/// <see cref="Render"/> to retrieve a texture that shows the scene.
/// </remarks>
[Serializable]
public class PreviewScene
{
/*
* Implementation Notes
*
* Objects added to the preview scene are marked as HideAndDontSave
* so that they survive assembly reload. This design avoids unnecessary
* reloading and was chosen over an alternative in which the scene
* needs to be recreated in OnEnable e.g. before each enter-playmode.
* This may lead to memory leaks if callers forget to call Destroy,
* but still seems to be the best implementation since the Unity preview
* scene is also not unloaded unless explicitly called. In order for callers
* to dispose the struct correctly, they need to keep a serialized reference
* to it, so that the reference also survives assembly reload.
*
* Ideally, as a convenience to users, the code would detect memory leaks:
* In the constructor, remember the caller via reflection. In Destroy,
* set a flag indicating that resources have been cleared and prevent
* the finalizer from being called. In the finalizer check if the flag
* was set and if not, log an error. However, this doesn't work robustly,
* because of Unity serialization which will call the default constructor
* of a serialized reference
*/
/// <summary>
/// Indicates that GameObjects can be added to the scene
/// or that camera properties can be modified.
/// </summary>
public bool IsLoaded { get; private set; }
/// <summary>
/// The preview camera. Only valid when the scene is loaded.
/// </summary>
public CameraProxy Camera
{
get
{
if (cameraProxy == null || cameraProxy.IsValid == false)
cameraProxy = new CameraProxy(camera);
return cameraProxy;
}
}
public CustomRenderSettings CustomRenderSettings => customRenderSettings;
/// <summary>
/// The number of GameObjects that have been added
/// to the preview scene by the user.
/// </summary>
/// <remarks>
/// This does not include the already existing preview camera,
/// but it will count invisible objects added, e.g. lights.
/// </remarks>
public int ObjectCount => gameObjects?.Count ?? 0;
[NonSerialized]
private CameraProxy cameraProxy;
[SerializeField]
private CustomRenderSettings customRenderSettings = new CustomRenderSettings();
[SerializeField]
private Scene scene;
[SerializeField]
private Camera camera;
/// <summary>
/// All GameObjects added to the scene.
/// </summary>
[SerializeField]
private List<GameObject> gameObjects;
/// <summary>
/// The focused scene object. Null if all GameObjects are drawn.
/// </summary>
[SerializeField]
private GameObject currentTarget;
/// <summary>
/// A position that is not visible to the preview camera.
/// </summary>
private static readonly Vector3 offScreenPosition = new Vector3(1000f, 0f, 0f);
/// <summary>
/// Initialize the preview scene with a default render target.
/// Pairs with <see cref="Destroy"/>.
/// </summary>
public void Load()
{
Load(renderTargetSize: new Vector2Int(128, 128));
}
/// <summary>
/// Initializes the preview scene with the provided render target size.
/// Pairs with <see cref="Destroy"/>.
/// </summary>
/// <param name="renderTargetSize">The size of the render texture in pixels.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// If the render target size is less than one pixel.
/// </exception>
public void Load(Vector2Int renderTargetSize)
{
if (renderTargetSize.x < 1 || renderTargetSize.y < 1)
throw new ArgumentOutOfRangeException(nameof(renderTargetSize));
scene = EditorSceneManager.NewPreviewScene();
scene.name = "Preview Scene";
camera = CreateCamera(scene);
camera.targetTexture = CreateRenderTarget(renderTargetSize);
gameObjects = new List<GameObject>();
IsLoaded = true;
}
/// <summary>
/// Closes the preview scene and releases all resources.
/// Pairs with <see cref="Load()"/>.
/// </summary>
public void Destroy()
{
if (IsLoaded == false)
return;
// The target texture needs to be removed from the camera
// before destroying it to avoid Unity logging the error:
// "Releasing render texture that is set as Camera.targetTexture!"
RenderTexture renderTexture = camera.targetTexture;
camera.targetTexture = null;
Object.DestroyImmediate(renderTexture);
Object.DestroyImmediate(camera.gameObject);
camera = null;
gameObjects.Clear();
EditorSceneManager.ClosePreviewScene(scene);
IsLoaded = false;
}
/// <summary>
/// Instantiates the provided prefab into the preview scene
/// and returns the GameObject instance for modification.
/// </summary>
public GameObject Instantiate(GameObject prefab)
{
var instance = Object.Instantiate(prefab);
Add(instance);
return instance;
}
/// <summary>
/// Adds the provided instance to the preview scene.
/// </summary>
/// <remarks>
/// Note, that the argument will be destroyed together
/// with the preview scene automatically.
/// </remarks>
public void Add(GameObject instance)
{
instance.hideFlags = HideFlags.HideAndDontSave;
SceneManager.MoveGameObjectToScene(instance, scene);
gameObjects.Add(instance);
}
public void MoveAllOffscreen()
{
for (int i = 0; i < gameObjects.Count; i++)
gameObjects[i].transform.position = offScreenPosition;
}
/// <summary>
/// Focuses the preview camera on the target identified by the provided index.
/// All other scene objects will be hidden.
/// </summary>
public void Focus(int targetIndex)
{
if (currentTarget != null)
currentTarget.transform.position = offScreenPosition;
currentTarget = gameObjects[targetIndex];
currentTarget.transform.position = Vector3.zero;
}
/// <summary>
/// Destroys all added GameObjects within the preview scene.
/// </summary>
public void Clear()
{
foreach (var go in gameObjects)
Object.DestroyImmediate(go);
gameObjects.Clear();
}
/// <summary>
/// Renders the preview scene and returns a refreshed render target.
/// </summary>
public Texture Render()
{
bool overrideActive = false;
if (customRenderSettings.UseActiveSceneSettings == false &&
Unsupported.SetOverrideLightingSettings(scene))
{
overrideActive = true;
ApplyRenderSettings(customRenderSettings);
}
camera.Render();
if (overrideActive)
Unsupported.RestoreOverrideLightingSettings();
return camera.targetTexture;
}
private void ApplyRenderSettings(CustomRenderSettings settings)
{
RenderSettings.ambientMode = settings.AmbientMode;
RenderSettings.ambientSkyColor = settings.AmbientSkyColor;
RenderSettings.ambientEquatorColor = settings.AmbientEquatorColor;
RenderSettings.ambientGroundColor = settings.AmbientGroundColor;
RenderSettings.ambientIntensity = settings.AmbientIntensity;
RenderSettings.ambientLight = settings.AmbientColor;
RenderSettings.subtractiveShadowColor = settings.SubtractiveShadowColor;
RenderSettings.skybox = settings.Skybox;
RenderSettings.sun = settings.Sun;
RenderSettings.ambientProbe = settings.AmbientProbe;
RenderSettings.customReflection = settings.CustomReflection;
RenderSettings.reflectionIntensity = settings.ReflectionIntensity;
RenderSettings.reflectionBounces = settings.ReflectionBounces;
RenderSettings.defaultReflectionMode = settings.DefaultReflectionMode;
RenderSettings.defaultReflectionResolution = settings.DefaultReflectionResolution;
}
public void Render(RenderTexture renderTarget)
{
var previousTarget = camera.targetTexture;
camera.targetTexture = renderTarget;
Render();
camera.targetTexture = previousTarget;
}
/// <summary>
/// Renders the preview scene and immediately draws the render target
/// to a GUI texture with the provided rect position and size.
/// </summary>
public void RenderToGUI(Rect rect, bool alphaBlend = true)
{
if (Event.current.type != EventType.Repaint)
return;
Texture texture = Render();
GUI.DrawTexture(
rect,
texture,
ScaleMode.StretchToFill,
alphaBlend);
}
private static Camera CreateCamera(Scene scene)
{
var go = new GameObject("Preview Camera");
go.hideFlags = HideFlags.HideAndDontSave;
SceneManager.MoveGameObjectToScene(go, scene);
Camera camera = go.AddComponent<Camera>();
camera.scene = scene;
camera.cameraType = CameraType.Preview;
camera.clearFlags = CameraClearFlags.Color;
camera.backgroundColor = Color.clear;
camera.renderingPath = RenderingPath.Forward;
camera.useOcclusionCulling = false;
camera.allowHDR = false;
camera.allowMSAA = true;
camera.nearClipPlane = 0.5f;
camera.farClipPlane = 20f;
camera.transform.position = new Vector3(0f, 0f, -10f);
return camera;
}
private RenderTexture CreateRenderTarget(Vector2Int renderTargetSize)
{
var renderTexture = new RenderTexture(
renderTargetSize.x,
renderTargetSize.y,
24);
renderTexture.hideFlags = HideFlags.HideAndDontSave;
renderTexture.name = "Preview RT";
renderTexture.antiAliasing = 4;
return renderTexture;
}
/// <summary>
/// A wrapper that allows controlled modifications of
/// a camera's properties without exposing the object instance.
/// </summary>
/// <remarks>
/// This is used to prevent client code from making
/// invalid changes or destroying the camera reference.
/// </remarks>
public class CameraProxy
{
public bool IsValid => camera != null;
private readonly Camera camera;
public CameraProxy(Camera camera)
{
this.camera = camera;
}
public void SetPositionAndRotation(Vector3 position, Quaternion rotation)
{
camera.transform.SetPositionAndRotation(position, rotation);
}
public Vector3 Position
{
get => camera.transform.position;
set => camera.transform.position = value;
}
public Quaternion Rotation
{
get => camera.transform.rotation;
set => camera.transform.rotation = value;
}
public void LookAt(Vector3 targetPosition, Vector3 worldUp)
{
camera.transform.LookAt(targetPosition, worldUp);
}
public float OrthographicSize
{
set
{
camera.orthographic = true;
camera.orthographicSize = value;
}
}
public float FieldOfView
{
set
{
camera.orthographic = false;
camera.fieldOfView = value;
}
}
public void SetClipPlanes(float near, float far)
{
camera.nearClipPlane = near;
camera.farClipPlane = far;
}
public Color BackgroundColor
{
get => camera.backgroundColor;
set => camera.backgroundColor = value;
}
public CameraClearFlags ClearFlags
{
get => camera.clearFlags;
set => camera.clearFlags = value;
}
}
}
}