PreviewEditor.cs
9.13 KB
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
using UnityEditor;
using UnityEngine;
using UnityEditorInternal;
using System;
using System.Linq;
using System.Collections.Generic;
namespace VRM
{
/// <summary>
/// Prefabをインスタンス化してPreviewに表示する
///
/// * https://github.com/Unity-Technologies/UnityCsReference/blob/11bcfd801fccd2a52b09bb6fd636c1ddcc9f1705/Editor/Mono/Inspector/ModelInspector.cs
///
/// </summary>
public abstract class PreviewEditor : Editor
{
/// <summary>
/// PreviewRenderUtilityを管理する。
///
/// * PreviewRenderUtility.m_cameraのUnityVersionによる切り分け
///
/// </summary>
PreviewFaceRenderer m_renderer;
/// <summary>
/// Prefabをインスタンス化したシーンを管理する。
///
/// * BlendShapeのBake
/// * MaterialMorphの適用
/// * Previewカメラのコントロール
/// * Previewライティングのコントロール
///
/// </summary>
PreviewSceneManager m_scene;
protected PreviewSceneManager PreviewSceneManager
{
get { return m_scene; }
}
/// <summary>
/// Previewシーンに表示するPrefab
/// </summary>
GameObject m_prefab;
protected GameObject Prefab
{
get { return m_prefab; }
private set
{
if (m_prefab == value) return;
//Debug.LogFormat("Prefab = {0}", value);
m_prefab = value;
if (m_scene != null)
{
//Debug.LogFormat("OnDestroy");
GameObject.DestroyImmediate(m_scene.gameObject);
m_scene = null;
}
if (m_prefab != null)
{
m_scene = VRM.PreviewSceneManager.GetOrCreate(m_prefab);
if (m_scene != null)
{
m_scene.gameObject.SetActive(false);
}
Bake();
}
}
}
protected abstract PreviewSceneManager.BakeValue GetBakeValue();
/// <summary>
/// Preview シーンに BlendShape と MaterialValue を適用する
/// </summary>
protected void Bake()
{
if (m_scene != null)
{
//Debug.Log("Bake");
m_scene.Bake(GetBakeValue());
}
}
protected virtual GameObject GetPrefab()
{
var assetPath = AssetDatabase.GetAssetPath(target);
if (string.IsNullOrEmpty(assetPath))
{
return null;
}
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
if (prefab == null)
{
var parent = UniGLTF.UnityPath.FromUnityPath(assetPath).Parent;
var prefabPath = parent.Parent.Child(parent.FileNameWithoutExtension + ".prefab");
prefab = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath.Value);
}
return prefab;
}
protected virtual void OnEnable()
{
m_renderer = new PreviewFaceRenderer();
Prefab = GetPrefab();
}
protected virtual void OnDisable()
{
if (m_renderer != null)
{
m_renderer.Dispose();
m_renderer = null;
}
}
protected virtual void OnDestroy()
{
if (m_scene != null)
{
//Debug.LogFormat("OnDestroy");
m_scene.Clean();
GameObject.DestroyImmediate(m_scene.gameObject);
m_scene = null;
}
}
protected static void Separator()
{
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
//GUILayout.Space();
GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(1));
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
}
public override void OnInspectorGUI()
{
//base.OnInspectorGUI();
Prefab = (GameObject)EditorGUILayout.ObjectField("Preview Prefab", Prefab, typeof(GameObject), false);
//Separator();
}
private static int sliderHash = "Slider".GetHashCode();
float m_yaw = 180.0f;
float m_pitch;
Vector3 m_position = new Vector3(0, 0, -0.8f);
// very important to override this, it tells Unity to render an ObjectPreview at the bottom of the inspector
public override bool HasPreviewGUI() { return true; }
public RenderTexture PreviewTexture;
// the main ObjectPreview function... it's called constantly, like other IMGUI On*GUI() functions
public override void OnPreviewGUI(Rect r, GUIStyle background)
{
// if this is happening, you have bigger problems
if (!ShaderUtil.hardwareSupportsRectRenderTexture)
{
if (Event.current.type == EventType.Repaint)
{
EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 40f),
"Mesh preview requires\nrender texture support");
}
return;
}
var src = r;
var min = Mathf.Min(r.width, r.height);
r.width = min;
r.height = min;
r.x = src.x + (src.width - min) / 2;
r.y = src.y + (src.height - min) / 2;
//previewDir = Drag2D(previewDir, r);
{
int controlId = GUIUtility.GetControlID(sliderHash, FocusType.Passive);
Event e = Event.current;
switch (e.GetTypeForControl(controlId))
{
case EventType.MouseDown:
if (r.Contains(e.mousePosition) && (double)r.width > 50.0)
{
GUIUtility.hotControl = controlId;
e.Use();
EditorGUIUtility.SetWantsMouseJumping(1);
break;
}
break;
case EventType.MouseUp:
if (GUIUtility.hotControl == controlId)
GUIUtility.hotControl = 0;
EditorGUIUtility.SetWantsMouseJumping(0);
break;
case EventType.MouseDrag:
if (GUIUtility.hotControl == controlId)
{
if (e.button == 2)
{
var shift = e.delta * (!e.shift ? 1f : 3f) / Mathf.Min(r.width, r.height);
m_position.x -= shift.x;
m_position.y += shift.y;
e.Use();
GUI.changed = true;
}
else if (
e.button == 0 ||
e.button == 1)
{
var shift = e.delta * (!e.shift ? 1f : 3f) / Mathf.Min(r.width, r.height) * 140f;
m_yaw += shift.x;
m_pitch += shift.y;
m_pitch = Mathf.Clamp(m_pitch, -90f, 90f);
e.Use();
GUI.changed = true;
}
break;
}
break;
case EventType.ScrollWheel:
//Debug.LogFormat("wheel: {0}", current.delta);
if (r.Contains(e.mousePosition))
{
if (e.delta.y > 0)
{
m_position.z *= 1.1f;
Repaint();
}
else if (e.delta.y < 0)
{
m_position.z *= 0.9f;
Repaint();
}
}
break;
}
//return scrollPosition;
}
//Debug.LogFormat("{0}", previewDir);
if (Event.current.type != EventType.Repaint)
{
// if we don't need to update yet, then don't
return;
}
if (m_renderer != null && m_scene != null)
{
PreviewTexture = m_renderer.Render(r, background, m_scene, m_yaw, m_pitch, m_position) as RenderTexture;
if (PreviewTexture != null)
{
// draw the RenderTexture in the ObjectPreview pane
GUI.DrawTexture(r, PreviewTexture, ScaleMode.StretchToFill, false);
}
}
}
}
}