Tanoda
pb_AssetPreview.cs
Go to the documentation of this file.
1using UnityEngine;
2
3namespace GILES
4{
9 public static class pb_AssetPreview
10 {
11 private static Camera _previewCamera = null;
12
13 private static Camera previewCamera
14 {
15 get
16 {
17 if (_previewCamera == null)
18 {
19 GameObject go = new GameObject();
20 go.name = "Asset Preview Camera";
21 go.transform.localRotation = Quaternion.Euler(30f, -30f, 0f);
22 _previewCamera = go.AddComponent<Camera>();
23 _previewCamera.nearClipPlane = 0.01f;
24 go.SetActive(false);
25 }
26
27 return _previewCamera;
28 }
29 }
30
31 public static Texture2D GeneratePreview(Object obj, int width, int height)
32 {
33 Texture2D tex = null;
34 GameObject go = obj as GameObject;
35
36 if (PrepareCamera(previewCamera, go))
37 {
38 go = GameObject.Instantiate(go);
39 go.transform.position = Vector3.zero;
40
41 RenderTexture renderTexture = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Default, 1);
42 RenderTexture.active = renderTexture;
43
44 previewCamera.targetTexture = renderTexture;
45 previewCamera.Render();
46
47 tex = new Texture2D(width, height);
48 tex.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
49 tex.Apply();
50
51 RenderTexture.ReleaseTemporary(renderTexture);
52
53 pb_ObjectUtility.Destroy(go);
54 }
55
56 return tex;
57 }
58
63 public static bool PrepareCamera(Camera cam, GameObject target, int width, int height)
64 {
65 if (target == null)
66 return false;
67
68 Bounds bounds;
69
70 MeshFilter mf = target.GetComponentInChildren<MeshFilter>();
71
72 if (mf != null && mf.sharedMesh != null)
73 {
74 bounds = mf.sharedMesh.bounds;
75 }
76 else
77 {
78 Renderer renderer = target.GetComponentInChildren<Renderer>();
79
80 if (renderer == null)
81 return false;
82
83 bounds = renderer.bounds;
84 }
85
86 float dist = pb_ObjectUtility.CalcMinDistanceToBounds(cam, bounds, (mf != null ? mf.gameObject.transform.lossyScale.x : 1));
87
88 cam.transform.position = cam.transform.forward * -(dist) + Vector3.one * 100;
89
90 return true;
91 }
92
93 public static bool PrepareCamera(Camera cam, GameObject target)
94 {
95 if (target == null)
96 return false;
97
98 Bounds bounds = Macro.GetBounds(target);
99
100 MeshFilter mf = target.GetComponentInChildren<MeshFilter>();
101
102 if (bounds == default)
103 {
104 if (mf != null && mf.sharedMesh != null)
105 {
106 bounds = mf.sharedMesh.bounds;
107 }
108 else
109 {
110 Renderer renderer = target.GetComponentInChildren<Renderer>();
111
112 if (renderer == null)
113 return false;
114
115 bounds = renderer.bounds;
116 }
117 }
118
119 float dist = pb_ObjectUtility.CalcMinDistanceToBounds(cam, bounds, (mf != null ? mf.gameObject.transform.lossyScale.x : 1));
120
121 cam.transform.position = cam.transform.forward * -(dist) + Vector3.one * 100;
122
123 return true;
124 }
125 }
126}
Definition: Macro.cs:12
static Bounds GetBounds(GameObject go)
Definition: Macro.cs:482
UnityEngine.Object Object