-
Notifications
You must be signed in to change notification settings - Fork 434
Expand file tree
/
Copy pathGridView.cs
More file actions
81 lines (66 loc) · 2.66 KB
/
Copy pathGridView.cs
File metadata and controls
81 lines (66 loc) · 2.66 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
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace GraphProcessor
{
public class GridView : VisualElement
{
private static readonly int ID_RT_TRANSFORM = Shader.PropertyToID("_RtTransform");
private static readonly int ID_SCALE = Shader.PropertyToID("_Scale");
private Material material;
private RenderTexture renderTexture;
private VisualElement transformSource;
public GridView()
{
}
public GridView(VisualElement transformSource)
{
this.transformSource = transformSource;
RegisterCallback<AttachToPanelEvent>(OnAttachToPanel);
RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanel);
style.position = Position.Absolute;
style.backgroundSize = new BackgroundSize(BackgroundSizeType.Cover);
style.width = new Length(Screen.currentResolution.width, LengthUnit.Pixel);
style.height = new Length(Screen.currentResolution.height, LengthUnit.Pixel);
}
private void OnAttachToPanel(AttachToPanelEvent e)
{
renderTexture = RenderTexture.GetTemporary(
Screen.currentResolution.width,
Screen.currentResolution.height,
0, RenderTextureFormat.ARGB32,
RenderTextureReadWrite.Default);
renderTexture.Create();
material = new Material(Shader.Find("GraphView/Grid"));
style.backgroundImage = Background.FromRenderTexture(renderTexture);
Redraw();
EditorApplication.update -= Redraw;
EditorApplication.update += Redraw;
}
private void Redraw()
{
if (transformSource == null || renderTexture == null || material == null)
return;
material.SetVector(ID_RT_TRANSFORM, new Vector4(
Screen.currentResolution.width, Screen.currentResolution.height,
transformSource.resolvedStyle.translate.x, transformSource.resolvedStyle.translate.y
));
material.SetFloat(ID_SCALE, transformSource.resolvedStyle.scale.value.x);
Graphics.SetRenderTarget(renderTexture);
Graphics.Blit(null, material);
}
private void OnDetachFromPanel(DetachFromPanelEvent e)
{
if (renderTexture)
{
RenderTexture.ReleaseTemporary(renderTexture);
}
if (material)
{
Object.DestroyImmediate(material);
}
style.backgroundImage = null;
EditorApplication.update -= Redraw;
}
}
}