-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPaintPhysicsMaterialOverlayPalette.cs
414 lines (357 loc) · 12.5 KB
/
PaintPhysicsMaterialOverlayPalette.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
using System;
using System.Collections.Generic;
using System.Linq;
using Sirenix.OdinInspector;
using UnityEditor;
using UnityEditor.Overlays;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;
using Object = UnityEngine.Object;
namespace GameContent.Scripts.Editor
{
[CreateAssetMenu]
public class PaintPhysicsMaterialOverlayPalette : ScriptableObject
{
#if ODIN_INSPECTOR
[DrawWithUnity]
#endif
public List<Entry2D> Entries2D = new();
#if ODIN_INSPECTOR
[DrawWithUnity]
#endif
public List<Entry3D> Entries3D = new();
[Serializable]
public abstract class EntryBase
{
public Color Color = Color.white;
public abstract Object Material { get; }
public string Name => Material ? Material.name : "null";
}
[Serializable]
public sealed class Entry3D : EntryBase
{
public PhysicMaterial _material;
public override Object Material => _material;
}
[Serializable]
public sealed class Entry2D : EntryBase
{
public PhysicsMaterial2D _material;
public override Object Material => _material;
}
}
/// <summary>
/// Allow to display or paint Physical Material by mouse pointer and hotkeys
/// </summary>
[Serializable]
[Overlay(typeof(SceneView), "Paint Physics Material", true)]
public sealed class PaintPhysicsMaterialOverlay : Overlay
{
private static GUIContent IconGUIContent = EditorGUIUtility.IconContent("d_PhysicMaterial Icon");
[SerializeField] private float MaxDistance = 50;
[SerializeField] private float Alpha = .25f;
[SerializeField] private bool DrawAll;
[SerializeField] private bool UseLeftClick = true;
[SerializeField] private KeyCode paintHotKey = KeyCode.V;
private PaintPhysicsMaterialOverlayPalette.EntryBase current;
[SerializeField] private PaintPhysicsMaterialOverlayPalette palette;
//[SerializeField] private Vector2 WindowSize = new Vector2(200, 460);
//private Vector2 scroll;
private Material wireMat;
private GUIContent _guiContent;
private Shader MeshColliderShader;
private static readonly int ColorProperty = Shader.PropertyToID("_Color");
public override VisualElement CreatePanelContent()
{
var root = new VisualElement { name = nameof(PaintPhysicsMaterialOverlay) };
root.Add(new IMGUIContainer(OnWindowGUI));
_guiContent = IconGUIContent;
_guiContent.text = "Paint Physical Material";
_guiContent.tooltip = "Allow to display or paint Physical Material by mouse pointer and hotkeys";
SceneView.duringSceneGui -= OnToolGUI;
SceneView.duringSceneGui += OnToolGUI;
return root;
}
private void OnToolGUI(EditorWindow sceneWindow)
{
if (!(sceneWindow is SceneView sceneView)) return;
if (collapsed) return;
PrefabStage stage = PrefabStageUtility.GetCurrentPrefabStage();
Scene scene = stage ? stage.scene : SceneManager.GetActiveScene();
if (Event.current.type == EventType.Repaint)
{
if (DrawAll) DrawAllColliders(scene, sceneView);
DrawColliderCurrent(scene);
sceneView.Repaint();
}
bool isClicked = UseLeftClick && Event.current.type == EventType.MouseDown && Event.current.button == 0 &&
Event.current.isMouse;
bool isHotkey = paintHotKey != KeyCode.None && Event.current.type == EventType.KeyDown &&
Event.current.keyCode == paintHotKey &&
Event.current.isKey;
if (isHotkey || isClicked)
{
if (isClicked)
{
GUIUtility.hotControl = -1;
Event.current.Use();
}
SwapMaterialUnderCursor(scene);
}
}
private void OnWindowGUI()
{
float lastLabelWidth = EditorGUIUtility.labelWidth;
EditorGUIUtility.labelWidth = 100;
MaxDistance = EditorGUILayout.Slider("Max Distance", MaxDistance, 0, 1000);
Alpha = EditorGUILayout.Slider(nameof(Alpha), Alpha, 0, 1);
DrawAll = EditorGUILayout.Toggle("Draw All", DrawAll);
UseLeftClick = EditorGUILayout.Toggle("Use Left Click", UseLeftClick);
EditorGUI.BeginChangeCheck();
KeyCode newKey = (KeyCode)EditorGUILayout.EnumPopup("Hotkey", paintHotKey);
if (EditorGUI.EndChangeCheck())
{
//Undo.RecordObject(this, "paintHotKey");
paintHotKey = newKey;
}
GUI.enabled = false;
EditorGUILayout.ObjectField(current?.Material, typeof(Object), false);
GUI.enabled = true;
palette = (PaintPhysicsMaterialOverlayPalette)EditorGUILayout.ObjectField(palette, typeof(PaintPhysicsMaterialOverlayPalette), false);
//scroll = GUILayout.BeginScrollView(scroll, GUILayout.MaxHeight(WindowSize.y - 60));
if (palette)
{
foreach (var entry in palette.Entries3D)
{
DrawMaterialToggle(entry);
}
}
//GUILayout.EndScrollView();
EditorGUIUtility.labelWidth = lastLabelWidth;
}
private void DrawAllColliders(Scene scene, SceneView sceneView)
{
if (Alpha == 0) return;
if (!palette) return;
Vector3 cameraPosition = sceneView.camera.transform.position;
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(sceneView.camera);
Collider[] colliders = scene.GetRootGameObjects().SelectMany(go => go.GetComponentsInChildren<Collider>())
.ToArray();
foreach (Collider collider in colliders)
{
if ((collider.bounds.center - cameraPosition).sqrMagnitude < MaxDistance * MaxDistance &&
GeometryUtility.TestPlanesAABB(planes, collider.bounds))
{
DrawCollider(collider, Alpha);
}
}
}
private void DrawColliderCurrent(Scene scene)
{
Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
bool result = scene.GetPhysicsScene().Raycast(ray.origin, ray.direction, out RaycastHit hit, 1000);
if (!result) return;
if (!palette) return;
DrawCollider(hit.collider, 1);
}
private void DrawCollider(Collider collider, float alpha)
{
if (collider.isTrigger) return;
if (!collider.enabled) return;
var entry = palette.Entries3D.FirstOrDefault(e => e.Material == collider.sharedMaterial);
if (entry == null) return;
Color color = entry.Color.SetAlpha(entry.Color.a * alpha);
switch (collider)
{
case BoxCollider box:
{
DrawCube(box, color);
break;
}
case CapsuleCollider capsule:
{
DrawCapsule(capsule, color);
break;
}
case SphereCollider sphere:
{
DrawSphere(sphere, color);
break;
}
case MeshCollider mesh:
{
DrawMesh(mesh, color);
break;
}
}
}
private void SwapMaterialUnderCursor(Scene scene)
{
Debug.Log($"click Swap material");
Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
bool result = scene.GetPhysicsScene().Raycast(ray.origin, ray.direction, out RaycastHit hit, 1000);
if (result)
{
Debug.Log($"Swap material '{hit.collider.name}'", hit.collider);
SwapMaterial(hit.collider);
}
}
void SwapMaterial(Collider collider)
{
if (current is PaintPhysicsMaterialOverlayPalette.Entry3D entry3D)
{
Undo.RecordObject(collider, $"swap material {current.Name}");
collider.sharedMaterial = entry3D._material;
}
}
void DrawMaterialToggle(PaintPhysicsMaterialOverlayPalette.EntryBase entry)
{
if (entry == null) return;
GUI.color = entry.Color.SetAlpha(1);
bool newResult = GUILayout.Toggle(entry == current, entry.Name, "button");
GUI.color = Color.white;
if (newResult)
{
current = entry;
}
}
private void DrawCube(BoxCollider box, Color color)
{
Matrix4x4 old = Handles.matrix;
Handles.color = color;
Handles.matrix = box.transform.localToWorldMatrix;
Handles.DrawWireCube(box.center, box.size);
Handles.matrix = old;
}
private void DrawCapsule(CapsuleCollider capsule, Color color)
{
Handles.color = color;
// calculate top and bottom center sphere locations.
float offset = capsule.height / 2;
Vector3 top, bottom = top = capsule.center;
float radius = capsule.radius;
#if (UNITY_2017_2_OR_NEWER)
Vector3 scale = capsule.transform.localToWorldMatrix.lossyScale;
#else
Vector3 scale = CurrentAttachTo.transform.lossyScale;
#endif
switch (capsule.direction)
{
case 0://x axis
//adjust radius by the bigger scale.
radius *= scale.y > scale.z ? scale.y : scale.z;
// adjust the offset to top and bottom mid points for spheres based on radius / scale in that direction
offset -= radius / scale.x;
// offset top and bottom points.
top.x += offset;
bottom.x -= offset;
break;
case 1:
radius *= scale.x > scale.z ? scale.x : scale.z;
offset -= radius / scale.y;
top.y += offset;
bottom.y -= offset;
break;
case 2:
radius *= scale.x > scale.y ? scale.x : scale.y;
offset -= radius / scale.z;
top.z += offset;
bottom.z -= offset;
break;
}
if (capsule.height < capsule.radius * 2)
{
// draw just the sphere if the radius and the height will make a sphere.
Vector3 worldCenter = capsule.transform.localToWorldMatrix.MultiplyPoint(capsule.center);
Handles.DrawWireDisc(worldCenter, Vector3.forward, radius);
Handles.DrawWireDisc(worldCenter, Vector3.right, radius);
Handles.DrawWireDisc(worldCenter, Vector3.up, radius);
return;
}
Vector3 worldTop = capsule.transform.localToWorldMatrix.MultiplyPoint3x4(top);
Vector3 worldBottom = capsule.transform.localToWorldMatrix.MultiplyPoint3x4(bottom);
Vector3 up = worldTop - worldBottom;
Vector3 cross1 = Vector3.up;
// dont want to cross if in same direction, forward works in this case as the first cross
if (up.normalized == cross1 || up.normalized == -cross1)
{
cross1 = Vector3.forward;
}
Vector3 right = Vector3.Cross(up, -cross1).normalized;
Vector3 forward = Vector3.Cross(up, -right).normalized;
// full circles at top and bottom
Handles.DrawWireDisc(worldTop, up, radius);
Handles.DrawWireDisc(worldBottom, up, radius);
// half arcs at top and bottom
Handles.DrawWireArc(worldTop, forward, right, 180f, radius);
Handles.DrawWireArc(worldTop, -right, forward, 180f, radius);
Handles.DrawWireArc(worldBottom, -forward, right, 180f, radius);
Handles.DrawWireArc(worldBottom, right, forward, 180f, radius);
// connect bottom and top side points
Handles.DrawLine(worldTop + right * radius, worldBottom + right * radius);
Handles.DrawLine(worldTop - right * radius, worldBottom - right * radius);
Handles.DrawLine(worldTop + forward * radius, worldBottom + forward * radius);
Handles.DrawLine(worldTop - forward * radius, worldBottom - forward * radius);
}
private void DrawSphere(SphereCollider sphere, Color color)
{
Handles.color = color;
Vector3 worldCenter = sphere.transform.localToWorldMatrix.MultiplyPoint3x4(sphere.center);
// Draw all normal axis' rings at the world center location for both perspective and isometric/orthographic
float radius = sphere.radius;
#if (UNITY_2017_2_OR_NEWER)
Vector3 scale = sphere.transform.localToWorldMatrix.lossyScale;
#else
Vector3 scale = CurrentAttachTo.transform.lossyScale;
#endif
float largestScale = Mathf.Max(Mathf.Max(scale.x, scale.y), scale.z);
radius *= largestScale;
Handles.DrawWireDisc(worldCenter, Vector3.forward, radius);
Handles.DrawWireDisc(worldCenter, Vector3.right, radius);
Handles.DrawWireDisc(worldCenter, Vector3.up, radius);
// orthographic camera
if (Camera.current != null)
{
if (Camera.current.orthographic)
{
// simple, use cameras forward in orthographic
Handles.DrawWireDisc(worldCenter, Camera.current.transform.forward, radius);
}
else
{
// draw a circle facing the camera covering all the radius in prespective mode
Vector3 normal = worldCenter - Handles.inverseMatrix.MultiplyPoint(Camera.current.transform.position);
float sqrMagnitude = normal.sqrMagnitude;
float r2 = radius * radius;
float r4m = r2 * r2 / sqrMagnitude;
float newRadius = Mathf.Sqrt(r2 - r4m);
Handles.DrawWireDisc(worldCenter - r2 * normal / sqrMagnitude, normal, newRadius);
}
}
}
private void DrawMesh(MeshCollider mesh, Color color)
{
// try to find mesh shader
if (MeshColliderShader == null) MeshColliderShader = Shader.Find("Unlit/Color");
if (!MeshColliderShader) return;
if (!wireMat) wireMat = new Material(MeshColliderShader);
if (MeshColliderShader == null || mesh.sharedMesh == null) return;
wireMat.SetColor(ColorProperty, color);
wireMat.SetPass(0);
GL.wireframe = true;
Graphics.DrawMeshNow(mesh.sharedMesh, mesh.transform.localToWorldMatrix);
GL.wireframe = false;
// Graphics.DrawMeshNow(mesh.sharedMesh, mesh.transform.localToWorldMatrix);
}
}
public static class Utility
{
public static Color SetAlpha(this Color color, float alpha)
{
color.a = alpha;
return color;
}
}
}