-
Notifications
You must be signed in to change notification settings - Fork 0
/
BooleanEditor.cs
383 lines (308 loc) · 14.5 KB
/
BooleanEditor.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
using UnityEngine;
using UnityEngine.ProBuilder;
using UnityEngine.ProBuilder.Csg;
using UnityEngine.ProBuilder.MeshOperations;
namespace UnityEditor.ProBuilder
{
/// <summary>
/// Editor window for accessing boolean functionality.
/// </summary>
sealed class BooleanEditor : ConfigurableWindow
{
enum BooleanOp
{
Intersection,
Union,
Subtraction
}
const int k_Padding = 6;
const int k_PreviewInset = 2;
GameObject m_LeftGameObject, m_RightGameObject;
int m_PreviewHeight, m_PreviewWidth;
Rect m_LeftObjectField = new Rect(k_Padding + k_PreviewInset, k_Padding + k_PreviewInset, 0f, 0f);
Rect m_RightObjectField = new Rect(0f, k_Padding + k_PreviewInset, 0f, 0f);
Rect m_ReverseOperationOrderRect = new Rect(0f, 0f, 42f, 42f);
static GUIStyle previewBackground;
static GUIStyle unicodeIconStyle;
Color backgroundColor = new Color(.15625f, .15625f, .15625f, 1f);
Texture2D backgroundTexture;
Editor m_LeftPreviewEditor, m_RightPreviewEditor;
BooleanOp operation = BooleanOp.Intersection;
bool mouseClickedSwapRect = false;
Vector2Int screen = Vector2Int.zero;
static readonly string k_ReverseArrowsIcon = ((char)8644).ToString();
[MenuItem("Tools/" + PreferenceKeys.pluginTitle + "/Experimental/Boolean (CSG) Tool", false, PreferenceKeys.menuMisc)]
public static void MenuOpenBooleanTool()
{
GetWindow<BooleanEditor>(true, "Boolean (Experimental)", true).Show();
}
void OnEnable()
{
minSize = new Vector2(200f, 135f);
var meshes = Selection.transforms.GetComponents<ProBuilderMesh>();
if (meshes.Length == 2)
{
m_LeftGameObject = meshes[0].gameObject;
m_RightGameObject = meshes[1].gameObject;
}
previewBackground = new GUIStyle();
backgroundTexture = new Texture2D(2, 2);
backgroundTexture.SetPixels(new Color[] {
backgroundColor,
backgroundColor,
backgroundColor,
backgroundColor
});
backgroundTexture.Apply();
previewBackground.normal.background = backgroundTexture;
unicodeIconStyle = new GUIStyle();
unicodeIconStyle.fontSize = 32;
unicodeIconStyle.normal.textColor = Color.white;
unicodeIconStyle.alignment = TextAnchor.MiddleCenter;
var arrowSize = unicodeIconStyle.CalcSize(UI.EditorGUIUtility.TempContent(k_ReverseArrowsIcon));
m_ReverseOperationOrderRect.width = arrowSize.x;
m_ReverseOperationOrderRect.width = arrowSize.y;
}
void OnDisable()
{
if (backgroundTexture != null)
{
DestroyImmediate(backgroundTexture);
}
}
void OnGUI()
{
DoContextMenu();
Event e = Event.current;
screen.x = (int)position.width;
screen.y = (int)position.height;
// Since image wells eat mouse clicks, listen for a mouse up when hovering over 'reverse operation order' button
switch (e.type)
{
case EventType.MouseDown:
if (m_ReverseOperationOrderRect.Contains(e.mousePosition))
{
mouseClickedSwapRect = true;
e.Use();
}
break;
case EventType.MouseUp:
if (mouseClickedSwapRect && m_ReverseOperationOrderRect.Contains(Event.current.mousePosition))
{
ReverseOperationOrder();
e.Use();
}
mouseClickedSwapRect = false;
break;
case EventType.Ignore:
mouseClickedSwapRect = false;
break;
}
DrawPreviewWells();
if (ListenForDragAndDrop())
return;
GUILayout.BeginHorizontal();
ProBuilderMesh lpb = m_LeftGameObject != null ? m_LeftGameObject.GetComponent<ProBuilderMesh>() : null;
ProBuilderMesh rpb = m_RightGameObject != null ? m_RightGameObject.GetComponent<ProBuilderMesh>() : null;
EditorGUI.BeginChangeCheck();
lpb = (ProBuilderMesh) EditorGUILayout.ObjectField(lpb, typeof(ProBuilderMesh), true);
rpb = (ProBuilderMesh) EditorGUILayout.ObjectField(rpb, typeof(ProBuilderMesh), true);
if (EditorGUI.EndChangeCheck())
{
DestroyImmediate(m_LeftPreviewEditor);
DestroyImmediate(m_RightPreviewEditor);
}
m_LeftGameObject = lpb != null ? lpb.gameObject : null;
m_RightGameObject = rpb != null ? rpb.gameObject : null;
GUILayout.EndHorizontal();
// Boolean controls
GUILayout.Space(4);
GUI.backgroundColor = PreferenceKeys.proBuilderDarkGray;
UI.EditorGUIUtility.DrawSeparator(2);
GUI.backgroundColor = Color.white;
operation = (BooleanOp) EditorGUILayout.EnumPopup("Operation", operation);
if (GUILayout.Button("Apply"))
{
switch (operation)
{
case BooleanOp.Union:
MenuUnion(m_LeftGameObject.GetComponent<ProBuilderMesh>(), m_RightGameObject.GetComponent<ProBuilderMesh>());
break;
case BooleanOp.Intersection:
MenuIntersect(m_LeftGameObject.GetComponent<ProBuilderMesh>(), m_RightGameObject.GetComponent<ProBuilderMesh>());
break;
case BooleanOp.Subtraction:
MenuSubtract(m_LeftGameObject.GetComponent<ProBuilderMesh>(), m_RightGameObject.GetComponent<ProBuilderMesh>());
break;
}
}
}
void ReverseOperationOrder()
{
GameObject tmp = m_LeftGameObject;
m_LeftGameObject = m_RightGameObject;
m_RightGameObject = tmp;
m_LeftPreviewEditor = null;
m_RightPreviewEditor = null;
}
// Draw the mesh previews
void DrawPreviewWells()
{
GUILayout.BeginHorizontal();
m_LeftObjectField = GUILayoutUtility.GetRect(GUIContent.none, UI.EditorStyles.sceneTextBox, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
m_RightObjectField = GUILayoutUtility.GetRect(GUIContent.none, UI.EditorStyles.sceneTextBox, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
GUILayout.EndHorizontal();
GUI.Box(m_LeftObjectField, GUIContent.none, UI.EditorStyles.sceneTextBox);
GUI.Box(m_RightObjectField, GUIContent.none, UI.EditorStyles.sceneTextBox);
m_ReverseOperationOrderRect.x = (screen.x / 2f) - (m_ReverseOperationOrderRect.width / 2f);
m_ReverseOperationOrderRect.y = m_LeftObjectField.y + m_LeftObjectField.height * .5f - m_ReverseOperationOrderRect.height * .5f;
m_LeftObjectField = InsetRect(m_LeftObjectField, k_PreviewInset);
m_RightObjectField = InsetRect(m_RightObjectField, k_PreviewInset);
if (m_LeftGameObject != null)
{
if (m_LeftPreviewEditor == null)
m_LeftPreviewEditor = UnityEditor.Editor.CreateEditor(m_LeftGameObject);
m_LeftPreviewEditor.OnPreviewGUI(m_LeftObjectField, previewBackground);
}
else
{
GUI.Label(m_LeftObjectField, "Drag GameObject Here", EditorStyles.centeredGreyMiniLabel);
}
if (m_RightGameObject != null)
{
if (m_RightPreviewEditor == null)
m_RightPreviewEditor = UnityEditor.Editor.CreateEditor(m_RightGameObject);
m_RightPreviewEditor.OnPreviewGUI(m_RightObjectField, previewBackground);
}
else
{
GUI.Label(m_RightObjectField, "Drag GameObject Here", EditorStyles.centeredGreyMiniLabel);
}
// Show text summary
if (m_LeftGameObject && m_RightGameObject)
{
var title = UI.EditorGUIUtility.TempContent(
operation == BooleanOp.Intersection
? m_LeftGameObject.name + " Intersects " + m_RightGameObject.name
: operation == BooleanOp.Union
? m_LeftGameObject.name + " Union " + m_RightGameObject.name
: m_LeftGameObject.name + " Subtracts " + m_RightGameObject.name);
var size = EditorStyles.boldLabel.CalcSize(title);
GUI.Label(new Rect(k_Padding + 2, k_Padding + 2, screen.x, size.y), title, EditorStyles.boldLabel);
}
// http://xahlee.info/comp/unicode_arrows.html
if (GUI.Button(m_ReverseOperationOrderRect, k_ReverseArrowsIcon, unicodeIconStyle))
ReverseOperationOrder();
}
static Rect InsetRect(Rect rect, int pad)
{
return new Rect(rect.x + pad, rect.y + pad, rect.width - pad * 2, rect.height - pad * 2);
}
/**
* Accept drags into window.
* MUST BE CALLED AFTER PREVIEW WELL RECTS ARE CALCULATED
*/
bool ListenForDragAndDrop()
{
Vector2 mPos = Event.current.mousePosition;
bool inLeft = m_LeftObjectField.Contains(mPos);
if (!inLeft && !m_RightObjectField.Contains(mPos))
return false;
if ((Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragPerform) && DragAndDrop.objectReferences.Length > 0)
{
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
if (Event.current.type == EventType.DragPerform)
{
DragAndDrop.AcceptDrag();
foreach (Object pb in DragAndDrop.objectReferences)
{
if ((pb is GameObject && ((GameObject)pb).GetComponent<ProBuilderMesh>()) || pb is ProBuilderMesh)
{
if (pb == m_LeftGameObject || pb == m_RightGameObject) continue;
if (inLeft)
{
m_LeftGameObject = (GameObject)pb;
if (m_LeftPreviewEditor != null)
{
DestroyImmediate(m_LeftPreviewEditor);
m_LeftPreviewEditor = null;
}
}
else
{
m_RightGameObject = (GameObject)pb;
if (m_RightPreviewEditor != null)
{
DestroyImmediate(m_RightPreviewEditor);
m_RightPreviewEditor = null;
}
}
return true;
}
}
}
Repaint();
}
return false;
}
enum BooleanOperation
{
Union,
Subtract,
Intersect
}
static ActionResult MenuBooleanOperation(BooleanOperation operation, ProBuilderMesh lhs, ProBuilderMesh rhs)
{
if (lhs == null || rhs == null)
return new ActionResult(ActionResult.Status.Failure, "Must Select 2 Objects");
string op_string = operation == BooleanOperation.Union ? "Union" : (operation == BooleanOperation.Subtract ? "Subtract" : "Intersect");
ProBuilderMesh[] sel = new ProBuilderMesh[] { lhs, rhs };
UndoUtility.RecordSelection(sel, op_string);
UnityEngine.ProBuilder.Csg.Model result;
switch (operation)
{
case BooleanOperation.Union:
result = Boolean.Union(lhs.gameObject, rhs.gameObject);
break;
case BooleanOperation.Subtract:
result = Boolean.Subtract(lhs.gameObject, rhs.gameObject);
break;
default:
result = Boolean.Intersect(lhs.gameObject, rhs.gameObject);
break;
}
var materials = result.materials.ToArray();
ProBuilderMesh pb = ProBuilderMesh.Create();
pb.GetComponent<MeshFilter>().sharedMesh = (Mesh) result;
pb.GetComponent<MeshRenderer>().sharedMaterials = materials;
MeshImporter importer = new MeshImporter(pb.gameObject);
importer.Import(new MeshImportSettings() { quads = true, smoothing = true, smoothingAngle = 1f });
pb.Rebuild();
pb.CenterPivot(null);
Selection.objects = new Object[] { pb.gameObject };
return new ActionResult(ActionResult.Status.Success, op_string);
}
/**
* Union operation between two ProBuilder objects.
*/
public static ActionResult MenuUnion(ProBuilderMesh lhs, ProBuilderMesh rhs)
{
return MenuBooleanOperation(BooleanOperation.Union, lhs, rhs);
}
/**
* Subtract boolean operation between two pb_Objects.
*/
public static ActionResult MenuSubtract(ProBuilderMesh lhs, ProBuilderMesh rhs)
{
return MenuBooleanOperation(BooleanOperation.Subtract, lhs, rhs);
}
/**
* Intersect boolean operation between two pb_Objects.
*/
public static ActionResult MenuIntersect(ProBuilderMesh lhs, ProBuilderMesh rhs)
{
return MenuBooleanOperation(BooleanOperation.Intersect, lhs, rhs);
}
}
}
#endif