-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
RuntimePreviewGenerator.cs
544 lines (449 loc) · 16.9 KB
/
RuntimePreviewGenerator.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//#define DEBUG_BOUNDS
using System;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;
public static class RuntimePreviewGenerator
{
private class CameraSetup
{
private Vector3 position;
private Quaternion rotation;
private Color backgroundColor;
private bool orthographic;
private float orthographicSize;
private float nearClipPlane;
private float farClipPlane;
private float aspect;
private int cullingMask;
private CameraClearFlags clearFlags;
private RenderTexture targetTexture;
public void GetSetup( Camera camera )
{
position = camera.transform.position;
rotation = camera.transform.rotation;
backgroundColor = camera.backgroundColor;
orthographic = camera.orthographic;
orthographicSize = camera.orthographicSize;
nearClipPlane = camera.nearClipPlane;
farClipPlane = camera.farClipPlane;
aspect = camera.aspect;
cullingMask = camera.cullingMask;
clearFlags = camera.clearFlags;
targetTexture = camera.targetTexture;
}
public void ApplySetup( Camera camera )
{
camera.transform.position = position;
camera.transform.rotation = rotation;
camera.backgroundColor = backgroundColor;
camera.orthographic = orthographic;
camera.orthographicSize = orthographicSize;
camera.aspect = aspect;
camera.cullingMask = cullingMask;
camera.clearFlags = clearFlags;
// Assigning order or nearClipPlane and farClipPlane may matter because Unity clamps near to far and far to near
if( nearClipPlane < camera.farClipPlane )
{
camera.nearClipPlane = nearClipPlane;
camera.farClipPlane = farClipPlane;
}
else
{
camera.farClipPlane = farClipPlane;
camera.nearClipPlane = nearClipPlane;
}
camera.targetTexture = targetTexture;
targetTexture = null;
}
}
private const int PREVIEW_LAYER = 22;
private static Vector3 PREVIEW_POSITION = new Vector3( -250f, -250f, -250f );
private static Camera renderCamera;
private static readonly CameraSetup cameraSetup = new CameraSetup();
private static readonly Vector3[] boundingBoxPoints = new Vector3[8];
private static readonly List<Renderer> renderersList = new List<Renderer>( 64 );
private static readonly List<int> layersList = new List<int>( 64 );
#if DEBUG_BOUNDS
private static Material boundsDebugMaterial;
#endif
private static Camera m_internalCamera = null;
private static Camera InternalCamera
{
get
{
if( m_internalCamera == null )
{
m_internalCamera = new GameObject( "ModelPreviewGeneratorCamera" ).AddComponent<Camera>();
m_internalCamera.enabled = false;
m_internalCamera.nearClipPlane = 0.01f;
m_internalCamera.cullingMask = 1 << PREVIEW_LAYER;
m_internalCamera.gameObject.hideFlags = HideFlags.HideAndDontSave;
}
return m_internalCamera;
}
}
private static Camera m_previewRenderCamera;
public static Camera PreviewRenderCamera
{
get { return m_previewRenderCamera; }
set { m_previewRenderCamera = value; }
}
private static Vector3 m_previewDirection = new Vector3( -0.57735f, -0.57735f, -0.57735f ); // Normalized (-1,-1,-1)
public static Vector3 PreviewDirection
{
get { return m_previewDirection; }
set { m_previewDirection = value.normalized; }
}
private static float m_padding;
public static float Padding
{
get { return m_padding; }
set { m_padding = Mathf.Clamp( value, -0.25f, 0.25f ); }
}
private static Color m_backgroundColor = new Color( 0.3f, 0.3f, 0.3f, 1f );
public static Color BackgroundColor
{
get { return m_backgroundColor; }
set { m_backgroundColor = value; }
}
private static bool m_orthographicMode = false;
public static bool OrthographicMode
{
get { return m_orthographicMode; }
set { m_orthographicMode = value; }
}
private static bool m_markTextureNonReadable = true;
public static bool MarkTextureNonReadable
{
get { return m_markTextureNonReadable; }
set { m_markTextureNonReadable = value; }
}
public static Texture2D GenerateMaterialPreview( Material material, PrimitiveType previewObject, int width = 64, int height = 64 )
{
return GenerateMaterialPreviewWithShader( material, previewObject, null, null, width, height );
}
public static Texture2D GenerateMaterialPreviewWithShader( Material material, PrimitiveType previewPrimitive, Shader shader, string replacementTag, int width = 64, int height = 64 )
{
GameObject previewModel = GameObject.CreatePrimitive( previewPrimitive );
previewModel.gameObject.hideFlags = HideFlags.HideAndDontSave;
previewModel.GetComponent<Renderer>().sharedMaterial = material;
try
{
return GenerateModelPreviewWithShader( previewModel.transform, shader, replacementTag, width, height, false );
}
catch( Exception e )
{
Debug.LogException( e );
}
finally
{
Object.DestroyImmediate( previewModel );
}
return null;
}
public static Texture2D GenerateModelPreview( Transform model, int width = 64, int height = 64, bool shouldCloneModel = false )
{
return GenerateModelPreviewWithShader( model, null, null, width, height, shouldCloneModel );
}
public static Texture2D GenerateModelPreviewWithShader( Transform model, Shader shader, string replacementTag, int width = 64, int height = 64, bool shouldCloneModel = false )
{
if( !model )
return null;
Texture2D result = null;
if( !model.gameObject.scene.IsValid() || !model.gameObject.scene.isLoaded )
shouldCloneModel = true;
Transform previewObject;
if( shouldCloneModel )
{
previewObject = (Transform) Object.Instantiate( model, null, false );
previewObject.gameObject.hideFlags = HideFlags.HideAndDontSave;
}
else
{
previewObject = model;
layersList.Clear();
GetLayerRecursively( previewObject );
}
bool isStatic = IsStatic( model );
bool wasActive = previewObject.gameObject.activeSelf;
Vector3 prevPos = previewObject.position;
Quaternion prevRot = previewObject.rotation;
#if DEBUG_BOUNDS
Transform boundsDebugCube = null;
#endif
try
{
SetupCamera();
SetLayerRecursively( previewObject );
if( !isStatic )
{
previewObject.position = PREVIEW_POSITION;
previewObject.rotation = Quaternion.identity;
}
if( !wasActive )
previewObject.gameObject.SetActive( true );
Bounds previewBounds = new Bounds();
if( !CalculateBounds( previewObject, out previewBounds ) )
return null;
#if DEBUG_BOUNDS
if( !boundsDebugMaterial )
{
boundsDebugMaterial = new Material( Shader.Find( "Sprites/Default" ) )
{
hideFlags = HideFlags.HideAndDontSave,
color = new Color( 0.5f, 0.5f, 0.5f, 0.5f )
};
}
boundsDebugCube = GameObject.CreatePrimitive( PrimitiveType.Cube ).transform;
boundsDebugCube.localPosition = previewBounds.center;
boundsDebugCube.localScale = previewBounds.size;
boundsDebugCube.gameObject.layer = PREVIEW_LAYER;
boundsDebugCube.gameObject.hideFlags = HideFlags.HideAndDontSave;
boundsDebugCube.GetComponent<Renderer>().sharedMaterial = boundsDebugMaterial;
#endif
renderCamera.aspect = (float) width / height;
renderCamera.transform.rotation = Quaternion.LookRotation( previewObject.rotation * m_previewDirection, previewObject.up );
CalculateCameraPosition( renderCamera, previewBounds, m_padding );
renderCamera.farClipPlane = ( renderCamera.transform.position - previewBounds.center ).magnitude + previewBounds.size.magnitude;
RenderTexture activeRT = RenderTexture.active;
RenderTexture renderTexture = null;
try
{
renderTexture = RenderTexture.GetTemporary( width, height, 16 );
RenderTexture.active = renderTexture;
if( m_backgroundColor.a < 1f )
GL.Clear( true, true, m_backgroundColor );
renderCamera.targetTexture = renderTexture;
if( !shader )
renderCamera.Render();
else
renderCamera.RenderWithShader( shader, replacementTag ?? string.Empty );
renderCamera.targetTexture = null;
result = new Texture2D( width, height, m_backgroundColor.a < 1f ? TextureFormat.RGBA32 : TextureFormat.RGB24, false );
result.ReadPixels( new Rect( 0f, 0f, width, height ), 0, 0, false );
result.Apply( false, m_markTextureNonReadable );
}
finally
{
RenderTexture.active = activeRT;
if( renderTexture )
RenderTexture.ReleaseTemporary( renderTexture );
}
}
catch( Exception e )
{
Debug.LogException( e );
}
finally
{
#if DEBUG_BOUNDS
if( boundsDebugCube )
Object.DestroyImmediate( boundsDebugCube.gameObject );
#endif
if( shouldCloneModel )
Object.DestroyImmediate( previewObject.gameObject );
else
{
if( !wasActive )
previewObject.gameObject.SetActive( false );
if( !isStatic )
{
previewObject.position = prevPos;
previewObject.rotation = prevRot;
}
int index = 0;
SetLayerRecursively( previewObject, ref index );
}
if( renderCamera == m_previewRenderCamera )
cameraSetup.ApplySetup( renderCamera );
}
return result;
}
// Calculates AABB bounds of the target object (AABB will include its child objects)
public static bool CalculateBounds( Transform target, out Bounds bounds )
{
renderersList.Clear();
target.GetComponentsInChildren( renderersList );
bounds = new Bounds();
bool hasBounds = false;
for( int i = 0; i < renderersList.Count; i++ )
{
if( !renderersList[i].enabled )
continue;
if( !hasBounds )
{
bounds = renderersList[i].bounds;
hasBounds = true;
}
else
bounds.Encapsulate( renderersList[i].bounds );
}
return hasBounds;
}
// Moves camera in a way such that it will encapsulate bounds perfectly
public static void CalculateCameraPosition( Camera camera, Bounds bounds, float padding = 0f )
{
Transform cameraTR = camera.transform;
Vector3 cameraDirection = cameraTR.forward;
float aspect = camera.aspect;
if( padding != 0f )
bounds.size *= 1f + padding * 2f; // Padding applied to both edges, hence multiplied by 2
Vector3 boundsCenter = bounds.center;
Vector3 boundsExtents = bounds.extents;
Vector3 boundsSize = 2f * boundsExtents;
// Calculate corner points of the Bounds
Vector3 point = boundsCenter + boundsExtents;
boundingBoxPoints[0] = point;
point.x -= boundsSize.x;
boundingBoxPoints[1] = point;
point.y -= boundsSize.y;
boundingBoxPoints[2] = point;
point.x += boundsSize.x;
boundingBoxPoints[3] = point;
point.z -= boundsSize.z;
boundingBoxPoints[4] = point;
point.x -= boundsSize.x;
boundingBoxPoints[5] = point;
point.y += boundsSize.y;
boundingBoxPoints[6] = point;
point.x += boundsSize.x;
boundingBoxPoints[7] = point;
if( camera.orthographic )
{
cameraTR.position = boundsCenter;
float minX = float.PositiveInfinity, minY = float.PositiveInfinity;
float maxX = float.NegativeInfinity, maxY = float.NegativeInfinity;
for( int i = 0; i < boundingBoxPoints.Length; i++ )
{
Vector3 localPoint = cameraTR.InverseTransformPoint( boundingBoxPoints[i] );
if( localPoint.x < minX )
minX = localPoint.x;
if( localPoint.x > maxX )
maxX = localPoint.x;
if( localPoint.y < minY )
minY = localPoint.y;
if( localPoint.y > maxY )
maxY = localPoint.y;
}
float distance = boundsExtents.magnitude + 1f;
camera.orthographicSize = Mathf.Max( maxY - minY, ( maxX - minX ) / aspect ) * 0.5f;
cameraTR.position = boundsCenter - cameraDirection * distance;
}
else
{
Vector3 cameraUp = cameraTR.up, cameraRight = cameraTR.right;
float verticalFOV = camera.fieldOfView * 0.5f;
float horizontalFOV = Mathf.Atan( Mathf.Tan( verticalFOV * Mathf.Deg2Rad ) * aspect ) * Mathf.Rad2Deg;
// Normals of the camera's frustum planes
Vector3 topFrustumPlaneNormal = Quaternion.AngleAxis( 90f + verticalFOV, -cameraRight ) * cameraDirection;
Vector3 bottomFrustumPlaneNormal = Quaternion.AngleAxis( 90f + verticalFOV, cameraRight ) * cameraDirection;
Vector3 rightFrustumPlaneNormal = Quaternion.AngleAxis( 90f + horizontalFOV, cameraUp ) * cameraDirection;
Vector3 leftFrustumPlaneNormal = Quaternion.AngleAxis( 90f + horizontalFOV, -cameraUp ) * cameraDirection;
// Credit for algorithm: https://stackoverflow.com/a/66113254/2373034
// 1. Find edge points of the bounds using the camera's frustum planes
// 2. Create a plane for each edge point that goes through the point and has the corresponding frustum plane's normal
// 3. Find the intersection line of horizontal edge points' planes (horizontalIntersection) and vertical edge points' planes (verticalIntersection)
// If we move the camera along horizontalIntersection, the bounds will always with the camera's width perfectly (similar effect goes for verticalIntersection)
// 4. Find the closest line segment between these two lines (horizontalIntersection and verticalIntersection) and place the camera at the farthest point on that line
int leftmostPoint = -1, rightmostPoint = -1, topmostPoint = -1, bottommostPoint = -1;
for( int i = 0; i < boundingBoxPoints.Length; i++ )
{
if( leftmostPoint < 0 && IsOutermostPointInDirection( i, leftFrustumPlaneNormal ) )
leftmostPoint = i;
if( rightmostPoint < 0 && IsOutermostPointInDirection( i, rightFrustumPlaneNormal ) )
rightmostPoint = i;
if( topmostPoint < 0 && IsOutermostPointInDirection( i, topFrustumPlaneNormal ) )
topmostPoint = i;
if( bottommostPoint < 0 && IsOutermostPointInDirection( i, bottomFrustumPlaneNormal ) )
bottommostPoint = i;
}
Ray horizontalIntersection = GetPlanesIntersection( new Plane( leftFrustumPlaneNormal, boundingBoxPoints[leftmostPoint] ), new Plane( rightFrustumPlaneNormal, boundingBoxPoints[rightmostPoint] ) );
Ray verticalIntersection = GetPlanesIntersection( new Plane( topFrustumPlaneNormal, boundingBoxPoints[topmostPoint] ), new Plane( bottomFrustumPlaneNormal, boundingBoxPoints[bottommostPoint] ) );
Vector3 closestPoint1, closestPoint2;
FindClosestPointsOnTwoLines( horizontalIntersection, verticalIntersection, out closestPoint1, out closestPoint2 );
cameraTR.position = Vector3.Dot( closestPoint1 - closestPoint2, cameraDirection ) < 0 ? closestPoint1 : closestPoint2;
}
}
// Returns whether or not the given point is the outermost point in the given direction among all points of the bounds
private static bool IsOutermostPointInDirection( int pointIndex, Vector3 direction )
{
Vector3 point = boundingBoxPoints[pointIndex];
for( int i = 0; i < boundingBoxPoints.Length; i++ )
{
if( i != pointIndex && Vector3.Dot( direction, boundingBoxPoints[i] - point ) > 0 )
return false;
}
return true;
}
// Credit: https://stackoverflow.com/a/32410473/2373034
// Returns the intersection line of the 2 planes
private static Ray GetPlanesIntersection( Plane p1, Plane p2 )
{
Vector3 p3Normal = Vector3.Cross( p1.normal, p2.normal );
float det = p3Normal.sqrMagnitude;
return new Ray( ( ( Vector3.Cross( p3Normal, p2.normal ) * p1.distance ) + ( Vector3.Cross( p1.normal, p3Normal ) * p2.distance ) ) / det, p3Normal );
}
// Credit: http://wiki.unity3d.com/index.php/3d_Math_functions
// Returns the edge points of the closest line segment between 2 lines
private static void FindClosestPointsOnTwoLines( Ray line1, Ray line2, out Vector3 closestPointLine1, out Vector3 closestPointLine2 )
{
Vector3 line1Direction = line1.direction;
Vector3 line2Direction = line2.direction;
float a = Vector3.Dot( line1Direction, line1Direction );
float b = Vector3.Dot( line1Direction, line2Direction );
float e = Vector3.Dot( line2Direction, line2Direction );
float d = a * e - b * b;
Vector3 r = line1.origin - line2.origin;
float c = Vector3.Dot( line1Direction, r );
float f = Vector3.Dot( line2Direction, r );
float s = ( b * f - c * e ) / d;
float t = ( a * f - c * b ) / d;
closestPointLine1 = line1.origin + line1Direction * s;
closestPointLine2 = line2.origin + line2Direction * t;
}
private static void SetupCamera()
{
if( m_previewRenderCamera )
{
cameraSetup.GetSetup( m_previewRenderCamera );
renderCamera = m_previewRenderCamera;
renderCamera.nearClipPlane = 0.01f;
renderCamera.cullingMask = PREVIEW_LAYER;
}
else
renderCamera = InternalCamera;
renderCamera.backgroundColor = m_backgroundColor;
renderCamera.orthographic = m_orthographicMode;
renderCamera.clearFlags = m_backgroundColor.a < 1f ? CameraClearFlags.Depth : CameraClearFlags.Color;
}
private static bool IsStatic( Transform obj )
{
if( obj.gameObject.isStatic )
return true;
for( int i = 0; i < obj.childCount; i++ )
{
if( IsStatic( obj.GetChild( i ) ) )
return true;
}
return false;
}
private static void SetLayerRecursively( Transform obj )
{
obj.gameObject.layer = PREVIEW_LAYER;
for( int i = 0; i < obj.childCount; i++ )
SetLayerRecursively( obj.GetChild( i ) );
}
private static void GetLayerRecursively( Transform obj )
{
layersList.Add( obj.gameObject.layer );
for( int i = 0; i < obj.childCount; i++ )
GetLayerRecursively( obj.GetChild( i ) );
}
private static void SetLayerRecursively( Transform obj, ref int index )
{
obj.gameObject.layer = layersList[index++];
for( int i = 0; i < obj.childCount; i++ )
SetLayerRecursively( obj.GetChild( i ), ref index );
}
}